mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
solver @ModelAttributes in OwnerController
Implement CommonAttribute for owner attributes names constants
This commit is contained in:
parent
e77795d7f1
commit
da3666f1aa
8 changed files with 169 additions and 85 deletions
|
@ -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");
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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<String, Object> 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<String, Object> 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<String, Object> model) {
|
||||
@GetMapping(CommonEndPoint.OWNERS)
|
||||
public String processFindForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, BindingResult result, Map<String, Object> model) {
|
||||
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if (owner.getLastName() == null) {
|
||||
|
@ -92,38 +93,38 @@ class OwnerController {
|
|||
Collection<OwnerDTO> 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()) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue