Remove PetRepository and use Owner as aggregate

Owner is really the aggregate root in DDD terms and there is no
need to directly access the Pet entity.
This commit is contained in:
Dave Syer 2022-01-05 08:12:09 +00:00 committed by Dave Syer
parent 778161f018
commit c9534421c8
21 changed files with 178 additions and 202 deletions

View file

@ -69,11 +69,8 @@ class OwnerControllerTests {
@MockBean
private VisitRepository visits;
private Owner george;
@BeforeEach
void setup() {
george = new Owner();
private Owner george() {
Owner george = new Owner();
george.setId(TEST_OWNER_ID);
george.setFirstName("George");
george.setLastName("Franklin");
@ -88,16 +85,21 @@ class OwnerControllerTests {
max.setName("Max");
max.setBirthDate(LocalDate.now());
george.setPetsInternal(Collections.singleton(max));
return george;
};
@BeforeEach
void setup() {
given(this.owners.findByLastName(eq("Franklin"), any(Pageable.class)))
.willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));
.willReturn(new PageImpl<Owner>(Lists.newArrayList(george())));
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<Owner>(Lists.newArrayList(george())));
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george());
Visit visit = new Visit();
visit.setDate(LocalDate.now());
given(this.visits.findByPetId(max.getId())).willReturn(Collections.singletonList(visit));
given(this.visits.findByPetId(george().getPet("Max").getId())).willReturn(Collections.singletonList(visit));
}
@ -132,14 +134,14 @@ class OwnerControllerTests {
@Test
void testProcessFindFormSuccess() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george, new Owner()));
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george(), new Owner()));
Mockito.when(this.owners.findByLastName(anyString(), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
}
@Test
void testProcessFindFormByLastName() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george));
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george()));
Mockito.when(this.owners.findByLastName(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin")).andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));

View file

@ -47,9 +47,6 @@ class PetControllerTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private PetRepository pets;
@MockBean
private OwnerRepository owners;
@ -58,10 +55,12 @@ class PetControllerTests {
PetType cat = new PetType();
cat.setId(3);
cat.setName("hamster");
given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat));
given(this.owners.findById(TEST_OWNER_ID)).willReturn(new Owner());
given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
given(this.owners.findPetTypes()).willReturn(Lists.newArrayList(cat));
Owner owner = new Owner();
Pet pet = new Pet();
owner.addPet(pet);
pet.setId(TEST_PET_ID);
given(this.owners.findById(TEST_OWNER_ID)).willReturn(owner);
}
@Test

View file

@ -16,12 +16,8 @@
package org.springframework.samples.petclinic.owner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import java.text.ParseException;
import java.util.ArrayList;
@ -29,8 +25,12 @@ import java.util.Collection;
import java.util.List;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
/**
* Test class for {@link PetTypeFormatter}
@ -41,7 +41,7 @@ import static org.mockito.BDDMockito.given;
class PetTypeFormatterTests {
@Mock
private PetRepository pets;
private OwnerRepository pets;
private PetTypeFormatter petTypeFormatter;

View file

@ -37,6 +37,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(VisitController.class)
class VisitControllerTests {
private static final int TEST_OWNER_ID = 1;
private static final int TEST_PET_ID = 1;
@Autowired
@ -46,29 +48,34 @@ class VisitControllerTests {
private VisitRepository visits;
@MockBean
private PetRepository pets;
private OwnerRepository owners;
@BeforeEach
void init() {
given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
Owner owner = new Owner();
Pet pet = new Pet();
owner.addPet(pet);
pet.setId(TEST_PET_ID);
given(this.owners.findById(TEST_OWNER_ID)).willReturn(owner);
}
@Test
void testInitNewVisitForm() throws Exception {
mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)).andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdateVisitForm"));
mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/visits/new", TEST_OWNER_ID, TEST_PET_ID))
.andExpect(status().isOk()).andExpect(view().name("pets/createOrUpdateVisitForm"));
}
@Test
void testProcessNewVisitFormSuccess() throws Exception {
mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George")
.param("description", "Visit Description")).andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}"));
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/visits/new", TEST_OWNER_ID, TEST_PET_ID)
.param("name", "George").param("description", "Visit Description"))
.andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/owners/{ownerId}"));
}
@Test
void testProcessNewVisitFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George"))
mockMvc.perform(
post("/owners/{ownerId}/pets/{petId}/visits/new", TEST_OWNER_ID, TEST_PET_ID).param("name", "George"))
.andExpect(model().attributeHasErrors("visit")).andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdateVisitForm"));
}

View file

@ -32,7 +32,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.vet.Vet;
import org.springframework.samples.petclinic.vet.VetRepository;
@ -77,9 +76,6 @@ class ClinicServiceTests {
@Autowired
protected OwnerRepository owners;
@Autowired
protected PetRepository pets;
@Autowired
protected VisitRepository visits;
@ -140,17 +136,9 @@ class ClinicServiceTests {
assertThat(owner.getLastName()).isEqualTo(newLastName);
}
@Test
void shouldFindPetWithCorrectId() {
Pet pet7 = this.pets.findById(7);
assertThat(pet7.getName()).startsWith("Samantha");
assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
}
@Test
void shouldFindAllPetTypes() {
Collection<PetType> petTypes = this.pets.findPetTypes();
Collection<PetType> petTypes = this.owners.findPetTypes();
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertThat(petType1.getName()).isEqualTo("cat");
@ -166,32 +154,34 @@ class ClinicServiceTests {
Pet pet = new Pet();
pet.setName("bowser");
Collection<PetType> types = this.pets.findPetTypes();
Collection<PetType> types = this.owners.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(LocalDate.now());
owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
this.pets.save(pet);
this.owners.save(owner6);
owner6 = this.owners.findById(6);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// checks that id has been generated
pet = owner6.getPet("bowser");
assertThat(pet.getId()).isNotNull();
}
@Test
@Transactional
void shouldUpdatePetName() throws Exception {
Pet pet7 = this.pets.findById(7);
Owner owner6 = this.owners.findById(6);
Pet pet7 = owner6.getPet(7);
String oldName = pet7.getName();
String newName = oldName + "X";
pet7.setName(newName);
this.pets.save(pet7);
this.owners.save(owner6);
pet7 = this.pets.findById(7);
owner6 = this.owners.findById(6);
pet7 = owner6.getPet(7);
assertThat(pet7.getName()).isEqualTo(newName);
}
@ -209,15 +199,16 @@ class ClinicServiceTests {
@Test
@Transactional
void shouldAddNewVisitForPet() {
Pet pet7 = this.pets.findById(7);
Owner owner6 = this.owners.findById(6);
Pet pet7 = owner6.getPet(7);
int found = pet7.getVisits().size();
Visit visit = new Visit();
pet7.addVisit(visit);
visit.setDescription("test");
this.visits.save(visit);
this.pets.save(pet7);
this.owners.save(owner6);
pet7 = this.pets.findById(7);
owner6 = this.owners.findById(6);
assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
assertThat(visit.getId()).isNotNull();
}

View file

@ -47,17 +47,16 @@ class VetControllerTests {
@MockBean
private VetRepository vets;
private Vet james;
private Vet helen;
@BeforeEach
void setup() {
james = new Vet();
private Vet james() {
Vet james = new Vet();
james.setFirstName("James");
james.setLastName("Carter");
james.setId(1);
helen = new Vet();
return james;
};
private Vet helen() {
Vet helen = new Vet();
helen.setFirstName("Helen");
helen.setLastName("Leary");
helen.setId(2);
@ -65,8 +64,14 @@ class VetControllerTests {
radiology.setId(1);
radiology.setName("radiology");
helen.addSpecialty(radiology);
given(this.vets.findAll()).willReturn(Lists.newArrayList(james, helen));
given(this.vets.findAll(any(Pageable.class))).willReturn(new PageImpl<Vet>(Lists.newArrayList(james, helen)));
return helen;
};
@BeforeEach
void setup() {
given(this.vets.findAll()).willReturn(Lists.newArrayList(james(), helen()));
given(this.vets.findAll(any(Pageable.class)))
.willReturn(new PageImpl<Vet>(Lists.newArrayList(james(), helen())));
}