Skip to content

Commit 45a4b9a

Browse files
committed
/country/{id}: show 404 page when country not found.
1 parent 0fd5eed commit 45a4b9a

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/main/java/ru/mystamps/web/controller/InfoCountryController.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818

1919
package ru.mystamps.web.controller;
2020

21+
import java.io.IOException;
22+
2123
import javax.inject.Inject;
24+
import javax.servlet.http.HttpServletResponse;
2225

2326
import org.springframework.stereotype.Controller;
2427
import org.springframework.ui.Model;
2528
import org.springframework.web.bind.annotation.PathVariable;
2629
import org.springframework.web.bind.annotation.RequestMapping;
2730
import org.springframework.web.bind.annotation.RequestMethod;
2831

29-
import ru.mystamps.web.Url;
32+
import ru.mystamps.web.entity.Country;
3033
import ru.mystamps.web.service.CountryService;
34+
import ru.mystamps.web.Url;
3135

3236
@Controller
3337
@RequestMapping(Url.INFO_COUNTRY_PAGE)
@@ -41,8 +45,18 @@ public class InfoCountryController {
4145
}
4246

4347
@RequestMapping(method = RequestMethod.GET)
44-
public String showInfo(@PathVariable("id") final Integer id, final Model model) {
45-
model.addAttribute("country", countryService.findById(id));
48+
public String showInfo(
49+
@PathVariable("id") final Integer id,
50+
final Model model,
51+
final HttpServletResponse response) throws IOException {
52+
53+
final Country country = countryService.findById(id);
54+
if (country == null) {
55+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
56+
return null;
57+
}
58+
59+
model.addAttribute("country", country);
4660
return "country/info";
4761
}
4862

src/test/config/testng.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
</classes>
3939
</test>
4040

41+
<test name="When user open not existing country page">
42+
<classes>
43+
<class name="ru.mystamps.web.tests.cases.WhenUserOpenNotExistingCountryPage" />
44+
</classes>
45+
</test>
46+
4147
<test name="When user add series">
4248
<classes>
4349
<class name="ru.mystamps.web.tests.cases.WhenUserAddSeries" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2012 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.tests.cases;
20+
21+
import java.net.HttpURLConnection;
22+
23+
import static org.fest.assertions.api.Assertions.assertThat;
24+
25+
import org.testng.annotations.Test;
26+
27+
import ru.mystamps.web.tests.page.NotFoundErrorPage;
28+
import ru.mystamps.web.Url;
29+
30+
import static ru.mystamps.web.tests.TranslationUtils.tr;
31+
32+
public class WhenUserOpenNotExistingCountryPage extends WhenUserAtAnyPage<NotFoundErrorPage> {
33+
34+
public WhenUserOpenNotExistingCountryPage() {
35+
super(NotFoundErrorPage.class);
36+
hasTitleWithoutStandardPrefix(tr("t_404_title"));
37+
hasResponseServerCode(HttpURLConnection.HTTP_NOT_FOUND);
38+
}
39+
40+
@Test(groups = "logic")
41+
public void shouldShow404Page() {
42+
final String absentCountryId = "999";
43+
page.open(Url.INFO_COUNTRY_PAGE.replace("{id}", absentCountryId));
44+
45+
checkStandardStructure();
46+
47+
assertThat(page.getErrorMessage()).isEqualTo(tr("t_404_description", "\n"));
48+
assertThat(page.getErrorCode()).isEqualTo("404");
49+
}
50+
51+
}

0 commit comments

Comments
 (0)