add equals and hashcode

This commit is contained in:
PEDSF 2020-10-14 19:23:34 +02:00
parent 20cfa0a078
commit 4d62c09efb
13 changed files with 420 additions and 6 deletions

17
pom.xml
View file

@ -137,6 +137,23 @@
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -128,4 +128,25 @@ public class OwnerDTO extends PersonDTO {
.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 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;
}
} }

View file

@ -46,4 +46,21 @@ public class PersonDTO extends BaseDTO {
this.lastName = lastName; 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;
}
} }

View file

@ -84,4 +84,25 @@ public class PetDTO extends NamedDTO {
visit.setPetId(this.getId()); 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;
}
} }

View file

@ -65,4 +65,23 @@ public class VisitDTO extends BaseDTO {
this.petId = petId; 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;
}
} }

View file

@ -44,4 +44,18 @@ public class NamedEntity extends BaseEntity {
return this.getName(); 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();
}
} }

View file

@ -147,4 +147,25 @@ 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;
}
} }

View file

@ -51,4 +51,21 @@ public class Person extends BaseEntity {
this.lastName = lastName; 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;
}
} }

View file

@ -107,4 +107,27 @@ public class Pet extends NamedEntity {
visit.setPetId(this.getId()); 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;
}
} }

View file

@ -76,4 +76,23 @@ public class Visit extends BaseEntity {
this.petId = petId; 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;
}
} }

View file

@ -2,8 +2,11 @@ package org.springframework.samples.petclinic.service;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
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.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collection; import java.util.Collection;
@ -13,21 +16,42 @@ import java.util.HashSet;
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 ModelMapper modelMapper = new ModelMapper(); private final ModelMapper modelMapper = new ModelMapper();
private PetService petService;
public OwnerService(OwnerRepository ownerRepository) { public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository) {
this.ownerRepository = ownerRepository; this.ownerRepository = ownerRepository;
this.petRepository = petRepository;
petService = new PetService(petRepository);
} }
@Override @Override
public Owner dtoToEntity(OwnerDTO dto) { 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 @Override
public OwnerDTO entityToDTO(Owner entity) { 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 @Override

View file

@ -22,12 +22,20 @@ public class PetService implements BaseService<Pet, PetDTO> {
@Override @Override
public Pet dtoToEntity(PetDTO dto) { 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 @Override
public PetDTO entityToDTO(Pet entity) { 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 @Override

View file

@ -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<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")
void dtosToEntities() {
Collection<OwnerDTO> ownerDTOS = new HashSet<>();
Collection<Owner> expected = new HashSet<>();
Collection<Owner> 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<OwnerDTO> 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);
}
}