Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -127,16 +136,16 @@ 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) {
return "error";
}
catalogService.removeCatalogItem(catalogItem);
catalogMetrics.incrementItemsDeleted();
return "redirect:/";
return "redirect:/catalog";
}

private void populateDropdowns(Model model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h2 class="esh-body-title">Create</h2>

<div class="form-group">
<div class="col-md-offset-2 col-md-3 text-right esh-button-actions">
<a th:href="@{/}" class="btn esh-button esh-button-secondary">[ Cancel ]</a>
<a th:href="@{/catalog}" class="btn esh-button esh-button-secondary">[ Cancel ]</a>
<input type="submit" value="[ Create ]" class="btn esh-button esh-button-primary" />
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h3>Are you sure you want to delete this?</h3>
<dt></dt>
<dd class="text-right esh-button-actions">
<form th:action="@{/catalog/delete/{id}(id=${catalogItem.id})}" method="post">
<a th:href="@{/}" class="btn esh-button esh-button-secondary">[ Cancel ]</a>
<a th:href="@{/catalog}" class="btn esh-button esh-button-secondary">[ Cancel ]</a>
<button type="submit" class="btn esh-button esh-button-primary">[ Delete ]</button>
</form>
</dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h2 class="esh-body-title">Details</h2>
</div>
<p class="esh-link-list esh-link-item--margin">
<a th:href="@{/catalog/edit/{id}(id=${catalogItem.id})}" class="esh-link-item">Edit</a> |
<a th:href="@{/}" class="esh-link-item">Back to List</a>
<a th:href="@{/catalog}" class="esh-link-item">Back to List</a>
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ <h2 class="esh-body-title">Edit</h2>

<div class="form-group">
<div class="col-md-12 text-right esh-button-actions">
<a th:href="@{/}" class="btn esh-button esh-button-secondary">[ Cancel ]</a>
<a th:href="@{/catalog}" class="btn esh-button esh-button-secondary">[ Cancel ]</a>
<button type="submit" class="btn esh-button esh-button-primary">[ Save ]</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@
<div class="container">
<article class="esh-pager-wrapper row">
<nav>
<a th:href="@{/(pageSize=${paginatedItems.itemsPerPage},pageIndex=${paginatedItems.actualPage - 1})}"
<a th:href="@{/catalog(pageSize=${paginatedItems.itemsPerPage},pageIndex=${paginatedItems.actualPage - 1})}"
th:classappend="${paginatedItems.actualPage == 0} ? 'esh-pager-item--hidden'"
class="esh-pager-item esh-pager-item--navigable">Previous</a>

<span class="esh-pager-item">
Showing <span th:text="${paginatedItems.itemsPerPage}"></span> of <span th:text="${paginatedItems.totalItems}"></span> products - Page <span th:text="${paginatedItems.actualPage + 1}"></span> - <span th:text="${paginatedItems.totalPages}"></span>
</span>

<a th:href="@{/(pageSize=${paginatedItems.itemsPerPage},pageIndex=${paginatedItems.actualPage + 1})}"
<a th:href="@{/catalog(pageSize=${paginatedItems.itemsPerPage},pageIndex=${paginatedItems.actualPage + 1})}"
th:classappend="${paginatedItems.actualPage >= paginatedItems.totalPages - 1} ? 'esh-pager-item--hidden'"
class="esh-pager-item esh-pager-item--navigable">Next</a>
</nav>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<h1>Error</h1>
<h2 th:text="${message != null ? message : 'An error occurred while processing your request.'}">An error occurred while processing your request.</h2>
<p th:if="${status != null}" th:text="'Status: ' + ${status}"></p>
<a th:href="@{/}" class="btn btn-primary">Back to catalog</a>
<a th:href="@{/catalog}" class="btn btn-primary">Back to catalog</a>
</div>
</section>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<body>
<header class="navbar navbar-light navbar-static-top">
<div class="esh-header-brand">
<a th:href="@{/}">
<a th:href="@{/catalog}">
<img th:src="@{/images/brand.png}" />
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
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;

Expand All @@ -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)
Expand Down Expand Up @@ -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"))
Expand All @@ -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"));
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down