Implemented unitary tests for services

This commit is contained in:
PEDSF 2020-10-17 16:20:31 +02:00
parent 4d62c09efb
commit f3e9c39681
35 changed files with 1001 additions and 366 deletions

View file

@ -58,7 +58,7 @@ class PetController {
@ModelAttribute("types")
public Collection<PetTypeDTO> populatePetTypes() {
return this.petTypeService.findPetTypes();
return this.petService.findPetTypes();
}
@ModelAttribute("owner")

View file

@ -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());
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -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());
}
}

View file

@ -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;
}
}

View file

@ -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<PetTypeDTO> {
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<PetTypeDTO> {
@Override
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) {
if (type.getName().equals(text)) {
return type;

View file

@ -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();
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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<Owner, Integer> {
@ -56,6 +58,12 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
@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<Owner> findAll();
/**
* Save an {@link Owner} to the data store, either inserting or updating it.
* @param owner the {@link Owner} to save

View file

@ -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<Pet, Integer> {
@ -49,9 +51,14 @@ public interface PetRepository extends Repository<Pet, Integer> {
* @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<Pet> findAll();
/**
* Save a {@link Pet} to the data store, either inserting or updating it.
* @param pet the {@link Pet} to save

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<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.
* @return a <code>Collection</code> of <code>Vet</code>s
@ -43,4 +51,10 @@ public interface VetRepository extends Repository<Vet, Integer> {
@Cacheable("vets")
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);
}

View file

@ -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<Visit, Integer> {
@ -41,6 +43,19 @@ public interface VisitRepository extends Repository<Visit, Integer> {
*/
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);
/**
* 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();
}

View file

@ -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<E, D> {
/**
@ -9,27 +14,45 @@ public interface BaseService<E, D> {
* @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<D> entitiesToDTOS(Collection<E> entities);
List<D> entitiesToDTOS(List<E> entities);
/**
* Convert Entities Models Collection to Data Transfert Object Collection
* @param dtos Collection of DTO
* @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);
}

View file

@ -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<Owner, OwnerDTO> {
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();
}
if (dto != null) {
Owner owner = modelMapper.map(dto, Owner.class);
for(PetDTO petDTO: dto.getPets()) {
owner.addPet(petService.dtoToEntity(petDTO));
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();
}
if (entity != null) {
OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class);
for(Pet pet : entity.getPets()) {
ownerDTO.addPet(petService.entityToDTO(pet));
}
entity.getPets().forEach(pet -> ownerDTO.addPet(petService.entityToDTO(pet)));
return ownerDTO;
}
@Override
public Collection<OwnerDTO> entitiesToDTOS(Collection<Owner> entities) {
Collection<OwnerDTO> dtos = new HashSet<>();
for (Owner entity : entities) {
dtos.add(entityToDTO(entity));
return new OwnerDTO();
}
@Override
public List<OwnerDTO> entitiesToDTOS(List<Owner> entities) {
List<OwnerDTO> dtos = new ArrayList<>();
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
return dtos;
}
@Override
public Collection<Owner> dtosToEntities(Collection<OwnerDTO> dtos) {
Collection<Owner> entities = new HashSet<>();
public List<Owner> dtosToEntities(List<OwnerDTO> dtos) {
List<Owner> 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<OwnerDTO> findByLastName(String lastName) {
Collection<Owner> owners = ownerRepository.findByLastName(lastName);
return entitiesToDTOS(owners);
}
@Override
public OwnerDTO findById(int ownerId) {
Owner owner = ownerRepository.findById(ownerId);
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()));
}
}

View file

@ -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<Pet, PetDTO> {
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<PetDTO> entitiesToDTOS(Collection<Pet> entities) {
Collection<PetDTO> dtos = new HashSet<>();
public List<PetDTO> entitiesToDTOS(List<Pet> entities) {
List<PetDTO> dtos = new ArrayList<>();
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
for (Pet entity : entities) {
dtos.add(entityToDTO(entity));
}
return dtos;
}
@Override
public Collection<Pet> dtosToEntities(Collection<PetDTO> dtos) {
Collection<Pet> entities = new HashSet<>();
public List<Pet> dtosToEntities(List<PetDTO> dtos) {
List<Pet> 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<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);
}
}

View file

@ -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<PetType, PetTypeDTO> {
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) {
if (dto != null) {
return modelMapper.map(dto, PetType.class);
}
return new PetType();
}
@Override
public PetTypeDTO entityToDTO(PetType entity) {
if (entity != null) {
return modelMapper.map(entity, PetTypeDTO.class);
}
@Override
public Collection<PetTypeDTO> entitiesToDTOS(Collection<PetType> entities) {
Collection<PetTypeDTO> dtos = new HashSet<>();
for (PetType entity : entities) {
dtos.add(entityToDTO(entity));
return new PetTypeDTO();
}
@Override
public List<PetTypeDTO> entitiesToDTOS(List<PetType> entities) {
List<PetTypeDTO> dtos = new ArrayList<>();
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
return dtos;
}
@Override
public Collection<PetType> dtosToEntities(Collection<PetTypeDTO> dtos) {
Collection<PetType> entities = new HashSet<>();
public List<PetType> dtosToEntities(List<PetTypeDTO> dtos) {
List<PetType> entities = new ArrayList<>();
for (PetTypeDTO dto : dtos) {
entities.add(dtoToEntity(dto));
}
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
return entities;
}
public Collection<PetTypeDTO> findPetTypes() {
Collection<PetType> petTypes = petRepository.findPetTypes();
return entitiesToDTOS(petTypes);
@Override
public PetTypeDTO findById(int id) {
return entityToDTO(petTypeRepository.findById(id));
}
@Override
public List<PetTypeDTO> findAll() {
return entitiesToDTOS(petTypeRepository.findAll());
}
@Override
public void save(PetTypeDTO dto) {
petTypeRepository.save(dtoToEntity(dto));
}
}

View file

@ -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;
}
}

View file

@ -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));
}
}

View file

@ -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<Vet, VetDTO> {
@ -22,39 +30,52 @@ public class VetService implements BaseService<Vet, VetDTO> {
@Override
public Vet dtoToEntity(VetDTO dto) {
if (dto != null) {
return modelMapper.map(dto, Vet.class);
}
return new Vet();
}
@Override
public VetDTO entityToDTO(Vet entity) {
if (entity != null) {
return modelMapper.map(entity, VetDTO.class);
}
return new VetDTO();
}
@Override
public Collection<VetDTO> entitiesToDTOS(Collection<Vet> entities) {
Collection<VetDTO> dtos = new HashSet<>();
public List<VetDTO> entitiesToDTOS(List<Vet> entities) {
List<VetDTO> dtos = new ArrayList<>();
for (Vet entity : entities) {
dtos.add(entityToDTO(entity));
}
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
return dtos;
}
@Override
public Collection<Vet> dtosToEntities(Collection<VetDTO> dtos) {
Collection<Vet> entities = new HashSet<>();
public List<Vet> dtosToEntities(List<VetDTO> dtos) {
List<Vet> entities = new ArrayList<>();
for (VetDTO dto : dtos) {
entities.add(dtoToEntity(dto));
}
dtos.forEach(dto -> entities.add(dtoToEntity(dto)));
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();
return entitiesToDTOS(vets);
return entitiesToDTOS(Lists.from(vets.iterator()));
}
@Override
public void save(VetDTO dto) {
vetRepository.save(dtoToEntity(dto));
}
}

View file

@ -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<Visit, VisitDTO> {
@ -22,35 +29,51 @@ public class VisitService implements BaseService<Visit, VisitDTO> {
@Override
public Visit dtoToEntity(VisitDTO dto) {
if (dto != null) {
return modelMapper.map(dto, Visit.class);
}
return new Visit();
}
@Override
public VisitDTO entityToDTO(Visit entity) {
if (entity != null) {
return modelMapper.map(entity, VisitDTO.class);
}
@Override
public Collection<VisitDTO> entitiesToDTOS(Collection<Visit> entities) {
Collection<VisitDTO> dtos = new HashSet<>();
for (Visit entity : entities) {
dtos.add(entityToDTO(entity));
return new VisitDTO();
}
@Override
public List<VisitDTO> entitiesToDTOS(List<Visit> entities) {
List<VisitDTO> dtos = new ArrayList<>();
entities.forEach(entity -> dtos.add(entityToDTO(entity)));
return dtos;
}
@Override
public Collection<Visit> dtosToEntities(Collection<VisitDTO> dtos) {
Collection<Visit> entities = new HashSet<>();
public List<Visit> dtosToEntities(List<VisitDTO> dtos) {
List<Visit> 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<VisitDTO> findAll() {
return entitiesToDTOS(visitRepository.findAll());
}
@Override
public void save(VisitDTO visitDTO) {
Visit visit = dtoToEntity(visitDTO);
visitRepository.save(visit);

View file

@ -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

View file

@ -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));
}
/**

View file

@ -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));
}
/**

View file

@ -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<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
@Tag("dtosToEntities")
@DisplayName("Verify the convertion from DTOs list to Entities list")
void dtosToEntities() {
Collection<OwnerDTO> ownerDTOS = new HashSet<>();
Collection<Owner> expected = new HashSet<>();
Collection<Owner> found;
List<OwnerDTO> ownerDTOS = ownerService.findAll();
List<Owner> 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<Owner> 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<OwnerDTO> founds = ownerService.findByLastName(OWNER_LAST_NAME);
assertThat(founds).isEmpty();
@Tag("entitiesToDTOS")
@DisplayName("Verify the convertion from Entities list to DTOs list")
void entitiesToDTOS() {
List<OwnerDTO> expected = ownerService.findAll();
List<Owner> 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<OwnerDTO> 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<OwnerDTO> 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<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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}