From da3666f1aa9f1cd270cd0fa4a80b2a90da401cc2 Mon Sep 17 00:00:00 2001 From: PEDSF Date: Sat, 10 Oct 2020 16:24:19 +0200 Subject: [PATCH] solver @ModelAttributes in OwnerController Implement CommonAttribute for owner attributes names constants --- .../petclinic/common/CommonAttribute.java | 25 +++++--- .../petclinic/common/CommonEndPoint.java | 18 ++++++ .../samples/petclinic/common/CommonError.java | 17 ++++++ .../samples/petclinic/common/CommonView.java | 19 +++++++ .../petclinic/controller/OwnerController.java | 57 ++++++++++--------- .../petclinic/controller/PetController.java | 23 ++++---- .../controller/OwnerControllerTests.java | 47 ++++++++------- .../controller/PetControllerTests.java | 48 ++++++++++------ 8 files changed, 169 insertions(+), 85 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/common/CommonEndPoint.java create mode 100644 src/main/java/org/springframework/samples/petclinic/common/CommonError.java create mode 100644 src/main/java/org/springframework/samples/petclinic/common/CommonView.java diff --git a/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java b/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java index 5b312df20..eb2bc444b 100644 --- a/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java +++ b/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java @@ -1,15 +1,24 @@ package org.springframework.samples.petclinic.common; +/** + * Class for const attributes names to prevent error with attributes names + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ public final class CommonAttribute { - public static String NAME = "name"; + public static final String NAME = "name"; + public static final String SELECTIONS = "selections"; - public static String OWNER = "owner"; - public static String OWNER_LAST_NAME = "lastName"; - public static String OWNER_FIRST_NAME = "firstName"; - public static String OWNER_PHONE = "telephone"; - public static String OWNER_ADDRESS = "address"; - public static String OWNER_CITY = "city"; - public static String OWNER_PETS = "pets"; + public static final String OWNER = "owner"; + public static final String OWNER_ID = "id"; + public static final String OWNER_LAST_NAME = "lastName"; + public static final String OWNER_FIRST_NAME = "firstName"; + public static final String OWNER_PHONE = "telephone"; + public static final String OWNER_ADDRESS = "address"; + public static final String OWNER_CITY = "city"; + public static final String OWNER_PETS = "pets"; + + public static final String PET = "pet"; private CommonAttribute() { throw new IllegalStateException("Utility class"); diff --git a/src/main/java/org/springframework/samples/petclinic/common/CommonEndPoint.java b/src/main/java/org/springframework/samples/petclinic/common/CommonEndPoint.java new file mode 100644 index 000000000..923a607ab --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/common/CommonEndPoint.java @@ -0,0 +1,18 @@ +package org.springframework.samples.petclinic.common; + +/** + * Class for const endpoint names to prevent error with endpoint names + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +public final class CommonEndPoint { + public static final String OWNERS = "/owners"; + public static final String OWNERS_FIND = "/owners/find"; + public static final String OWNERS_ID = "/owners/{ownerId}"; + public static final String OWNERS_ID_EDIT = "/owners/{ownerId}/edit"; + public static final String OWNERS_NEW = "/owners/new"; + + private CommonEndPoint() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/common/CommonError.java b/src/main/java/org/springframework/samples/petclinic/common/CommonError.java new file mode 100644 index 000000000..7778b2a00 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/common/CommonError.java @@ -0,0 +1,17 @@ +package org.springframework.samples.petclinic.common; + +/** + * Class for const error names to prevent errors + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +public final class CommonError { + public static final String DUPLICATE_ARGS = "duplicate"; + public static final String DUPLICATE_MESSAGE = "already exists"; + public static final String NOT_FOUND_ARGS = "notFound"; + public static final String NOT_FOUND_MESSAGE = "notFound"; + + private CommonError() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/common/CommonView.java b/src/main/java/org/springframework/samples/petclinic/common/CommonView.java new file mode 100644 index 000000000..caeb76164 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/common/CommonView.java @@ -0,0 +1,19 @@ +package org.springframework.samples.petclinic.common; + +/** + * Class for const view names to prevent error with view names + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +public final class CommonView { + public static final String OWNER_OWNERS_R = "redirect:/owners/"; + public static final String OWNER_OWNERS_ID_R = "redirect:/owners/{ownerId}"; + public static final String OWNER_CREATE_OR_UPDATE = "owners/createOrUpdateOwnerForm"; + public static final String OWNER_FIND_OWNERS = "owners/findOwners"; + public static final String OWNER_OWNERS_LIST = "owners/ownersList"; + public static final String OWNER_DETAILS = "owners/ownerDetails"; + + private CommonView() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java index 7198334ef..35807aac5 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/OwnerController.java @@ -15,7 +15,11 @@ */ package org.springframework.samples.petclinic.controller; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.common.CommonAttribute; +import org.springframework.samples.petclinic.common.CommonEndPoint; +import org.springframework.samples.petclinic.common.CommonError; +import org.springframework.samples.petclinic.common.CommonView; import org.springframework.samples.petclinic.dto.*; import org.springframework.samples.petclinic.service.OwnerService; import org.springframework.samples.petclinic.service.VisitService; @@ -39,13 +43,10 @@ import java.util.Map; */ @Controller class OwnerController { - - private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; - private final OwnerService ownerService; - private final VisitService visitService; + @Autowired OwnerController(OwnerService ownerService, VisitService visitService) { this.ownerService = ownerService; this.visitService = visitService; @@ -53,35 +54,35 @@ class OwnerController { @InitBinder("owner") public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); + dataBinder.setDisallowedFields(CommonAttribute.OWNER_ID); } - @GetMapping("/owners/new") + @GetMapping(CommonEndPoint.OWNERS_NEW) public String initCreationForm(Map model) { OwnerDTO owner = new OwnerDTO(); model.put(CommonAttribute.OWNER, owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + return CommonView.OWNER_CREATE_OR_UPDATE; } - @PostMapping("/owners/new") - public String processCreationForm(@ModelAttribute("owner") @Valid OwnerDTO owner, BindingResult result) { + @PostMapping(CommonEndPoint.OWNERS_NEW) + public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner, BindingResult result) { if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + return CommonView.OWNER_CREATE_OR_UPDATE; } else { this.ownerService.save(owner); - return "redirect:/owners/" + owner.getId(); + return CommonView.OWNER_OWNERS_R + owner.getId(); } } - @GetMapping("/owners/find") + @GetMapping(CommonEndPoint.OWNERS_FIND) public String initFindForm(Map model) { model.put(CommonAttribute.OWNER, new OwnerDTO()); - return "owners/findOwners"; + return CommonView.OWNER_FIND_OWNERS; } - @GetMapping("/owners") - public String processFindForm(@ModelAttribute("owner") OwnerDTO owner, BindingResult result, Map model) { + @GetMapping(CommonEndPoint.OWNERS) + public String processFindForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, BindingResult result, Map model) { // allow parameterless GET request for /owners to return all records if (owner.getLastName() == null) { @@ -92,38 +93,38 @@ class OwnerController { Collection results = this.ownerService.findByLastName(owner.getLastName()); if (results.isEmpty()) { // no owners found - result.rejectValue(CommonAttribute.OWNER_LAST_NAME, "notFound", "not found"); - return "owners/findOwners"; + result.rejectValue(CommonAttribute.OWNER_LAST_NAME, CommonError.NOT_FOUND_ARGS, CommonError.NOT_FOUND_MESSAGE); + return CommonView.OWNER_FIND_OWNERS; } else if (results.size() == 1) { // 1 owner found owner = results.iterator().next(); - return "redirect:/owners/" + owner.getId(); + return CommonView.OWNER_OWNERS_R + owner.getId(); } else { // multiple owners found - model.put("selections", results); - return "owners/ownersList"; + model.put(CommonAttribute.SELECTIONS, results); + return CommonView.OWNER_OWNERS_LIST; } } - @GetMapping("/owners/{ownerId}/edit") + @GetMapping(CommonEndPoint.OWNERS_ID_EDIT) public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { OwnerDTO ownerDTO = this.ownerService.findById(ownerId); model.addAttribute(CommonAttribute.OWNER, ownerDTO); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + return CommonView.OWNER_CREATE_OR_UPDATE; } - @PostMapping("/owners/{ownerId}/edit") - public String processUpdateOwnerForm(@ModelAttribute("owner") @Valid OwnerDTO owner, BindingResult result, + @PostMapping(CommonEndPoint.OWNERS_ID_EDIT) + public String processUpdateOwnerForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner, BindingResult result, @PathVariable("ownerId") int ownerId) { if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + return CommonView.OWNER_CREATE_OR_UPDATE; } else { owner.setId(ownerId); this.ownerService.save(owner); - return "redirect:/owners/{ownerId}"; + return CommonView.OWNER_OWNERS_ID_R; } } @@ -132,9 +133,9 @@ class OwnerController { * @param ownerId the ID of the owner to display * @return a ModelMap with the model attributes for the view */ - @GetMapping("/owners/{ownerId}") + @GetMapping(CommonEndPoint.OWNERS_ID) public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { - ModelAndView modelAndView = new ModelAndView("owners/ownerDetails"); + ModelAndView modelAndView = new ModelAndView(CommonView.OWNER_DETAILS); OwnerDTO owner = this.ownerService.findById(ownerId); for (PetDTO petDTO : owner.getPets()) { diff --git a/src/main/java/org/springframework/samples/petclinic/controller/PetController.java b/src/main/java/org/springframework/samples/petclinic/controller/PetController.java index 198b83923..eb9726b32 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/PetController.java @@ -15,6 +15,9 @@ */ package org.springframework.samples.petclinic.controller; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.samples.petclinic.common.CommonAttribute; +import org.springframework.samples.petclinic.common.CommonError; import org.springframework.samples.petclinic.dto.*; import org.springframework.samples.petclinic.validator.PetDTOValidator; import org.springframework.samples.petclinic.service.*; @@ -64,7 +67,7 @@ class PetController { @InitBinder("owner") public void initOwnerBinder(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); + dataBinder.setDisallowedFields(CommonAttribute.OWNER_ID); } @InitBinder("pet") @@ -73,22 +76,22 @@ class PetController { } @GetMapping("/pets/new") - public String initCreationForm(OwnerDTO owner, ModelMap model) { + public String initCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, ModelMap model) { PetDTO pet = new PetDTO(); owner.addPet(pet); - model.put("pet", pet); + model.put(CommonAttribute.PET, pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } @PostMapping("/pets/new") - public String processCreationForm(@ModelAttribute("owner") OwnerDTO owner, - @ModelAttribute("pet") @Valid PetDTO pet, BindingResult result, ModelMap model) { + public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, + @ModelAttribute(CommonAttribute.PET) @Valid PetDTO pet, BindingResult result, ModelMap model) { if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) { - result.rejectValue("name", "duplicate", "already exists"); + result.rejectValue(CommonAttribute.NAME, CommonError.DUPLICATE_ARGS, CommonError.DUPLICATE_MESSAGE); } owner.addPet(pet); if (result.hasErrors()) { - model.put("pet", pet); + model.put(CommonAttribute.PET, pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } else { this.petService.save(pet); @@ -99,15 +102,15 @@ class PetController { @GetMapping("/pets/{petId}/edit") public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { PetDTO pet = this.petService.findById(petId); - model.put("pet", pet); + model.put(CommonAttribute.PET, pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } @PostMapping("/pets/{petId}/edit") - public String processUpdateForm(@Valid PetDTO pet, BindingResult result, OwnerDTO owner, ModelMap model) { + public String processUpdateForm(@ModelAttribute(CommonAttribute.PET) @Valid PetDTO pet, BindingResult result, OwnerDTO owner, ModelMap model) { if (result.hasErrors()) { pet.setOwner(owner); - model.put("pet", pet); + model.put(CommonAttribute.PET, pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } else { diff --git a/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java index f7c02d931..b429be5c4 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java @@ -29,6 +29,8 @@ 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.samples.petclinic.common.CommonAttribute; +import org.springframework.samples.petclinic.common.CommonEndPoint; +import org.springframework.samples.petclinic.common.CommonView; import org.springframework.samples.petclinic.dto.OwnerDTO; import org.springframework.samples.petclinic.dto.PetDTO; import org.springframework.samples.petclinic.dto.PetTypeDTO; @@ -52,6 +54,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * Test class for {@link OwnerController} * * @author Colin But + * @author Paul-Emmanuel DOS SANTOS FACAO */ @WebMvcTest(OwnerController.class) class OwnerControllerTests { @@ -94,16 +97,16 @@ class OwnerControllerTests { @Test void testInitCreationForm() throws Exception { - mockMvc.perform(get("/owners/new")) + mockMvc.perform(get(CommonEndPoint.OWNERS_NEW)) .andExpect(status().isOk()) .andExpect(model().attributeExists(CommonAttribute.OWNER)) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); + .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE)); } @Test void testProcessCreationFormSuccess() throws Exception { mockMvc.perform(post("/owners/new") - .param("firstName", "Joe") + .param(CommonAttribute.OWNER_FIRST_NAME, "Joe") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street") .param(CommonAttribute.OWNER_CITY, "London") @@ -113,57 +116,57 @@ class OwnerControllerTests { @Test void testProcessCreationFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/new") - .param("firstName", "Joe") + mockMvc.perform(post(CommonEndPoint.OWNERS_NEW) + .param(CommonAttribute.OWNER_FIRST_NAME, "Joe") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_CITY, "London")) .andExpect(status().isOk()) .andExpect(model().attributeHasErrors(CommonAttribute.OWNER)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE)) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); + .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE)); } @Test void testInitFindForm() throws Exception { - mockMvc.perform(get("/owners/find")) + mockMvc.perform(get(CommonEndPoint.OWNERS_FIND)) .andExpect(status().isOk()) .andExpect(model().attributeExists(CommonAttribute.OWNER)) - .andExpect(view().name("owners/findOwners")); + .andExpect(view().name(CommonView.OWNER_FIND_OWNERS)); } @Test void testProcessFindFormSuccess() throws Exception { given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO())); - mockMvc.perform(get("/owners")) + mockMvc.perform(get(CommonEndPoint.OWNERS)) .andExpect(status().isOk()) - .andExpect(view().name("owners/ownersList")); + .andExpect(view().name(CommonView.OWNER_OWNERS_LIST)); } @Test void testProcessFindFormByLastName() throws Exception { given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george)); - mockMvc.perform(get("/owners") + mockMvc.perform(get(CommonEndPoint.OWNERS) .param(CommonAttribute.OWNER_LAST_NAME, "Franklin")) .andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); + .andExpect(view().name(CommonView.OWNER_OWNERS_R + TEST_OWNER_ID)); } @Test void testProcessFindFormNoOwnersFound() throws Exception { - mockMvc.perform(get("/owners") + mockMvc.perform(get(CommonEndPoint.OWNERS) .param(CommonAttribute.OWNER_LAST_NAME,"Unknown Surname")) .andExpect(status().isOk()) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME)) .andExpect(model().attributeHasFieldErrorCode(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME, "notFound")) - .andExpect(view().name("owners/findOwners")); + .andExpect(view().name(CommonView.OWNER_FIND_OWNERS)); } @Test void testInitUpdateOwnerForm() throws Exception { - mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)) + mockMvc.perform(get(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID)) .andExpect(status().isOk()) .andExpect(model().attributeExists(CommonAttribute.OWNER)) .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin")))) @@ -171,24 +174,24 @@ class OwnerControllerTests { .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_ADDRESS, is("110 W. Liberty St.")))) .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_CITY, is("Madison")))) .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PHONE, is("6085551023")))) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); + .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE)); } @Test void testProcessUpdateOwnerFormSuccess() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) + mockMvc.perform(post(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID) .param(CommonAttribute.OWNER_FIRST_NAME, "Joe") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street") .param(CommonAttribute.OWNER_CITY, "London") .param(CommonAttribute.OWNER_PHONE, "01616291589")) .andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/owners/{ownerId}")); + .andExpect(view().name(CommonView.OWNER_OWNERS_ID_R)); } @Test void testProcessUpdateOwnerFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) + mockMvc.perform(post(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID) .param(CommonAttribute.OWNER_FIRST_NAME, "Joe") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_CITY, "London")) @@ -196,12 +199,12 @@ class OwnerControllerTests { .andExpect(model().attributeHasErrors(CommonAttribute.OWNER)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE)) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); + .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE)); } @Test void testShowOwner() throws Exception { - mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)) + mockMvc.perform(get(CommonEndPoint.OWNERS_ID, TEST_OWNER_ID)) .andExpect(status().isOk()) .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin")))) .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George")))) @@ -228,7 +231,7 @@ class OwnerControllerTests { description.appendText("Max did not have any visits"); } }))) - .andExpect(view().name("owners/ownerDetails")); + .andExpect(view().name(CommonView.OWNER_DETAILS)); } } diff --git a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java index 011842c87..87008e6e5 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java @@ -70,23 +70,28 @@ class PetControllerTests { PetTypeDTO cat = new PetTypeDTO(); cat.setId(3); cat.setName("hamster"); + given(this.petTypeService.findPetTypes()).willReturn(Lists.newArrayList(cat)); given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO()); given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO()); - } @Test void testInitCreationForm() throws Exception { - mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID)).andExpect(status().isOk()) - .andExpect(view().name("pets/createOrUpdatePetForm")).andExpect(model().attributeExists("pet")); + mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID)) + .andExpect(status().isOk()) + .andExpect(view().name("pets/createOrUpdatePetForm")) + .andExpect(model().attributeExists("pet")); } @Test void testProcessCreationFormSuccess() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty") - .param("type", "hamster").param("birthDate", "2015-02-12")).andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/owners/{ownerId}")); + mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID) + .param("name", "Betty") + .param("type", "hamster") + .param("birthDate", "2015-02-12")) + .andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/owners/{ownerId}")); } @Test @@ -95,31 +100,40 @@ class PetControllerTests { .param("name", "Betty") .param("birthDate","2015-02-12")) .andExpect(model().attributeHasNoErrors("owner")) - .andExpect(model().attributeHasErrors("pet")).andExpect(model().attributeHasFieldErrors("pet", "type")) - .andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")).andExpect(status().isOk()) + .andExpect(model().attributeHasErrors("pet")) + .andExpect(model().attributeHasFieldErrors("pet", "type")) + .andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")) + .andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdatePetForm")); } @Test void testInitUpdateForm() throws Exception { mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)) - .andExpect(status().isOk()).andExpect(model().attributeExists("pet")) - .andExpect(view().name("pets/createOrUpdatePetForm")); + .andExpect(status().isOk()) + .andExpect(model().attributeExists("pet")) + .andExpect(view().name("pets/createOrUpdatePetForm")); } @Test void testProcessUpdateFormSuccess() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty") - .param("type", "hamster").param("birthDate", "2015-02-12")).andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/owners/{ownerId}")); + mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID) + .param("name", "Betty") + .param("type", "hamster") + .param("birthDate", "2015-02-12")) + .andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/owners/{ownerId}")); } @Test void testProcessUpdateFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty") - .param("birthDate", "2015/02/12")).andExpect(model().attributeHasNoErrors("owner")) - .andExpect(model().attributeHasErrors("pet")).andExpect(status().isOk()) - .andExpect(view().name("pets/createOrUpdatePetForm")); + mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID) + .param("name", "Betty") + .param("birthDate", "2015/02/12")) + .andExpect(model().attributeHasNoErrors("owner")) + .andExpect(model().attributeHasErrors("pet")) + .andExpect(status().isOk()) + .andExpect(view().name("pets/createOrUpdatePetForm")); } }