diff --git a/src/main/java/org/springframework/samples/petclinic/controller/PetController.java b/src/main/java/org/springframework/samples/petclinic/controller/PetController.java index 134ef0fb1..56989396d 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/PetController.java @@ -58,7 +58,7 @@ class PetController { @ModelAttribute("types") public Collection populatePetTypes() { - return this.petTypeService.findPetTypes(); + return this.petService.findPetTypes(); } @ModelAttribute("owner") diff --git a/src/main/java/org/springframework/samples/petclinic/dto/NamedDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/NamedDTO.java index 29ce3830e..1d3168628 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/NamedDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/NamedDTO.java @@ -38,4 +38,16 @@ public class NamedDTO extends BaseDTO { return this.getName(); } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof NamedDTO)) + return false; + + NamedDTO namedDTO = (NamedDTO) o; + + return getName().equals(namedDTO.getName()); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java index 7f7cb1272..83d2f8bac 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java @@ -130,23 +130,20 @@ public class OwnerDTO extends PersonDTO { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof OwnerDTO)) return false; + if (this == o) + return true; + if (!(o instanceof OwnerDTO)) + return false; OwnerDTO ownerDTO = (OwnerDTO) o; - if (!getAddress().equals(ownerDTO.getAddress())) return false; - if (!getCity().equals(ownerDTO.getCity())) return false; - if (!getTelephone().equals(ownerDTO.getTelephone())) return false; + if (!getAddress().equals(ownerDTO.getAddress())) + return false; + if (!getCity().equals(ownerDTO.getCity())) + return false; + if (!getTelephone().equals(ownerDTO.getTelephone())) + return false; return getPets() != null ? getPets().equals(ownerDTO.getPets()) : ownerDTO.getPets() == null; } - @Override - public int hashCode() { - int result = getAddress().hashCode(); - result = 31 * result + getCity().hashCode(); - result = 31 * result + getTelephone().hashCode(); - result = 31 * result + (getPets() != null ? getPets().hashCode() : 0); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java index 07565c8d0..f41818528 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java @@ -48,19 +48,16 @@ public class PersonDTO extends BaseDTO { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof PersonDTO)) return false; + if (this == o) + return true; + if (!(o instanceof PersonDTO)) + return false; PersonDTO personDTO = (PersonDTO) o; - if (!getFirstName().equals(personDTO.getFirstName())) return false; + if (!getFirstName().equals(personDTO.getFirstName())) + return false; return getLastName().equals(personDTO.getLastName()); } - @Override - public int hashCode() { - int result = getFirstName().hashCode(); - result = 31 * result + getLastName().hashCode(); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java index e88359a20..6a2875540 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java @@ -86,23 +86,20 @@ public class PetDTO extends NamedDTO { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof PetDTO)) return false; + if (this == o) + return true; + if (!(o instanceof PetDTO)) + return false; PetDTO petDTO = (PetDTO) o; - if (!getBirthDate().equals(petDTO.getBirthDate())) return false; - if (!getType().equals(petDTO.getType())) return false; - if (!getOwner().equals(petDTO.getOwner())) return false; + if (!getBirthDate().equals(petDTO.getBirthDate())) + return false; + if (!getType().equals(petDTO.getType())) + return false; + if (!getOwner().equals(petDTO.getOwner())) + return false; return getVisits() != null ? getVisits().equals(petDTO.getVisits()) : petDTO.getVisits() == null; } - @Override - public int hashCode() { - int result = getBirthDate().hashCode(); - result = 31 * result + getType().hashCode(); - result = 31 * result + getOwner().hashCode(); - result = 31 * result + (getVisits() != null ? getVisits().hashCode() : 0); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/PetTypeDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/PetTypeDTO.java index 54d771c7f..100c134bc 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/PetTypeDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/PetTypeDTO.java @@ -7,4 +7,9 @@ package org.springframework.samples.petclinic.dto; */ public class PetTypeDTO extends NamedDTO { + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java index 880589592..e41f87859 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java @@ -57,4 +57,18 @@ public class VetDTO extends PersonDTO { getSpecialtiesInternal().add(specialty); } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof VetDTO)) + return false; + if (!super.equals(o)) + return false; + + VetDTO vetDTO = (VetDTO) o; + + return getSpecialties().equals(vetDTO.getSpecialties()); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java index 617dd4fbb..d976185b7 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java @@ -67,21 +67,18 @@ public class VisitDTO extends BaseDTO { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof VisitDTO)) return false; + if (this == o) + return true; + if (!(o instanceof VisitDTO)) + return false; VisitDTO visitDTO = (VisitDTO) o; - if (!getDate().equals(visitDTO.getDate())) return false; - if (!getDescription().equals(visitDTO.getDescription())) return false; + if (!getDate().equals(visitDTO.getDate())) + return false; + if (!getDescription().equals(visitDTO.getDescription())) + return false; return getPetId().equals(visitDTO.getPetId()); } - @Override - public int hashCode() { - int result = getDate().hashCode(); - result = 31 * result + getDescription().hashCode(); - result = 31 * result + getPetId().hashCode(); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/formatter/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/formatter/PetTypeFormatter.java index 28330c383..a93537845 100644 --- a/src/main/java/org/springframework/samples/petclinic/formatter/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/formatter/PetTypeFormatter.java @@ -21,6 +21,7 @@ import java.util.Locale; import org.springframework.format.Formatter; import org.springframework.samples.petclinic.dto.PetTypeDTO; +import org.springframework.samples.petclinic.service.PetService; import org.springframework.samples.petclinic.service.PetTypeService; import org.springframework.stereotype.Component; @@ -37,10 +38,10 @@ import org.springframework.stereotype.Component; @Component public class PetTypeFormatter implements Formatter { - private final PetTypeService petTypeService; + private final PetService petService; - public PetTypeFormatter(PetTypeService petTypeService) { - this.petTypeService = petTypeService; + public PetTypeFormatter(PetService petService) { + this.petService = petService; } @Override @@ -50,7 +51,7 @@ public class PetTypeFormatter implements Formatter { @Override public PetTypeDTO parse(String text, Locale locale) throws ParseException { - Collection findPetTypes = this.petTypeService.findPetTypes(); + Collection findPetTypes = this.petService.findPetTypes(); for (PetTypeDTO type : findPetTypes) { if (type.getName().equals(text)) { return type; diff --git a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java index 7ac5aec0c..cafe7e218 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java @@ -46,16 +46,14 @@ public class NamedEntity extends BaseEntity { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof NamedEntity)) return false; + if (this == o) + return true; + if (!(o instanceof NamedEntity)) + return false; NamedEntity that = (NamedEntity) o; return getName().equals(that.getName()); } - @Override - public int hashCode() { - return getName().hashCode(); - } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/src/main/java/org/springframework/samples/petclinic/model/Owner.java index 24815138f..e5a4be5d5 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Owner.java @@ -147,25 +147,4 @@ public class Owner extends Person { .append(CommonAttribute.OWNER_PHONE, this.telephone).toString(); } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Owner)) return false; - - Owner owner = (Owner) o; - - if (!getAddress().equals(owner.getAddress())) return false; - if (!getCity().equals(owner.getCity())) return false; - if (!getTelephone().equals(owner.getTelephone())) return false; - return getPets() != null ? getPets().equals(owner.getPets()) : owner.getPets() == null; - } - - @Override - public int hashCode() { - int result = getAddress().hashCode(); - result = 31 * result + getCity().hashCode(); - result = 31 * result + getTelephone().hashCode(); - result = 31 * result + (getPets() != null ? getPets().hashCode() : 0); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Person.java b/src/main/java/org/springframework/samples/petclinic/model/Person.java index 536deb548..709c3bec3 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Person.java @@ -53,19 +53,16 @@ public class Person extends BaseEntity { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Person)) return false; + if (this == o) + return true; + if (!(o instanceof Person)) + return false; Person person = (Person) o; - if (!getFirstName().equals(person.getFirstName())) return false; + if (!getFirstName().equals(person.getFirstName())) + return false; return getLastName().equals(person.getLastName()); } - @Override - public int hashCode() { - int result = getFirstName().hashCode(); - result = 31 * result + getLastName().hashCode(); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java index 40683de7d..f6b0b26ed 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -81,7 +81,7 @@ public class Pet extends NamedEntity { return this.owner; } - protected void setOwner(Owner owner) { + public void setOwner(Owner owner) { this.owner = owner; } @@ -109,25 +109,22 @@ public class Pet extends NamedEntity { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Pet)) return false; - if (!super.equals(o)) return false; + if (this == o) + return true; + if (!(o instanceof Pet)) + return false; + if (!super.equals(o)) + return false; Pet pet = (Pet) o; - if (!getBirthDate().equals(pet.getBirthDate())) return false; - if (!getType().equals(pet.getType())) return false; - if (!getOwner().equals(pet.getOwner())) return false; + if (!getBirthDate().equals(pet.getBirthDate())) + return false; + if (!getType().equals(pet.getType())) + return false; + if (getOwner() != null ? !getOwner().equals(pet.getOwner()) : pet.getOwner() != null) + return false; return getVisits() != null ? getVisits().equals(pet.getVisits()) : pet.getVisits() == null; } - @Override - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + getBirthDate().hashCode(); - result = 31 * result + getType().hashCode(); - result = 31 * result + getOwner().hashCode(); - result = 31 * result + (getVisits() != null ? getVisits().hashCode() : 0); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/PetType.java b/src/main/java/org/springframework/samples/petclinic/model/PetType.java index ac554ac44..8453778e8 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/PetType.java +++ b/src/main/java/org/springframework/samples/petclinic/model/PetType.java @@ -25,4 +25,9 @@ import javax.persistence.Table; @Table(name = "types") public class PetType extends NamedEntity { + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/src/main/java/org/springframework/samples/petclinic/model/Visit.java index f90594d4c..859ca61a0 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Visit.java @@ -78,21 +78,18 @@ public class Visit extends BaseEntity { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Visit)) return false; + if (this == o) + return true; + if (!(o instanceof Visit)) + return false; Visit visit = (Visit) o; - if (!getDate().equals(visit.getDate())) return false; - if (!getDescription().equals(visit.getDescription())) return false; + if (!getDate().equals(visit.getDate())) + return false; + if (!getDescription().equals(visit.getDescription())) + return false; return getPetId().equals(visit.getPetId()); } - @Override - public int hashCode() { - int result = getDate().hashCode(); - result = 31 * result + getDescription().hashCode(); - result = 31 * result + getPetId().hashCode(); - return result; - } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java index 123fc4e3a..14cc90528 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java @@ -16,6 +16,7 @@ package org.springframework.samples.petclinic.repository; import java.util.Collection; +import java.util.List; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; @@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Paul-Emmanuel DOS SANTOS FACAO */ public interface OwnerRepository extends Repository { @@ -56,6 +58,12 @@ public interface OwnerRepository extends Repository { @Transactional(readOnly = true) Owner findById(@Param("id") Integer id); + /** + * Retrieve all {@link Owner}s from the data store + * @return a Collection of {@link Owner}s (or an empty Collection if none + */ + List findAll(); + /** * Save an {@link Owner} to the data store, either inserting or updating it. * @param owner the {@link Owner} to save diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index a46664df8..fa90093f3 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -15,6 +15,7 @@ */ package org.springframework.samples.petclinic.repository; +import java.util.Collection; import java.util.List; import org.springframework.data.jpa.repository.Query; @@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Paul-Emmanuel DOS SANTOS FACAO */ public interface PetRepository extends Repository { @@ -49,9 +51,14 @@ public interface PetRepository extends Repository { * @param id the id to search for * @return the {@link Pet} if found */ - @Transactional(readOnly = true) Pet findById(Integer id); + /** + * Retrieve all {@link Pet}s from the data store + * @return a Collection of {@link Pet}s (or an empty Collection if none + */ + List findAll(); + /** * Save a {@link Pet} to the data store, either inserting or updating it. * @param pet the {@link Pet} to save diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetTypeRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetTypeRepository.java new file mode 100644 index 000000000..77eb7e6b3 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetTypeRepository.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2019 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 + * + * https://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.repository; + +import org.springframework.data.repository.Repository; +import org.springframework.samples.petclinic.model.PetType; + +import java.util.Collection; +import java.util.List; + +/** + * Repository class for PetType domain objects All method names are compliant + * with Spring Data naming conventions so this interface can easily be extended for Spring + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +public interface PetTypeRepository extends Repository { + + /** + * Retrieve a {@link PetType} from the data store by id. + * @param id the id to search for + * @return the {@link PetType} if found + */ + PetType findById(Integer id); + + /** + * Retrieve all {@link PetType}s from the data store + * @return a Collection of {@link PetType}s (or an empty Collection if none + */ + List findAll(); + + /** + * Save a {@link PetType} to the data store, either inserting or updating it. + * @param petType the {@link PetType} to save + */ + void save(PetType petType); + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/SpecialtyRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/SpecialtyRepository.java new file mode 100644 index 000000000..327161809 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/SpecialtyRepository.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2019 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 + * + * https://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.repository; + +import org.springframework.data.repository.Repository; +import org.springframework.samples.petclinic.model.Specialty; + +import java.util.Collection; +import java.util.List; + +/** + * Repository class for Speciality domain objects All method names are + * compliant with Spring Data naming conventions so this interface can easily be extended + * for Spring + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +public interface SpecialtyRepository extends Repository { + + /** + * Retrieve a {@link Specialty} from the data store by id. + * @param id the id to search for + * @return the {@link Specialty} if found + */ + Specialty findById(Integer id); + + /** + * Retrieve all {@link Specialty}s from the data store + * @return a Collection of {@link Specialty}s (or an empty Collection if none + */ + List findAll(); + + /** + * Save a {@link Specialty} to the data store, either inserting or updating it. + * @param specialty the {@link Specialty} to save + */ + void save(Specialty specialty); + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java index bb54d107d..ae585f19f 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java @@ -32,9 +32,17 @@ import org.springframework.transaction.annotation.Transactional; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Paul-Emmanuel DOS SANTOS FACAO */ public interface VetRepository extends Repository { + /** + * Retrieve a {@link Vet} from the data store by id. + * @param id the id to search for + * @return the {@link Vet} if found + */ + Vet findById(Integer id); + /** * Retrieve all Vets from the data store. * @return a Collection of Vets @@ -43,4 +51,10 @@ public interface VetRepository extends Repository { @Cacheable("vets") Collection findAll(); + /** + * Save a {@link Vet} to the data store, either inserting or updating it. + * @param vet the {@link Vet} to save + */ + void save(Vet vet); + } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java index 0326d04b9..06fe23dd8 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java @@ -15,6 +15,7 @@ */ package org.springframework.samples.petclinic.repository; +import java.util.Collection; import java.util.List; import org.springframework.data.repository.Repository; @@ -31,6 +32,7 @@ import org.springframework.samples.petclinic.model.Visit; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Paul-Emmanuel DOS SANTOS FACAO */ public interface VisitRepository extends Repository { @@ -41,6 +43,19 @@ public interface VisitRepository extends Repository { */ void save(Visit visit); + /** + * Retrieve a {@link Visit} from the data store by id. + * @param id the id to search for + * @return the {@link Visit} if found + */ + Visit findById(Integer id); + List findByPetId(Integer petId); + /** + * Retrieve all {@link Visit}s from the data store + * @return a Collection of {@link Visit}s (or an empty Collection if none + */ + List findAll(); + } diff --git a/src/main/java/org/springframework/samples/petclinic/service/BaseService.java b/src/main/java/org/springframework/samples/petclinic/service/BaseService.java index 1f97e4d0b..5b4cdf5de 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/BaseService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/BaseService.java @@ -1,7 +1,12 @@ package org.springframework.samples.petclinic.service; -import java.util.Collection; +import java.util.List; +/** + * Interface for all services + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ public interface BaseService { /** @@ -9,27 +14,45 @@ public interface BaseService { * @param dto DTO * @return Entity Model */ - public E dtoToEntity(D dto); + E dtoToEntity(D dto); /** * Convert Entity Model to Data Transfert Object * @param entity Entity Model * @return DTO */ - public D entityToDTO(E entity); + D entityToDTO(E entity); /** * Convert Entities Models Collection to Data Transfert Object Collection * @param entities Collection of Entity Model * @return Collection of DTO */ - public Collection entitiesToDTOS(Collection entities); + List entitiesToDTOS(List entities); /** * Convert Entities Models Collection to Data Transfert Object Collection * @param dtos Collection of DTO * @return Collection of Entity Model */ - public Collection dtosToEntities(Collection dtos); + List dtosToEntities(List dtos); + + /** + * Get DTO object from repository by his ID + * @param id identify object to be found + * @return + */ + D findById(int id); + + /** + * Get all DTO objects from repository + * @return + */ + List findAll(); + + /** + * Save DTO object to repository + */ + void save(D dto); } diff --git a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java b/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java index 1c75f8843..64b375f40 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java @@ -1,94 +1,102 @@ package org.springframework.samples.petclinic.service; import org.modelmapper.ModelMapper; +import org.modelmapper.internal.util.Lists; import org.springframework.samples.petclinic.dto.OwnerDTO; -import org.springframework.samples.petclinic.dto.PetDTO; import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.repository.OwnerRepository; import org.springframework.samples.petclinic.repository.PetRepository; +import org.springframework.samples.petclinic.repository.PetTypeRepository; +import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; +import java.util.List; +/** + * Simple Service between Owner entity and OwnerDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ @Service("OwnerService") public class OwnerService implements BaseService { private final OwnerRepository ownerRepository; + private final PetRepository petRepository; + private final ModelMapper modelMapper = new ModelMapper(); + private PetService petService; - public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository) { + public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository, + PetTypeRepository petTypeRepository, VisitRepository visitRepository) { this.ownerRepository = ownerRepository; this.petRepository = petRepository; - petService = new PetService(petRepository); + petService = new PetService(petRepository, petTypeRepository, visitRepository); } @Override public Owner dtoToEntity(OwnerDTO dto) { - if(dto == null) { - return new Owner(); - } - Owner owner = modelMapper.map(dto, Owner.class); - - for(PetDTO petDTO: dto.getPets()) { - owner.addPet(petService.dtoToEntity(petDTO)); + if (dto != null) { + Owner owner = modelMapper.map(dto, Owner.class); + dto.getPets().forEach(petDTO -> owner.addPet(petService.dtoToEntity(petDTO))); + return owner; } - return owner; + return new Owner(); } @Override public OwnerDTO entityToDTO(Owner entity) { - if(entity == null) { - return new OwnerDTO(); - } - OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class); - - for(Pet pet : entity.getPets()) { - ownerDTO.addPet(petService.entityToDTO(pet)); + if (entity != null) { + OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class); + entity.getPets().forEach(pet -> ownerDTO.addPet(petService.entityToDTO(pet))); + return ownerDTO; } - return ownerDTO; + return new OwnerDTO(); } @Override - public Collection entitiesToDTOS(Collection entities) { - Collection dtos = new HashSet<>(); + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); - for (Owner entity : entities) { - dtos.add(entityToDTO(entity)); - } + entities.forEach(entity -> dtos.add(entityToDTO(entity))); return dtos; } @Override - public Collection dtosToEntities(Collection dtos) { - Collection entities = new HashSet<>(); + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); - for (OwnerDTO dto : dtos) { - entities.add(dtoToEntity(dto)); - } + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); return entities; } - public void save(OwnerDTO ownerDTO) { - Owner owner = dtoToEntity(ownerDTO); - ownerRepository.save(owner); - } - - public Collection findByLastName(String lastName) { - Collection owners = ownerRepository.findByLastName(lastName); - return entitiesToDTOS(owners); - } - + @Override public OwnerDTO findById(int ownerId) { Owner owner = ownerRepository.findById(ownerId); return entityToDTO(owner); } + @Override + public List findAll() { + return entitiesToDTOS(ownerRepository.findAll()); + } + + @Override + public void save(OwnerDTO ownerDTO) { + Owner owner = dtoToEntity(ownerDTO); + ownerRepository.save(owner); + } + + public List findByLastName(String lastName) { + Collection owners = ownerRepository.findByLastName(lastName); + return entitiesToDTOS(Lists.from(owners.iterator())); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/service/PetService.java b/src/main/java/org/springframework/samples/petclinic/service/PetService.java index 191eb558d..793f62706 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/PetService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/PetService.java @@ -2,71 +2,103 @@ package org.springframework.samples.petclinic.service; import org.modelmapper.ModelMapper; import org.springframework.samples.petclinic.dto.PetDTO; +import org.springframework.samples.petclinic.dto.PetTypeDTO; import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.repository.PetRepository; +import org.springframework.samples.petclinic.repository.PetTypeRepository; +import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Service; -import java.util.Collection; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.List; +/** + * Simple Service between Pet entity and PetDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ @Service("PetService") public class PetService implements BaseService { private final PetRepository petRepository; + private final PetTypeService petTypeService; + + private final PetTypeRepository petTypeRepository; + + private final VisitService visitService; + private final ModelMapper modelMapper = new ModelMapper(); - public PetService(PetRepository petRepository) { + public PetService(PetRepository petRepository, PetTypeRepository petTypeRepository, + VisitRepository visitRepository) { this.petRepository = petRepository; + this.petTypeRepository = petTypeRepository; + this.visitService = new VisitService(visitRepository); + this.petTypeService = new PetTypeService(petTypeRepository); } @Override public Pet dtoToEntity(PetDTO dto) { - if(dto == null) { - return new Pet(); - } else { - return modelMapper.map(dto, Pet.class); + if (dto != null) { + Pet pet = modelMapper.map(dto, Pet.class); + dto.getVisits().forEach(visitDTO -> pet.addVisit(visitService.dtoToEntity(visitDTO))); + return pet; } + + return new Pet(); } @Override public PetDTO entityToDTO(Pet entity) { - if(entity == null) { - return new PetDTO(); - } else { - return modelMapper.map(entity, PetDTO.class); + if (entity != null) { + PetDTO petDTO = modelMapper.map(entity, PetDTO.class); + entity.getVisits().forEach(visit -> petDTO.addVisit(visitService.entityToDTO(visit))); + return petDTO; } + + return new PetDTO(); } @Override - public Collection entitiesToDTOS(Collection entities) { - Collection dtos = new HashSet<>(); + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); + + entities.forEach(entity -> dtos.add(entityToDTO(entity))); - for (Pet entity : entities) { - dtos.add(entityToDTO(entity)); - } return dtos; } @Override - public Collection dtosToEntities(Collection dtos) { - Collection entities = new HashSet<>(); + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); - for (PetDTO dto : dtos) { - entities.add(dtoToEntity(dto)); - } + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); return entities; } - public void save(PetDTO petDTO) { - Pet pet = dtoToEntity(petDTO); - petRepository.save(pet); - } - + @Override public PetDTO findById(int petId) { Pet pet = petRepository.findById(petId); return entityToDTO(pet); } + @Override + public List findAll() { + return entitiesToDTOS(petRepository.findAll()); + } + + @Override + public void save(PetDTO petDTO) { + petRepository.save(dtoToEntity(petDTO)); + } + + public List findPetTypes() { + List petTypes = petRepository.findPetTypes(); + + return petTypeService.entitiesToDTOS(petTypes); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/service/PetTypeService.java b/src/main/java/org/springframework/samples/petclinic/service/PetTypeService.java index 2dfa4a464..23c97f0a9 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/PetTypeService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/PetTypeService.java @@ -3,58 +3,77 @@ package org.springframework.samples.petclinic.service; import org.modelmapper.ModelMapper; import org.springframework.samples.petclinic.dto.PetTypeDTO; import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.repository.PetRepository; +import org.springframework.samples.petclinic.repository.PetTypeRepository; import org.springframework.stereotype.Service; -import java.util.Collection; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.List; +/** + * Simple Service between PetType entity and PetTypeDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ @Service("PerTypeService") public class PetTypeService implements BaseService { - private final PetRepository petRepository; + private final PetTypeRepository petTypeRepository; private final ModelMapper modelMapper = new ModelMapper(); - public PetTypeService(PetRepository petRepository) { - this.petRepository = petRepository; + public PetTypeService(PetTypeRepository petTypeRepository) { + this.petTypeRepository = petTypeRepository; } @Override public PetType dtoToEntity(PetTypeDTO dto) { - return modelMapper.map(dto, PetType.class); + if (dto != null) { + return modelMapper.map(dto, PetType.class); + } + + return new PetType(); } @Override public PetTypeDTO entityToDTO(PetType entity) { - return modelMapper.map(entity, PetTypeDTO.class); + if (entity != null) { + return modelMapper.map(entity, PetTypeDTO.class); + } + + return new PetTypeDTO(); } @Override - public Collection entitiesToDTOS(Collection entities) { - Collection dtos = new HashSet<>(); + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); - for (PetType entity : entities) { - dtos.add(entityToDTO(entity)); - } + entities.forEach(entity -> dtos.add(entityToDTO(entity))); return dtos; } @Override - public Collection dtosToEntities(Collection dtos) { - Collection entities = new HashSet<>(); + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); - for (PetTypeDTO dto : dtos) { - entities.add(dtoToEntity(dto)); - } + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); return entities; } - public Collection findPetTypes() { - Collection petTypes = petRepository.findPetTypes(); - return entitiesToDTOS(petTypes); + @Override + public PetTypeDTO findById(int id) { + return entityToDTO(petTypeRepository.findById(id)); + } + + @Override + public List findAll() { + return entitiesToDTOS(petTypeRepository.findAll()); + } + + @Override + public void save(PetTypeDTO dto) { + petTypeRepository.save(dtoToEntity(dto)); } } diff --git a/src/main/java/org/springframework/samples/petclinic/service/SpecialityService.java b/src/main/java/org/springframework/samples/petclinic/service/SpecialityService.java deleted file mode 100644 index f0065f06d..000000000 --- a/src/main/java/org/springframework/samples/petclinic/service/SpecialityService.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.springframework.samples.petclinic.service; - -import org.modelmapper.ModelMapper; -import org.springframework.samples.petclinic.dto.SpecialtyDTO; -import org.springframework.samples.petclinic.model.Specialty; -import org.springframework.stereotype.Service; - -import java.util.Collection; -import java.util.HashSet; - -@Service("SpecialityService") -public class SpecialityService implements BaseService { - - private final ModelMapper modelMapper = new ModelMapper(); - - @Override - public Specialty dtoToEntity(SpecialtyDTO dto) { - return modelMapper.map(dto, Specialty.class); - } - - @Override - public SpecialtyDTO entityToDTO(Specialty entity) { - return modelMapper.map(entity, SpecialtyDTO.class); - } - - @Override - public Collection entitiesToDTOS(Collection entities) { - Collection dtos = new HashSet<>(); - - for (Specialty entity : entities) { - dtos.add(entityToDTO(entity)); - } - - return dtos; - } - - @Override - public Collection dtosToEntities(Collection dtos) { - Collection entities = new HashSet<>(); - - for (SpecialtyDTO dto : dtos) { - entities.add(dtoToEntity(dto)); - } - - return entities; - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/service/SpecialtyService.java b/src/main/java/org/springframework/samples/petclinic/service/SpecialtyService.java new file mode 100644 index 000000000..d4eb07cb8 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/service/SpecialtyService.java @@ -0,0 +1,81 @@ +package org.springframework.samples.petclinic.service; + +import org.modelmapper.ModelMapper; +import org.springframework.samples.petclinic.dto.SpecialtyDTO; +import org.springframework.samples.petclinic.model.Specialty; +import org.springframework.samples.petclinic.repository.SpecialtyRepository; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; + +/** + * Simple Service between Specialty entity and SpecialtyDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ +@Service("SpecialityService") +public class SpecialtyService implements BaseService { + + private final SpecialtyRepository specialtyRepository; + + private final ModelMapper modelMapper = new ModelMapper(); + + public SpecialtyService(SpecialtyRepository specialtyRepository) { + this.specialtyRepository = specialtyRepository; + } + + @Override + public Specialty dtoToEntity(SpecialtyDTO dto) { + if (dto != null) { + return modelMapper.map(dto, Specialty.class); + } + + return new Specialty(); + } + + @Override + public SpecialtyDTO entityToDTO(Specialty entity) { + if (entity != null) { + return modelMapper.map(entity, SpecialtyDTO.class); + } + + return new SpecialtyDTO(); + } + + @Override + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); + + entities.forEach(entity -> dtos.add(entityToDTO(entity))); + + return dtos; + } + + @Override + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); + + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); + + return entities; + } + + @Override + public SpecialtyDTO findById(int id) { + return entityToDTO(specialtyRepository.findById(id)); + } + + @Override + public List findAll() { + return entitiesToDTOS(specialtyRepository.findAll()); + } + + @Override + public void save(SpecialtyDTO dto) { + specialtyRepository.save(dtoToEntity(dto)); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/service/VetService.java b/src/main/java/org/springframework/samples/petclinic/service/VetService.java index d768fa3e7..5f4775bd3 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/VetService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/VetService.java @@ -1,14 +1,22 @@ package org.springframework.samples.petclinic.service; import org.modelmapper.ModelMapper; +import org.modelmapper.internal.util.Lists; import org.springframework.samples.petclinic.dto.VetDTO; import org.springframework.samples.petclinic.repository.VetRepository; import org.springframework.samples.petclinic.model.Vet; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.List; +/** + * Simple Service between Vet entity and VetDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ @Service("VetService") public class VetService implements BaseService { @@ -22,39 +30,52 @@ public class VetService implements BaseService { @Override public Vet dtoToEntity(VetDTO dto) { - return modelMapper.map(dto, Vet.class); + if (dto != null) { + return modelMapper.map(dto, Vet.class); + } + + return new Vet(); } @Override public VetDTO entityToDTO(Vet entity) { - return modelMapper.map(entity, VetDTO.class); + if (entity != null) { + return modelMapper.map(entity, VetDTO.class); + } + return new VetDTO(); } @Override - public Collection entitiesToDTOS(Collection entities) { - Collection dtos = new HashSet<>(); + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); - for (Vet entity : entities) { - dtos.add(entityToDTO(entity)); - } + entities.forEach(entity -> dtos.add(entityToDTO(entity))); return dtos; } @Override - public Collection dtosToEntities(Collection dtos) { - Collection entities = new HashSet<>(); + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); - for (VetDTO dto : dtos) { - entities.add(dtoToEntity(dto)); - } + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); return entities; } - public Collection findAll() { + @Override + public VetDTO findById(int id) { + return entityToDTO(vetRepository.findById(id)); + } + + public List findAll() { Collection vets = vetRepository.findAll(); - return entitiesToDTOS(vets); + return entitiesToDTOS(Lists.from(vets.iterator())); + } + + @Override + public void save(VetDTO dto) { + vetRepository.save(dtoToEntity(dto)); } } diff --git a/src/main/java/org/springframework/samples/petclinic/service/VisitService.java b/src/main/java/org/springframework/samples/petclinic/service/VisitService.java index a53c84182..8f627cf80 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/VisitService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/VisitService.java @@ -6,9 +6,16 @@ import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.List; +/** + * Simple Service between Visit entity and VisitDTO Data Transfert Object. + * + * @author Paul-Emmanuel DOS SANTOS FACAO + */ @Service("VisitService") public class VisitService implements BaseService { @@ -22,35 +29,51 @@ public class VisitService implements BaseService { @Override public Visit dtoToEntity(VisitDTO dto) { - return modelMapper.map(dto, Visit.class); + if (dto != null) { + return modelMapper.map(dto, Visit.class); + } + + return new Visit(); } @Override public VisitDTO entityToDTO(Visit entity) { - return modelMapper.map(entity, VisitDTO.class); + if (entity != null) { + return modelMapper.map(entity, VisitDTO.class); + } + + return new VisitDTO(); } @Override - public Collection entitiesToDTOS(Collection entities) { - Collection dtos = new HashSet<>(); + public List entitiesToDTOS(List entities) { + List dtos = new ArrayList<>(); + + entities.forEach(entity -> dtos.add(entityToDTO(entity))); - for (Visit entity : entities) { - dtos.add(entityToDTO(entity)); - } return dtos; } @Override - public Collection dtosToEntities(Collection dtos) { - Collection entities = new HashSet<>(); + public List dtosToEntities(List dtos) { + List entities = new ArrayList<>(); - for (VisitDTO dto : dtos) { - entities.add(dtoToEntity(dto)); - } + dtos.forEach(dto -> entities.add(dtoToEntity(dto))); return entities; } + @Override + public VisitDTO findById(int id) { + return entityToDTO(visitRepository.findById(id)); + } + + @Override + public List findAll() { + return entitiesToDTOS(visitRepository.findAll()); + } + + @Override public void save(VisitDTO visitDTO) { Visit visit = dtoToEntity(visitDTO); visitRepository.save(visit); diff --git a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java index 69f26dc19..717507c5b 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java @@ -79,7 +79,7 @@ class PetControllerTests { given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO()); given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO()); - given(this.petTypeService.findPetTypes()).willReturn(Lists.newArrayList(cat)); + given(this.petService.findPetTypes()).willReturn(Lists.newArrayList(cat)); } @Test diff --git a/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java index dab699990..a9e8f2e72 100644 --- a/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java @@ -25,6 +25,7 @@ import org.mockito.junit.jupiter.MockitoExtension; 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.PetService; import org.springframework.samples.petclinic.service.PetTypeService; import java.text.ParseException; @@ -45,13 +46,13 @@ import static org.mockito.BDDMockito.given; class PetTypeDTOFormatterTests { @Mock - private PetTypeService petTypeService; + private PetService petService; private PetTypeFormatter petTypeFormatter; @BeforeEach void setup() { - this.petTypeFormatter = new PetTypeFormatter(petTypeService); + this.petTypeFormatter = new PetTypeFormatter(petService); } @Test @@ -64,17 +65,15 @@ class PetTypeDTOFormatterTests { @Test void shouldParse() throws ParseException { - given(this.petTypeService.findPetTypes()).willReturn(makePetTypes()); + given(this.petService.findPetTypes()).willReturn(makePetTypes()); PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); assertThat(petType.getName()).isEqualTo("Bird"); } @Test - void shouldThrowParseException() throws ParseException { - given(this.petTypeService.findPetTypes()).willReturn(makePetTypes()); - Assertions.assertThrows(ParseException.class, () -> { - petTypeFormatter.parse("Fish", Locale.ENGLISH); - }); + void shouldThrowParseException() { + given(this.petService.findPetTypes()).willReturn(makePetTypes()); + Assertions.assertThrows(ParseException.class, () -> petTypeFormatter.parse("Fish", Locale.ENGLISH)); } /** diff --git a/src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java index 966a2348d..902048f72 100644 --- a/src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java @@ -31,6 +31,7 @@ import org.mockito.junit.jupiter.MockitoExtension; 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.PetService; import org.springframework.samples.petclinic.service.PetTypeService; import static org.assertj.core.api.Assertions.assertThat; @@ -45,13 +46,13 @@ import static org.mockito.BDDMockito.given; class PetTypeFormatterTests { @Mock - private PetTypeService petTypeService; + private PetService petService; private PetTypeFormatter petTypeFormatter; @BeforeEach void setup() { - this.petTypeFormatter = new PetTypeFormatter(petTypeService); + this.petTypeFormatter = new PetTypeFormatter(petService); } @Test @@ -64,17 +65,15 @@ class PetTypeFormatterTests { @Test void shouldParse() throws ParseException { - given(this.petTypeService.findPetTypes()).willReturn(makePetTypes()); + given(this.petService.findPetTypes()).willReturn(makePetTypes()); PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); assertThat(petType.getName()).isEqualTo("Bird"); } @Test - void shouldThrowParseException() throws ParseException { - given(this.petTypeService.findPetTypes()).willReturn(makePetTypes()); - Assertions.assertThrows(ParseException.class, () -> { - petTypeFormatter.parse("Fish", Locale.ENGLISH); - }); + void shouldThrowParseException() { + given(this.petService.findPetTypes()).willReturn(makePetTypes()); + Assertions.assertThrows(ParseException.class, () -> petTypeFormatter.parse("Fish", Locale.ENGLISH)); } /** diff --git a/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java index dc7d4c30a..7160140c4 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java +++ b/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java @@ -2,6 +2,7 @@ package org.springframework.samples.petclinic.service; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -13,45 +14,67 @@ import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.repository.OwnerRepository; import org.springframework.samples.petclinic.repository.PetRepository; +import org.springframework.samples.petclinic.repository.PetTypeRepository; +import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; -import java.util.Collection; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; @Slf4j @DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) class OwnerServiceTest { + private final static Integer OWNER_ID = 55; + private final static String OWNER_FIRST_NAME = "Sam"; + private final static String OWNER_LAST_NAME = "Schultz"; + private final static String OWNER_ADDRESS = "4, Evans Street"; + private final static String OWNER_CITY = "Wollongong"; + private final static String OWNER_PHONE = "1234567890"; private final static Integer PET_ID = 11; + private final static String PET_NAME = "bowser"; + private final static String PET_BIRTH_DATE = "2020-07-11"; @Autowired private OwnerRepository ownerRepository; + @Autowired private PetRepository petRepository; + @Autowired + private PetTypeRepository petTypeRepository; + + @Autowired + private VisitRepository visitRepository; + private PetService petService; + private OwnerService ownerService; + private static Owner owner; + private static OwnerDTO ownerDTO; + private static Pet pet; + private static PetDTO petDTO; @BeforeEach void beforeEach() { - petService = new PetService(petRepository); - ownerService = new OwnerService(ownerRepository, petRepository); + petService = new PetService(petRepository, petTypeRepository, visitRepository); + ownerService = new OwnerService(ownerRepository, petRepository, petTypeRepository, visitRepository); pet = new Pet(); pet.setId(PET_ID); pet.setName(PET_NAME); @@ -81,8 +104,11 @@ class OwnerServiceTest { @Test @Tag("dtoToEntity") + @DisplayName("Verify the convertion from DTO to Entity") void dtoToEntity() { Owner found = ownerService.dtoToEntity(ownerDTO); + + assertThat(found.getId()).isEqualTo(owner.getId()); assertThat(found.getFirstName()).isEqualTo(owner.getFirstName()); assertThat(found.getLastName()).isEqualTo(owner.getLastName()); assertThat(found.getAddress()).isEqualTo(owner.getAddress()); @@ -91,7 +117,7 @@ class OwnerServiceTest { assertThat(found.getPets().size()).isEqualTo(owner.getPets().size()); - for(Pet pet: found.getPets()) { + for (Pet pet : found.getPets()) { assertThat(owner.getPets()).contains(pet); } @@ -99,8 +125,11 @@ class OwnerServiceTest { @Test @Tag("entityToDTO") + @DisplayName("Verify the convertion from Entity to DTO") void entityToDTO() { OwnerDTO found = ownerService.entityToDTO(owner); + + assertThat(found.getId()).isEqualTo(ownerDTO.getId()); assertThat(found.getFirstName()).isEqualTo(ownerDTO.getFirstName()); assertThat(found.getLastName()).isEqualTo(ownerDTO.getLastName()); assertThat(found.getAddress()).isEqualTo(ownerDTO.getAddress()); @@ -108,86 +137,81 @@ class OwnerServiceTest { assertThat(found.getTelephone()).isEqualTo(ownerDTO.getTelephone()); assertThat(found.getPets().size()).isEqualTo(ownerDTO.getPets().size()); - for(PetDTO petDTO: found.getPets()) { + for (PetDTO petDTO : found.getPets()) { assertThat(ownerDTO.getPets()).contains(petDTO); } } - @Test - @Tag("entitiesToDTOS") - void entitiesToDTOS() { - Collection owners = new HashSet<>(); - Collection expected = new HashSet<>(); - Collection found; - - for(int i =1 ; i<5; i++) { - OwnerDTO ownerDTO = ownerService.findById(i); - expected.add(ownerDTO); - owners.add(ownerService.dtoToEntity(ownerDTO)); - } - - found = ownerService.entitiesToDTOS(owners); - - assertThat(found).hasSameSizeAs(expected); - - for( int i=1; i<5; i++) { - assertThat(expected).contains(found.iterator().next()); - } - } - @Test @Tag("dtosToEntities") + @DisplayName("Verify the convertion from DTOs list to Entities list") void dtosToEntities() { - Collection ownerDTOS = new HashSet<>(); - Collection expected = new HashSet<>(); - Collection found; + List ownerDTOS = ownerService.findAll(); + List expected = new ArrayList<>(); + ownerDTOS.forEach(dto -> expected.add(ownerService.dtoToEntity(dto))); - for(int i =1 ; i<5; i++) { - OwnerDTO ownerDTO = ownerService.findById(i); - expected.add(ownerService.dtoToEntity(ownerDTO)); - ownerDTOS.add(ownerDTO); - } + List found = ownerService.dtosToEntities(ownerDTOS); - found = ownerService.dtosToEntities(ownerDTOS); - - assertThat(found).hasSameSizeAs(expected); - - for( int i=1; i<5; i++) { - assertThat(expected).contains(found.iterator().next()); - } + assertThat(found).hasSameSizeAs(expected).containsAll(expected); } @Test - @Transactional - @Tag("save") - void save() { - Collection founds = ownerService.findByLastName(OWNER_LAST_NAME); - assertThat(founds).isEmpty(); + @Tag("entitiesToDTOS") + @DisplayName("Verify the convertion from Entities list to DTOs list") + void entitiesToDTOS() { + List expected = ownerService.findAll(); + List owners = new ArrayList<>(); + expected.forEach(dto -> owners.add(ownerService.dtoToEntity(dto))); - ownerService.save(ownerDTO); - - OwnerDTO found = ownerService.findByLastName(OWNER_LAST_NAME).stream().findFirst().get(); - - assertThat(found).isEqualToIgnoringGivenFields(ownerDTO, "id"); - } - - @Test - @Tag("findByLastName") - void findByLastName() { - OwnerDTO expected = ownerService.findById(1); - OwnerDTO found = ownerService.findByLastName(expected.getLastName()).stream().findFirst().get(); - - assertThat(found).isEqualToComparingFieldByField(expected); + List found = ownerService.entitiesToDTOS(owners); + assertThat(found).hasSameSizeAs(expected).containsAll(expected); } @Test @Tag("findById") + @DisplayName("Verify that we get OwnerDTO by his ID") void findById() { - ownerService.save(ownerDTO); - OwnerDTO expected = ownerService.findByLastName(OWNER_LAST_NAME).stream().findFirst().get(); - OwnerDTO found = ownerService.findById(expected.getId()); + List allDTO = ownerService.findAll(); + OwnerDTO expected = allDTO.get(2); - assertThat(found).isEqualToComparingFieldByField(expected); + assertThat(ownerService.findById(expected.getId())).isEqualTo(expected); } + + @Test + @Tag("findByLastName") + @DisplayName("Verify that we get OwnerDTO by his LastName") + void findByLastName() { + OwnerDTO expected = ownerService.findById(1); + + Optional found = ownerService.findByLastName(expected.getLastName()).stream().findFirst(); + + found.ifPresent(dto -> assertThat(dto).isEqualToComparingFieldByField(expected)); + } + + @Test + @Tag("findAll") + @DisplayName("Verify that the OwnerDTO list contain all previous elements and the new saved one") + void findAll() { + List expected = ownerService.findAll(); + + assertThat(expected).doesNotContain(ownerDTO); + ownerService.save(ownerDTO); + + List found = ownerService.findAll(); + + assertThat(found).contains(ownerDTO).containsAll(expected); + } + + @Test + @Tag("save") + @DisplayName("Verify that all OwnerDTO list contain the new saved one") + void save() { + assertThat(ownerService.findAll()).doesNotContain(ownerDTO); + + ownerService.save(ownerDTO); + + assertThat(ownerService.findAll()).contains(ownerDTO); + } + } diff --git a/src/test/java/org/springframework/samples/petclinic/service/PetServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/PetServiceTest.java new file mode 100644 index 000000000..0ecb9af6e --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/service/PetServiceTest.java @@ -0,0 +1,176 @@ +package org.springframework.samples.petclinic.service; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.samples.petclinic.dto.OwnerDTO; +import org.springframework.samples.petclinic.dto.PetDTO; +import org.springframework.samples.petclinic.dto.PetTypeDTO; +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.repository.PetRepository; +import org.springframework.samples.petclinic.repository.PetTypeRepository; +import org.springframework.samples.petclinic.repository.VetRepository; +import org.springframework.samples.petclinic.repository.VisitRepository; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.util.Collection; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@Slf4j +@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) +class PetServiceTest { + + private final static Integer OWNER_ID = 5; + + private final static Integer PET_ID = 14; + + private final static String PET_NAME = "bowser"; + + private final static String PET_BIRTH_DATE = "2020-07-11"; + + @Autowired + private OwnerService ownerService; + + @Autowired + private PetRepository petRepository; + + @Autowired + private PetTypeRepository petTypeRepository; + + @Autowired + private VisitRepository visitRepository; + + private PetService petService; + + private static Owner owner; + + private static Pet pet; + + private static PetDTO petDTO; + + @BeforeEach + void beforeEach() { + this.petService = new PetService(petRepository, petTypeRepository, visitRepository); + + PetTypeService petTypeService = new PetTypeService(petTypeRepository); + Collection petTypeDTOS = petService.findPetTypes(); + PetTypeDTO petTypeDTO = petTypeDTOS.stream().findFirst().get(); + PetType petType = petTypeService.dtoToEntity(petTypeDTO); + pet = new Pet(); + pet.setId(PET_ID); + pet.setName(PET_NAME); + pet.setType(petType); + pet.setBirthDate(LocalDate.parse(PET_BIRTH_DATE)); + petDTO = new PetDTO(); + petDTO.setId(PET_ID); + petDTO.setName(PET_NAME); + petDTO.setType(petTypeDTO); + petDTO.setBirthDate(LocalDate.parse(PET_BIRTH_DATE)); + + OwnerDTO ownerDTO = ownerService.findById(OWNER_ID); + ownerDTO.addPet(petDTO); + + pet.setOwner(ownerService.dtoToEntity(ownerDTO)); + petDTO.setOwner(ownerDTO); + } + + @Test + @Tag("dtoToEntity") + @DisplayName("Verify the convertion from DTO to Entity") + void dtoToEntity() { + Pet found = petService.dtoToEntity(petDTO); + + assertThat(found.getId()).isEqualTo(pet.getId()); + assertThat(found.getName()).isEqualTo(pet.getName()); + assertThat(found.getBirthDate()).isEqualTo(pet.getBirthDate()); + assertThat(found.getType()).isEqualTo(pet.getType()); + assertThat(found.getOwner()).isEqualTo(pet.getOwner()); + assertThat(found.getVisits()).isEqualTo(pet.getVisits()); + } + + @Test + @Tag("entityToDTO") + @DisplayName("Verify the convertion from Entity to DTO") + void entityToDTO() { + PetDTO found = petService.entityToDTO(pet); + + assertThat(found.getId()).isEqualTo(petDTO.getId()); + assertThat(found.getName()).isEqualTo(petDTO.getName()); + assertThat(found.getBirthDate()).isEqualTo(petDTO.getBirthDate()); + assertThat(found.getType()).isEqualTo(petDTO.getType()); + assertThat(found.getOwner()).isEqualTo(petDTO.getOwner()); + assertThat(found.getVisits()).isEqualTo(petDTO.getVisits()); + } + + @Test + @Tag("dtosToEntities") + @DisplayName("Verify the convertion from DTOs list to Entities list") + void dtosToEntities() { + List expected = petRepository.findAll(); + List allDTO = petService.findAll(); + + List found = petService.dtosToEntities(allDTO); + + assertThat(found).hasSameSizeAs(expected).isEqualTo(expected); + } + + @Test + @Tag("entitiesToDTOS") + @DisplayName("Verify the convertion from Entity to DTO") + void entitiesToDTOS() { + List allEntity = petRepository.findAll(); + List expected = petService.findAll(); + + List found = petService.entitiesToDTOS(allEntity); + + assertThat(found).hasSameSizeAs(expected).isEqualTo(expected); + } + + @Test + @Tag("findById") + @DisplayName("Verify that we get PetDTO by his ID") + void findById() { + List allDTO = petService.findAll(); + PetDTO expected = allDTO.get(2); + + assertThat(petService.findById(expected.getId())).isEqualTo(expected); + } + + @Test + @Tag("findAll") + @DisplayName("Verify that the PetDTO list contain all previous elements and the new saved one") + void findAll() { + List expected = petService.findAll(); + + assertThat(expected).doesNotContain(petDTO); + petService.save(petDTO); + + List found = petService.findAll(); + + assertThat(found).contains(petDTO).containsAll(expected); + + } + + @Test + @Tag("save") + @DisplayName("Verify that all PetDTO list contain the new saved one") + void save() { + assertThat(petService.findAll()).doesNotContain(petDTO); + + petService.save(petDTO); + + assertThat(petService.findAll()).contains(petDTO); + } + +} diff --git a/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java new file mode 100644 index 000000000..289a8c501 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java @@ -0,0 +1,138 @@ +package org.springframework.samples.petclinic.service; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.samples.petclinic.dto.VetDTO; +import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.repository.VetRepository; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@Slf4j +@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) +class VetServiceTest { + + private final static Integer VET_ID = 11; + + private final static String VET_FIRST_NAME = "Sam"; + + private final static String VET_LAST_NAME = "Schultz"; + + @Autowired + private VetRepository vetRepository; + + private VetService vetService; + + private static Vet vet; + + private static VetDTO vetDTO; + + @BeforeEach + void beforeEach() { + vetService = new VetService(vetRepository); + vet = new Vet(); + vet.setId(VET_ID); + vet.setFirstName(VET_FIRST_NAME); + vet.setLastName(VET_LAST_NAME); + vetDTO = new VetDTO(); + vetDTO.setId(VET_ID); + vetDTO.setFirstName(VET_FIRST_NAME); + vetDTO.setLastName(VET_LAST_NAME); + + } + + @Test + @Tag("dtoToEntity") + @DisplayName("Verify the convertion from DTO to Entity") + void dtoToEntity() { + Vet found = vetService.dtoToEntity(vetDTO); + + assertThat(found.getId()).isEqualTo(vet.getId()); + assertThat(found.getFirstName()).isEqualTo(vet.getFirstName()); + assertThat(found.getLastName()).isEqualTo(vet.getLastName()); + } + + @Test + @Tag("entityToDTO") + @DisplayName("Verify the convertion from Entity to DTO") + void entityToDTO() { + VetDTO found = vetService.entityToDTO(vet); + + assertThat(found.getId()).isEqualTo(vetDTO.getId()); + assertThat(found.getFirstName()).isEqualTo(vetDTO.getFirstName()); + assertThat(found.getLastName()).isEqualTo(vetDTO.getLastName()); + } + + @Test + @DisplayName("Verify the convertion from DTOs list to Entities list") + @Tag("dtosToEntities") + void dtosToEntities() { + List vetDTOS = vetService.findAll(); + List expected = new ArrayList<>(); + vetDTOS.forEach(dto -> expected.add(vetService.dtoToEntity(dto))); + + Collection found = vetService.dtosToEntities(vetDTOS); + + assertThat(found).hasSameSizeAs(expected).isEqualTo(expected); + } + + @Test + @Tag("entitiesToDTOS") + @DisplayName("Verify the convertion from Entities list to DTOs list") + void entitiesToDTOS() { + List expected = vetService.findAll(); + List vets = new ArrayList<>(); + expected.forEach(dto -> vets.add(vetService.dtoToEntity(dto))); + + List found = vetService.entitiesToDTOS(vets); + + assertThat(found).hasSameSizeAs(expected).isEqualTo(expected); + } + + @Test + @Tag("findById") + @DisplayName("Verify that we get VetDTO by his ID") + void findById() { + List allDTO = vetService.findAll(); + VetDTO expected = allDTO.get(2); + + assertThat(vetService.findById(expected.getId())).isEqualTo(expected); + } + + @Test + @Tag("findAll") + @DisplayName("Verify that the VetDTO list contain all previous elements and the new saved one") + void findAll() { + List expected = vetService.findAll(); + + assertThat(expected).doesNotContain(vetDTO); + vetService.save(vetDTO); + + List found = vetService.findAll(); + + assertThat(found).contains(vetDTO).containsAll(expected); + } + + @Test + @Tag("save") + @DisplayName("Verify that all VetDTO list contain the new saved one") + void save() { + assertThat(vetService.findAll()).doesNotContain(vetDTO); + + vetService.save(vetDTO); + + assertThat(vetService.findAll()).contains(vetDTO); + } + +}