mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
Add @Tag and refactoring directories
This commit is contained in:
parent
e041803e58
commit
20cfa0a078
40 changed files with 334 additions and 239 deletions
|
@ -6,21 +6,45 @@ package org.springframework.samples.petclinic.common;
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public final class CommonAttribute {
|
public final class CommonAttribute {
|
||||||
|
|
||||||
|
public static final String DESCRIPTION = "description";
|
||||||
|
|
||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
public static final String SELECTIONS = "selections";
|
|
||||||
|
public static final String NEW = "new";
|
||||||
|
|
||||||
public static final String OWNER = "owner";
|
public static final String OWNER = "owner";
|
||||||
|
|
||||||
public static final String OWNER_ID = "id";
|
public static final String OWNER_ID = "id";
|
||||||
|
|
||||||
public static final String OWNER_LAST_NAME = "lastName";
|
public static final String OWNER_LAST_NAME = "lastName";
|
||||||
|
|
||||||
public static final String OWNER_FIRST_NAME = "firstName";
|
public static final String OWNER_FIRST_NAME = "firstName";
|
||||||
|
|
||||||
public static final String OWNER_PHONE = "telephone";
|
public static final String OWNER_PHONE = "telephone";
|
||||||
|
|
||||||
public static final String OWNER_ADDRESS = "address";
|
public static final String OWNER_ADDRESS = "address";
|
||||||
|
|
||||||
public static final String OWNER_CITY = "city";
|
public static final String OWNER_CITY = "city";
|
||||||
|
|
||||||
public static final String OWNER_PETS = "pets";
|
public static final String OWNER_PETS = "pets";
|
||||||
|
|
||||||
public static final String PET = "pet";
|
public static final String PET = "pet";
|
||||||
|
|
||||||
|
public static final String SELECTIONS = "selections";
|
||||||
|
|
||||||
|
public static final String PET_BIRTH_DATE = "birthDate";
|
||||||
|
|
||||||
|
public static final String PET_NAME = "name";
|
||||||
|
|
||||||
|
public static final String PET_TYPE = "type";
|
||||||
|
|
||||||
|
public static final String VETS = "vets";
|
||||||
|
|
||||||
|
public static final String VISIT = "visit";
|
||||||
|
|
||||||
private CommonAttribute() {
|
private CommonAttribute() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,31 @@ package org.springframework.samples.petclinic.common;
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public final class CommonEndPoint {
|
public final class CommonEndPoint {
|
||||||
|
|
||||||
public static final String OWNERS = "/owners";
|
public static final String OWNERS = "/owners";
|
||||||
|
|
||||||
public static final String OWNERS_FIND = "/owners/find";
|
public static final String OWNERS_FIND = "/owners/find";
|
||||||
|
|
||||||
public static final String OWNERS_ID = "/owners/{ownerId}";
|
public static final String OWNERS_ID = "/owners/{ownerId}";
|
||||||
|
|
||||||
public static final String OWNERS_ID_EDIT = "/owners/{ownerId}/edit";
|
public static final String OWNERS_ID_EDIT = "/owners/{ownerId}/edit";
|
||||||
|
|
||||||
public static final String OWNERS_NEW = "/owners/new";
|
public static final String OWNERS_NEW = "/owners/new";
|
||||||
|
|
||||||
|
public static final String PETS_NEW = "/pets/new";
|
||||||
|
|
||||||
|
public static final String PETS_ID_EDIT = "/pets/{petId}/edit";
|
||||||
|
|
||||||
|
public static final String VETS = "/vets";
|
||||||
|
|
||||||
|
public static final String VETS_HTML = "/vets.html";
|
||||||
|
|
||||||
|
public static final String VISITS_NEW = "/owners/*/pets/{petId}/visits/new";
|
||||||
|
|
||||||
|
public static final String VISITS_EDIT = "/owners/{ownerId}/pets/{petId}/visits/new";
|
||||||
|
|
||||||
private CommonEndPoint() {
|
private CommonEndPoint() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,21 @@ package org.springframework.samples.petclinic.common;
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public final class CommonError {
|
public final class CommonError {
|
||||||
|
|
||||||
public static final String DUPLICATE_ARGS = "duplicate";
|
public static final String DUPLICATE_ARGS = "duplicate";
|
||||||
|
|
||||||
public static final String DUPLICATE_MESSAGE = "already exists";
|
public static final String DUPLICATE_MESSAGE = "already exists";
|
||||||
|
|
||||||
public static final String NOT_FOUND_ARGS = "notFound";
|
public static final String NOT_FOUND_ARGS = "notFound";
|
||||||
|
|
||||||
public static final String NOT_FOUND_MESSAGE = "notFound";
|
public static final String NOT_FOUND_MESSAGE = "notFound";
|
||||||
|
|
||||||
|
public static final String REQUIRED_ARGS = "required";
|
||||||
|
|
||||||
|
public static final String REQUIRED_MESSAGE = "required";
|
||||||
|
|
||||||
private CommonError() {
|
private CommonError() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,27 @@ package org.springframework.samples.petclinic.common;
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public final class CommonView {
|
public final class CommonView {
|
||||||
|
|
||||||
public static final String OWNER_OWNERS_R = "redirect:/owners/";
|
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_OWNERS_ID_R = "redirect:/owners/{ownerId}";
|
||||||
|
|
||||||
public static final String OWNER_CREATE_OR_UPDATE = "owners/createOrUpdateOwnerForm";
|
public static final String OWNER_CREATE_OR_UPDATE = "owners/createOrUpdateOwnerForm";
|
||||||
|
|
||||||
public static final String OWNER_FIND_OWNERS = "owners/findOwners";
|
public static final String OWNER_FIND_OWNERS = "owners/findOwners";
|
||||||
|
|
||||||
public static final String OWNER_OWNERS_LIST = "owners/ownersList";
|
public static final String OWNER_OWNERS_LIST = "owners/ownersList";
|
||||||
|
|
||||||
public static final String OWNER_DETAILS = "owners/ownerDetails";
|
public static final String OWNER_DETAILS = "owners/ownerDetails";
|
||||||
|
|
||||||
|
public static final String PET_CREATE_OR_UPDATE = "pets/createOrUpdatePetForm";
|
||||||
|
|
||||||
|
public static final String VET_VETS_LIST = "vets/vetList";
|
||||||
|
|
||||||
|
public static final String VISIT_CREATE_OR_UPDATE = "pets/createOrUpdateVisitForm";
|
||||||
|
|
||||||
private CommonView() {
|
private CommonView() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
*/
|
*/
|
||||||
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.CommonEndPoint;
|
||||||
import org.springframework.samples.petclinic.common.CommonError;
|
import org.springframework.samples.petclinic.common.CommonError;
|
||||||
|
@ -43,7 +42,9 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
class OwnerController {
|
class OwnerController {
|
||||||
|
|
||||||
private final OwnerService ownerService;
|
private final OwnerService ownerService;
|
||||||
|
|
||||||
private final VisitService visitService;
|
private final VisitService visitService;
|
||||||
|
|
||||||
OwnerController(OwnerService ownerService, VisitService visitService) {
|
OwnerController(OwnerService ownerService, VisitService visitService) {
|
||||||
|
@ -64,7 +65,8 @@ class OwnerController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(CommonEndPoint.OWNERS_NEW)
|
@PostMapping(CommonEndPoint.OWNERS_NEW)
|
||||||
public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner, BindingResult result) {
|
public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner,
|
||||||
|
BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return CommonView.OWNER_CREATE_OR_UPDATE;
|
return CommonView.OWNER_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
|
@ -81,7 +83,8 @@ class OwnerController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(CommonEndPoint.OWNERS)
|
@GetMapping(CommonEndPoint.OWNERS)
|
||||||
public String processFindForm(@ModelAttribute(CommonAttribute.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,7 +95,8 @@ 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, CommonError.NOT_FOUND_ARGS, CommonError.NOT_FOUND_MESSAGE);
|
result.rejectValue(CommonAttribute.OWNER_LAST_NAME, CommonError.NOT_FOUND_ARGS,
|
||||||
|
CommonError.NOT_FOUND_MESSAGE);
|
||||||
return CommonView.OWNER_FIND_OWNERS;
|
return CommonView.OWNER_FIND_OWNERS;
|
||||||
}
|
}
|
||||||
else if (results.size() == 1) {
|
else if (results.size() == 1) {
|
||||||
|
@ -115,8 +119,8 @@ class OwnerController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(CommonEndPoint.OWNERS_ID_EDIT)
|
@PostMapping(CommonEndPoint.OWNERS_ID_EDIT)
|
||||||
public String processUpdateOwnerForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner, BindingResult result,
|
public String processUpdateOwnerForm(@ModelAttribute(CommonAttribute.OWNER) @Valid OwnerDTO owner,
|
||||||
@PathVariable("ownerId") int ownerId) {
|
BindingResult result, @PathVariable("ownerId") int ownerId) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return CommonView.OWNER_CREATE_OR_UPDATE;
|
return CommonView.OWNER_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,9 @@ package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.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.validator.PetDTOValidator;
|
import org.springframework.samples.petclinic.validator.PetDTOValidator;
|
||||||
import org.springframework.samples.petclinic.service.*;
|
import org.springframework.samples.petclinic.service.*;
|
||||||
|
@ -38,11 +40,9 @@ import java.util.Collection;
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/owners/{ownerId}")
|
@RequestMapping(CommonEndPoint.OWNERS_ID)
|
||||||
class PetController {
|
class PetController {
|
||||||
|
|
||||||
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
|
||||||
|
|
||||||
private final OwnerService ownerService;
|
private final OwnerService ownerService;
|
||||||
|
|
||||||
private final PetService petService;
|
private final PetService petService;
|
||||||
|
@ -76,15 +76,15 @@ class PetController {
|
||||||
dataBinder.setValidator(new PetDTOValidator());
|
dataBinder.setValidator(new PetDTOValidator());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/pets/new")
|
@GetMapping(CommonEndPoint.PETS_NEW)
|
||||||
public String initCreationForm(@ModelAttribute(CommonAttribute.OWNER) 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(CommonAttribute.PET, pet);
|
model.put(CommonAttribute.PET, pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return CommonView.PET_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/pets/new")
|
@PostMapping(CommonEndPoint.PETS_NEW)
|
||||||
public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner,
|
public String processCreationForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner,
|
||||||
@ModelAttribute(CommonAttribute.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) {
|
||||||
|
@ -93,31 +93,33 @@ class PetController {
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.put(CommonAttribute.PET, pet);
|
model.put(CommonAttribute.PET, pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return CommonView.PET_CREATE_OR_UPDATE;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
this.petService.save(pet);
|
this.petService.save(pet);
|
||||||
return "redirect:/owners/{ownerId}";
|
return CommonView.OWNER_OWNERS_ID_R;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/pets/{petId}/edit")
|
@GetMapping(CommonEndPoint.PETS_ID_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(CommonAttribute.PET, pet);
|
model.put(CommonAttribute.PET, pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return CommonView.PET_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/pets/{petId}/edit")
|
@PostMapping(CommonEndPoint.PETS_ID_EDIT)
|
||||||
public String processUpdateForm(@ModelAttribute(CommonAttribute.PET) @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(CommonAttribute.PET, pet);
|
model.put(CommonAttribute.PET, pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return CommonView.PET_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
this.petService.save(pet);
|
this.petService.save(pet);
|
||||||
return "redirect:/owners/{ownerId}";
|
return CommonView.OWNER_OWNERS_ID_R;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,11 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.controller;
|
package org.springframework.samples.petclinic.controller;
|
||||||
|
|
||||||
|
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.VetsDTO;
|
import org.springframework.samples.petclinic.dto.VetsDTO;
|
||||||
import org.springframework.samples.petclinic.service.VetService;
|
import org.springframework.samples.petclinic.service.VetService;
|
||||||
import org.springframework.samples.petclinic.vet.Vets;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
@ -40,17 +42,17 @@ class VetController {
|
||||||
this.vetService = vetService;
|
this.vetService = vetService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/vets.html")
|
@GetMapping(CommonEndPoint.VETS_HTML)
|
||||||
public String showVetList(Map<String, Object> model) {
|
public String showVetList(Map<String, Object> model) {
|
||||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||||
// objects so it is simpler for Object-Xml mapping
|
// objects so it is simpler for Object-Xml mapping
|
||||||
VetsDTO vets = new VetsDTO();
|
VetsDTO vets = new VetsDTO();
|
||||||
vets.getVetList().addAll(this.vetService.findAll());
|
vets.getVetList().addAll(this.vetService.findAll());
|
||||||
model.put("vets", vets);
|
model.put(CommonAttribute.VETS, vets);
|
||||||
return "vets/vetList";
|
return CommonView.VET_VETS_LIST;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping({ "/vets" })
|
@GetMapping({ CommonEndPoint.VETS })
|
||||||
public @ResponseBody VetsDTO showResourcesVetList() {
|
public @ResponseBody VetsDTO showResourcesVetList() {
|
||||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||||
// objects so it is simpler for JSon/Object mapping
|
// objects so it is simpler for JSon/Object mapping
|
||||||
|
|
|
@ -19,6 +19,9 @@ import java.util.Map;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
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.PetDTO;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.samples.petclinic.dto.VisitDTO;
|
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||||
import org.springframework.samples.petclinic.service.PetService;
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
|
@ -68,27 +71,28 @@ class VisitController {
|
||||||
public VisitDTO loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
|
public VisitDTO loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||||
PetDTO pet = this.petService.findById(petId);
|
PetDTO pet = this.petService.findById(petId);
|
||||||
pet.setVisitsInternal(this.visitService.findByPetId(petId));
|
pet.setVisitsInternal(this.visitService.findByPetId(petId));
|
||||||
model.put("pet", pet);
|
model.put(CommonAttribute.PET, pet);
|
||||||
VisitDTO visit = new VisitDTO();
|
VisitDTO visit = new VisitDTO();
|
||||||
pet.addVisit(visit);
|
pet.addVisit(visit);
|
||||||
return visit;
|
return visit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||||
@GetMapping("/owners/*/pets/{petId}/visits/new")
|
@GetMapping(CommonEndPoint.VISITS_NEW)
|
||||||
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||||
return "pets/createOrUpdateVisitForm";
|
return CommonView.PET_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||||
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
|
@PostMapping(CommonEndPoint.VISITS_EDIT)
|
||||||
public String processNewVisitForm(@ModelAttribute("visit") @Valid VisitDTO visit, BindingResult result) {
|
public String processNewVisitForm(@ModelAttribute(CommonAttribute.VISIT) @Valid VisitDTO visit,
|
||||||
|
BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return "pets/createOrUpdateVisitForm";
|
return CommonView.VISIT_CREATE_OR_UPDATE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.visitService.save(visit);
|
this.visitService.save(visit);
|
||||||
return "redirect:/owners/{ownerId}";
|
return CommonView.OWNER_OWNERS_ID_R;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.dto;
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.core.style.ToStringCreator;
|
import org.springframework.core.style.ToStringCreator;
|
||||||
|
import org.springframework.samples.petclinic.common.CommonAttribute;
|
||||||
|
|
||||||
import javax.validation.constraints.Digits;
|
import javax.validation.constraints.Digits;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
@ -120,11 +121,11 @@ public class OwnerDTO extends PersonDTO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringCreator(this)
|
return new ToStringCreator(this).append(CommonAttribute.OWNER_ID, this.getId())
|
||||||
|
.append(CommonAttribute.NEW, this.isNew()).append(CommonAttribute.OWNER_LAST_NAME, this.getLastName())
|
||||||
.append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName())
|
.append(CommonAttribute.OWNER_FIRST_NAME, this.getFirstName())
|
||||||
.append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city)
|
.append(CommonAttribute.OWNER_ADDRESS, this.address).append(CommonAttribute.OWNER_CITY, this.city)
|
||||||
.append("telephone", this.telephone).toString();
|
.append(CommonAttribute.OWNER_PHONE, this.telephone).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ package org.springframework.samples.petclinic.dto;
|
||||||
|
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.samples.petclinic.vet.Specialty;
|
import org.springframework.samples.petclinic.model.Specialty;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
|
@ -13,13 +13,12 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.formatter;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.format.Formatter;
|
import org.springframework.format.Formatter;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
|
@ -38,7 +37,6 @@ import org.springframework.stereotype.Component;
|
||||||
@Component
|
@Component
|
||||||
public class PetTypeFormatter implements Formatter<PetTypeDTO> {
|
public class PetTypeFormatter implements Formatter<PetTypeDTO> {
|
||||||
|
|
||||||
// private final PetRepository pets;
|
|
||||||
private final PetTypeService petTypeService;
|
private final PetTypeService petTypeService;
|
||||||
|
|
||||||
public PetTypeFormatter(PetTypeService petTypeService) {
|
public PetTypeFormatter(PetTypeService petTypeService) {
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -32,7 +32,7 @@ import javax.validation.constraints.NotEmpty;
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.core.style.ToStringCreator;
|
import org.springframework.core.style.ToStringCreator;
|
||||||
import org.springframework.samples.petclinic.model.Person;
|
import org.springframework.samples.petclinic.common.CommonAttribute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing an owner.
|
* Simple JavaBean domain object representing an owner.
|
||||||
|
@ -140,11 +140,11 @@ public class Owner extends Person {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringCreator(this)
|
return new ToStringCreator(this).append(CommonAttribute.OWNER_ID, this.getId())
|
||||||
|
.append(CommonAttribute.NEW, this.isNew()).append(CommonAttribute.OWNER_LAST_NAME, this.getLastName())
|
||||||
.append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName())
|
.append(CommonAttribute.OWNER_FIRST_NAME, this.getFirstName())
|
||||||
.append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city)
|
.append(CommonAttribute.OWNER_ADDRESS, this.address).append(CommonAttribute.OWNER_CITY, this.city)
|
||||||
.append("telephone", this.telephone).toString();
|
.append(CommonAttribute.OWNER_PHONE, this.telephone).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -34,8 +34,6 @@ import javax.persistence.Transient;
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.samples.petclinic.model.NamedEntity;
|
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple business object representing a pet.
|
* Simple business object representing a pet.
|
|
@ -13,13 +13,11 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import org.springframework.samples.petclinic.model.NamedEntity;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Juergen Hoeller Can be Cat, Dog, Hamster...
|
* @author Juergen Hoeller Can be Cat, Dog, Hamster...
|
||||||
*/
|
*/
|
|
@ -13,15 +13,13 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.vet;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import org.springframework.samples.petclinic.model.NamedEntity;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models a {@link Vet Vet's} specialty (for example, dentistry).
|
* Models a {@link Vet Vet's} specialty (for example, dentistry).
|
||||||
*
|
*
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.vet;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -31,7 +31,6 @@ import javax.xml.bind.annotation.XmlElement;
|
||||||
|
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.samples.petclinic.model.Person;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing a veterinarian.
|
* Simple JavaBean domain object representing a veterinarian.
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.vet;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.visit;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@ -23,7 +23,6 @@ import javax.persistence.Table;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing a visit.
|
* Simple JavaBean domain object representing a visit.
|
|
@ -20,7 +20,7 @@ import java.util.Collection;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.samples.petclinic.owner.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,8 +19,8 @@ import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.owner.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.owner.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,7 +19,7 @@ import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.vet.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,7 +19,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.model.BaseEntity;
|
import org.springframework.samples.petclinic.model.BaseEntity;
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repository class for <code>Visit</code> domain objects All method names are compliant
|
* Repository class for <code>Visit</code> domain objects All method names are compliant
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||||
import org.springframework.samples.petclinic.owner.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.samples.petclinic.owner.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.owner.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.SpecialtyDTO;
|
import org.springframework.samples.petclinic.dto.SpecialtyDTO;
|
||||||
import org.springframework.samples.petclinic.vet.Specialty;
|
import org.springframework.samples.petclinic.model.Specialty;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
|
@ -3,7 +3,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.VetDTO;
|
import org.springframework.samples.petclinic.dto.VetDTO;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||||
import org.springframework.samples.petclinic.vet.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
|
@ -2,7 +2,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.VisitDTO;
|
import org.springframework.samples.petclinic.dto.VisitDTO;
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
|
@ -34,9 +34,7 @@ class CacheConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
|
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
|
||||||
return cm -> {
|
return cm -> cm.createCache("vets", cacheConfiguration());
|
||||||
cm.createCache("vets", cacheConfiguration());
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.validator;
|
package org.springframework.samples.petclinic.validator;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.common.CommonAttribute;
|
||||||
|
import org.springframework.samples.petclinic.common.CommonError;
|
||||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
|
@ -29,28 +31,27 @@ import org.springframework.validation.Validator;
|
||||||
*
|
*
|
||||||
* @author Ken Krebs
|
* @author Ken Krebs
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public class PetDTOValidator implements Validator {
|
public class PetDTOValidator implements Validator {
|
||||||
|
|
||||||
private static final String REQUIRED = "required";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validate(Object obj, Errors errors) {
|
public void validate(Object obj, Errors errors) {
|
||||||
PetDTO pet = (PetDTO) obj;
|
PetDTO pet = (PetDTO) obj;
|
||||||
String name = pet.getName();
|
String name = pet.getName();
|
||||||
// name validation
|
// name validation
|
||||||
if (!StringUtils.hasLength(name)) {
|
if (!StringUtils.hasLength(name)) {
|
||||||
errors.rejectValue("name", REQUIRED, REQUIRED);
|
errors.rejectValue(CommonAttribute.PET_NAME, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// type validation
|
// type validation
|
||||||
if (pet.isNew() && pet.getType() == null) {
|
if (pet.isNew() && pet.getType() == null) {
|
||||||
errors.rejectValue("type", REQUIRED, REQUIRED);
|
errors.rejectValue(CommonAttribute.PET_TYPE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// birth date validation
|
// birth date validation
|
||||||
if (pet.getBirthDate() == null) {
|
if (pet.getBirthDate() == null) {
|
||||||
errors.rejectValue("birthDate", REQUIRED, REQUIRED);
|
errors.rejectValue(CommonAttribute.PET_BIRTH_DATE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,9 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.validator;
|
package org.springframework.samples.petclinic.validator;
|
||||||
|
|
||||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
import org.springframework.samples.petclinic.common.CommonAttribute;
|
||||||
import org.springframework.samples.petclinic.owner.Pet;
|
import org.springframework.samples.petclinic.common.CommonError;
|
||||||
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
|
@ -30,28 +31,27 @@ import org.springframework.validation.Validator;
|
||||||
*
|
*
|
||||||
* @author Ken Krebs
|
* @author Ken Krebs
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public class PetValidator implements Validator {
|
public class PetValidator implements Validator {
|
||||||
|
|
||||||
private static final String REQUIRED = "required";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validate(Object obj, Errors errors) {
|
public void validate(Object obj, Errors errors) {
|
||||||
Pet pet = (Pet) obj;
|
Pet pet = (Pet) obj;
|
||||||
String name = pet.getName();
|
String name = pet.getName();
|
||||||
// name validation
|
// name validation
|
||||||
if (!StringUtils.hasLength(name)) {
|
if (!StringUtils.hasLength(name)) {
|
||||||
errors.rejectValue("name", REQUIRED, REQUIRED);
|
errors.rejectValue(CommonAttribute.PET_NAME, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// type validation
|
// type validation
|
||||||
if (pet.isNew() && pet.getType() == null) {
|
if (pet.isNew() && pet.getType() == null) {
|
||||||
errors.rejectValue("type", REQUIRED, REQUIRED);
|
errors.rejectValue(CommonAttribute.PET_TYPE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// birth date validation
|
// birth date validation
|
||||||
if (pet.getBirthDate() == null) {
|
if (pet.getBirthDate() == null) {
|
||||||
errors.rejectValue("birthDate", REQUIRED, REQUIRED);
|
errors.rejectValue(CommonAttribute.PET_BIRTH_DATE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.assertj.core.util.Lists;
|
||||||
import org.hamcrest.BaseMatcher;
|
import org.hamcrest.BaseMatcher;
|
||||||
import org.hamcrest.Description;
|
import org.hamcrest.Description;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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;
|
||||||
|
@ -96,123 +97,129 @@ class OwnerControllerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("initCreationForm")
|
||||||
void testInitCreationForm() throws Exception {
|
void testInitCreationForm() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.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(CommonView.OWNER_CREATE_OR_UPDATE));
|
.andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormSuccess() throws Exception {
|
void testProcessCreationFormSuccess() throws Exception {
|
||||||
mockMvc.perform(post("/owners/new")
|
mockMvc.perform(post("/owners/new").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, "01316761638")).andExpect(status().is3xxRedirection());
|
||||||
.param(CommonAttribute.OWNER_PHONE, "01316761638"))
|
|
||||||
.andExpect(status().is3xxRedirection());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormHasErrors() throws Exception {
|
void testProcessCreationFormHasErrors() throws Exception {
|
||||||
mockMvc.perform(post(CommonEndPoint.OWNERS_NEW)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_NEW).param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
|
||||||
.param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
|
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs").param(CommonAttribute.OWNER_CITY, "London"))
|
||||||
.param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
|
.andExpect(status().isOk()).andExpect(model().attributeHasErrors(CommonAttribute.OWNER))
|
||||||
.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_ADDRESS))
|
||||||
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE))
|
.andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE))
|
||||||
.andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
|
.andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("initFindForm")
|
||||||
void testInitFindForm() throws Exception {
|
void testInitFindForm() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.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(CommonView.OWNER_FIND_OWNERS));
|
.andExpect(view().name(CommonView.OWNER_FIND_OWNERS));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processFindForm")
|
||||||
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(CommonEndPoint.OWNERS))
|
mockMvc.perform(get(CommonEndPoint.OWNERS)).andExpect(status().isOk())
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(view().name(CommonView.OWNER_OWNERS_LIST));
|
.andExpect(view().name(CommonView.OWNER_OWNERS_LIST));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processFindForm")
|
||||||
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(CommonEndPoint.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(CommonView.OWNER_OWNERS_R + TEST_OWNER_ID));
|
.andExpect(view().name(CommonView.OWNER_OWNERS_R + TEST_OWNER_ID));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processFindForm")
|
||||||
void testProcessFindFormNoOwnersFound() throws Exception {
|
void testProcessFindFormNoOwnersFound() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.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(CommonView.OWNER_FIND_OWNERS));
|
.andExpect(view().name(CommonView.OWNER_FIND_OWNERS));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("initUpdateOwnerForm")
|
||||||
void testInitUpdateOwnerForm() throws Exception {
|
void testInitUpdateOwnerForm() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.OWNERS_ID_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,
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George"))))
|
hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin"))))
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_ADDRESS, is("110 W. Liberty St."))))
|
.andExpect(model().attribute(CommonAttribute.OWNER,
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_CITY, is("Madison"))))
|
hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George"))))
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PHONE, is("6085551023"))))
|
.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(CommonView.OWNER_CREATE_OR_UPDATE));
|
.andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processUpdateOwnerForm")
|
||||||
void testProcessUpdateOwnerFormSuccess() throws Exception {
|
void testProcessUpdateOwnerFormSuccess() throws Exception {
|
||||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID_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_CITY, "London")
|
||||||
.param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street")
|
.param(CommonAttribute.OWNER_PHONE, "01616291589")).andExpect(status().is3xxRedirection())
|
||||||
.param(CommonAttribute.OWNER_CITY, "London")
|
|
||||||
.param(CommonAttribute.OWNER_PHONE, "01616291589"))
|
|
||||||
.andExpect(status().is3xxRedirection())
|
|
||||||
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processUpdateOwnerForm")
|
||||||
void testProcessUpdateOwnerFormHasErrors() throws Exception {
|
void testProcessUpdateOwnerFormHasErrors() throws Exception {
|
||||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID)
|
mockMvc.perform(
|
||||||
.param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
|
post(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID).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(model().attributeHasErrors(CommonAttribute.OWNER))
|
||||||
.andExpect(status().isOk())
|
|
||||||
.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(CommonView.OWNER_CREATE_OR_UPDATE));
|
.andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processUpdateOwnerForm")
|
||||||
void testShowOwner() throws Exception {
|
void testShowOwner() throws Exception {
|
||||||
mockMvc.perform(get(CommonEndPoint.OWNERS_ID, TEST_OWNER_ID))
|
mockMvc.perform(get(CommonEndPoint.OWNERS_ID, TEST_OWNER_ID)).andExpect(status().isOk())
|
||||||
.andExpect(status().isOk())
|
.andExpect(model().attribute(CommonAttribute.OWNER,
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin"))))
|
hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin"))))
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George"))))
|
.andExpect(model().attribute(CommonAttribute.OWNER,
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_ADDRESS, is("110 W. Liberty St."))))
|
hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George"))))
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_CITY, is("Madison"))))
|
.andExpect(model().attribute(CommonAttribute.OWNER,
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PHONE, is("6085551023"))))
|
hasProperty(CommonAttribute.OWNER_ADDRESS, is("110 W. Liberty St."))))
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PETS, not(empty()))))
|
.andExpect(model().attribute(CommonAttribute.OWNER,
|
||||||
.andExpect(model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PETS, new BaseMatcher<List<PetDTO>>() {
|
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
|
@Override
|
||||||
public boolean matches(Object item) {
|
public boolean matches(Object item) {
|
||||||
|
|
|
@ -25,16 +25,21 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
|
|
||||||
import org.assertj.core.util.Lists;
|
import org.assertj.core.util.Lists;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.FilterType;
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
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.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;
|
||||||
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
|
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
||||||
import org.springframework.samples.petclinic.service.OwnerService;
|
import org.springframework.samples.petclinic.service.OwnerService;
|
||||||
import org.springframework.samples.petclinic.service.PetService;
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
|
@ -44,6 +49,7 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||||
* Test class for the {@link PetController}
|
* Test class for the {@link PetController}
|
||||||
*
|
*
|
||||||
* @author Colin But
|
* @author Colin But
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
@WebMvcTest(value = PetController.class,
|
@WebMvcTest(value = PetController.class,
|
||||||
includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
|
includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
|
||||||
|
@ -77,63 +83,60 @@ class PetControllerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("initCreationForm")
|
||||||
void testInitCreationForm() throws Exception {
|
void testInitCreationForm() throws Exception {
|
||||||
mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
|
mockMvc.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE))
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"))
|
.andExpect(model().attributeExists(CommonAttribute.PET));
|
||||||
.andExpect(model().attributeExists("pet"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormSuccess() throws Exception {
|
void testProcessCreationFormSuccess() throws Exception {
|
||||||
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
.param("name", "Betty")
|
.param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_TYPE, "hamster")
|
||||||
.param("type", "hamster")
|
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12")).andExpect(status().is3xxRedirection())
|
||||||
.param("birthDate", "2015-02-12"))
|
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
||||||
.andExpect(status().is3xxRedirection())
|
|
||||||
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processCreationForm")
|
||||||
void testProcessCreationFormHasErrors() throws Exception {
|
void testProcessCreationFormHasErrors() throws Exception {
|
||||||
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||||
.param("name", "Betty")
|
.param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||||
.param("birthDate","2015-02-12"))
|
.andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasErrors(CommonAttribute.PET))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasFieldErrors(CommonAttribute.PET, CommonAttribute.PET_TYPE))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "type"))
|
.andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.PET_TYPE,
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required"))
|
CommonError.REQUIRED_ARGS))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("initUpdateForm")
|
||||||
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(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk()).andExpect(model().attributeExists(CommonAttribute.PET))
|
||||||
.andExpect(model().attributeExists("pet"))
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processUpdateForm")
|
||||||
void testProcessUpdateFormSuccess() throws Exception {
|
void testProcessUpdateFormSuccess() throws Exception {
|
||||||
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID)
|
||||||
.param("name", "Betty")
|
.param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_TYPE, "hamster")
|
||||||
.param("type", "hamster")
|
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12")).andExpect(status().is3xxRedirection())
|
||||||
.param("birthDate", "2015-02-12"))
|
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
||||||
.andExpect(status().is3xxRedirection())
|
|
||||||
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processUpdateForm")
|
||||||
void testProcessUpdateFormHasErrors() throws Exception {
|
void testProcessUpdateFormHasErrors() throws Exception {
|
||||||
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
|
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID)
|
||||||
.param("name", "Betty")
|
.param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||||
.param("birthDate", "2015/02/12"))
|
.andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasErrors(CommonAttribute.PET)).andExpect(status().isOk())
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,19 +26,25 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
|
|
||||||
import org.assertj.core.util.Lists;
|
import org.assertj.core.util.Lists;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
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.VetDTO;
|
import org.springframework.samples.petclinic.dto.VetDTO;
|
||||||
import org.springframework.samples.petclinic.service.VetService;
|
import org.springframework.samples.petclinic.service.VetService;
|
||||||
import org.springframework.samples.petclinic.vet.Specialty;
|
import org.springframework.samples.petclinic.model.Specialty;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.ResultActions;
|
import org.springframework.test.web.servlet.ResultActions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class for the {@link VetController}
|
* Test class for the {@link VetController}
|
||||||
|
*
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
@WebMvcTest(VetController.class)
|
@WebMvcTest(VetController.class)
|
||||||
class VetControllerTests {
|
class VetControllerTests {
|
||||||
|
@ -67,15 +73,19 @@ class VetControllerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("showVetList")
|
||||||
void testShowVetListHtml() throws Exception {
|
void testShowVetListHtml() throws Exception {
|
||||||
mockMvc.perform(get("/vets.html")).andExpect(status().isOk()).andExpect(model().attributeExists("vets"))
|
mockMvc.perform(get(CommonEndPoint.VETS_HTML)).andExpect(status().isOk())
|
||||||
.andExpect(view().name("vets/vetList"));
|
.andExpect(model().attributeExists(CommonAttribute.VETS))
|
||||||
|
.andExpect(view().name(CommonView.VET_VETS_LIST));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("showResourcesVetList")
|
||||||
void testShowResourcesVetList() throws Exception {
|
void testShowResourcesVetList() throws Exception {
|
||||||
ResultActions actions = mockMvc.perform(get("/vets").accept(MediaType.APPLICATION_JSON))
|
ResultActions actions = mockMvc.perform(get(CommonEndPoint.VETS).accept(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
|
|
||||||
actions.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
actions.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(jsonPath("$.vetList[0].id").value(1));
|
.andExpect(jsonPath("$.vetList[0].id").value(1));
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,10 +24,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.CommonEndPoint;
|
||||||
|
import org.springframework.samples.petclinic.common.CommonView;
|
||||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
import org.springframework.samples.petclinic.service.PetService;
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.service.VisitService;
|
import org.springframework.samples.petclinic.service.VisitService;
|
||||||
|
@ -58,24 +62,26 @@ class VisitControllerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("initNewVisitForm")
|
||||||
void testInitNewVisitForm() throws Exception {
|
void testInitNewVisitForm() throws Exception {
|
||||||
mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))
|
mockMvc.perform(get(CommonEndPoint.VISITS_NEW, TEST_PET_ID)).andExpect(status().isOk())
|
||||||
.andExpect(status().isOk())
|
.andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
|
||||||
.andExpect(view().name("pets/createOrUpdateVisitForm"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processNewVisitForm")
|
||||||
void testProcessNewVisitFormSuccess() throws Exception {
|
void testProcessNewVisitFormSuccess() throws Exception {
|
||||||
mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George")
|
mockMvc.perform(post(CommonEndPoint.VISITS_NEW, TEST_PET_ID).param(CommonAttribute.NAME, "George")
|
||||||
.param("description", "Visit Description")).andExpect(status().is3xxRedirection())
|
.param(CommonAttribute.DESCRIPTION, "Visit Description")).andExpect(status().is3xxRedirection())
|
||||||
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("processNewVisitForm")
|
||||||
void testProcessNewVisitFormHasErrors() throws Exception {
|
void testProcessNewVisitFormHasErrors() throws Exception {
|
||||||
mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George"))
|
mockMvc.perform(post(CommonEndPoint.VISITS_NEW, TEST_PET_ID).param(CommonAttribute.NAME, "George"))
|
||||||
.andExpect(model().attributeHasErrors("visit")).andExpect(status().isOk())
|
.andExpect(model().attributeHasErrors(CommonAttribute.VISIT)).andExpect(status().isOk())
|
||||||
.andExpect(view().name("pets/createOrUpdateVisitForm"));
|
.andExpect(view().name(CommonView.VISIT_CREATE_OR_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.formater;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
@ -23,6 +23,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
|
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
||||||
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.formater;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -29,8 +29,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
||||||
import org.springframework.samples.petclinic.service.PetService;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.vet;
|
package org.springframework.samples.petclinic.model;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.util.SerializationUtils;
|
import org.springframework.util.SerializationUtils;
|
|
@ -25,14 +25,14 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.samples.petclinic.owner.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
import org.springframework.samples.petclinic.owner.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||||
import org.springframework.samples.petclinic.owner.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
import org.springframework.samples.petclinic.vet.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||||
import org.springframework.samples.petclinic.visit.Visit;
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.model;
|
package org.springframework.samples.petclinic.validator;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -24,6 +24,7 @@ import javax.validation.Validator;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
import org.springframework.samples.petclinic.model.Person;
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
Loading…
Reference in a new issue