diff --git a/ch03/tacos-sd-jpa/src/main/java/tacos/web/DesignTacoController.java b/ch03/tacos-sd-jpa/src/main/java/tacos/web/DesignTacoController.java index 2513862..3140ee7 100644 --- a/ch03/tacos-sd-jpa/src/main/java/tacos/web/DesignTacoController.java +++ b/ch03/tacos-sd-jpa/src/main/java/tacos/web/DesignTacoController.java @@ -1,8 +1,9 @@ package tacos.web; -import java.util.ArrayList; +import static java.util.stream.Collectors.groupingBy; + import java.util.List; -import java.util.stream.Collectors; +import java.util.Map; import javax.validation.Valid; @@ -18,8 +19,8 @@ import tacos.Ingredient; import tacos.Ingredient.Type; -import tacos.TacoOrder; import tacos.Taco; +import tacos.TacoOrder; import tacos.data.IngredientRepository; @Controller @@ -35,17 +36,22 @@ public DesignTacoController( this.ingredientRepo = ingredientRepo; } - @ModelAttribute - public void addIngredientsToModel(Model model) { - List ingredients = new ArrayList<>(); - ingredientRepo.findAll().forEach(i -> ingredients.add(i)); + @ModelAttribute + public void addIngredientsToModel(Model model) { + List ingredients = (List) ingredientRepo.findAll(); - Type[] types = Ingredient.Type.values(); - for (Type type : types) { - model.addAttribute(type.toString().toLowerCase(), - filterByType(ingredients, type)); - } - } + // group the Ingredients by type and put them in a list + Map> ingredientsByType = + ingredients.stream().collect(groupingBy(Ingredient::getType)); + + + // iterate through the Map and set the model attributes for the check boxes + ingredientsByType.forEach((type , list) -> { + model.addAttribute(type.toString().toLowerCase(), list); + }); + + } + @ModelAttribute(name = "tacoOrder") public TacoOrder order() { @@ -76,12 +82,4 @@ public String processTaco( return "redirect:/orders/current"; } - private Iterable filterByType( - List ingredients, Type type) { - return ingredients - .stream() - .filter(x -> x.getType().equals(type)) - .collect(Collectors.toList()); - } - } diff --git a/ch03/tacos-sd-jpa/src/main/java/tacos/web/OrderController.java b/ch03/tacos-sd-jpa/src/main/java/tacos/web/OrderController.java index 9c972ee..7aa5e6d 100644 --- a/ch03/tacos-sd-jpa/src/main/java/tacos/web/OrderController.java +++ b/ch03/tacos-sd-jpa/src/main/java/tacos/web/OrderController.java @@ -2,7 +2,6 @@ import javax.validation.Valid; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -25,7 +24,7 @@ public OrderController(OrderRepository orderRepo) { } @GetMapping("/current") - public String orderForm() { + public String orderForm(TacoOrder order) { return "orderForm"; } diff --git a/ch03/tacos-sd-jpa/src/test/java/tacos/OrderControllerTest.java b/ch03/tacos-sd-jpa/src/test/java/tacos/OrderControllerTest.java new file mode 100644 index 0000000..88c6f97 --- /dev/null +++ b/ch03/tacos-sd-jpa/src/test/java/tacos/OrderControllerTest.java @@ -0,0 +1,66 @@ +package tacos; + +import static org.hamcrest.CoreMatchers.containsString; +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.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; + +import tacos.data.IngredientRepository; +import tacos.data.OrderRepository; +import tacos.web.OrderController; + +@WebMvcTest(controllers = {OrderController.class}) +class OrderControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + IngredientRepository ingredMockRepo; + + @MockBean + OrderRepository orderMockRepo; + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void testShowOrderForm() throws Exception { + mockMvc.perform(get("/orders/current")) + .andDo(print()) + .andExpect(view().name("orderForm")) + // more accurate to be sure that the OrderForm is displayed + .andExpect(content().string(containsString("

Deliver my taco masterpieces to...

"))); + } + + @Test + public void testSubmitOrderFormWithFieldEmpty() throws Exception { + mockMvc.perform(post("/orders")) + .andDo(print()) + // We stay on Order Form since validation errors + .andExpect(view().name("orderForm")) + // Thus model has errors + .andExpect(model().hasErrors()) + // We receive an HTTP Code = 200 since + // we get back the form instead of a redirect + .andExpect(status().isOk()); + } + +} \ No newline at end of file diff --git a/ch03/tacos-sd-jpa/src/test/java/tacos/data/OrderRepositoryTests.java b/ch03/tacos-sd-jpa/src/test/java/tacos/data/OrderRepositoryTests.java index ae48e0b..5714972 100644 --- a/ch03/tacos-sd-jpa/src/test/java/tacos/data/OrderRepositoryTests.java +++ b/ch03/tacos-sd-jpa/src/test/java/tacos/data/OrderRepositoryTests.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import tacos.Ingredient;