Modularize and migrate to aggregate-oriented domain

Vet, Owner, Visit. The Visit "aggregate" is a little artificial
but it demonstrates a useful point about not holding on to
references of "parent" (reference data) objects, i.e. the Visit has
an Integer petId, instead of a Pet field. In principle this app is
now almost ready to migrate to multiple services if anyone wanted
to do that.
This commit is contained in:
Dave Syer 2016-11-10 22:10:56 +00:00
parent 8c8599298a
commit 83ff9a50e3
34 changed files with 270 additions and 384 deletions

View file

@ -21,6 +21,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* PetClinic Spring Boot Application. * PetClinic Spring Boot Application.
*
* @author Dave Syer
* *
*/ */
@SpringBootApplication @SpringBootApplication

View file

@ -1,5 +1,5 @@
/** /**
* The classes in this package represent PetClinic's business layer. * The classes in this package represent utilities used by the domain.
*/ */
package org.springframework.samples.petclinic.model; package org.springframework.samples.petclinic.model;

View file

@ -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.model; package org.springframework.samples.petclinic.owner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -32,6 +32,7 @@ import org.hibernate.validator.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;
/** /**
* Simple JavaBean domain object representing an owner. * Simple JavaBean domain object representing an owner.

View file

@ -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.web; package org.springframework.samples.petclinic.owner;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -21,8 +21,6 @@ import java.util.Map;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
@ -40,15 +38,15 @@ import org.springframework.web.servlet.ModelAndView;
* @author Michael Isvy * @author Michael Isvy
*/ */
@Controller @Controller
public class OwnerController { class OwnerController {
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final ClinicService clinicService; private final OwnerRepository owners;
@Autowired @Autowired
public OwnerController(ClinicService clinicService) { public OwnerController(OwnerRepository clinicService) {
this.clinicService = clinicService; this.owners = clinicService;
} }
@InitBinder @InitBinder
@ -68,7 +66,7 @@ public class OwnerController {
if (result.hasErrors()) { if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else { } else {
this.clinicService.saveOwner(owner); this.owners.save(owner);
return "redirect:/owners/" + owner.getId(); return "redirect:/owners/" + owner.getId();
} }
} }
@ -88,7 +86,7 @@ public class OwnerController {
} }
// find owners by last name // find owners by last name
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName()); Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
if (results.isEmpty()) { if (results.isEmpty()) {
// no owners found // no owners found
result.rejectValue("lastName", "notFound", "not found"); result.rejectValue("lastName", "notFound", "not found");
@ -106,7 +104,7 @@ public class OwnerController {
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET) @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET)
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.clinicService.findOwnerById(ownerId); Owner owner = this.owners.findById(ownerId);
model.addAttribute(owner); model.addAttribute(owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} }
@ -117,7 +115,7 @@ public class OwnerController {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else { } else {
owner.setId(ownerId); owner.setId(ownerId);
this.clinicService.saveOwner(owner); this.owners.save(owner);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }
@ -131,7 +129,7 @@ public class OwnerController {
@RequestMapping("/owners/{ownerId}") @RequestMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails"); ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(this.clinicService.findOwnerById(ownerId)); mav.addObject(this.owners.findById(ownerId));
return mav; return mav;
} }

View file

@ -13,14 +13,14 @@
* 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.repository; package org.springframework.samples.petclinic.owner;
import java.util.Collection; 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.model.Owner; import org.springframework.transaction.annotation.Transactional;
/** /**
* Repository class for <code>Owner</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Owner</code> domain objects All method names are compliant with Spring Data naming
@ -41,6 +41,7 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
* found) * found)
*/ */
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
@Transactional(readOnly = true)
Collection<Owner> findByLastName(@Param("lastName") String lastName); Collection<Owner> findByLastName(@Param("lastName") String lastName);
/** /**
@ -49,6 +50,7 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
* @return the {@link Owner} if found * @return the {@link Owner} if found
*/ */
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id") @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
@Transactional(readOnly = true)
Owner findById(@Param("id") Integer id); Owner findById(@Param("id") Integer id);
/** /**

View file

@ -13,11 +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.model; package org.springframework.samples.petclinic.owner;
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;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
@ -62,10 +64,9 @@ public class Pet extends NamedEntity {
@JoinColumn(name = "owner_id") @JoinColumn(name = "owner_id")
private Owner owner; private Owner owner;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER) @OneToMany(cascade = CascadeType.ALL, mappedBy="petId", fetch = FetchType.EAGER)
private Set<Visit> visits = new LinkedHashSet<>(); private Set<Visit> visits = new LinkedHashSet<>();
public void setBirthDate(Date birthDate) { public void setBirthDate(Date birthDate) {
this.birthDate = birthDate; this.birthDate = birthDate;
} }
@ -109,11 +110,7 @@ public class Pet extends NamedEntity {
public void addVisit(Visit visit) { public void addVisit(Visit visit) {
getVisitsInternal().add(visit); getVisitsInternal().add(visit);
visit.setPet(this); visit.setPetId(this.id);
} }
} }

View file

@ -13,23 +13,23 @@
* 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.web; package org.springframework.samples.petclinic.owner;
import java.util.Collection;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.validation.Valid; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Collection; import org.springframework.web.bind.annotation.RequestMethod;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
@ -38,24 +38,26 @@ import java.util.Collection;
*/ */
@Controller @Controller
@RequestMapping("/owners/{ownerId}") @RequestMapping("/owners/{ownerId}")
public class PetController { class PetController {
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
private final ClinicService clinicService; private final PetRepository pets;
private final OwnerRepository owners;
@Autowired @Autowired
public PetController(ClinicService clinicService) { public PetController(PetRepository pets, OwnerRepository owners) {
this.clinicService = clinicService; this.pets = pets;
this.owners = owners;
} }
@ModelAttribute("types") @ModelAttribute("types")
public Collection<PetType> populatePetTypes() { public Collection<PetType> populatePetTypes() {
return this.clinicService.findPetTypes(); return this.pets.findPetTypes();
} }
@ModelAttribute("owner") @ModelAttribute("owner")
public Owner findOwner(@PathVariable("ownerId") int ownerId) { public Owner findOwner(@PathVariable("ownerId") int ownerId) {
return this.clinicService.findOwnerById(ownerId); return this.owners.findById(ownerId);
} }
@InitBinder("owner") @InitBinder("owner")
@ -86,14 +88,14 @@ public class PetController {
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} else { } else {
owner.addPet(pet); owner.addPet(pet);
this.clinicService.savePet(pet); this.pets.save(pet);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }
@RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.GET) @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.GET)
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
Pet pet = this.clinicService.findPetById(petId); Pet pet = this.pets.findById(petId);
model.put("pet", pet); model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} }
@ -105,7 +107,7 @@ public class PetController {
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} else { } else {
owner.addPet(pet); owner.addPet(pet);
this.clinicService.savePet(pet); this.pets.save(pet);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }

View file

@ -13,14 +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.repository; package org.springframework.samples.petclinic.owner;
import java.util.List; 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.model.Pet; import org.springframework.transaction.annotation.Transactional;
import org.springframework.samples.petclinic.model.PetType;
/** /**
* Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming
@ -38,6 +37,7 @@ public interface PetRepository extends Repository<Pet, Integer> {
* @return a Collection of {@link PetType}s. * @return a Collection of {@link PetType}s.
*/ */
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
@Transactional(readOnly = true)
List<PetType> findPetTypes(); List<PetType> findPetTypes();
/** /**
@ -45,6 +45,7 @@ public interface PetRepository extends Repository<Pet, Integer> {
* @param id the id to search for * @param id the id to search for
* @return the {@link Pet} if found * @return the {@link Pet} if found
*/ */
@Transactional(readOnly = true)
Pet findById(Integer id); Pet findById(Integer id);
/** /**

View file

@ -13,11 +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.model; package org.springframework.samples.petclinic.owner;
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 * @author Juergen Hoeller
* Can be Cat, Dog, Hamster... * Can be Cat, Dog, Hamster...

View file

@ -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.web; package org.springframework.samples.petclinic.owner;
import java.text.ParseException; import java.text.ParseException;
@ -22,8 +22,6 @@ import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter; import org.springframework.format.Formatter;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -32,7 +30,6 @@ import org.springframework.stereotype.Component;
* Spring ref doc: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#format-Formatter-SPI * Spring ref doc: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#format-Formatter-SPI
* - A nice blog entry from Gordon Dickens: http://gordondickens.com/wordpress/2010/09/30/using-spring-3-0-custom-type-converter/ * - A nice blog entry from Gordon Dickens: http://gordondickens.com/wordpress/2010/09/30/using-spring-3-0-custom-type-converter/
* <p/> * <p/>
* Also see how the bean 'conversionService' has been declared inside /WEB-INF/mvc-core-config.xml
* *
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller * @author Juergen Hoeller
@ -41,12 +38,12 @@ import org.springframework.stereotype.Component;
@Component @Component
public class PetTypeFormatter implements Formatter<PetType> { public class PetTypeFormatter implements Formatter<PetType> {
private final ClinicService clinicService; private final PetRepository pets;
@Autowired @Autowired
public PetTypeFormatter(ClinicService clinicService) { public PetTypeFormatter(PetRepository pets) {
this.clinicService = clinicService; this.pets = pets;
} }
@Override @Override
@ -56,7 +53,7 @@ public class PetTypeFormatter implements Formatter<PetType> {
@Override @Override
public PetType parse(String text, Locale locale) throws ParseException { public PetType parse(String text, Locale locale) throws ParseException {
Collection<PetType> findPetTypes = this.clinicService.findPetTypes(); Collection<PetType> findPetTypes = this.pets.findPetTypes();
for (PetType type : findPetTypes) { for (PetType type : findPetTypes) {
if (type.getName().equals(text)) { if (type.getName().equals(text)) {
return type; return type;

View file

@ -13,9 +13,8 @@
* 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.web; package org.springframework.samples.petclinic.owner;
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;

View file

@ -13,16 +13,15 @@
* 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.web; package org.springframework.samples.petclinic.owner;
import java.util.Map; import java.util.Map;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.visit.Visit;
import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
@ -37,16 +36,19 @@ import org.springframework.web.bind.annotation.RequestMethod;
* @author Ken Krebs * @author Ken Krebs
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Michael Isvy * @author Michael Isvy
* @author Dave Syer
*/ */
@Controller @Controller
public class VisitController { class VisitController {
private final ClinicService clinicService; private final VisitRepository visits;
private final PetRepository pets;
@Autowired @Autowired
public VisitController(ClinicService clinicService) { public VisitController(VisitRepository visits, PetRepository pets) {
this.clinicService = clinicService; this.visits = visits;
this.pets = pets;
} }
@InitBinder @InitBinder
@ -65,8 +67,9 @@ public class VisitController {
* @return Pet * @return Pet
*/ */
@ModelAttribute("visit") @ModelAttribute("visit")
public Visit loadPetWithVisit(@PathVariable("petId") int petId) { public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
Pet pet = this.clinicService.findPetById(petId); Pet pet = this.pets.findById(petId);
model.put("pet", pet);
Visit visit = new Visit(); Visit visit = new Visit();
pet.addVisit(visit); pet.addVisit(visit);
return visit; return visit;
@ -84,7 +87,7 @@ public class VisitController {
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm"; return "pets/createOrUpdateVisitForm";
} else { } else {
this.clinicService.saveVisit(visit); this.visits.save(visit);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }

View file

@ -1,53 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.service;
import java.util.Collection;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Visit;
/**
* Mostly used as a facade so all controllers have a single point of entry
*
* @author Michael Isvy
*/
public interface ClinicService {
Collection<PetType> findPetTypes() throws DataAccessException;
Owner findOwnerById(int id) throws DataAccessException;
Pet findPetById(int id) throws DataAccessException;
void savePet(Pet pet) throws DataAccessException;
void saveVisit(Visit visit) throws DataAccessException;
Collection<Vet> findVets() throws DataAccessException;
void saveOwner(Owner owner) throws DataAccessException;
Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException;
Collection<Visit> findVisitsByPetId(int petId);
}

View file

@ -1,108 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.model.*;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.cache.annotation.CacheResult;
import java.util.Collection;
/**
* Mostly used as a facade for all Petclinic controllers Also a placeholder
* for @Transactional and @CacheResult annotations
*
* @author Michael Isvy
*/
@Service
public class ClinicServiceImpl implements ClinicService {
private PetRepository petRepository;
private VetRepository vetRepository;
private OwnerRepository ownerRepository;
private VisitRepository visitRepository;
@Autowired
public ClinicServiceImpl(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository,
VisitRepository visitRepository) {
this.petRepository = petRepository;
this.vetRepository = vetRepository;
this.ownerRepository = ownerRepository;
this.visitRepository = visitRepository;
}
@Override
@Transactional(readOnly = true)
public Collection<PetType> findPetTypes() throws DataAccessException {
return petRepository.findPetTypes();
}
@Override
@Transactional(readOnly = true)
public Owner findOwnerById(int id) throws DataAccessException {
return ownerRepository.findById(id);
}
@Override
@Transactional(readOnly = true)
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
return ownerRepository.findByLastName(lastName);
}
@Override
@Transactional
public void saveOwner(Owner owner) throws DataAccessException {
ownerRepository.save(owner);
}
@Override
@Transactional
public void saveVisit(Visit visit) throws DataAccessException {
visitRepository.save(visit);
}
@Override
@Transactional(readOnly = true)
public Pet findPetById(int id) throws DataAccessException {
return petRepository.findById(id);
}
@Override
@Transactional
public void savePet(Pet pet) throws DataAccessException {
petRepository.save(pet);
}
@Override
@Transactional(readOnly = true)
@CacheResult(cacheName = "vets")
public Collection<Vet> findVets() throws DataAccessException {
return vetRepository.findAll();
}
@Override
public Collection<Visit> findVisitsByPetId(int petId) {
return visitRepository.findByPetId(petId);
}
}

View file

@ -1,4 +1,4 @@
package org.springframework.samples.petclinic.config; package org.springframework.samples.petclinic.system;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.cache.CacheManager; import javax.cache.CacheManager;
@ -23,7 +23,7 @@ import org.springframework.context.annotation.Profile;
@Configuration @Configuration
@EnableCaching @EnableCaching
@Profile("production") @Profile("production")
public class CacheConfig { class CacheConfig {
@Bean @Bean
public JCacheManagerCustomizer cacheManagerCustomizer() { public JCacheManagerCustomizer cacheManagerCustomizer() {

View file

@ -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.web; package org.springframework.samples.petclinic.system;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -27,7 +27,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
* Also see how a view that resolves to "error" has been added ("error.html"). * Also see how a view that resolves to "error" has been added ("error.html").
*/ */
@Controller @Controller
public class CrashController { class CrashController {
@RequestMapping(value = "/oups", method = RequestMethod.GET) @RequestMapping(value = "/oups", method = RequestMethod.GET)
public String triggerException() { public String triggerException() {

View file

@ -1,11 +1,11 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.system;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
public class WelcomeController { class WelcomeController {
@RequestMapping("/") @RequestMapping("/")
public String welcome() { public String welcome() {

View file

@ -13,11 +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.model; package org.springframework.samples.petclinic.vet;
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).
* *

View file

@ -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.model; package org.springframework.samples.petclinic.vet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -31,6 +31,7 @@ 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.

View file

@ -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.web; package org.springframework.samples.petclinic.vet;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Vets;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -31,13 +29,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
@Controller @Controller
public class VetController { class VetController {
private final ClinicService clinicService; private final VetRepository vets;
@Autowired @Autowired
public VetController(ClinicService clinicService) { public VetController(VetRepository clinicService) {
this.clinicService = clinicService; this.vets = clinicService;
} }
@RequestMapping(value = { "/vets.html" }) @RequestMapping(value = { "/vets.html" })
@ -45,7 +43,7 @@ public class VetController {
// 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
Vets vets = new Vets(); Vets vets = new Vets();
vets.getVetList().addAll(this.clinicService.findVets()); vets.getVetList().addAll(this.vets.findAll());
model.put("vets", vets); model.put("vets", vets);
return "vets/vetList"; return "vets/vetList";
} }
@ -55,7 +53,7 @@ public class VetController {
// 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
Vets vets = new Vets(); Vets vets = new Vets();
vets.getVetList().addAll(this.clinicService.findVets()); vets.getVetList().addAll(this.vets.findAll());
return vets; return vets;
} }

View file

@ -13,13 +13,15 @@
* 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.repository; package org.springframework.samples.petclinic.vet;
import java.util.Collection; import java.util.Collection;
import javax.cache.annotation.CacheResult;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.model.Vet; import org.springframework.transaction.annotation.Transactional;
/** /**
* Repository class for <code>Vet</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Vet</code> domain objects All method names are compliant with Spring Data naming
@ -37,6 +39,8 @@ public interface VetRepository extends Repository<Vet, Integer> {
* *
* @return a <code>Collection</code> of <code>Vet</code>s * @return a <code>Collection</code> of <code>Vet</code>s
*/ */
@Transactional(readOnly = true)
@CacheResult(cacheName = "vets")
Collection<Vet> findAll() throws DataAccessException; Collection<Vet> findAll() throws DataAccessException;

View file

@ -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.model; package org.springframework.samples.petclinic.vet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View file

@ -13,24 +13,25 @@
* 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.model; package org.springframework.samples.petclinic.visit;
import org.hibernate.validator.constraints.NotEmpty; import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import java.util.Date;
import org.hibernate.validator.constraints.NotEmpty;
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.
* *
* @author Ken Krebs * @author Ken Krebs
* @author Dave Syer
*/ */
@Entity @Entity
@Table(name = "visits") @Table(name = "visits")
@ -54,9 +55,8 @@ public class Visit extends BaseEntity {
/** /**
* Holds value of property pet. * Holds value of property pet.
*/ */
@ManyToOne @Column(name = "pet_id")
@JoinColumn(name = "pet_id") private Integer petId;
private Pet pet;
/** /**
@ -104,21 +104,21 @@ public class Visit extends BaseEntity {
} }
/** /**
* Getter for property pet. * Getter for property pet id.
* *
* @return Value of property pet. * @return Value of property pet id.
*/ */
public Pet getPet() { public Integer getPetId() {
return this.pet; return this.petId;
} }
/** /**
* Setter for property pet. * Setter for property pet id.
* *
* @param pet New value of property pet. * @param pet New value of property pet id.
*/ */
public void setPet(Pet pet) { public void setPetId(Integer petId) {
this.pet = pet; this.petId = petId;
} }
} }

View file

@ -13,14 +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.repository; package org.springframework.samples.petclinic.visit;
import java.util.List; import java.util.List;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
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.model.Visit;
/** /**
* Repository class for <code>Visit</code> domain objects All method names are compliant with Spring Data naming * Repository class for <code>Visit</code> domain objects All method names are compliant with Spring Data naming

View file

@ -1,5 +0,0 @@
/**
* The classes in this package represent PetClinic's web presentation layer.
*/
package org.springframework.samples.petclinic.web;

View file

@ -19,12 +19,12 @@
</tr> </tr>
</thead> </thead>
<tr> <tr>
<td th:text="${visit.pet.name}" /></td> <td th:text="${pet.name}" /></td>
<td <td
th:text="${#calendars.format(visit.pet.birthDate, 'yyyy-MM-dd')}" /></td> th:text="${#calendars.format(pet.birthDate, 'yyyy-MM-dd')}" /></td>
<td th:text="${visit.pet.type}" /></td> <td th:text="${pet.type}" /></td>
<td <td
th:text="${visit.pet.owner?.firstName + ' ' + visit.pet.owner?.lastName}" /></td> th:text="${pet.owner?.firstName + ' ' + pet.owner?.lastName}" /></td>
</tr> </tr>
</table> </table>
@ -38,7 +38,7 @@
<div class="form-group"> <div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="petId" th:value="${visit.pet.id}" /> <input type="hidden" name="petId" th:value="${pet.id}" />
<button class="btn btn-default" type="submit">Add Visit</button> <button class="btn btn-default" type="submit">Add Visit</button>
</div> </div>
</div> </div>
@ -51,7 +51,7 @@
<th>Date</th> <th>Date</th>
<th>Description</th> <th>Description</th>
</tr> </tr>
<tr th:if="${!visit['new']}" th:each="visit : ${visit.pet.visits}"> <tr th:if="${!visit['new']}" th:each="visit : ${pet.visits}">
<td th:text="${#calendars.format(visit.date, 'yyyy-MM-dd')}" /></td> <td th:text="${#calendars.format(visit.date, 'yyyy-MM-dd')}" /></td>
<td th:text=" ${visit.description}" /></td> <td th:text=" ${visit.description}" /></td>
</tr> </tr>

View file

@ -1,4 +1,13 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.owner;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.junit.Before; import org.junit.Before;
@ -7,18 +16,12 @@ import org.junit.runner.RunWith;
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.model.Owner; import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.owner.OwnerController;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/** /**
* Test class for {@link OwnerController} * Test class for {@link OwnerController}
* *
@ -34,7 +37,7 @@ public class OwnerControllerTests {
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
private ClinicService clinicService; private OwnerRepository owners;
private Owner george; private Owner george;
@ -47,7 +50,7 @@ public class OwnerControllerTests {
george.setAddress("110 W. Liberty St."); george.setAddress("110 W. Liberty St.");
george.setCity("Madison"); george.setCity("Madison");
george.setTelephone("6085551023"); george.setTelephone("6085551023");
given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(george); given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
} }
@Test @Test
@ -94,7 +97,7 @@ public class OwnerControllerTests {
@Test @Test
public void testProcessFindFormSuccess() throws Exception { public void testProcessFindFormSuccess() throws Exception {
given(this.clinicService.findOwnerByLastName("")).willReturn(Lists.newArrayList(george, new Owner())); given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
mockMvc.perform(get("/owners")) mockMvc.perform(get("/owners"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(view().name("owners/ownersList")); .andExpect(view().name("owners/ownersList"));
@ -102,7 +105,7 @@ public class OwnerControllerTests {
@Test @Test
public void testProcessFindFormByLastName() throws Exception { public void testProcessFindFormByLastName() throws Exception {
given(this.clinicService.findOwnerByLastName(george.getLastName())).willReturn(Lists.newArrayList(george)); given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
mockMvc.perform(get("/owners") mockMvc.perform(get("/owners")
.param("lastName", "Franklin") .param("lastName", "Franklin")
) )

View file

@ -1,4 +1,11 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.owner;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.junit.Before; import org.junit.Before;
@ -9,18 +16,16 @@ 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.model.Owner; import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.owner.PetController;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/** /**
* Test class for the {@link PetController} * Test class for the {@link PetController}
* *
@ -41,16 +46,19 @@ public class PetControllerTests {
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
private ClinicService clinicService; private PetRepository pets;
@MockBean
private OwnerRepository owners;
@Before @Before
public void setup() { public void setup() {
PetType cat = new PetType(); PetType cat = new PetType();
cat.setId(3); cat.setId(3);
cat.setName("hamster"); cat.setName("hamster");
given(this.clinicService.findPetTypes()).willReturn(Lists.newArrayList(cat)); given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat));
given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(new Owner()); given(this.owners.findById(TEST_OWNER_ID)).willReturn(new Owner());
given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet()); given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
} }

View file

@ -1,4 +1,12 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.owner;
import static org.junit.Assert.assertEquals;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -6,13 +14,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
import java.text.ParseException;
import java.util.*;
import static org.junit.Assert.assertEquals;
/** /**
* Test class for {@link PetTypeFormatter} * Test class for {@link PetTypeFormatter}
@ -23,33 +27,33 @@ import static org.junit.Assert.assertEquals;
public class PetTypeFormatterTests { public class PetTypeFormatterTests {
@Mock @Mock
private ClinicService clinicService; private PetRepository pets;
private PetTypeFormatter petTypeFormatter; private PetTypeFormatter petTypeFormatter;
@Before @Before
public void setup() { public void setup() {
petTypeFormatter = new PetTypeFormatter(clinicService); this.petTypeFormatter = new PetTypeFormatter(pets);
} }
@Test @Test
public void testPrint() { public void testPrint() {
PetType petType = new PetType(); PetType petType = new PetType();
petType.setName("Hamster"); petType.setName("Hamster");
String petTypeName = petTypeFormatter.print(petType, Locale.ENGLISH); String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH);
assertEquals("Hamster", petTypeName); assertEquals("Hamster", petTypeName);
} }
@Test @Test
public void shouldParse() throws ParseException { public void shouldParse() throws ParseException {
Mockito.when(clinicService.findPetTypes()).thenReturn(makePetTypes()); Mockito.when(this.pets.findPetTypes()).thenReturn(makePetTypes());
PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
assertEquals("Bird", petType.getName()); assertEquals("Bird", petType.getName());
} }
@Test(expected = ParseException.class) @Test(expected = ParseException.class)
public void shouldThrowParseException() throws ParseException { public void shouldThrowParseException() throws ParseException {
Mockito.when(clinicService.findPetTypes()).thenReturn(makePetTypes()); Mockito.when(this.pets.findPetTypes()).thenReturn(makePetTypes());
petTypeFormatter.parse("Fish", Locale.ENGLISH); petTypeFormatter.parse("Fish", Locale.ENGLISH);
} }
@ -58,8 +62,8 @@ public class PetTypeFormatterTests {
* *
* @return {@link Collection} of {@link PetType} * @return {@link Collection} of {@link PetType}
*/ */
private Collection<PetType> makePetTypes() { private List<PetType> makePetTypes() {
Collection<PetType> petTypes = new ArrayList<>(); List<PetType> petTypes = new ArrayList<>();
petTypes.add(new PetType(){ petTypes.add(new PetType(){
{ {
setName("Dog"); setName("Dog");

View file

@ -1,4 +1,11 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.owner;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -6,16 +13,13 @@ import org.junit.runner.RunWith;
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.model.Pet; import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.VisitController;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/** /**
* Test class for {@link VisitController} * Test class for {@link VisitController}
* *
@ -31,11 +35,14 @@ public class VisitControllerTests {
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
private ClinicService clinicService; private VisitRepository visits;
@MockBean
private PetRepository pets;
@Before @Before
public void init() { public void init() {
given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet()); given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
} }
@Test @Test

View file

@ -1,21 +1,28 @@
package org.springframework.samples.petclinic.service; package org.springframework.samples.petclinic.service;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collection;
import java.util.Date;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
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.model.*; import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.vet.Vet;
import org.springframework.samples.petclinic.vet.VetRepository;
import org.springframework.samples.petclinic.visit.Visit;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Integration test of the Service and the Repository layer. * Integration test of the Service and the Repository layer.
* <p> * <p>
@ -34,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
* @author Michael Isvy * @author Michael Isvy
* @author Dave Syer
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -41,20 +49,29 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ClinicServiceTests { public class ClinicServiceTests {
@Autowired @Autowired
protected ClinicService clinicService; protected OwnerRepository owners;
@Autowired
protected PetRepository pets;
@Autowired
protected VisitRepository visits;
@Autowired
protected VetRepository vets;
@Test @Test
public void shouldFindOwnersByLastName() { public void shouldFindOwnersByLastName() {
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis"); Collection<Owner> owners = this.owners.findByLastName("Davis");
assertThat(owners.size()).isEqualTo(2); assertThat(owners.size()).isEqualTo(2);
owners = this.clinicService.findOwnerByLastName("Daviss"); owners = this.owners.findByLastName("Daviss");
assertThat(owners.isEmpty()).isTrue(); assertThat(owners.isEmpty()).isTrue();
} }
@Test @Test
public void shouldFindSingleOwnerWithPet() { public void shouldFindSingleOwnerWithPet() {
Owner owner = this.clinicService.findOwnerById(1); Owner owner = this.owners.findById(1);
assertThat(owner.getLastName()).startsWith("Franklin"); assertThat(owner.getLastName()).startsWith("Franklin");
assertThat(owner.getPets().size()).isEqualTo(1); assertThat(owner.getPets().size()).isEqualTo(1);
assertThat(owner.getPets().get(0).getType()).isNotNull(); assertThat(owner.getPets().get(0).getType()).isNotNull();
@ -64,7 +81,7 @@ public class ClinicServiceTests {
@Test @Test
@Transactional @Transactional
public void shouldInsertOwner() { public void shouldInsertOwner() {
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Schultz"); Collection<Owner> owners = this.owners.findByLastName("Schultz");
int found = owners.size(); int found = owners.size();
Owner owner = new Owner(); Owner owner = new Owner();
@ -73,31 +90,31 @@ public class ClinicServiceTests {
owner.setAddress("4, Evans Street"); owner.setAddress("4, Evans Street");
owner.setCity("Wollongong"); owner.setCity("Wollongong");
owner.setTelephone("4444444444"); owner.setTelephone("4444444444");
this.clinicService.saveOwner(owner); this.owners.save(owner);
assertThat(owner.getId().longValue()).isNotEqualTo(0); assertThat(owner.getId().longValue()).isNotEqualTo(0);
owners = this.clinicService.findOwnerByLastName("Schultz"); owners = this.owners.findByLastName("Schultz");
assertThat(owners.size()).isEqualTo(found + 1); assertThat(owners.size()).isEqualTo(found + 1);
} }
@Test @Test
@Transactional @Transactional
public void shouldUpdateOwner() { public void shouldUpdateOwner() {
Owner owner = this.clinicService.findOwnerById(1); Owner owner = this.owners.findById(1);
String oldLastName = owner.getLastName(); String oldLastName = owner.getLastName();
String newLastName = oldLastName + "X"; String newLastName = oldLastName + "X";
owner.setLastName(newLastName); owner.setLastName(newLastName);
this.clinicService.saveOwner(owner); this.owners.save(owner);
// retrieving new name from database // retrieving new name from database
owner = this.clinicService.findOwnerById(1); owner = this.owners.findById(1);
assertThat(owner.getLastName()).isEqualTo(newLastName); assertThat(owner.getLastName()).isEqualTo(newLastName);
} }
@Test @Test
public void shouldFindPetWithCorrectId() { public void shouldFindPetWithCorrectId() {
Pet pet7 = this.clinicService.findPetById(7); Pet pet7 = this.pets.findById(7);
assertThat(pet7.getName()).startsWith("Samantha"); assertThat(pet7.getName()).startsWith("Samantha");
assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean"); assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
@ -105,7 +122,7 @@ public class ClinicServiceTests {
@Test @Test
public void shouldFindAllPetTypes() { public void shouldFindAllPetTypes() {
Collection<PetType> petTypes = this.clinicService.findPetTypes(); Collection<PetType> petTypes = this.pets.findPetTypes();
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertThat(petType1.getName()).isEqualTo("cat"); assertThat(petType1.getName()).isEqualTo("cat");
@ -116,21 +133,21 @@ public class ClinicServiceTests {
@Test @Test
@Transactional @Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() { public void shouldInsertPetIntoDatabaseAndGenerateId() {
Owner owner6 = this.clinicService.findOwnerById(6); Owner owner6 = this.owners.findById(6);
int found = owner6.getPets().size(); int found = owner6.getPets().size();
Pet pet = new Pet(); Pet pet = new Pet();
pet.setName("bowser"); pet.setName("bowser");
Collection<PetType> types = this.clinicService.findPetTypes(); Collection<PetType> types = this.pets.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date()); pet.setBirthDate(new Date());
owner6.addPet(pet); owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1); assertThat(owner6.getPets().size()).isEqualTo(found + 1);
this.clinicService.savePet(pet); this.pets.save(pet);
this.clinicService.saveOwner(owner6); this.owners.save(owner6);
owner6 = this.clinicService.findOwnerById(6); owner6 = this.owners.findById(6);
assertThat(owner6.getPets().size()).isEqualTo(found + 1); assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// checks that id has been generated // checks that id has been generated
assertThat(pet.getId()).isNotNull(); assertThat(pet.getId()).isNotNull();
@ -139,20 +156,20 @@ public class ClinicServiceTests {
@Test @Test
@Transactional @Transactional
public void shouldUpdatePetName() throws Exception { public void shouldUpdatePetName() throws Exception {
Pet pet7 = this.clinicService.findPetById(7); Pet pet7 = this.pets.findById(7);
String oldName = pet7.getName(); String oldName = pet7.getName();
String newName = oldName + "X"; String newName = oldName + "X";
pet7.setName(newName); pet7.setName(newName);
this.clinicService.savePet(pet7); this.pets.save(pet7);
pet7 = this.clinicService.findPetById(7); pet7 = this.pets.findById(7);
assertThat(pet7.getName()).isEqualTo(newName); assertThat(pet7.getName()).isEqualTo(newName);
} }
@Test @Test
public void shouldFindVets() { public void shouldFindVets() {
Collection<Vet> vets = this.clinicService.findVets(); Collection<Vet> vets = this.vets.findAll();
Vet vet = EntityUtils.getById(vets, Vet.class, 3); Vet vet = EntityUtils.getById(vets, Vet.class, 3);
assertThat(vet.getLastName()).isEqualTo("Douglas"); assertThat(vet.getLastName()).isEqualTo("Douglas");
@ -164,27 +181,26 @@ public class ClinicServiceTests {
@Test @Test
@Transactional @Transactional
public void shouldAddNewVisitForPet() { public void shouldAddNewVisitForPet() {
Pet pet7 = this.clinicService.findPetById(7); Pet pet7 = this.pets.findById(7);
int found = pet7.getVisits().size(); int found = pet7.getVisits().size();
Visit visit = new Visit(); Visit visit = new Visit();
pet7.addVisit(visit); pet7.addVisit(visit);
visit.setDescription("test"); visit.setDescription("test");
this.clinicService.saveVisit(visit); this.visits.save(visit);
this.clinicService.savePet(pet7); this.pets.save(pet7);
pet7 = this.clinicService.findPetById(7); pet7 = this.pets.findById(7);
assertThat(pet7.getVisits().size()).isEqualTo(found + 1); assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
assertThat(visit.getId()).isNotNull(); assertThat(visit.getId()).isNotNull();
} }
@Test @Test
public void shouldFindVisitsByPetId() throws Exception { public void shouldFindVisitsByPetId() throws Exception {
Collection<Visit> visits = this.clinicService.findVisitsByPetId(7); Collection<Visit> visits = this.visits.findByPetId(7);
assertThat(visits.size()).isEqualTo(2); assertThat(visits.size()).isEqualTo(2);
Visit[] visitArr = visits.toArray(new Visit[visits.size()]); Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
assertThat(visitArr[0].getPet()).isNotNull();
assertThat(visitArr[0].getDate()).isNotNull(); assertThat(visitArr[0].getDate()).isNotNull();
assertThat(visitArr[0].getPet().getId()).isEqualTo(7); assertThat(visitArr[0].getPetId()).isEqualTo(7);
} }
} }

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.util; package org.springframework.samples.petclinic.service;
import java.util.Collection; import java.util.Collection;

View file

@ -1,4 +1,4 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.system;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
@ -7,6 +7,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.samples.petclinic.PetClinicApplication; import org.springframework.samples.petclinic.PetClinicApplication;
import org.springframework.samples.petclinic.system.CrashController;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;

View file

@ -1,4 +1,13 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.vet;
import static org.hamcrest.xml.HasXPath.hasXPath;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.junit.Before; import org.junit.Before;
@ -8,18 +17,14 @@ 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.model.Specialty; import org.springframework.samples.petclinic.vet.Specialty;
import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.vet.Vet;
import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.vet.VetController;
import org.springframework.samples.petclinic.vet.VetRepository;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
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;
import static org.hamcrest.xml.HasXPath.hasXPath;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/** /**
* Test class for the {@link VetController} * Test class for the {@link VetController}
*/ */
@ -31,7 +36,7 @@ public class VetControllerTests {
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
private ClinicService clinicService; private VetRepository vets;
@Before @Before
public void setup() { public void setup() {
@ -47,7 +52,7 @@ public class VetControllerTests {
radiology.setId(1); radiology.setId(1);
radiology.setName("radiology"); radiology.setName("radiology");
helen.addSpecialty(radiology); helen.addSpecialty(radiology);
given(this.clinicService.findVets()).willReturn(Lists.newArrayList(james, helen)); given(this.vets.findAll()).willReturn(Lists.newArrayList(james, helen));
} }
@Test @Test