solver @ModelAttributes in OwnerController

Implement CommonAttribute for owner attributes names constants
This commit is contained in:
PEDSF 2020-10-10 16:24:19 +02:00
parent e77795d7f1
commit da3666f1aa
8 changed files with 169 additions and 85 deletions

View file

@ -1,15 +1,24 @@
package org.springframework.samples.petclinic.common; 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 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 final String OWNER = "owner";
public static String OWNER_LAST_NAME = "lastName"; public static final String OWNER_ID = "id";
public static String OWNER_FIRST_NAME = "firstName"; public static final String OWNER_LAST_NAME = "lastName";
public static String OWNER_PHONE = "telephone"; public static final String OWNER_FIRST_NAME = "firstName";
public static String OWNER_ADDRESS = "address"; public static final String OWNER_PHONE = "telephone";
public static String OWNER_CITY = "city"; public static final String OWNER_ADDRESS = "address";
public static String OWNER_PETS = "pets"; public static final String OWNER_CITY = "city";
public static final String OWNER_PETS = "pets";
public static final String PET = "pet";
private CommonAttribute() { private CommonAttribute() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");

View file

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

View file

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

View file

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

View file

@ -15,7 +15,11 @@
*/ */
package org.springframework.samples.petclinic.controller; 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.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.dto.*;
import org.springframework.samples.petclinic.service.OwnerService; import org.springframework.samples.petclinic.service.OwnerService;
import org.springframework.samples.petclinic.service.VisitService; import org.springframework.samples.petclinic.service.VisitService;
@ -39,13 +43,10 @@ import java.util.Map;
*/ */
@Controller @Controller
class OwnerController { class OwnerController {
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final OwnerService ownerService; private final OwnerService ownerService;
private final VisitService visitService; private final VisitService visitService;
@Autowired
OwnerController(OwnerService ownerService, VisitService visitService) { OwnerController(OwnerService ownerService, VisitService visitService) {
this.ownerService = ownerService; this.ownerService = ownerService;
this.visitService = visitService; this.visitService = visitService;
@ -53,35 +54,35 @@ class OwnerController {
@InitBinder("owner") @InitBinder("owner")
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id"); dataBinder.setDisallowedFields(CommonAttribute.OWNER_ID);
} }
@GetMapping("/owners/new") @GetMapping(CommonEndPoint.OWNERS_NEW)
public String initCreationForm(Map<String, Object> model) { public String initCreationForm(Map<String, Object> model) {
OwnerDTO owner = new OwnerDTO(); OwnerDTO owner = new OwnerDTO();
model.put(CommonAttribute.OWNER, owner); model.put(CommonAttribute.OWNER, owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return CommonView.OWNER_CREATE_OR_UPDATE;
} }
@PostMapping("/owners/new") @PostMapping(CommonEndPoint.OWNERS_NEW)
public String processCreationForm(@ModelAttribute("owner") @Valid OwnerDTO owner, BindingResult result) { public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner, BindingResult result) {
if (result.hasErrors()) { if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return CommonView.OWNER_CREATE_OR_UPDATE;
} }
else { else {
this.ownerService.save(owner); 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<String, Object> model) { public String initFindForm(Map<String, Object> model) {
model.put(CommonAttribute.OWNER, new OwnerDTO()); model.put(CommonAttribute.OWNER, new OwnerDTO());
return "owners/findOwners"; return CommonView.OWNER_FIND_OWNERS;
} }
@GetMapping("/owners") @GetMapping(CommonEndPoint.OWNERS)
public String processFindForm(@ModelAttribute("owner") OwnerDTO owner, BindingResult result, Map<String, Object> model) { public String processFindForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, BindingResult result, Map<String, Object> model) {
// allow parameterless GET request for /owners to return all records // allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) { if (owner.getLastName() == null) {
@ -92,38 +93,38 @@ class OwnerController {
Collection<OwnerDTO> results = this.ownerService.findByLastName(owner.getLastName()); Collection<OwnerDTO> results = this.ownerService.findByLastName(owner.getLastName());
if (results.isEmpty()) { if (results.isEmpty()) {
// no owners found // no owners found
result.rejectValue(CommonAttribute.OWNER_LAST_NAME, "notFound", "not found"); result.rejectValue(CommonAttribute.OWNER_LAST_NAME, CommonError.NOT_FOUND_ARGS, CommonError.NOT_FOUND_MESSAGE);
return "owners/findOwners"; return CommonView.OWNER_FIND_OWNERS;
} }
else if (results.size() == 1) { else if (results.size() == 1) {
// 1 owner found // 1 owner found
owner = results.iterator().next(); owner = results.iterator().next();
return "redirect:/owners/" + owner.getId(); return CommonView.OWNER_OWNERS_R + owner.getId();
} }
else { else {
// multiple owners found // multiple owners found
model.put("selections", results); model.put(CommonAttribute.SELECTIONS, results);
return "owners/ownersList"; return CommonView.OWNER_OWNERS_LIST;
} }
} }
@GetMapping("/owners/{ownerId}/edit") @GetMapping(CommonEndPoint.OWNERS_ID_EDIT)
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
OwnerDTO ownerDTO = this.ownerService.findById(ownerId); OwnerDTO ownerDTO = this.ownerService.findById(ownerId);
model.addAttribute(CommonAttribute.OWNER, ownerDTO); model.addAttribute(CommonAttribute.OWNER, ownerDTO);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return CommonView.OWNER_CREATE_OR_UPDATE;
} }
@PostMapping("/owners/{ownerId}/edit") @PostMapping(CommonEndPoint.OWNERS_ID_EDIT)
public String processUpdateOwnerForm(@ModelAttribute("owner") @Valid OwnerDTO owner, BindingResult result, public String processUpdateOwnerForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner, BindingResult result,
@PathVariable("ownerId") int ownerId) { @PathVariable("ownerId") int ownerId) {
if (result.hasErrors()) { if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return CommonView.OWNER_CREATE_OR_UPDATE;
} }
else { else {
owner.setId(ownerId); owner.setId(ownerId);
this.ownerService.save(owner); 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 * @param ownerId the ID of the owner to display
* @return a ModelMap with the model attributes for the view * @return a ModelMap with the model attributes for the view
*/ */
@GetMapping("/owners/{ownerId}") @GetMapping(CommonEndPoint.OWNERS_ID)
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { 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); OwnerDTO owner = this.ownerService.findById(ownerId);
for (PetDTO petDTO : owner.getPets()) { for (PetDTO petDTO : owner.getPets()) {

View file

@ -15,6 +15,9 @@
*/ */
package org.springframework.samples.petclinic.controller; 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.dto.*;
import org.springframework.samples.petclinic.validator.PetDTOValidator; import org.springframework.samples.petclinic.validator.PetDTOValidator;
import org.springframework.samples.petclinic.service.*; import org.springframework.samples.petclinic.service.*;
@ -64,7 +67,7 @@ class PetController {
@InitBinder("owner") @InitBinder("owner")
public void initOwnerBinder(WebDataBinder dataBinder) { public void initOwnerBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id"); dataBinder.setDisallowedFields(CommonAttribute.OWNER_ID);
} }
@InitBinder("pet") @InitBinder("pet")
@ -73,22 +76,22 @@ class PetController {
} }
@GetMapping("/pets/new") @GetMapping("/pets/new")
public String initCreationForm(OwnerDTO owner, ModelMap model) { public String initCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, ModelMap model) {
PetDTO pet = new PetDTO(); PetDTO pet = new PetDTO();
owner.addPet(pet); owner.addPet(pet);
model.put("pet", pet); model.put(CommonAttribute.PET, pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} }
@PostMapping("/pets/new") @PostMapping("/pets/new")
public String processCreationForm(@ModelAttribute("owner") OwnerDTO owner, public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner,
@ModelAttribute("pet") @Valid PetDTO pet, BindingResult result, ModelMap model) { @ModelAttribute(CommonAttribute.PET) @Valid PetDTO pet, BindingResult result, ModelMap model) {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) { 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); owner.addPet(pet);
if (result.hasErrors()) { if (result.hasErrors()) {
model.put("pet", pet); model.put(CommonAttribute.PET, pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} else { } else {
this.petService.save(pet); this.petService.save(pet);
@ -99,15 +102,15 @@ class PetController {
@GetMapping("/pets/{petId}/edit") @GetMapping("/pets/{petId}/edit")
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
PetDTO pet = this.petService.findById(petId); PetDTO pet = this.petService.findById(petId);
model.put("pet", pet); model.put(CommonAttribute.PET, pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} }
@PostMapping("/pets/{petId}/edit") @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()) { if (result.hasErrors()) {
pet.setOwner(owner); pet.setOwner(owner);
model.put("pet", pet); model.put(CommonAttribute.PET, pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} }
else { else {

View file

@ -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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.samples.petclinic.common.CommonAttribute; 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.OwnerDTO;
import org.springframework.samples.petclinic.dto.PetDTO; import org.springframework.samples.petclinic.dto.PetDTO;
import org.springframework.samples.petclinic.dto.PetTypeDTO; 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} * Test class for {@link OwnerController}
* *
* @author Colin But * @author Colin But
* @author Paul-Emmanuel DOS SANTOS FACAO
*/ */
@WebMvcTest(OwnerController.class) @WebMvcTest(OwnerController.class)
class OwnerControllerTests { class OwnerControllerTests {
@ -94,16 +97,16 @@ class OwnerControllerTests {
@Test @Test
void testInitCreationForm() throws Exception { void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/new")) mockMvc.perform(get(CommonEndPoint.OWNERS_NEW))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeExists(CommonAttribute.OWNER)) .andExpect(model().attributeExists(CommonAttribute.OWNER))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
} }
@Test @Test
void testProcessCreationFormSuccess() throws Exception { void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/owners/new") mockMvc.perform(post("/owners/new")
.param("firstName", "Joe") .param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street") .param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street")
.param(CommonAttribute.OWNER_CITY, "London") .param(CommonAttribute.OWNER_CITY, "London")
@ -113,57 +116,57 @@ class OwnerControllerTests {
@Test @Test
void testProcessCreationFormHasErrors() throws Exception { void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/new") mockMvc.perform(post(CommonEndPoint.OWNERS_NEW)
.param("firstName", "Joe") .param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_CITY, "London")) .param(CommonAttribute.OWNER_CITY, "London"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeHasErrors(CommonAttribute.OWNER)) .andExpect(model().attributeHasErrors(CommonAttribute.OWNER))
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS))
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
} }
@Test @Test
void testInitFindForm() throws Exception { void testInitFindForm() throws Exception {
mockMvc.perform(get("/owners/find")) mockMvc.perform(get(CommonEndPoint.OWNERS_FIND))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeExists(CommonAttribute.OWNER)) .andExpect(model().attributeExists(CommonAttribute.OWNER))
.andExpect(view().name("owners/findOwners")); .andExpect(view().name(CommonView.OWNER_FIND_OWNERS));
} }
@Test @Test
void testProcessFindFormSuccess() throws Exception { void testProcessFindFormSuccess() throws Exception {
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO())); given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO()));
mockMvc.perform(get("/owners")) mockMvc.perform(get(CommonEndPoint.OWNERS))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(view().name("owners/ownersList")); .andExpect(view().name(CommonView.OWNER_OWNERS_LIST));
} }
@Test @Test
void testProcessFindFormByLastName() throws Exception { void testProcessFindFormByLastName() throws Exception {
given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george)); 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")) .param(CommonAttribute.OWNER_LAST_NAME, "Franklin"))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); .andExpect(view().name(CommonView.OWNER_OWNERS_R + TEST_OWNER_ID));
} }
@Test @Test
void testProcessFindFormNoOwnersFound() throws Exception { void testProcessFindFormNoOwnersFound() throws Exception {
mockMvc.perform(get("/owners") mockMvc.perform(get(CommonEndPoint.OWNERS)
.param(CommonAttribute.OWNER_LAST_NAME,"Unknown Surname")) .param(CommonAttribute.OWNER_LAST_NAME,"Unknown Surname"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME))
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME, "notFound")) .andExpect(model().attributeHasFieldErrorCode(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME, "notFound"))
.andExpect(view().name("owners/findOwners")); .andExpect(view().name(CommonView.OWNER_FIND_OWNERS));
} }
@Test @Test
void testInitUpdateOwnerForm() throws Exception { 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(status().isOk())
.andExpect(model().attributeExists(CommonAttribute.OWNER)) .andExpect(model().attributeExists(CommonAttribute.OWNER))
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin")))) .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_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_CITY, is("Madison"))))
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PHONE, is("6085551023")))) .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 @Test
void testProcessUpdateOwnerFormSuccess() throws Exception { 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_FIRST_NAME, "Joe")
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street") .param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street")
.param(CommonAttribute.OWNER_CITY, "London") .param(CommonAttribute.OWNER_CITY, "London")
.param(CommonAttribute.OWNER_PHONE, "01616291589")) .param(CommonAttribute.OWNER_PHONE, "01616291589"))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}")); .andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
} }
@Test @Test
void testProcessUpdateOwnerFormHasErrors() throws Exception { 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_FIRST_NAME, "Joe")
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs") .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_CITY, "London")) .param(CommonAttribute.OWNER_CITY, "London"))
@ -196,12 +199,12 @@ class OwnerControllerTests {
.andExpect(model().attributeHasErrors(CommonAttribute.OWNER)) .andExpect(model().attributeHasErrors(CommonAttribute.OWNER))
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS))
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE)) .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
} }
@Test @Test
void testShowOwner() throws Exception { 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(status().isOk())
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin")))) .andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin"))))
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George")))) .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"); description.appendText("Max did not have any visits");
} }
}))) })))
.andExpect(view().name("owners/ownerDetails")); .andExpect(view().name(CommonView.OWNER_DETAILS));
} }
} }

View file

@ -70,22 +70,27 @@ class PetControllerTests {
PetTypeDTO cat = new PetTypeDTO(); PetTypeDTO cat = new PetTypeDTO();
cat.setId(3); cat.setId(3);
cat.setName("hamster"); cat.setName("hamster");
given(this.petTypeService.findPetTypes()).willReturn(Lists.newArrayList(cat)); given(this.petTypeService.findPetTypes()).willReturn(Lists.newArrayList(cat));
given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO()); given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO());
given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO()); given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO());
} }
@Test @Test
void testInitCreationForm() throws Exception { void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID)).andExpect(status().isOk()) mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
.andExpect(view().name("pets/createOrUpdatePetForm")).andExpect(model().attributeExists("pet")); .andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm"))
.andExpect(model().attributeExists("pet"));
} }
@Test @Test
void testProcessCreationFormSuccess() throws Exception { void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty") mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
.param("type", "hamster").param("birthDate", "2015-02-12")).andExpect(status().is3xxRedirection()) .param("name", "Betty")
.param("type", "hamster")
.param("birthDate", "2015-02-12"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}")); .andExpect(view().name("redirect:/owners/{ownerId}"));
} }
@ -95,30 +100,39 @@ class PetControllerTests {
.param("name", "Betty") .param("name", "Betty")
.param("birthDate","2015-02-12")) .param("birthDate","2015-02-12"))
.andExpect(model().attributeHasNoErrors("owner")) .andExpect(model().attributeHasNoErrors("owner"))
.andExpect(model().attributeHasErrors("pet")).andExpect(model().attributeHasFieldErrors("pet", "type")) .andExpect(model().attributeHasErrors("pet"))
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")).andExpect(status().isOk()) .andExpect(model().attributeHasFieldErrors("pet", "type"))
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required"))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm")); .andExpect(view().name("pets/createOrUpdatePetForm"));
} }
@Test @Test
void testInitUpdateForm() throws Exception { void testInitUpdateForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)) mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
.andExpect(status().isOk()).andExpect(model().attributeExists("pet")) .andExpect(status().isOk())
.andExpect(model().attributeExists("pet"))
.andExpect(view().name("pets/createOrUpdatePetForm")); .andExpect(view().name("pets/createOrUpdatePetForm"));
} }
@Test @Test
void testProcessUpdateFormSuccess() throws Exception { void testProcessUpdateFormSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty") mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("type", "hamster").param("birthDate", "2015-02-12")).andExpect(status().is3xxRedirection()) .param("name", "Betty")
.param("type", "hamster")
.param("birthDate", "2015-02-12"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}")); .andExpect(view().name("redirect:/owners/{ownerId}"));
} }
@Test @Test
void testProcessUpdateFormHasErrors() throws Exception { void testProcessUpdateFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty") mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("birthDate", "2015/02/12")).andExpect(model().attributeHasNoErrors("owner")) .param("name", "Betty")
.andExpect(model().attributeHasErrors("pet")).andExpect(status().isOk()) .param("birthDate", "2015/02/12"))
.andExpect(model().attributeHasNoErrors("owner"))
.andExpect(model().attributeHasErrors("pet"))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm")); .andExpect(view().name("pets/createOrUpdatePetForm"));
} }