diff --git a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java
index c7e0eec5..be99c3b7 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java
+++ b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/SessionInfoInterceptor.java
@@ -34,6 +34,10 @@ public void postHandle(
if (modelAndView == null) {
return;
}
+ String viewName = modelAndView.getViewName();
+ if (viewName != null && viewName.startsWith("redirect:")) {
+ return;
+ }
HttpSession session = request.getSession(true);
diff --git a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java
index 70c6f206..bce29c80 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java
+++ b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/config/WebMvcConfig.java
@@ -2,6 +2,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@@ -17,4 +18,9 @@ public WebMvcConfig(SessionInfoInterceptor sessionInfoInterceptor) {
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sessionInfoInterceptor);
}
+
+ @Override
+ public void addViewControllers(ViewControllerRegistry registry) {
+ registry.addRedirectViewController("/", "/catalog");
+ }
}
diff --git a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java
index 25d921df..dd1fc32c 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java
+++ b/eShopLegacyMVC-SpringBoot/src/main/java/com/eshop/catalog/controller/CatalogController.java
@@ -18,9 +18,10 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
-@RequestMapping("/")
+@RequestMapping("/catalog")
public class CatalogController {
private static final Logger log = LoggerFactory.getLogger(CatalogController.class);
@@ -49,7 +50,7 @@ public String index(
return "catalog/index";
}
- @GetMapping("/catalog/details/{id}")
+ @GetMapping("/details/{id}")
public String details(@PathVariable int id, Model model) {
log.info("Now loading... /Catalog/Details?id={}", id);
CatalogItem catalogItem = catalogService.findCatalogItem(id);
@@ -63,7 +64,7 @@ public String details(@PathVariable int id, Model model) {
return "catalog/details";
}
- @GetMapping("/catalog/create")
+ @GetMapping("/create")
public String createForm(Model model) {
log.info("Now loading... /Catalog/Create");
populateDropdowns(model);
@@ -72,8 +73,12 @@ public String createForm(Model model) {
return "catalog/create";
}
- @PostMapping("/catalog/create")
- public String create(@Valid CatalogItem catalogItem, BindingResult result, Model model) {
+ @PostMapping("/create")
+ public String create(
+ @Valid CatalogItem catalogItem,
+ BindingResult result,
+ Model model,
+ RedirectAttributes redirectAttributes) {
log.info("Now processing... /Catalog/Create?catalogItemName={}", catalogItem.getName());
if (result.hasErrors()) {
populateDropdowns(model);
@@ -82,10 +87,10 @@ public String create(@Valid CatalogItem catalogItem, BindingResult result, Model
}
catalogService.createCatalogItem(catalogItem);
catalogMetrics.incrementItemsCreated();
- return "redirect:/";
+ return "redirect:/catalog";
}
- @GetMapping("/catalog/edit/{id}")
+ @GetMapping("/edit/{id}")
public String editForm(@PathVariable int id, Model model) {
log.info("Now loading... /Catalog/Edit?id={}", id);
CatalogItem catalogItem = catalogService.findCatalogItem(id);
@@ -99,9 +104,13 @@ public String editForm(@PathVariable int id, Model model) {
return "catalog/edit";
}
- @PostMapping("/catalog/edit/{id}")
+ @PostMapping("/edit/{id}")
public String edit(
- @PathVariable int id, @Valid CatalogItem catalogItem, BindingResult result, Model model) {
+ @PathVariable int id,
+ @Valid CatalogItem catalogItem,
+ BindingResult result,
+ Model model,
+ RedirectAttributes redirectAttributes) {
log.info("Now processing... /Catalog/Edit?id={}", catalogItem.getId());
if (result.hasErrors()) {
catalogItem.setPictureUri("/items/" + catalogItem.getId() + "/pic");
@@ -111,10 +120,10 @@ public String edit(
}
catalogService.updateCatalogItem(catalogItem);
catalogMetrics.incrementItemsUpdated();
- return "redirect:/";
+ return "redirect:/catalog";
}
- @GetMapping("/catalog/delete/{id}")
+ @GetMapping("/delete/{id}")
public String deleteForm(@PathVariable int id, Model model) {
log.info("Now loading... /Catalog/Delete?id={}", id);
CatalogItem catalogItem = catalogService.findCatalogItem(id);
@@ -127,8 +136,8 @@ public String deleteForm(@PathVariable int id, Model model) {
return "catalog/delete";
}
- @PostMapping("/catalog/delete/{id}")
- public String deleteConfirmed(@PathVariable int id) {
+ @PostMapping("/delete/{id}")
+ public String deleteConfirmed(@PathVariable int id, RedirectAttributes redirectAttributes) {
log.info("Now processing... /Catalog/DeleteConfirmed?id={}", id);
CatalogItem catalogItem = catalogService.findCatalogItem(id);
if (catalogItem == null) {
@@ -136,7 +145,7 @@ public String deleteConfirmed(@PathVariable int id) {
}
catalogService.removeCatalogItem(catalogItem);
catalogMetrics.incrementItemsDeleted();
- return "redirect:/";
+ return "redirect:/catalog";
}
private void populateDropdowns(Model model) {
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html
index ff513636..42c4ffbc 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html
+++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/create.html
@@ -94,7 +94,7 @@
Create
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html
index e0daebb5..9a59eebc 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html
+++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/delete.html
@@ -44,7 +44,7 @@ Are you sure you want to delete this?
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html
index bad3d7e3..200bc282 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html
+++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/details.html
@@ -43,7 +43,7 @@ Details
Edit |
- Back to List
+ Back to List
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html
index 379d4927..888e1325 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html
+++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/edit.html
@@ -99,7 +99,7 @@ Edit
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html
index 39d0eb4b..87f6c66c 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html
+++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/catalog/index.html
@@ -55,7 +55,7 @@
diff --git a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html
index 112067e1..904b90b8 100644
--- a/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html
+++ b/eShopLegacyMVC-SpringBoot/src/main/resources/templates/layout.html
@@ -14,7 +14,7 @@
diff --git a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/SmokeTest.java b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/SmokeTest.java
index 854beec6..095708ec 100644
--- a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/SmokeTest.java
+++ b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/SmokeTest.java
@@ -52,8 +52,13 @@ void bootstrapCss_isAccessible() throws Exception {
}
@Test
- void homepage_returns200() throws Exception {
- mockMvc.perform(get("/")).andExpect(status().isOk());
+ void rootRedirectsToCatalog() throws Exception {
+ mockMvc.perform(get("/")).andExpect(status().is3xxRedirection());
+ }
+
+ @Test
+ void catalogIndex_returns200() throws Exception {
+ mockMvc.perform(get("/catalog")).andExpect(status().isOk());
}
@Test
diff --git a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/CatalogControllerTest.java b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/CatalogControllerTest.java
index 19995ffa..92bc30dc 100644
--- a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/CatalogControllerTest.java
+++ b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/CatalogControllerTest.java
@@ -1,7 +1,6 @@
package com.eshop.catalog.controller;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -9,7 +8,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@@ -25,8 +24,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
-import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.security.test.context.support.WithMockUser;
+import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(CatalogController.class)
@@ -71,7 +70,7 @@ void index_returnsCatalogIndexView() throws Exception {
when(catalogService.getCatalogItemsPaginated(10, 0)).thenReturn(paginatedItems);
mockMvc
- .perform(get("/"))
+ .perform(get("/catalog"))
.andExpect(status().isOk())
.andExpect(view().name("catalog/index"))
.andExpect(model().attributeExists("paginatedItems"))
@@ -85,7 +84,7 @@ void index_withCustomPagination() throws Exception {
when(catalogService.getCatalogItemsPaginated(5, 1)).thenReturn(paginatedItems);
mockMvc
- .perform(get("/").param("pageSize", "5").param("pageIndex", "1"))
+ .perform(get("/catalog").param("pageSize", "5").param("pageIndex", "1"))
.andExpect(status().isOk())
.andExpect(view().name("catalog/index"));
}
@@ -143,7 +142,7 @@ void create_validItem_redirectsToIndex() throws Exception {
.param("restockThreshold", "5")
.param("maxStockThreshold", "100"))
.andExpect(status().is3xxRedirection())
- .andExpect(redirectedUrlPattern("/**"));
+ .andExpect(redirectedUrl("/catalog"));
verify(catalogService).createCatalogItem(any(CatalogItem.class));
verify(catalogMetrics).incrementItemsCreated();
@@ -203,7 +202,7 @@ void edit_validItem_redirectsToIndex() throws Exception {
.param("restockThreshold", "5")
.param("maxStockThreshold", "100"))
.andExpect(status().is3xxRedirection())
- .andExpect(redirectedUrlPattern("/**"));
+ .andExpect(redirectedUrl("/catalog"));
verify(catalogService).updateCatalogItem(any(CatalogItem.class));
verify(catalogMetrics).incrementItemsUpdated();
@@ -251,7 +250,7 @@ void deleteConfirmed_existingItem_redirectsToIndex() throws Exception {
mockMvc
.perform(post("/catalog/delete/1").with(csrf()))
.andExpect(status().is3xxRedirection())
- .andExpect(redirectedUrlPattern("/**"));
+ .andExpect(redirectedUrl("/catalog"));
verify(catalogService).removeCatalogItem(sampleItem);
verify(catalogMetrics).incrementItemsDeleted();
diff --git a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/PicControllerTest.java b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/PicControllerTest.java
index 2e0c6883..aa392bb9 100644
--- a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/PicControllerTest.java
+++ b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/controller/PicControllerTest.java
@@ -2,7 +2,6 @@
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.eshop.catalog.config.CatalogMetrics;
@@ -11,7 +10,6 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
-import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/service/CatalogServiceImplTest.java b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/service/CatalogServiceImplTest.java
index b0a237ab..b914e661 100644
--- a/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/service/CatalogServiceImplTest.java
+++ b/eShopLegacyMVC-SpringBoot/src/test/java/com/eshop/catalog/service/CatalogServiceImplTest.java
@@ -2,7 +2,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;