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
40 changes: 19 additions & 21 deletions ch03/tacos-sd-jpa/src/main/java/tacos/web/DesignTacoController.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -35,17 +36,22 @@ public DesignTacoController(
this.ingredientRepo = ingredientRepo;
}

@ModelAttribute
public void addIngredientsToModel(Model model) {
List<Ingredient> ingredients = new ArrayList<>();
ingredientRepo.findAll().forEach(i -> ingredients.add(i));
@ModelAttribute
public void addIngredientsToModel(Model model) {
List<Ingredient> ingredients = (List<Ingredient>) 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<Type, List<Ingredient>> 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() {
Expand Down Expand Up @@ -76,12 +82,4 @@ public String processTaco(
return "redirect:/orders/current";
}

private Iterable<Ingredient> filterByType(
List<Ingredient> ingredients, Type type) {
return ingredients
.stream()
.filter(x -> x.getType().equals(type))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +24,7 @@ public OrderController(OrderRepository orderRepo) {
}

@GetMapping("/current")
public String orderForm() {
public String orderForm(TacoOrder order) {
return "orderForm";
}

Expand Down
66 changes: 66 additions & 0 deletions ch03/tacos-sd-jpa/src/test/java/tacos/OrderControllerTest.java
Original file line number Diff line number Diff line change
@@ -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("<h3>Deliver my taco masterpieces to...</h3>")));
}

@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());
}

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