solver @ModelAttributes in OwnerController

Implement CommonAttribute for owner attributes names constants
This commit is contained in:
PEDSF 2020-10-10 15:15:09 +02:00
parent 66c486891a
commit e77795d7f1
3 changed files with 67 additions and 48 deletions

View file

@ -0,0 +1,17 @@
package org.springframework.samples.petclinic.common;
public final class CommonAttribute {
public static String NAME = "name";
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";
private CommonAttribute() {
throw new IllegalStateException("Utility class");
}
}

View file

@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.controller;
import org.springframework.samples.petclinic.common.CommonAttribute;
import org.springframework.samples.petclinic.dto.*;
import org.springframework.samples.petclinic.service.OwnerService;
import org.springframework.samples.petclinic.service.VisitService;
@ -58,7 +59,7 @@ class OwnerController {
@GetMapping("/owners/new")
public String initCreationForm(Map<String, Object> model) {
OwnerDTO owner = new OwnerDTO();
model.put("owner", owner);
model.put(CommonAttribute.OWNER, owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
@ -75,12 +76,12 @@ class OwnerController {
@GetMapping("/owners/find")
public String initFindForm(Map<String, Object> model) {
model.put("owner", new OwnerDTO());
model.put(CommonAttribute.OWNER, new OwnerDTO());
return "owners/findOwners";
}
@GetMapping("/owners")
public String processFindForm(OwnerDTO owner, BindingResult result, Map<String, Object> model) {
public String processFindForm(@ModelAttribute("owner") OwnerDTO owner, BindingResult result, Map<String, Object> model) {
// allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) {
@ -91,7 +92,7 @@ class OwnerController {
Collection<OwnerDTO> results = this.ownerService.findByLastName(owner.getLastName());
if (results.isEmpty()) {
// no owners found
result.rejectValue("lastName", "notFound", "not found");
result.rejectValue(CommonAttribute.OWNER_LAST_NAME, "notFound", "not found");
return "owners/findOwners";
}
else if (results.size() == 1) {
@ -109,12 +110,12 @@ class OwnerController {
@GetMapping("/owners/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
OwnerDTO ownerDTO = this.ownerService.findById(ownerId);
model.addAttribute(ownerDTO);
model.addAttribute(CommonAttribute.OWNER, ownerDTO);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/owners/{ownerId}/edit")
public String processUpdateOwnerForm(@Valid OwnerDTO owner, BindingResult result,
public String processUpdateOwnerForm(@ModelAttribute("owner") @Valid OwnerDTO owner, BindingResult result,
@PathVariable("ownerId") int ownerId) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
@ -133,15 +134,15 @@ class OwnerController {
*/
@GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
ModelAndView modelAndView = new ModelAndView("owners/ownerDetails");
OwnerDTO owner = this.ownerService.findById(ownerId);
for (PetDTO petDTO : owner.getPets()) {
petDTO.setVisitsInternal(visitService.findByPetId(petDTO.getId()));
}
mav.addObject(owner);
return mav;
modelAndView.addObject(CommonAttribute.OWNER, owner);
return modelAndView;
}
}

View file

@ -28,6 +28,7 @@ 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.samples.petclinic.common.CommonAttribute;
import org.springframework.samples.petclinic.dto.OwnerDTO;
import org.springframework.samples.petclinic.dto.PetDTO;
import org.springframework.samples.petclinic.dto.PetTypeDTO;
@ -95,7 +96,7 @@ class OwnerControllerTests {
void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/new"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(model().attributeExists(CommonAttribute.OWNER))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}
@ -103,10 +104,10 @@ class OwnerControllerTests {
void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/owners/new")
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("address", "123 Caramel Street")
.param("city", "London")
.param("telephone", "01316761638"))
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street")
.param(CommonAttribute.OWNER_CITY, "London")
.param(CommonAttribute.OWNER_PHONE, "01316761638"))
.andExpect(status().is3xxRedirection());
}
@ -114,12 +115,12 @@ class OwnerControllerTests {
void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/new")
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("city", "London"))
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_CITY, "London"))
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner"))
.andExpect(model().attributeHasFieldErrors("owner", "address"))
.andExpect(model().attributeHasFieldErrors("owner", "telephone"))
.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"));
}
@ -127,7 +128,7 @@ class OwnerControllerTests {
void testInitFindForm() throws Exception {
mockMvc.perform(get("/owners/find"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(model().attributeExists(CommonAttribute.OWNER))
.andExpect(view().name("owners/findOwners"));
}
@ -145,7 +146,7 @@ class OwnerControllerTests {
given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
mockMvc.perform(get("/owners")
.param("lastName", "Franklin"))
.param(CommonAttribute.OWNER_LAST_NAME, "Franklin"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
}
@ -153,10 +154,10 @@ class OwnerControllerTests {
@Test
void testProcessFindFormNoOwnersFound() throws Exception {
mockMvc.perform(get("/owners")
.param("lastName", "Unknown Surname"))
.param(CommonAttribute.OWNER_LAST_NAME,"Unknown Surname"))
.andExpect(status().isOk())
.andExpect(model().attributeHasFieldErrors("owner", "lastName"))
.andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME))
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME, "notFound"))
.andExpect(view().name("owners/findOwners"));
}
@ -164,23 +165,23 @@ class OwnerControllerTests {
void testInitUpdateOwnerForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
.andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
.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_FIRST_NAME, is("George"))))
.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"));
}
@Test
void testProcessUpdateOwnerFormSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("address", "123 Caramel Street")
.param("city", "London")
.param("telephone", "01616291589"))
.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}"));
}
@ -188,13 +189,13 @@ class OwnerControllerTests {
@Test
void testProcessUpdateOwnerFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("city", "London"))
.param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
.param(CommonAttribute.OWNER_CITY, "London"))
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner"))
.andExpect(model().attributeHasFieldErrors("owner", "address"))
.andExpect(model().attributeHasFieldErrors("owner", "telephone"))
.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"));
}
@ -202,13 +203,13 @@ class OwnerControllerTests {
void testShowOwner() throws Exception {
mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))
.andExpect(status().isOk())
.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
.andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
.andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))
.andExpect(model().attribute("owner", hasProperty("pets", new BaseMatcher<List<PetDTO>>() {
.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_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(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PETS, not(empty()))))
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PETS, new BaseMatcher<List<PetDTO>>() {
@Override
public boolean matches(Object item) {