mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
Fix find owner testing
This commit is contained in:
parent
bb2a8fa73a
commit
c6b54da5df
9 changed files with 69 additions and 19 deletions
|
@ -44,6 +44,7 @@ import java.util.Map;
|
|||
class OwnerController {
|
||||
|
||||
private final OwnerService ownerService;
|
||||
|
||||
private final VisitService visitService;
|
||||
|
||||
OwnerController(OwnerService ownerService, VisitService visitService) {
|
||||
|
@ -82,7 +83,7 @@ class OwnerController {
|
|||
}
|
||||
|
||||
@GetMapping(CommonEndPoint.OWNERS)
|
||||
public String processFindForm(OwnerDTO owner, BindingResult result,
|
||||
public String processFindForm(@ModelAttribute(CommonAttribute.OWNER) OwnerDTO owner, BindingResult result,
|
||||
Map<String, Object> model) {
|
||||
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
|
|
|
@ -86,7 +86,7 @@ public class OwnerDTO extends PersonDTO {
|
|||
|
||||
public void addPet(PetDTO pet) {
|
||||
|
||||
if(!this.getPets().contains(pet)) {
|
||||
if (!this.getPets().contains(pet)) {
|
||||
getPetsInternal().add(pet);
|
||||
}
|
||||
pet.setOwner(this);
|
||||
|
|
|
@ -97,7 +97,7 @@ public class PetDTO extends NamedDTO {
|
|||
return false;
|
||||
if (!getType().equals(petDTO.getType()))
|
||||
return false;
|
||||
if (!getOwner().equals(petDTO.getOwner()))
|
||||
if (!getOwner().getId().equals(petDTO.getOwner().getId()))
|
||||
return false;
|
||||
return getVisits() != null ? getVisits().equals(petDTO.getVisits()) : petDTO.getVisits() == null;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import net.bytebuddy.matcher.FilterableList;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.internal.util.Lists;
|
||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||
|
@ -44,7 +43,11 @@ public class OwnerService implements BaseService<Owner, OwnerDTO> {
|
|||
public Owner dtoToEntity(OwnerDTO dto) {
|
||||
if (dto != null) {
|
||||
Owner owner = modelMapper.map(dto, Owner.class);
|
||||
dto.getPets().forEach(petDTO -> owner.addPet(petService.dtoToEntity(petDTO)));
|
||||
dto.getPets().forEach(petDTO -> {
|
||||
Pet pet = modelMapper.map(petDTO, Pet.class);
|
||||
pet.setOwner(owner);
|
||||
owner.addPet(pet);
|
||||
});
|
||||
return owner;
|
||||
}
|
||||
|
||||
|
@ -55,12 +58,11 @@ public class OwnerService implements BaseService<Owner, OwnerDTO> {
|
|||
public OwnerDTO entityToDTO(Owner entity) {
|
||||
if (entity != null) {
|
||||
OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class);
|
||||
|
||||
for( Pet pet : entity.getPets()) {
|
||||
PetDTO petDTO = petService.entityToDTO(pet);
|
||||
entity.getPets().forEach(pet -> {
|
||||
PetDTO petDTO = modelMapper.map(pet, PetDTO.class);
|
||||
petDTO.setOwner(ownerDTO);
|
||||
ownerDTO.addPet(petDTO);
|
||||
}
|
||||
|
||||
});
|
||||
return ownerDTO;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
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.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;
|
||||
|
@ -43,7 +45,21 @@ public class PetService implements BaseService<Pet, PetDTO> {
|
|||
public Pet dtoToEntity(PetDTO dto) {
|
||||
if (dto != null) {
|
||||
Pet pet = modelMapper.map(dto, Pet.class);
|
||||
Owner owner = modelMapper.map(dto.getOwner(), Owner.class);
|
||||
|
||||
dto.getVisits().forEach(visitDTO -> pet.addVisit(visitService.dtoToEntity(visitDTO)));
|
||||
|
||||
dto.getOwner().getPets().forEach(petDTO -> {
|
||||
if (petDTO.getId().equals(dto.getId())) {
|
||||
owner.addPet(pet);
|
||||
}
|
||||
else {
|
||||
Pet otherPet = modelMapper.map(petDTO, Pet.class);
|
||||
otherPet.setOwner(owner);
|
||||
owner.addPet(otherPet);
|
||||
}
|
||||
});
|
||||
pet.setOwner(owner);
|
||||
return pet;
|
||||
}
|
||||
|
||||
|
@ -54,7 +70,21 @@ public class PetService implements BaseService<Pet, PetDTO> {
|
|||
public PetDTO entityToDTO(Pet entity) {
|
||||
if (entity != null) {
|
||||
PetDTO petDTO = modelMapper.map(entity, PetDTO.class);
|
||||
OwnerDTO ownerDTO = modelMapper.map(entity.getOwner(), OwnerDTO.class);
|
||||
|
||||
entity.getVisits().forEach(visit -> petDTO.addVisit(visitService.entityToDTO(visit)));
|
||||
|
||||
entity.getOwner().getPets().forEach(pet -> {
|
||||
if (pet.getId().equals(entity.getId())) {
|
||||
ownerDTO.addPet(petDTO);
|
||||
}
|
||||
else {
|
||||
PetDTO otherPetDTO = modelMapper.map(pet, PetDTO.class);
|
||||
otherPetDTO.setOwner(ownerDTO);
|
||||
ownerDTO.addPet(otherPetDTO);
|
||||
}
|
||||
});
|
||||
petDTO.setOwner(ownerDTO);
|
||||
return petDTO;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,9 @@ import java.util.List;
|
|||
public class VetService implements BaseService<Vet, VetDTO> {
|
||||
|
||||
private final VetRepository vetRepository;
|
||||
|
||||
private final SpecialtyService specialtyService;
|
||||
|
||||
private final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
public VetService(VetRepository vetRepository, SpecialtyRepository specialtyRepository) {
|
||||
|
|
|
@ -10,8 +10,10 @@ 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.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetTypeRepository;
|
||||
|
@ -20,6 +22,7 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -29,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
|
||||
class OwnerServiceTest {
|
||||
|
||||
private final static Integer OWNER_ID = 55;
|
||||
private final static Integer OWNER_ID = 11;
|
||||
|
||||
private final static String OWNER_FIRST_NAME = "Sam";
|
||||
|
||||
|
@ -75,13 +78,19 @@ class OwnerServiceTest {
|
|||
void beforeEach() {
|
||||
petService = new PetService(petRepository, petTypeRepository, visitRepository);
|
||||
ownerService = new OwnerService(ownerRepository, 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));
|
||||
|
||||
owner = new Owner();
|
||||
|
@ -118,7 +127,7 @@ class OwnerServiceTest {
|
|||
assertThat(found.getPets().size()).isEqualTo(owner.getPets().size());
|
||||
|
||||
for (Pet pet : found.getPets()) {
|
||||
assertThat(owner.getPets()).contains(pet);
|
||||
assertThat(owner.getPets()).extracting("id").contains(pet.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -138,7 +147,7 @@ class OwnerServiceTest {
|
|||
assertThat(found.getPets().size()).isEqualTo(ownerDTO.getPets().size());
|
||||
|
||||
for (PetDTO petDTO : found.getPets()) {
|
||||
assertThat(ownerDTO.getPets()).contains(petDTO);
|
||||
assertThat(ownerDTO.getPets()).extracting("id").contains(petDTO.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,7 +209,10 @@ class OwnerServiceTest {
|
|||
|
||||
List<OwnerDTO> found = ownerService.findAll();
|
||||
|
||||
assertThat(found).contains(ownerDTO).containsAll(expected);
|
||||
assertThat(found).hasSize(expected.size() + 1)
|
||||
.usingElementComparatorOnFields("lastName", "firstName", "address", "city", "telephone")
|
||||
.contains(ownerDTO).containsAnyElementsOf(expected);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -210,8 +222,10 @@ class OwnerServiceTest {
|
|||
assertThat(ownerService.findAll()).doesNotContain(ownerDTO);
|
||||
|
||||
ownerService.save(ownerDTO);
|
||||
List<OwnerDTO> found = ownerService.findAll();
|
||||
|
||||
assertThat(ownerService.findAll()).contains(ownerDTO);
|
||||
assertThat(found).usingElementComparatorOnFields("lastName", "firstName", "address", "city", "telephone")
|
||||
.contains(ownerDTO);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ class PetServiceTest {
|
|||
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.getOwner().getId()).isEqualTo(pet.getOwner().getId());
|
||||
assertThat(found.getVisits()).isEqualTo(pet.getVisits());
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ class PetServiceTest {
|
|||
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.getOwner().getId()).isEqualTo(petDTO.getOwner().getId());
|
||||
assertThat(found.getVisits()).isEqualTo(petDTO.getVisits());
|
||||
}
|
||||
|
||||
|
@ -158,7 +158,7 @@ class PetServiceTest {
|
|||
|
||||
List<PetDTO> found = petService.findAll();
|
||||
|
||||
assertThat(found).contains(petDTO).containsAll(expected);
|
||||
assertThat(found).hasSize(expected.size() + 1).contains(petDTO).containsAll(expected);
|
||||
|
||||
}
|
||||
|
||||
|
@ -170,7 +170,7 @@ class PetServiceTest {
|
|||
|
||||
petService.save(petDTO);
|
||||
|
||||
assertThat(petService.findAll()).contains(petDTO);
|
||||
assertThat(petService.findAll()).containsAnyOf(petDTO);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ class VetServiceTest {
|
|||
|
||||
@Autowired
|
||||
private VetRepository vetRepository;
|
||||
|
||||
@Autowired
|
||||
private SpecialtyRepository specialtyRepository;
|
||||
|
||||
|
|
Loading…
Reference in a new issue