diff --git a/pom.xml b/pom.xml
index 3636c8154..b48f5e05d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -137,6 +137,23 @@
spring-boot-devtools
true
+
+ junit
+ junit
+ 4.13
+ test
+
+
+ junit
+ junit
+ test
+
+
+ org.projectlombok
+ lombok
+ 1.18.12
+ test
+
diff --git a/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java
index 60ee8a05d..7f7cb1272 100644
--- a/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java
+++ b/src/main/java/org/springframework/samples/petclinic/dto/OwnerDTO.java
@@ -128,4 +128,25 @@ public class OwnerDTO extends PersonDTO {
.append(CommonAttribute.OWNER_PHONE, this.telephone).toString();
}
+ @Override
+ public boolean equals(Object o) {
+ 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;
+ return getPets() != null ? getPets().equals(ownerDTO.getPets()) : ownerDTO.getPets() == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getAddress().hashCode();
+ result = 31 * result + getCity().hashCode();
+ result = 31 * result + getTelephone().hashCode();
+ result = 31 * result + (getPets() != null ? getPets().hashCode() : 0);
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java
index 963341601..07565c8d0 100644
--- a/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java
+++ b/src/main/java/org/springframework/samples/petclinic/dto/PersonDTO.java
@@ -46,4 +46,21 @@ public class PersonDTO extends BaseDTO {
this.lastName = lastName;
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof PersonDTO)) return false;
+
+ PersonDTO personDTO = (PersonDTO) o;
+
+ if (!getFirstName().equals(personDTO.getFirstName())) return false;
+ return getLastName().equals(personDTO.getLastName());
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getFirstName().hashCode();
+ result = 31 * result + getLastName().hashCode();
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java
index 5b5925adc..e88359a20 100644
--- a/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java
+++ b/src/main/java/org/springframework/samples/petclinic/dto/PetDTO.java
@@ -84,4 +84,25 @@ public class PetDTO extends NamedDTO {
visit.setPetId(this.getId());
}
+ @Override
+ public boolean equals(Object o) {
+ 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;
+ return getVisits() != null ? getVisits().equals(petDTO.getVisits()) : petDTO.getVisits() == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getBirthDate().hashCode();
+ result = 31 * result + getType().hashCode();
+ result = 31 * result + getOwner().hashCode();
+ result = 31 * result + (getVisits() != null ? getVisits().hashCode() : 0);
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java
index 835b0dd75..617dd4fbb 100644
--- a/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java
+++ b/src/main/java/org/springframework/samples/petclinic/dto/VisitDTO.java
@@ -65,4 +65,23 @@ public class VisitDTO extends BaseDTO {
this.petId = petId;
}
+ @Override
+ public boolean equals(Object o) {
+ 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;
+ return getPetId().equals(visitDTO.getPetId());
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getDate().hashCode();
+ result = 31 * result + getDescription().hashCode();
+ result = 31 * result + getPetId().hashCode();
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java
index 088e52e81..7ac5aec0c 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java
@@ -44,4 +44,18 @@ public class NamedEntity extends BaseEntity {
return this.getName();
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof NamedEntity)) return false;
+
+ NamedEntity that = (NamedEntity) o;
+
+ return getName().equals(that.getName());
+ }
+
+ @Override
+ public int hashCode() {
+ return getName().hashCode();
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/src/main/java/org/springframework/samples/petclinic/model/Owner.java
index e5a4be5d5..24815138f 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Owner.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Owner.java
@@ -147,4 +147,25 @@ public class Owner extends Person {
.append(CommonAttribute.OWNER_PHONE, this.telephone).toString();
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Owner)) return false;
+
+ Owner owner = (Owner) o;
+
+ if (!getAddress().equals(owner.getAddress())) return false;
+ if (!getCity().equals(owner.getCity())) return false;
+ if (!getTelephone().equals(owner.getTelephone())) return false;
+ return getPets() != null ? getPets().equals(owner.getPets()) : owner.getPets() == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getAddress().hashCode();
+ result = 31 * result + getCity().hashCode();
+ result = 31 * result + getTelephone().hashCode();
+ result = 31 * result + (getPets() != null ? getPets().hashCode() : 0);
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Person.java b/src/main/java/org/springframework/samples/petclinic/model/Person.java
index 15fabacc3..536deb548 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Person.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Person.java
@@ -51,4 +51,21 @@ public class Person extends BaseEntity {
this.lastName = lastName;
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Person)) return false;
+
+ Person person = (Person) o;
+
+ if (!getFirstName().equals(person.getFirstName())) return false;
+ return getLastName().equals(person.getLastName());
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getFirstName().hashCode();
+ result = 31 * result + getLastName().hashCode();
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java
index c1db8010e..40683de7d 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java
@@ -107,4 +107,27 @@ public class Pet extends NamedEntity {
visit.setPetId(this.getId());
}
+ @Override
+ public boolean equals(Object o) {
+ 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;
+ return getVisits() != null ? getVisits().equals(pet.getVisits()) : pet.getVisits() == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = super.hashCode();
+ result = 31 * result + getBirthDate().hashCode();
+ result = 31 * result + getType().hashCode();
+ result = 31 * result + getOwner().hashCode();
+ result = 31 * result + (getVisits() != null ? getVisits().hashCode() : 0);
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/src/main/java/org/springframework/samples/petclinic/model/Visit.java
index 3ccbcfada..f90594d4c 100644
--- a/src/main/java/org/springframework/samples/petclinic/model/Visit.java
+++ b/src/main/java/org/springframework/samples/petclinic/model/Visit.java
@@ -76,4 +76,23 @@ public class Visit extends BaseEntity {
this.petId = petId;
}
+ @Override
+ public boolean equals(Object o) {
+ 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;
+ return getPetId().equals(visit.getPetId());
+ }
+
+ @Override
+ public int hashCode() {
+ int result = getDate().hashCode();
+ result = 31 * result + getDescription().hashCode();
+ result = 31 * result + getPetId().hashCode();
+ return result;
+ }
}
diff --git a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java b/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java
index 404706c33..1c75f8843 100644
--- a/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java
+++ b/src/main/java/org/springframework/samples/petclinic/service/OwnerService.java
@@ -2,8 +2,11 @@ package org.springframework.samples.petclinic.service;
import org.modelmapper.ModelMapper;
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.stereotype.Service;
import java.util.Collection;
@@ -13,21 +16,42 @@ import java.util.HashSet;
public class OwnerService implements BaseService {
private final OwnerRepository ownerRepository;
-
+ private final PetRepository petRepository;
private final ModelMapper modelMapper = new ModelMapper();
+ private PetService petService;
- public OwnerService(OwnerRepository ownerRepository) {
+ public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository) {
this.ownerRepository = ownerRepository;
+ this.petRepository = petRepository;
+ petService = new PetService(petRepository);
}
@Override
public Owner dtoToEntity(OwnerDTO dto) {
- return modelMapper.map(dto, Owner.class);
+ if(dto == null) {
+ return new Owner();
+ }
+ Owner owner = modelMapper.map(dto, Owner.class);
+
+ for(PetDTO petDTO: dto.getPets()) {
+ owner.addPet(petService.dtoToEntity(petDTO));
+ }
+
+ return owner;
}
@Override
public OwnerDTO entityToDTO(Owner entity) {
- return modelMapper.map(entity, OwnerDTO.class);
+ if(entity == null) {
+ return new OwnerDTO();
+ }
+ OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class);
+
+ for(Pet pet : entity.getPets()) {
+ ownerDTO.addPet(petService.entityToDTO(pet));
+ }
+
+ return ownerDTO;
}
@Override
diff --git a/src/main/java/org/springframework/samples/petclinic/service/PetService.java b/src/main/java/org/springframework/samples/petclinic/service/PetService.java
index 6c470dfa1..191eb558d 100644
--- a/src/main/java/org/springframework/samples/petclinic/service/PetService.java
+++ b/src/main/java/org/springframework/samples/petclinic/service/PetService.java
@@ -22,12 +22,20 @@ public class PetService implements BaseService {
@Override
public Pet dtoToEntity(PetDTO dto) {
- return modelMapper.map(dto, Pet.class);
+ if(dto == null) {
+ return new Pet();
+ } else {
+ return modelMapper.map(dto, Pet.class);
+ }
}
@Override
public PetDTO entityToDTO(Pet entity) {
- return modelMapper.map(entity, PetDTO.class);
+ if(entity == null) {
+ return new PetDTO();
+ } else {
+ return modelMapper.map(entity, PetDTO.class);
+ }
}
@Override
diff --git a/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java
new file mode 100644
index 000000000..dc7d4c30a
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java
@@ -0,0 +1,193 @@
+package org.springframework.samples.petclinic.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
+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.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.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.time.LocalDate;
+import java.util.Collection;
+import java.util.HashSet;
+
+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;
+
+ 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);
+ pet = new Pet();
+ pet.setId(PET_ID);
+ pet.setName(PET_NAME);
+ pet.setBirthDate(LocalDate.parse(PET_BIRTH_DATE));
+ petDTO = new PetDTO();
+ petDTO.setId(PET_ID);
+ petDTO.setName(PET_NAME);
+ petDTO.setBirthDate(LocalDate.parse(PET_BIRTH_DATE));
+
+ owner = new Owner();
+ owner.setId(OWNER_ID);
+ owner.setFirstName(OWNER_FIRST_NAME);
+ owner.setLastName(OWNER_LAST_NAME);
+ owner.setAddress(OWNER_ADDRESS);
+ owner.setCity(OWNER_CITY);
+ owner.setTelephone(OWNER_PHONE);
+ owner.addPet(pet);
+ ownerDTO = new OwnerDTO();
+ ownerDTO.setId(OWNER_ID);
+ ownerDTO.setFirstName(OWNER_FIRST_NAME);
+ ownerDTO.setLastName(OWNER_LAST_NAME);
+ ownerDTO.setAddress(OWNER_ADDRESS);
+ ownerDTO.setCity(OWNER_CITY);
+ ownerDTO.setTelephone(OWNER_PHONE);
+ ownerDTO.addPet(petDTO);
+ }
+
+ @Test
+ @Tag("dtoToEntity")
+ void dtoToEntity() {
+ Owner found = ownerService.dtoToEntity(ownerDTO);
+ assertThat(found.getFirstName()).isEqualTo(owner.getFirstName());
+ assertThat(found.getLastName()).isEqualTo(owner.getLastName());
+ assertThat(found.getAddress()).isEqualTo(owner.getAddress());
+ assertThat(found.getCity()).isEqualTo(owner.getCity());
+ assertThat(found.getTelephone()).isEqualTo(owner.getTelephone());
+
+ assertThat(found.getPets().size()).isEqualTo(owner.getPets().size());
+
+ for(Pet pet: found.getPets()) {
+ assertThat(owner.getPets()).contains(pet);
+ }
+
+ }
+
+ @Test
+ @Tag("entityToDTO")
+ void entityToDTO() {
+ OwnerDTO found = ownerService.entityToDTO(owner);
+ assertThat(found.getFirstName()).isEqualTo(ownerDTO.getFirstName());
+ assertThat(found.getLastName()).isEqualTo(ownerDTO.getLastName());
+ assertThat(found.getAddress()).isEqualTo(ownerDTO.getAddress());
+ assertThat(found.getCity()).isEqualTo(ownerDTO.getCity());
+ assertThat(found.getTelephone()).isEqualTo(ownerDTO.getTelephone());
+ assertThat(found.getPets().size()).isEqualTo(ownerDTO.getPets().size());
+
+ for(PetDTO petDTO: found.getPets()) {
+ assertThat(ownerDTO.getPets()).contains(petDTO);
+ }
+ }
+
+ @Test
+ @Tag("entitiesToDTOS")
+ void entitiesToDTOS() {
+ Collection owners = new HashSet<>();
+ Collection expected = new HashSet<>();
+ Collection found;
+
+ for(int i =1 ; i<5; i++) {
+ OwnerDTO ownerDTO = ownerService.findById(i);
+ expected.add(ownerDTO);
+ owners.add(ownerService.dtoToEntity(ownerDTO));
+ }
+
+ found = ownerService.entitiesToDTOS(owners);
+
+ assertThat(found).hasSameSizeAs(expected);
+
+ for( int i=1; i<5; i++) {
+ assertThat(expected).contains(found.iterator().next());
+ }
+ }
+
+ @Test
+ @Tag("dtosToEntities")
+ void dtosToEntities() {
+ Collection ownerDTOS = new HashSet<>();
+ Collection expected = new HashSet<>();
+ Collection found;
+
+ for(int i =1 ; i<5; i++) {
+ OwnerDTO ownerDTO = ownerService.findById(i);
+ expected.add(ownerService.dtoToEntity(ownerDTO));
+ ownerDTOS.add(ownerDTO);
+ }
+
+ found = ownerService.dtosToEntities(ownerDTOS);
+
+ assertThat(found).hasSameSizeAs(expected);
+
+ for( int i=1; i<5; i++) {
+ assertThat(expected).contains(found.iterator().next());
+ }
+ }
+
+ @Test
+ @Transactional
+ @Tag("save")
+ void save() {
+ Collection founds = ownerService.findByLastName(OWNER_LAST_NAME);
+ assertThat(founds).isEmpty();
+
+ 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);
+
+ }
+
+ @Test
+ @Tag("findById")
+ void findById() {
+ ownerService.save(ownerDTO);
+ OwnerDTO expected = ownerService.findByLastName(OWNER_LAST_NAME).stream().findFirst().get();
+ OwnerDTO found = ownerService.findById(expected.getId());
+
+ assertThat(found).isEqualToComparingFieldByField(expected);
+ }
+}