mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
Implemented unitary tests for services
This commit is contained in:
parent
4d62c09efb
commit
f3e9c39681
35 changed files with 1001 additions and 366 deletions
|
@ -58,7 +58,7 @@ class PetController {
|
||||||
|
|
||||||
@ModelAttribute("types")
|
@ModelAttribute("types")
|
||||||
public Collection<PetTypeDTO> populatePetTypes() {
|
public Collection<PetTypeDTO> populatePetTypes() {
|
||||||
return this.petTypeService.findPetTypes();
|
return this.petService.findPetTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute("owner")
|
@ModelAttribute("owner")
|
||||||
|
|
|
@ -38,4 +38,16 @@ public class NamedDTO extends BaseDTO {
|
||||||
return this.getName();
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,23 +130,20 @@ public class OwnerDTO extends PersonDTO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof OwnerDTO)) return false;
|
return true;
|
||||||
|
if (!(o instanceof OwnerDTO))
|
||||||
|
return false;
|
||||||
|
|
||||||
OwnerDTO ownerDTO = (OwnerDTO) o;
|
OwnerDTO ownerDTO = (OwnerDTO) o;
|
||||||
|
|
||||||
if (!getAddress().equals(ownerDTO.getAddress())) return false;
|
if (!getAddress().equals(ownerDTO.getAddress()))
|
||||||
if (!getCity().equals(ownerDTO.getCity())) return false;
|
return false;
|
||||||
if (!getTelephone().equals(ownerDTO.getTelephone())) 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;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,19 +48,16 @@ public class PersonDTO extends BaseDTO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof PersonDTO)) return false;
|
return true;
|
||||||
|
if (!(o instanceof PersonDTO))
|
||||||
|
return false;
|
||||||
|
|
||||||
PersonDTO personDTO = (PersonDTO) o;
|
PersonDTO personDTO = (PersonDTO) o;
|
||||||
|
|
||||||
if (!getFirstName().equals(personDTO.getFirstName())) return false;
|
if (!getFirstName().equals(personDTO.getFirstName()))
|
||||||
|
return false;
|
||||||
return getLastName().equals(personDTO.getLastName());
|
return getLastName().equals(personDTO.getLastName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int result = getFirstName().hashCode();
|
|
||||||
result = 31 * result + getLastName().hashCode();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,23 +86,20 @@ public class PetDTO extends NamedDTO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof PetDTO)) return false;
|
return true;
|
||||||
|
if (!(o instanceof PetDTO))
|
||||||
|
return false;
|
||||||
|
|
||||||
PetDTO petDTO = (PetDTO) o;
|
PetDTO petDTO = (PetDTO) o;
|
||||||
|
|
||||||
if (!getBirthDate().equals(petDTO.getBirthDate())) return false;
|
if (!getBirthDate().equals(petDTO.getBirthDate()))
|
||||||
if (!getType().equals(petDTO.getType())) return false;
|
return false;
|
||||||
if (!getOwner().equals(petDTO.getOwner())) 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;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,4 +7,9 @@ package org.springframework.samples.petclinic.dto;
|
||||||
*/
|
*/
|
||||||
public class PetTypeDTO extends NamedDTO {
|
public class PetTypeDTO extends NamedDTO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return super.equals(obj);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,4 +57,18 @@ public class VetDTO extends PersonDTO {
|
||||||
getSpecialtiesInternal().add(specialty);
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,21 +67,18 @@ public class VisitDTO extends BaseDTO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof VisitDTO)) return false;
|
return true;
|
||||||
|
if (!(o instanceof VisitDTO))
|
||||||
|
return false;
|
||||||
|
|
||||||
VisitDTO visitDTO = (VisitDTO) o;
|
VisitDTO visitDTO = (VisitDTO) o;
|
||||||
|
|
||||||
if (!getDate().equals(visitDTO.getDate())) return false;
|
if (!getDate().equals(visitDTO.getDate()))
|
||||||
if (!getDescription().equals(visitDTO.getDescription())) return false;
|
return false;
|
||||||
|
if (!getDescription().equals(visitDTO.getDescription()))
|
||||||
|
return false;
|
||||||
return getPetId().equals(visitDTO.getPetId());
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.util.Locale;
|
||||||
|
|
||||||
import org.springframework.format.Formatter;
|
import org.springframework.format.Formatter;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -37,10 +38,10 @@ import org.springframework.stereotype.Component;
|
||||||
@Component
|
@Component
|
||||||
public class PetTypeFormatter implements Formatter<PetTypeDTO> {
|
public class PetTypeFormatter implements Formatter<PetTypeDTO> {
|
||||||
|
|
||||||
private final PetTypeService petTypeService;
|
private final PetService petService;
|
||||||
|
|
||||||
public PetTypeFormatter(PetTypeService petTypeService) {
|
public PetTypeFormatter(PetService petService) {
|
||||||
this.petTypeService = petTypeService;
|
this.petService = petService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -50,7 +51,7 @@ public class PetTypeFormatter implements Formatter<PetTypeDTO> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PetTypeDTO parse(String text, Locale locale) throws ParseException {
|
public PetTypeDTO parse(String text, Locale locale) throws ParseException {
|
||||||
Collection<PetTypeDTO> findPetTypes = this.petTypeService.findPetTypes();
|
Collection<PetTypeDTO> findPetTypes = this.petService.findPetTypes();
|
||||||
for (PetTypeDTO type : findPetTypes) {
|
for (PetTypeDTO type : findPetTypes) {
|
||||||
if (type.getName().equals(text)) {
|
if (type.getName().equals(text)) {
|
||||||
return type;
|
return type;
|
||||||
|
|
|
@ -46,16 +46,14 @@ public class NamedEntity extends BaseEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof NamedEntity)) return false;
|
return true;
|
||||||
|
if (!(o instanceof NamedEntity))
|
||||||
|
return false;
|
||||||
|
|
||||||
NamedEntity that = (NamedEntity) o;
|
NamedEntity that = (NamedEntity) o;
|
||||||
|
|
||||||
return getName().equals(that.getName());
|
return getName().equals(that.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return getName().hashCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,25 +147,4 @@ public class Owner extends Person {
|
||||||
.append(CommonAttribute.OWNER_PHONE, this.telephone).toString();
|
.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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,19 +53,16 @@ public class Person extends BaseEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof Person)) return false;
|
return true;
|
||||||
|
if (!(o instanceof Person))
|
||||||
|
return false;
|
||||||
|
|
||||||
Person person = (Person) o;
|
Person person = (Person) o;
|
||||||
|
|
||||||
if (!getFirstName().equals(person.getFirstName())) return false;
|
if (!getFirstName().equals(person.getFirstName()))
|
||||||
|
return false;
|
||||||
return getLastName().equals(person.getLastName());
|
return getLastName().equals(person.getLastName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int result = getFirstName().hashCode();
|
|
||||||
result = 31 * result + getLastName().hashCode();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ public class Pet extends NamedEntity {
|
||||||
return this.owner;
|
return this.owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setOwner(Owner owner) {
|
public void setOwner(Owner owner) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,25 +109,22 @@ public class Pet extends NamedEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof Pet)) return false;
|
return true;
|
||||||
if (!super.equals(o)) return false;
|
if (!(o instanceof Pet))
|
||||||
|
return false;
|
||||||
|
if (!super.equals(o))
|
||||||
|
return false;
|
||||||
|
|
||||||
Pet pet = (Pet) o;
|
Pet pet = (Pet) o;
|
||||||
|
|
||||||
if (!getBirthDate().equals(pet.getBirthDate())) return false;
|
if (!getBirthDate().equals(pet.getBirthDate()))
|
||||||
if (!getType().equals(pet.getType())) return false;
|
return false;
|
||||||
if (!getOwner().equals(pet.getOwner())) 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;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,4 +25,9 @@ import javax.persistence.Table;
|
||||||
@Table(name = "types")
|
@Table(name = "types")
|
||||||
public class PetType extends NamedEntity {
|
public class PetType extends NamedEntity {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return super.equals(obj);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,21 +78,18 @@ public class Visit extends BaseEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (!(o instanceof Visit)) return false;
|
return true;
|
||||||
|
if (!(o instanceof Visit))
|
||||||
|
return false;
|
||||||
|
|
||||||
Visit visit = (Visit) o;
|
Visit visit = (Visit) o;
|
||||||
|
|
||||||
if (!getDate().equals(visit.getDate())) return false;
|
if (!getDate().equals(visit.getDate()))
|
||||||
if (!getDescription().equals(visit.getDescription())) return false;
|
return false;
|
||||||
|
if (!getDescription().equals(visit.getDescription()))
|
||||||
|
return false;
|
||||||
return getPetId().equals(visit.getPetId());
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package org.springframework.samples.petclinic.repository;
|
package org.springframework.samples.petclinic.repository;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
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;
|
||||||
|
@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public interface OwnerRepository extends Repository<Owner, Integer> {
|
public interface OwnerRepository extends Repository<Owner, Integer> {
|
||||||
|
|
||||||
|
@ -56,6 +58,12 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
Owner findById(@Param("id") Integer id);
|
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<Owner> findAll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save an {@link Owner} to the data store, either inserting or updating it.
|
* Save an {@link Owner} to the data store, either inserting or updating it.
|
||||||
* @param owner the {@link Owner} to save
|
* @param owner the {@link Owner} to save
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.repository;
|
package org.springframework.samples.petclinic.repository;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public interface PetRepository extends Repository<Pet, Integer> {
|
public interface PetRepository extends Repository<Pet, Integer> {
|
||||||
|
|
||||||
|
@ -49,9 +51,14 @@ 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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all {@link Pet}s from the data store
|
||||||
|
* @return a Collection of {@link Pet}s (or an empty Collection if none
|
||||||
|
*/
|
||||||
|
List<Pet> findAll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a {@link Pet} to the data store, either inserting or updating it.
|
* Save a {@link Pet} to the data store, either inserting or updating it.
|
||||||
* @param pet the {@link Pet} to save
|
* @param pet the {@link Pet} to save
|
||||||
|
|
|
@ -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 <code>PetType</code> 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<PetType, Integer> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<PetType> 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);
|
||||||
|
|
||||||
|
}
|
|
@ -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 <code>Speciality</code> 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<Specialty, Integer> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Specialty> 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);
|
||||||
|
|
||||||
|
}
|
|
@ -32,9 +32,17 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public interface VetRepository extends Repository<Vet, Integer> {
|
public interface VetRepository extends Repository<Vet, Integer> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 <code>Vet</code>s from the data store.
|
* Retrieve all <code>Vet</code>s from the data store.
|
||||||
* @return a <code>Collection</code> of <code>Vet</code>s
|
* @return a <code>Collection</code> of <code>Vet</code>s
|
||||||
|
@ -43,4 +51,10 @@ public interface VetRepository extends Repository<Vet, Integer> {
|
||||||
@Cacheable("vets")
|
@Cacheable("vets")
|
||||||
Collection<Vet> findAll();
|
Collection<Vet> 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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.repository;
|
package org.springframework.samples.petclinic.repository;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
|
@ -31,6 +32,7 @@ import org.springframework.samples.petclinic.model.Visit;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Michael Isvy
|
* @author Michael Isvy
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
public interface VisitRepository extends Repository<Visit, Integer> {
|
public interface VisitRepository extends Repository<Visit, Integer> {
|
||||||
|
|
||||||
|
@ -41,6 +43,19 @@ public interface VisitRepository extends Repository<Visit, Integer> {
|
||||||
*/
|
*/
|
||||||
void save(Visit visit);
|
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<Visit> findByPetId(Integer petId);
|
List<Visit> 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<Visit> findAll();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
package org.springframework.samples.petclinic.service;
|
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<E, D> {
|
public interface BaseService<E, D> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,27 +14,45 @@ public interface BaseService<E, D> {
|
||||||
* @param dto DTO
|
* @param dto DTO
|
||||||
* @return Entity Model
|
* @return Entity Model
|
||||||
*/
|
*/
|
||||||
public E dtoToEntity(D dto);
|
E dtoToEntity(D dto);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert Entity Model to Data Transfert Object
|
* Convert Entity Model to Data Transfert Object
|
||||||
* @param entity Entity Model
|
* @param entity Entity Model
|
||||||
* @return DTO
|
* @return DTO
|
||||||
*/
|
*/
|
||||||
public D entityToDTO(E entity);
|
D entityToDTO(E entity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert Entities Models Collection to Data Transfert Object Collection
|
* Convert Entities Models Collection to Data Transfert Object Collection
|
||||||
* @param entities Collection of Entity Model
|
* @param entities Collection of Entity Model
|
||||||
* @return Collection of DTO
|
* @return Collection of DTO
|
||||||
*/
|
*/
|
||||||
public Collection<D> entitiesToDTOS(Collection<E> entities);
|
List<D> entitiesToDTOS(List<E> entities);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert Entities Models Collection to Data Transfert Object Collection
|
* Convert Entities Models Collection to Data Transfert Object Collection
|
||||||
* @param dtos Collection of DTO
|
* @param dtos Collection of DTO
|
||||||
* @return Collection of Entity Model
|
* @return Collection of Entity Model
|
||||||
*/
|
*/
|
||||||
public Collection<E> dtosToEntities(Collection<D> dtos);
|
List<E> dtosToEntities(List<D> 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<D> findAll();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save DTO object to repository
|
||||||
|
*/
|
||||||
|
void save(D dto);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,94 +1,102 @@
|
||||||
package org.springframework.samples.petclinic.service;
|
package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.modelmapper.internal.util.Lists;
|
||||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
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.OwnerRepository;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
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.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
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")
|
@Service("OwnerService")
|
||||||
public class OwnerService implements BaseService<Owner, OwnerDTO> {
|
public class OwnerService implements BaseService<Owner, OwnerDTO> {
|
||||||
|
|
||||||
private final OwnerRepository ownerRepository;
|
private final OwnerRepository ownerRepository;
|
||||||
|
|
||||||
private final PetRepository petRepository;
|
private final PetRepository petRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
private PetService petService;
|
private PetService petService;
|
||||||
|
|
||||||
public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository) {
|
public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository,
|
||||||
|
PetTypeRepository petTypeRepository, VisitRepository visitRepository) {
|
||||||
this.ownerRepository = ownerRepository;
|
this.ownerRepository = ownerRepository;
|
||||||
this.petRepository = petRepository;
|
this.petRepository = petRepository;
|
||||||
petService = new PetService(petRepository);
|
petService = new PetService(petRepository, petTypeRepository, visitRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Owner dtoToEntity(OwnerDTO dto) {
|
public Owner dtoToEntity(OwnerDTO dto) {
|
||||||
if(dto == null) {
|
if (dto != null) {
|
||||||
return new Owner();
|
Owner owner = modelMapper.map(dto, Owner.class);
|
||||||
}
|
dto.getPets().forEach(petDTO -> owner.addPet(petService.dtoToEntity(petDTO)));
|
||||||
Owner owner = modelMapper.map(dto, Owner.class);
|
return owner;
|
||||||
|
|
||||||
for(PetDTO petDTO: dto.getPets()) {
|
|
||||||
owner.addPet(petService.dtoToEntity(petDTO));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return owner;
|
return new Owner();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OwnerDTO entityToDTO(Owner entity) {
|
public OwnerDTO entityToDTO(Owner entity) {
|
||||||
if(entity == null) {
|
if (entity != null) {
|
||||||
return new OwnerDTO();
|
OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class);
|
||||||
}
|
entity.getPets().forEach(pet -> ownerDTO.addPet(petService.entityToDTO(pet)));
|
||||||
OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class);
|
return ownerDTO;
|
||||||
|
|
||||||
for(Pet pet : entity.getPets()) {
|
|
||||||
ownerDTO.addPet(petService.entityToDTO(pet));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ownerDTO;
|
return new OwnerDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<OwnerDTO> entitiesToDTOS(Collection<Owner> entities) {
|
public List<OwnerDTO> entitiesToDTOS(List<Owner> entities) {
|
||||||
Collection<OwnerDTO> dtos = new HashSet<>();
|
List<OwnerDTO> dtos = new ArrayList<>();
|
||||||
|
|
||||||
for (Owner entity : entities) {
|
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
|
||||||
dtos.add(entityToDTO(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Owner> dtosToEntities(Collection<OwnerDTO> dtos) {
|
public List<Owner> dtosToEntities(List<OwnerDTO> dtos) {
|
||||||
Collection<Owner> entities = new HashSet<>();
|
List<Owner> entities = new ArrayList<>();
|
||||||
|
|
||||||
for (OwnerDTO dto : dtos) {
|
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
|
||||||
entities.add(dtoToEntity(dto));
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(OwnerDTO ownerDTO) {
|
@Override
|
||||||
Owner owner = dtoToEntity(ownerDTO);
|
|
||||||
ownerRepository.save(owner);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Collection<OwnerDTO> findByLastName(String lastName) {
|
|
||||||
Collection<Owner> owners = ownerRepository.findByLastName(lastName);
|
|
||||||
return entitiesToDTOS(owners);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OwnerDTO findById(int ownerId) {
|
public OwnerDTO findById(int ownerId) {
|
||||||
Owner owner = ownerRepository.findById(ownerId);
|
Owner owner = ownerRepository.findById(ownerId);
|
||||||
return entityToDTO(owner);
|
return entityToDTO(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<OwnerDTO> findAll() {
|
||||||
|
return entitiesToDTOS(ownerRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(OwnerDTO ownerDTO) {
|
||||||
|
Owner owner = dtoToEntity(ownerDTO);
|
||||||
|
ownerRepository.save(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OwnerDTO> findByLastName(String lastName) {
|
||||||
|
Collection<Owner> owners = ownerRepository.findByLastName(lastName);
|
||||||
|
return entitiesToDTOS(Lists.from(owners.iterator()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,71 +2,103 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
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.PetRepository;
|
||||||
|
import org.springframework.samples.petclinic.repository.PetTypeRepository;
|
||||||
|
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Service between Pet entity and PetDTO Data Transfert Object.
|
||||||
|
*
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
|
*/
|
||||||
@Service("PetService")
|
@Service("PetService")
|
||||||
public class PetService implements BaseService<Pet, PetDTO> {
|
public class PetService implements BaseService<Pet, PetDTO> {
|
||||||
|
|
||||||
private final PetRepository petRepository;
|
private final PetRepository petRepository;
|
||||||
|
|
||||||
|
private final PetTypeService petTypeService;
|
||||||
|
|
||||||
|
private final PetTypeRepository petTypeRepository;
|
||||||
|
|
||||||
|
private final VisitService visitService;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public PetService(PetRepository petRepository) {
|
public PetService(PetRepository petRepository, PetTypeRepository petTypeRepository,
|
||||||
|
VisitRepository visitRepository) {
|
||||||
this.petRepository = petRepository;
|
this.petRepository = petRepository;
|
||||||
|
this.petTypeRepository = petTypeRepository;
|
||||||
|
this.visitService = new VisitService(visitRepository);
|
||||||
|
this.petTypeService = new PetTypeService(petTypeRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pet dtoToEntity(PetDTO dto) {
|
public Pet dtoToEntity(PetDTO dto) {
|
||||||
if(dto == null) {
|
if (dto != null) {
|
||||||
return new Pet();
|
Pet pet = modelMapper.map(dto, Pet.class);
|
||||||
} else {
|
dto.getVisits().forEach(visitDTO -> pet.addVisit(visitService.dtoToEntity(visitDTO)));
|
||||||
return modelMapper.map(dto, Pet.class);
|
return pet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new Pet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PetDTO entityToDTO(Pet entity) {
|
public PetDTO entityToDTO(Pet entity) {
|
||||||
if(entity == null) {
|
if (entity != null) {
|
||||||
return new PetDTO();
|
PetDTO petDTO = modelMapper.map(entity, PetDTO.class);
|
||||||
} else {
|
entity.getVisits().forEach(visit -> petDTO.addVisit(visitService.entityToDTO(visit)));
|
||||||
return modelMapper.map(entity, PetDTO.class);
|
return petDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new PetDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<PetDTO> entitiesToDTOS(Collection<Pet> entities) {
|
public List<PetDTO> entitiesToDTOS(List<Pet> entities) {
|
||||||
Collection<PetDTO> dtos = new HashSet<>();
|
List<PetDTO> dtos = new ArrayList<>();
|
||||||
|
|
||||||
|
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
|
||||||
|
|
||||||
for (Pet entity : entities) {
|
|
||||||
dtos.add(entityToDTO(entity));
|
|
||||||
}
|
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Pet> dtosToEntities(Collection<PetDTO> dtos) {
|
public List<Pet> dtosToEntities(List<PetDTO> dtos) {
|
||||||
Collection<Pet> entities = new HashSet<>();
|
List<Pet> entities = new ArrayList<>();
|
||||||
|
|
||||||
for (PetDTO dto : dtos) {
|
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
|
||||||
entities.add(dtoToEntity(dto));
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(PetDTO petDTO) {
|
@Override
|
||||||
Pet pet = dtoToEntity(petDTO);
|
|
||||||
petRepository.save(pet);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PetDTO findById(int petId) {
|
public PetDTO findById(int petId) {
|
||||||
Pet pet = petRepository.findById(petId);
|
Pet pet = petRepository.findById(petId);
|
||||||
return entityToDTO(pet);
|
return entityToDTO(pet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PetDTO> findAll() {
|
||||||
|
return entitiesToDTOS(petRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(PetDTO petDTO) {
|
||||||
|
petRepository.save(dtoToEntity(petDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PetTypeDTO> findPetTypes() {
|
||||||
|
List<PetType> petTypes = petRepository.findPetTypes();
|
||||||
|
|
||||||
|
return petTypeService.entitiesToDTOS(petTypes);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,58 +3,77 @@ package org.springframework.samples.petclinic.service;
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Service between PetType entity and PetTypeDTO Data Transfert Object.
|
||||||
|
*
|
||||||
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
|
*/
|
||||||
@Service("PerTypeService")
|
@Service("PerTypeService")
|
||||||
public class PetTypeService implements BaseService<PetType, PetTypeDTO> {
|
public class PetTypeService implements BaseService<PetType, PetTypeDTO> {
|
||||||
|
|
||||||
private final PetRepository petRepository;
|
private final PetTypeRepository petTypeRepository;
|
||||||
|
|
||||||
private final ModelMapper modelMapper = new ModelMapper();
|
private final ModelMapper modelMapper = new ModelMapper();
|
||||||
|
|
||||||
public PetTypeService(PetRepository petRepository) {
|
public PetTypeService(PetTypeRepository petTypeRepository) {
|
||||||
this.petRepository = petRepository;
|
this.petTypeRepository = petTypeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PetType dtoToEntity(PetTypeDTO dto) {
|
public PetType dtoToEntity(PetTypeDTO dto) {
|
||||||
return modelMapper.map(dto, PetType.class);
|
if (dto != null) {
|
||||||
|
return modelMapper.map(dto, PetType.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PetType();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PetTypeDTO entityToDTO(PetType entity) {
|
public PetTypeDTO entityToDTO(PetType entity) {
|
||||||
return modelMapper.map(entity, PetTypeDTO.class);
|
if (entity != null) {
|
||||||
|
return modelMapper.map(entity, PetTypeDTO.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PetTypeDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<PetTypeDTO> entitiesToDTOS(Collection<PetType> entities) {
|
public List<PetTypeDTO> entitiesToDTOS(List<PetType> entities) {
|
||||||
Collection<PetTypeDTO> dtos = new HashSet<>();
|
List<PetTypeDTO> dtos = new ArrayList<>();
|
||||||
|
|
||||||
for (PetType entity : entities) {
|
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
|
||||||
dtos.add(entityToDTO(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<PetType> dtosToEntities(Collection<PetTypeDTO> dtos) {
|
public List<PetType> dtosToEntities(List<PetTypeDTO> dtos) {
|
||||||
Collection<PetType> entities = new HashSet<>();
|
List<PetType> entities = new ArrayList<>();
|
||||||
|
|
||||||
for (PetTypeDTO dto : dtos) {
|
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
|
||||||
entities.add(dtoToEntity(dto));
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<PetTypeDTO> findPetTypes() {
|
@Override
|
||||||
Collection<PetType> petTypes = petRepository.findPetTypes();
|
public PetTypeDTO findById(int id) {
|
||||||
return entitiesToDTOS(petTypes);
|
return entityToDTO(petTypeRepository.findById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PetTypeDTO> findAll() {
|
||||||
|
return entitiesToDTOS(petTypeRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(PetTypeDTO dto) {
|
||||||
|
petTypeRepository.save(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<Specialty, SpecialtyDTO> {
|
|
||||||
|
|
||||||
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<SpecialtyDTO> entitiesToDTOS(Collection<Specialty> entities) {
|
|
||||||
Collection<SpecialtyDTO> dtos = new HashSet<>();
|
|
||||||
|
|
||||||
for (Specialty entity : entities) {
|
|
||||||
dtos.add(entityToDTO(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
return dtos;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<Specialty> dtosToEntities(Collection<SpecialtyDTO> dtos) {
|
|
||||||
Collection<Specialty> entities = new HashSet<>();
|
|
||||||
|
|
||||||
for (SpecialtyDTO dto : dtos) {
|
|
||||||
entities.add(dtoToEntity(dto));
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<Specialty, SpecialtyDTO> {
|
||||||
|
|
||||||
|
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<SpecialtyDTO> entitiesToDTOS(List<Specialty> entities) {
|
||||||
|
List<SpecialtyDTO> dtos = new ArrayList<>();
|
||||||
|
|
||||||
|
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
|
||||||
|
|
||||||
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Specialty> dtosToEntities(List<SpecialtyDTO> dtos) {
|
||||||
|
List<Specialty> 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<SpecialtyDTO> findAll() {
|
||||||
|
return entitiesToDTOS(specialtyRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(SpecialtyDTO dto) {
|
||||||
|
specialtyRepository.save(dtoToEntity(dto));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,14 +1,22 @@
|
||||||
package org.springframework.samples.petclinic.service;
|
package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.modelmapper.internal.util.Lists;
|
||||||
import org.springframework.samples.petclinic.dto.VetDTO;
|
import org.springframework.samples.petclinic.dto.VetDTO;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
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")
|
@Service("VetService")
|
||||||
public class VetService implements BaseService<Vet, VetDTO> {
|
public class VetService implements BaseService<Vet, VetDTO> {
|
||||||
|
|
||||||
|
@ -22,39 +30,52 @@ public class VetService implements BaseService<Vet, VetDTO> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vet dtoToEntity(VetDTO dto) {
|
public Vet dtoToEntity(VetDTO dto) {
|
||||||
return modelMapper.map(dto, Vet.class);
|
if (dto != null) {
|
||||||
|
return modelMapper.map(dto, Vet.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Vet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public VetDTO entityToDTO(Vet entity) {
|
public VetDTO entityToDTO(Vet entity) {
|
||||||
return modelMapper.map(entity, VetDTO.class);
|
if (entity != null) {
|
||||||
|
return modelMapper.map(entity, VetDTO.class);
|
||||||
|
}
|
||||||
|
return new VetDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<VetDTO> entitiesToDTOS(Collection<Vet> entities) {
|
public List<VetDTO> entitiesToDTOS(List<Vet> entities) {
|
||||||
Collection<VetDTO> dtos = new HashSet<>();
|
List<VetDTO> dtos = new ArrayList<>();
|
||||||
|
|
||||||
for (Vet entity : entities) {
|
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
|
||||||
dtos.add(entityToDTO(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Vet> dtosToEntities(Collection<VetDTO> dtos) {
|
public List<Vet> dtosToEntities(List<VetDTO> dtos) {
|
||||||
Collection<Vet> entities = new HashSet<>();
|
List<Vet> entities = new ArrayList<>();
|
||||||
|
|
||||||
for (VetDTO dto : dtos) {
|
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
|
||||||
entities.add(dtoToEntity(dto));
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<VetDTO> findAll() {
|
@Override
|
||||||
|
public VetDTO findById(int id) {
|
||||||
|
return entityToDTO(vetRepository.findById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<VetDTO> findAll() {
|
||||||
Collection<Vet> vets = vetRepository.findAll();
|
Collection<Vet> vets = vetRepository.findAll();
|
||||||
return entitiesToDTOS(vets);
|
return entitiesToDTOS(Lists.from(vets.iterator()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(VetDTO dto) {
|
||||||
|
vetRepository.save(dtoToEntity(dto));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,16 @@ import org.springframework.samples.petclinic.model.Visit;
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
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")
|
@Service("VisitService")
|
||||||
public class VisitService implements BaseService<Visit, VisitDTO> {
|
public class VisitService implements BaseService<Visit, VisitDTO> {
|
||||||
|
|
||||||
|
@ -22,35 +29,51 @@ public class VisitService implements BaseService<Visit, VisitDTO> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Visit dtoToEntity(VisitDTO dto) {
|
public Visit dtoToEntity(VisitDTO dto) {
|
||||||
return modelMapper.map(dto, Visit.class);
|
if (dto != null) {
|
||||||
|
return modelMapper.map(dto, Visit.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Visit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public VisitDTO entityToDTO(Visit entity) {
|
public VisitDTO entityToDTO(Visit entity) {
|
||||||
return modelMapper.map(entity, VisitDTO.class);
|
if (entity != null) {
|
||||||
|
return modelMapper.map(entity, VisitDTO.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new VisitDTO();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<VisitDTO> entitiesToDTOS(Collection<Visit> entities) {
|
public List<VisitDTO> entitiesToDTOS(List<Visit> entities) {
|
||||||
Collection<VisitDTO> dtos = new HashSet<>();
|
List<VisitDTO> dtos = new ArrayList<>();
|
||||||
|
|
||||||
|
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
|
||||||
|
|
||||||
for (Visit entity : entities) {
|
|
||||||
dtos.add(entityToDTO(entity));
|
|
||||||
}
|
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Visit> dtosToEntities(Collection<VisitDTO> dtos) {
|
public List<Visit> dtosToEntities(List<VisitDTO> dtos) {
|
||||||
Collection<Visit> entities = new HashSet<>();
|
List<Visit> entities = new ArrayList<>();
|
||||||
|
|
||||||
for (VisitDTO dto : dtos) {
|
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
|
||||||
entities.add(dtoToEntity(dto));
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public VisitDTO findById(int id) {
|
||||||
|
return entityToDTO(visitRepository.findById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<VisitDTO> findAll() {
|
||||||
|
return entitiesToDTOS(visitRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void save(VisitDTO visitDTO) {
|
public void save(VisitDTO visitDTO) {
|
||||||
Visit visit = dtoToEntity(visitDTO);
|
Visit visit = dtoToEntity(visitDTO);
|
||||||
visitRepository.save(visit);
|
visitRepository.save(visit);
|
||||||
|
|
|
@ -79,7 +79,7 @@ class PetControllerTests {
|
||||||
|
|
||||||
given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO());
|
given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO());
|
||||||
given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO());
|
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
|
@Test
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
@ -45,13 +46,13 @@ import static org.mockito.BDDMockito.given;
|
||||||
class PetTypeDTOFormatterTests {
|
class PetTypeDTOFormatterTests {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private PetTypeService petTypeService;
|
private PetService petService;
|
||||||
|
|
||||||
private PetTypeFormatter petTypeFormatter;
|
private PetTypeFormatter petTypeFormatter;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
this.petTypeFormatter = new PetTypeFormatter(petTypeService);
|
this.petTypeFormatter = new PetTypeFormatter(petService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -64,17 +65,15 @@ class PetTypeDTOFormatterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldParse() throws ParseException {
|
void shouldParse() throws ParseException {
|
||||||
given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
|
given(this.petService.findPetTypes()).willReturn(makePetTypes());
|
||||||
PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
|
PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
|
||||||
assertThat(petType.getName()).isEqualTo("Bird");
|
assertThat(petType.getName()).isEqualTo("Bird");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldThrowParseException() throws ParseException {
|
void shouldThrowParseException() {
|
||||||
given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
|
given(this.petService.findPetTypes()).willReturn(makePetTypes());
|
||||||
Assertions.assertThrows(ParseException.class, () -> {
|
Assertions.assertThrows(ParseException.class, () -> petTypeFormatter.parse("Fish", Locale.ENGLISH));
|
||||||
petTypeFormatter.parse("Fish", Locale.ENGLISH);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,6 +31,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||||
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
|
||||||
import org.springframework.samples.petclinic.model.PetType;
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
|
import org.springframework.samples.petclinic.service.PetService;
|
||||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -45,13 +46,13 @@ import static org.mockito.BDDMockito.given;
|
||||||
class PetTypeFormatterTests {
|
class PetTypeFormatterTests {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private PetTypeService petTypeService;
|
private PetService petService;
|
||||||
|
|
||||||
private PetTypeFormatter petTypeFormatter;
|
private PetTypeFormatter petTypeFormatter;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
this.petTypeFormatter = new PetTypeFormatter(petTypeService);
|
this.petTypeFormatter = new PetTypeFormatter(petService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -64,17 +65,15 @@ class PetTypeFormatterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldParse() throws ParseException {
|
void shouldParse() throws ParseException {
|
||||||
given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
|
given(this.petService.findPetTypes()).willReturn(makePetTypes());
|
||||||
PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
|
PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
|
||||||
assertThat(petType.getName()).isEqualTo("Bird");
|
assertThat(petType.getName()).isEqualTo("Bird");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldThrowParseException() throws ParseException {
|
void shouldThrowParseException() {
|
||||||
given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
|
given(this.petService.findPetTypes()).willReturn(makePetTypes());
|
||||||
Assertions.assertThrows(ParseException.class, () -> {
|
Assertions.assertThrows(ParseException.class, () -> petTypeFormatter.parse("Fish", Locale.ENGLISH));
|
||||||
petTypeFormatter.parse("Fish", Locale.ENGLISH);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.springframework.samples.petclinic.service;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Tag;
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -13,45 +14,67 @@ import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.model.Pet;
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
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.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Collection;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
|
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
|
||||||
class OwnerServiceTest {
|
class OwnerServiceTest {
|
||||||
|
|
||||||
private final static Integer OWNER_ID = 55;
|
private final static Integer OWNER_ID = 55;
|
||||||
|
|
||||||
private final static String OWNER_FIRST_NAME = "Sam";
|
private final static String OWNER_FIRST_NAME = "Sam";
|
||||||
|
|
||||||
private final static String OWNER_LAST_NAME = "Schultz";
|
private final static String OWNER_LAST_NAME = "Schultz";
|
||||||
|
|
||||||
private final static String OWNER_ADDRESS = "4, Evans Street";
|
private final static String OWNER_ADDRESS = "4, Evans Street";
|
||||||
|
|
||||||
private final static String OWNER_CITY = "Wollongong";
|
private final static String OWNER_CITY = "Wollongong";
|
||||||
|
|
||||||
private final static String OWNER_PHONE = "1234567890";
|
private final static String OWNER_PHONE = "1234567890";
|
||||||
|
|
||||||
private final static Integer PET_ID = 11;
|
private final static Integer PET_ID = 11;
|
||||||
|
|
||||||
private final static String PET_NAME = "bowser";
|
private final static String PET_NAME = "bowser";
|
||||||
|
|
||||||
private final static String PET_BIRTH_DATE = "2020-07-11";
|
private final static String PET_BIRTH_DATE = "2020-07-11";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private OwnerRepository ownerRepository;
|
private OwnerRepository ownerRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PetRepository petRepository;
|
private PetRepository petRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PetTypeRepository petTypeRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private VisitRepository visitRepository;
|
||||||
|
|
||||||
private PetService petService;
|
private PetService petService;
|
||||||
|
|
||||||
private OwnerService ownerService;
|
private OwnerService ownerService;
|
||||||
|
|
||||||
private static Owner owner;
|
private static Owner owner;
|
||||||
|
|
||||||
private static OwnerDTO ownerDTO;
|
private static OwnerDTO ownerDTO;
|
||||||
|
|
||||||
private static Pet pet;
|
private static Pet pet;
|
||||||
|
|
||||||
private static PetDTO petDTO;
|
private static PetDTO petDTO;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void beforeEach() {
|
void beforeEach() {
|
||||||
petService = new PetService(petRepository);
|
petService = new PetService(petRepository, petTypeRepository, visitRepository);
|
||||||
ownerService = new OwnerService(ownerRepository, petRepository);
|
ownerService = new OwnerService(ownerRepository, petRepository, petTypeRepository, visitRepository);
|
||||||
pet = new Pet();
|
pet = new Pet();
|
||||||
pet.setId(PET_ID);
|
pet.setId(PET_ID);
|
||||||
pet.setName(PET_NAME);
|
pet.setName(PET_NAME);
|
||||||
|
@ -81,8 +104,11 @@ class OwnerServiceTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("dtoToEntity")
|
@Tag("dtoToEntity")
|
||||||
|
@DisplayName("Verify the convertion from DTO to Entity")
|
||||||
void dtoToEntity() {
|
void dtoToEntity() {
|
||||||
Owner found = ownerService.dtoToEntity(ownerDTO);
|
Owner found = ownerService.dtoToEntity(ownerDTO);
|
||||||
|
|
||||||
|
assertThat(found.getId()).isEqualTo(owner.getId());
|
||||||
assertThat(found.getFirstName()).isEqualTo(owner.getFirstName());
|
assertThat(found.getFirstName()).isEqualTo(owner.getFirstName());
|
||||||
assertThat(found.getLastName()).isEqualTo(owner.getLastName());
|
assertThat(found.getLastName()).isEqualTo(owner.getLastName());
|
||||||
assertThat(found.getAddress()).isEqualTo(owner.getAddress());
|
assertThat(found.getAddress()).isEqualTo(owner.getAddress());
|
||||||
|
@ -91,7 +117,7 @@ class OwnerServiceTest {
|
||||||
|
|
||||||
assertThat(found.getPets().size()).isEqualTo(owner.getPets().size());
|
assertThat(found.getPets().size()).isEqualTo(owner.getPets().size());
|
||||||
|
|
||||||
for(Pet pet: found.getPets()) {
|
for (Pet pet : found.getPets()) {
|
||||||
assertThat(owner.getPets()).contains(pet);
|
assertThat(owner.getPets()).contains(pet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,8 +125,11 @@ class OwnerServiceTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("entityToDTO")
|
@Tag("entityToDTO")
|
||||||
|
@DisplayName("Verify the convertion from Entity to DTO")
|
||||||
void entityToDTO() {
|
void entityToDTO() {
|
||||||
OwnerDTO found = ownerService.entityToDTO(owner);
|
OwnerDTO found = ownerService.entityToDTO(owner);
|
||||||
|
|
||||||
|
assertThat(found.getId()).isEqualTo(ownerDTO.getId());
|
||||||
assertThat(found.getFirstName()).isEqualTo(ownerDTO.getFirstName());
|
assertThat(found.getFirstName()).isEqualTo(ownerDTO.getFirstName());
|
||||||
assertThat(found.getLastName()).isEqualTo(ownerDTO.getLastName());
|
assertThat(found.getLastName()).isEqualTo(ownerDTO.getLastName());
|
||||||
assertThat(found.getAddress()).isEqualTo(ownerDTO.getAddress());
|
assertThat(found.getAddress()).isEqualTo(ownerDTO.getAddress());
|
||||||
|
@ -108,86 +137,81 @@ class OwnerServiceTest {
|
||||||
assertThat(found.getTelephone()).isEqualTo(ownerDTO.getTelephone());
|
assertThat(found.getTelephone()).isEqualTo(ownerDTO.getTelephone());
|
||||||
assertThat(found.getPets().size()).isEqualTo(ownerDTO.getPets().size());
|
assertThat(found.getPets().size()).isEqualTo(ownerDTO.getPets().size());
|
||||||
|
|
||||||
for(PetDTO petDTO: found.getPets()) {
|
for (PetDTO petDTO : found.getPets()) {
|
||||||
assertThat(ownerDTO.getPets()).contains(petDTO);
|
assertThat(ownerDTO.getPets()).contains(petDTO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
@Tag("entitiesToDTOS")
|
|
||||||
void entitiesToDTOS() {
|
|
||||||
Collection<Owner> owners = new HashSet<>();
|
|
||||||
Collection<OwnerDTO> expected = new HashSet<>();
|
|
||||||
Collection<OwnerDTO> 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
|
@Test
|
||||||
@Tag("dtosToEntities")
|
@Tag("dtosToEntities")
|
||||||
|
@DisplayName("Verify the convertion from DTOs list to Entities list")
|
||||||
void dtosToEntities() {
|
void dtosToEntities() {
|
||||||
Collection<OwnerDTO> ownerDTOS = new HashSet<>();
|
List<OwnerDTO> ownerDTOS = ownerService.findAll();
|
||||||
Collection<Owner> expected = new HashSet<>();
|
List<Owner> expected = new ArrayList<>();
|
||||||
Collection<Owner> found;
|
ownerDTOS.forEach(dto -> expected.add(ownerService.dtoToEntity(dto)));
|
||||||
|
|
||||||
for(int i =1 ; i<5; i++) {
|
List<Owner> found = ownerService.dtosToEntities(ownerDTOS);
|
||||||
OwnerDTO ownerDTO = ownerService.findById(i);
|
|
||||||
expected.add(ownerService.dtoToEntity(ownerDTO));
|
|
||||||
ownerDTOS.add(ownerDTO);
|
|
||||||
}
|
|
||||||
|
|
||||||
found = ownerService.dtosToEntities(ownerDTOS);
|
assertThat(found).hasSameSizeAs(expected).containsAll(expected);
|
||||||
|
|
||||||
assertThat(found).hasSameSizeAs(expected);
|
|
||||||
|
|
||||||
for( int i=1; i<5; i++) {
|
|
||||||
assertThat(expected).contains(found.iterator().next());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Tag("entitiesToDTOS")
|
||||||
@Tag("save")
|
@DisplayName("Verify the convertion from Entities list to DTOs list")
|
||||||
void save() {
|
void entitiesToDTOS() {
|
||||||
Collection<OwnerDTO> founds = ownerService.findByLastName(OWNER_LAST_NAME);
|
List<OwnerDTO> expected = ownerService.findAll();
|
||||||
assertThat(founds).isEmpty();
|
List<Owner> owners = new ArrayList<>();
|
||||||
|
expected.forEach(dto -> owners.add(ownerService.dtoToEntity(dto)));
|
||||||
|
|
||||||
ownerService.save(ownerDTO);
|
List<OwnerDTO> found = ownerService.entitiesToDTOS(owners);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
assertThat(found).hasSameSizeAs(expected).containsAll(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("findById")
|
@Tag("findById")
|
||||||
|
@DisplayName("Verify that we get OwnerDTO by his ID")
|
||||||
void findById() {
|
void findById() {
|
||||||
ownerService.save(ownerDTO);
|
List<OwnerDTO> allDTO = ownerService.findAll();
|
||||||
OwnerDTO expected = ownerService.findByLastName(OWNER_LAST_NAME).stream().findFirst().get();
|
OwnerDTO expected = allDTO.get(2);
|
||||||
OwnerDTO found = ownerService.findById(expected.getId());
|
|
||||||
|
|
||||||
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<OwnerDTO> 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<OwnerDTO> expected = ownerService.findAll();
|
||||||
|
|
||||||
|
assertThat(expected).doesNotContain(ownerDTO);
|
||||||
|
ownerService.save(ownerDTO);
|
||||||
|
|
||||||
|
List<OwnerDTO> 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<PetTypeDTO> 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<Pet> expected = petRepository.findAll();
|
||||||
|
List<PetDTO> allDTO = petService.findAll();
|
||||||
|
|
||||||
|
List<Pet> found = petService.dtosToEntities(allDTO);
|
||||||
|
|
||||||
|
assertThat(found).hasSameSizeAs(expected).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("entitiesToDTOS")
|
||||||
|
@DisplayName("Verify the convertion from Entity to DTO")
|
||||||
|
void entitiesToDTOS() {
|
||||||
|
List<Pet> allEntity = petRepository.findAll();
|
||||||
|
List<PetDTO> expected = petService.findAll();
|
||||||
|
|
||||||
|
List<PetDTO> 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<PetDTO> 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<PetDTO> expected = petService.findAll();
|
||||||
|
|
||||||
|
assertThat(expected).doesNotContain(petDTO);
|
||||||
|
petService.save(petDTO);
|
||||||
|
|
||||||
|
List<PetDTO> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<VetDTO> vetDTOS = vetService.findAll();
|
||||||
|
List<Vet> expected = new ArrayList<>();
|
||||||
|
vetDTOS.forEach(dto -> expected.add(vetService.dtoToEntity(dto)));
|
||||||
|
|
||||||
|
Collection<Vet> 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<VetDTO> expected = vetService.findAll();
|
||||||
|
List<Vet> vets = new ArrayList<>();
|
||||||
|
expected.forEach(dto -> vets.add(vetService.dtoToEntity(dto)));
|
||||||
|
|
||||||
|
List<VetDTO> 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<VetDTO> 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<VetDTO> expected = vetService.findAll();
|
||||||
|
|
||||||
|
assertThat(expected).doesNotContain(vetDTO);
|
||||||
|
vetService.save(vetDTO);
|
||||||
|
|
||||||
|
List<VetDTO> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue