diff --git a/springboot-petclinic-server/pom.xml b/springboot-petclinic-server/pom.xml index ba10ded5670d3b8f742c91682c1e4a7d2d6081f2..e721322fa28d66a35b1786fc3df643e1b5c13474 100644 --- a/springboot-petclinic-server/pom.xml +++ b/springboot-petclinic-server/pom.xml @@ -12,9 +12,10 @@ <name>Spring Petclinic :: Spring MVC REST server</name> <properties> - <java.version>1.7</java.version> + <java.version>1.8</java.version> <docker.image.prefix>arey</docker.image.prefix> + <assertj.version>3.5.2</assertj.version> </properties> <dependencies> @@ -72,6 +73,13 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj.version}</version> + <scope>test</scope> + </dependency> + <!-- EhCache --> <dependency> <groupId>javax.cache</groupId> diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java index a1d61178c67282d9190220878a438d60403b0af5..0713bea2afefdb019621128ad3dce4a1c1ecfed9 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/pet/PetResource.java @@ -56,7 +56,7 @@ public class PetResource extends AbstractResourceController { @GetMapping("/owners/{ownerId}/pets/new") public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) { - Owner owner = this.ownerService.findOwnerById(ownerId); + Owner owner = ownerService.findOwnerById(ownerId); Pet pet = new Pet(); owner.addPet(pet); model.put("pet", pet); @@ -70,7 +70,7 @@ public class PetResource extends AbstractResourceController { @PathVariable("ownerId") int ownerId) { Pet pet = new Pet(); - Owner owner = this.ownerService.findOwnerById(ownerId); + Owner owner = ownerService.findOwnerById(ownerId); owner.addPet(pet); save(pet, petRequest); @@ -87,19 +87,15 @@ public class PetResource extends AbstractResourceController { pet.setName(petRequest.getName()); pet.setBirthDate(petRequest.getBirthDate()); - for (PetType petType : petService.findPetTypes()) { - if (petType.getId() == petRequest.getTypeId()) { - pet.setType(petType); - } - } + petService.findPetTypeById(petRequest.getTypeId()) + .ifPresent(pet::setType); petService.savePet(pet); } @GetMapping("/owner/*/pet/{petId}") public PetDetails findPet(@PathVariable("petId") int petId) { - Pet pet = this.petService.findPetById(petId); - return new PetDetails(pet); + return new PetDetails(petService.findPetById(petId)); } static class PetRequest { diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java index 38254dde3a0b282e32379e249a71f31399f19058..cd805ee04db380534192ec467b1a7cbc72901c33 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/boundary/web/vet/VetResource.java @@ -42,6 +42,6 @@ public class VetResource extends AbstractResourceController { @GetMapping("/vets") public Collection<Vet> showResourcesVetList() { - return this.vetService.findVets(); + return vetService.findVets(); } } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java index d9893772b0b1a0a2556ee7ae240dcaaa533a7383..54a7c1f0317452874dbca36076d0bfa4e669b10b 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetRepository.java @@ -17,8 +17,10 @@ package org.springframework.samples.petclinic.domain.model.pet; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import java.util.List; +import java.util.Optional; /** * Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming @@ -38,6 +40,9 @@ public interface PetRepository extends Repository<Pet, Integer> { @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") List<PetType> findPetTypes(); + @Query("FROM PetType ptype WHERE ptype.id = :typeId") + Optional<PetType> findPetTypeById(@Param("typeId") int typeId); + /** * Retrieve a {@link Pet} from the data store by id. * @param id the id to search for diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java index 2a733a010055404717b55ea356576ac4f89c1314..51d4f312f56019fcffa719344cb69bbed0bd0705 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/domain/model/pet/PetService.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; +import java.util.Optional; /** * @author mszarlinski on 2016-10-30. @@ -34,4 +35,9 @@ public class PetService { public Collection<PetType> findPetTypes() throws DataAccessException { return petRepository.findPetTypes(); } + + @Transactional(readOnly = true) + public Optional<PetType> findPetTypeById(int typeId) { + return petRepository.findPetTypeById(typeId); + } } diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java index 9e4786a0df3f68c36e7db18206dd49d4d83ab69f..8c69bb3ce29dd85bbe13240739ecccb098147a36 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/infrastructure/config/CacheConfig.java @@ -12,7 +12,6 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import javax.cache.CacheManager; import java.util.concurrent.TimeUnit; /** @@ -24,17 +23,14 @@ public class CacheConfig { @Bean public JCacheManagerCustomizer cacheManagerCustomizer() { - return new JCacheManagerCustomizer() { - @Override - public void customize(CacheManager cacheManager) { - CacheConfiguration<Object, Object> config = CacheConfigurationBuilder - .newCacheConfigurationBuilder(Object.class, Object.class, - ResourcePoolsBuilder.newResourcePoolsBuilder() - .heap(100, EntryUnit.ENTRIES)) - .withExpiry(Expirations.timeToLiveExpiration(Duration.of(60, TimeUnit.SECONDS))) - .build(); - cacheManager.createCache("vets", Eh107Configuration.fromEhcacheCacheConfiguration(config)); - } + return cacheManager -> { + CacheConfiguration<Object, Object> config = CacheConfigurationBuilder + .newCacheConfigurationBuilder(Object.class, Object.class, + ResourcePoolsBuilder.newResourcePoolsBuilder() + .heap(100, EntryUnit.ENTRIES)) + .withExpiry(Expirations.timeToLiveExpiration(Duration.of(60, TimeUnit.SECONDS))) + .build(); + cacheManager.createCache("vets", Eh107Configuration.fromEhcacheCacheConfiguration(config)); }; } diff --git a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/PetClinicTests.java b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/PetClinicTests.java index 8987f73aa1d626c175bdcadc73492e09a34a28b1..f2853755b4a965a02fb63dbc6cf48ec95bb6c4a0 100644 --- a/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/PetClinicTests.java +++ b/springboot-petclinic-server/src/test/java/org/springframework/samples/petclinic/domain/model/PetClinicTests.java @@ -57,21 +57,21 @@ public class PetClinicTests { @Test public void shouldFindSingleOwnerWithPet() { - Owner owner = this.ownerService.findOwnerById(1); + Owner owner = ownerService.findOwnerById(1); assertThat(owner.getLastName()).startsWith("Franklin"); assertThat(owner.getPets().size()).isEqualTo(1); } @Test public void shouldReturnAllOwnersInCaseLastNameIsEmpty() { - Collection<Owner> owners = this.ownerService.findAll(); + Collection<Owner> owners = ownerService.findAll(); assertThat(owners).extracting("lastName").contains("Davis", "Franklin"); } @Test @Transactional public void shouldInsertOwner() { - Collection<Owner> owners = this.ownerService.findAll(); + Collection<Owner> owners = ownerService.findAll(); int found = owners.size(); Owner owner = new Owner(); @@ -80,31 +80,31 @@ public class PetClinicTests { owner.setAddress("4, Evans Street"); owner.setCity("Wollongong"); owner.setTelephone("4444444444"); - this.ownerService.saveOwner(owner); + ownerService.saveOwner(owner); assertThat(owner.getId().longValue()).isNotEqualTo(0); - owners = this.ownerService.findAll(); + owners = ownerService.findAll(); assertThat(owners.size()).isEqualTo(found + 1); } @Test @Transactional public void shouldUpdateOwner() { - Owner owner = this.ownerService.findOwnerById(1); + Owner owner = ownerService.findOwnerById(1); String oldLastName = owner.getLastName(); String newLastName = oldLastName + "X"; owner.setLastName(newLastName); - this.ownerService.saveOwner(owner); + ownerService.saveOwner(owner); // retrieving new name from database - owner = this.ownerService.findOwnerById(1); + owner = ownerService.findOwnerById(1); assertThat(owner.getLastName()).isEqualTo(newLastName); } @Test public void shouldFindPetWithCorrectId() { - Pet pet7 = this.petService.findPetById(7); + Pet pet7 = petService.findPetById(7); assertThat(pet7.getName()).startsWith("Samantha"); assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean"); @@ -112,7 +112,7 @@ public class PetClinicTests { @Test public void shouldFindAllPetTypes() { - Collection<PetType> petTypes = this.petService.findPetTypes(); + Collection<PetType> petTypes = petService.findPetTypes(); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); assertThat(petType1.getName()).isEqualTo("cat"); @@ -120,24 +120,30 @@ public class PetClinicTests { assertThat(petType4.getName()).isEqualTo("snake"); } + @Test + public void shouldFindPetTypeById() { + assertThat(petService.findPetTypeById(1)).hasValueSatisfying(t -> t.getName().equals("cat")); + assertThat(petService.findPetTypeById(4)).hasValueSatisfying(t -> t.getName().equals("snake")); + } + @Test @Transactional public void shouldInsertPetIntoDatabaseAndGenerateId() { - Owner owner6 = this.ownerService.findOwnerById(6); + Owner owner6 = ownerService.findOwnerById(6); int found = owner6.getPets().size(); Pet pet = new Pet(); pet.setName("bowser"); - Collection<PetType> types = this.petService.findPetTypes(); + Collection<PetType> types = petService.findPetTypes(); pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setBirthDate(new Date()); owner6.addPet(pet); assertThat(owner6.getPets().size()).isEqualTo(found + 1); - this.petService.savePet(pet); - this.ownerService.saveOwner(owner6); + petService.savePet(pet); + ownerService.saveOwner(owner6); - owner6 = this.ownerService.findOwnerById(6); + owner6 = ownerService.findOwnerById(6); assertThat(owner6.getPets().size()).isEqualTo(found + 1); // checks that id has been generated assertThat(pet.getId()).isNotNull(); @@ -146,20 +152,20 @@ public class PetClinicTests { @Test @Transactional public void shouldUpdatePetName() throws Exception { - Pet pet7 = this.petService.findPetById(7); + Pet pet7 = petService.findPetById(7); String oldName = pet7.getName(); String newName = oldName + "X"; pet7.setName(newName); - this.petService.savePet(pet7); + petService.savePet(pet7); - pet7 = this.petService.findPetById(7); + pet7 = petService.findPetById(7); assertThat(pet7.getName()).isEqualTo(newName); } @Test public void shouldFindVets() { - Collection<Vet> vets = this.vetService.findVets(); + Collection<Vet> vets = vetService.findVets(); Vet vet = EntityUtils.getById(vets, Vet.class, 3); assertThat(vet.getLastName()).isEqualTo("Douglas"); @@ -171,15 +177,15 @@ public class PetClinicTests { @Test @Transactional public void shouldAddNewVisitForPet() { - Pet pet7 = this.petService.findPetById(7); + Pet pet7 = petService.findPetById(7); int found = pet7.getVisits().size(); Visit visit = new Visit(); pet7.addVisit(visit); visit.setDescription("test"); - this.visitService.saveVisit(visit); - this.petService.savePet(pet7); + visitService.saveVisit(visit); + petService.savePet(pet7); - pet7 = this.petService.findPetById(7); + pet7 = petService.findPetById(7); assertThat(pet7.getVisits().size()).isEqualTo(found + 1); assertThat(visit.getId()).isNotNull(); }