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