diff --git a/pom.xml b/pom.xml index d683118ad..4c77af264 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 org.springframework.samples spring-petclinic @@ -62,12 +60,12 @@ org.springframework.boot spring-boot-starter-test test - - - org.junit.vintage - junit-vintage-engine - - + + + org.junit.vintage + junit-vintage-engine + + @@ -317,20 +315,20 @@ libsass-maven-plugin 0.2.26 - - generate-resources - - - compile - - + + generate-resources + + + compile + + - ${basedir}/src/main/scss/ - ${basedir}/src/main/resources/static/resources/css/ - ${project.build.directory}/webjars/META-INF/resources/webjars/bootstrap/${webjars-bootstrap.version}/scss/ + ${basedir}/src/main/scss/ + ${basedir}/src/main/resources/static/resources/css/ + ${project.build.directory}/webjars/META-INF/resources/webjars/bootstrap/${webjars-bootstrap.version}/scss/ - + @@ -363,7 +361,7 @@ - + @@ -376,7 +374,7 @@ - + @@ -389,7 +387,7 @@ - + diff --git a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java index 191253587..0b2ffc129 100644 --- a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java +++ b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java @@ -25,7 +25,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @author Dave Syer * */ -@SpringBootApplication(proxyBeanMethods = false) +@SpringBootApplication public class PetClinicApplication { public static void main(String[] args) { diff --git a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java index 3b47a954c..4cb9ffc0c 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java @@ -15,11 +15,12 @@ */ package org.springframework.samples.petclinic.model; +import java.io.Serializable; + import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; -import java.io.Serializable; /** * Simple JavaBean domain object with an id property. Used as a base class for objects diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index 7f2ef905e..4b2104690 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -60,7 +60,7 @@ public class Owner extends Person { @Digits(fraction = 0, integer = 10) private String telephone; - @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER) + @OneToMany(cascade = CascadeType.ALL, mappedBy = "ownerId", fetch = FetchType.EAGER) private Set pets; public String getAddress() { @@ -108,7 +108,7 @@ public class Owner extends Person { if (pet.isNew()) { getPetsInternal().add(pet); } - pet.setOwner(this); + pet.setOwnerId(getId()); } /** @@ -120,6 +120,23 @@ public class Owner extends Person { return getPet(name, false); } + /** + * Return the Pet with the given id, or null if none found for this Owner. + * @param name to test + * @return a pet if pet id is already in use + */ + public Pet getPet(Integer id) { + for (Pet pet : getPetsInternal()) { + if (!pet.isNew()) { + Integer compId = pet.getId(); + if (compId.equals(id)) { + return pet; + } + } + } + return null; + } + /** * Return the Pet with the given name, or null if none found for this Owner. * @param name to test @@ -130,7 +147,7 @@ public class Owner extends Person { for (Pet pet : getPetsInternal()) { if (!ignoreNew || !pet.isNew()) { String compName = pet.getName(); - compName = compName.toLowerCase(); + compName = compName == null ? "" : compName.toLowerCase(); if (compName.equals(name)) { return pet; } @@ -141,11 +158,10 @@ public class Owner extends Person { @Override public String toString() { - return new ToStringCreator(this) - - .append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName()) - .append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city) - .append("telephone", this.telephone).toString(); + return new ToStringCreator(this).append("id", this.getId()).append("new", this.isNew()) + .append("lastName", this.getLastName()).append("firstName", this.getFirstName()) + .append("address", this.address).append("city", this.city).append("telephone", this.telephone) + .toString(); } } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index 091510122..062df4576 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -15,6 +15,8 @@ */ package org.springframework.samples.petclinic.owner; +import java.util.List; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; @@ -35,6 +37,14 @@ import org.springframework.transaction.annotation.Transactional; */ public interface OwnerRepository extends Repository { + /** + * Retrieve all {@link PetType}s from the data store. + * @return a Collection of {@link PetType}s. + */ + @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") + @Transactional(readOnly = true) + List findPetTypes(); + /** * Retrieve {@link Owner}s from the data store by last name, returning all owners * whose last name starts with the given name. diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java index 2b68005fd..8b162531c 100755 --- a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java @@ -56,13 +56,16 @@ public class Pet extends NamedEntity { @JoinColumn(name = "type_id") private PetType type; - @ManyToOne - @JoinColumn(name = "owner_id") - private Owner owner; + @Column + private Integer ownerId; @Transient private Set visits = new LinkedHashSet<>(); + public void setOwnerId(Integer ownerId) { + this.ownerId = ownerId; + } + public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; } @@ -79,14 +82,6 @@ public class Pet extends NamedEntity { this.type = type; } - public Owner getOwner() { - return this.owner; - } - - protected void setOwner(Owner owner) { - this.owner = owner; - } - protected Set getVisitsInternal() { if (this.visits == null) { this.visits = new HashSet<>(); diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index a55e599af..4ebc117b0 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -36,18 +36,15 @@ class PetController { private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; - private final PetRepository pets; - private final OwnerRepository owners; - public PetController(PetRepository pets, OwnerRepository owners) { - this.pets = pets; + public PetController(OwnerRepository owners) { this.owners = owners; } @ModelAttribute("types") public Collection populatePetTypes() { - return this.pets.findPetTypes(); + return this.owners.findPetTypes(); } @ModelAttribute("owner") @@ -84,14 +81,14 @@ class PetController { return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } else { - this.pets.save(pet); + this.owners.save(owner); return "redirect:/owners/{ownerId}"; } } @GetMapping("/pets/{petId}/edit") - public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { - Pet pet = this.pets.findById(petId); + public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model) { + Pet pet = owner.getPet(petId); model.put("pet", pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } @@ -99,13 +96,12 @@ class PetController { @PostMapping("/pets/{petId}/edit") public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) { if (result.hasErrors()) { - pet.setOwner(owner); model.put("pet", pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } else { owner.addPet(pet); - this.pets.save(pet); + this.owners.save(owner); return "redirect:/owners/{ownerId}"; } } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java deleted file mode 100644 index 069f9f37c..000000000 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.owner; - -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - -/** - * Repository class for Pet domain objects All method names are compliant - * with Spring Data naming conventions so this interface can easily be extended for Spring - * Data. See: - * https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation - * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Sam Brannen - * @author Michael Isvy - */ -public interface PetRepository extends Repository { - - /** - * Retrieve all {@link PetType}s from the data store. - * @return a Collection of {@link PetType}s. - */ - @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") - @Transactional(readOnly = true) - List findPetTypes(); - - /** - * Retrieve a {@link Pet} from the data store by id. - * @param id the id to search for - * @return the {@link Pet} if found - */ - @Transactional(readOnly = true) - Pet findById(Integer id); - - /** - * Save a {@link Pet} to the data store, either inserting or updating it. - * @param pet the {@link Pet} to save - */ - void save(Pet pet); - -} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetType.java b/src/main/java/org/springframework/samples/petclinic/owner/PetType.java index 3cb9fc1d0..6f0aa58d3 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetType.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetType.java @@ -15,11 +15,11 @@ */ package org.springframework.samples.petclinic.owner; -import org.springframework.samples.petclinic.model.NamedEntity; - import javax.persistence.Entity; import javax.persistence.Table; +import org.springframework.samples.petclinic.model.NamedEntity; + /** * @author Juergen Hoeller Can be Cat, Dog, Hamster... */ diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java index c97107f2e..4fa18da91 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java @@ -36,11 +36,11 @@ import java.util.Locale; @Component public class PetTypeFormatter implements Formatter { - private final PetRepository pets; + private final OwnerRepository owners; @Autowired - public PetTypeFormatter(PetRepository pets) { - this.pets = pets; + public PetTypeFormatter(OwnerRepository owners) { + this.owners = owners; } @Override @@ -50,7 +50,7 @@ public class PetTypeFormatter implements Formatter { @Override public PetType parse(String text, Locale locale) throws ParseException { - Collection findPetTypes = this.pets.findPetTypes(); + Collection findPetTypes = this.owners.findPetTypes(); for (PetType type : findPetTypes) { if (type.getName().equals(text)) { return type; diff --git a/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java b/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java index 135497f47..d441ffab4 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java @@ -37,11 +37,11 @@ class VisitController { private final VisitRepository visits; - private final PetRepository pets; + private final OwnerRepository owners; - public VisitController(VisitRepository visits, PetRepository pets) { + public VisitController(VisitRepository visits, OwnerRepository owners) { this.visits = visits; - this.pets = pets; + this.owners = owners; } @InitBinder @@ -57,8 +57,10 @@ class VisitController { * @return Pet */ @ModelAttribute("visit") - public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map model) { - Pet pet = this.pets.findById(petId); + public Visit loadPetWithVisit(@PathVariable("ownerId") int ownerId, @PathVariable("petId") int petId, + Map model) { + Owner owner = this.owners.findById(ownerId); + Pet pet = owner.getPet(petId); pet.setVisitsInternal(this.visits.findByPetId(petId)); model.put("pet", pet); Visit visit = new Visit(); @@ -66,13 +68,15 @@ class VisitController { return visit; } - // Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called - @GetMapping("/owners/*/pets/{petId}/visits/new") + // Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is + // called + @GetMapping("/owners/{ownerId}/pets/{petId}/visits/new") public String initNewVisitForm(@PathVariable("petId") int petId, Map model) { return "pets/createOrUpdateVisitForm"; } - // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called + // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is + // called @PostMapping("/owners/{ownerId}/pets/{petId}/visits/new") public String processNewVisitForm(@Valid Visit visit, BindingResult result) { if (result.hasErrors()) { diff --git a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java index 7f6193102..014becfce 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java @@ -15,14 +15,24 @@ */ package org.springframework.samples.petclinic.vet; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; +import javax.xml.bind.annotation.XmlElement; + import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.samples.petclinic.model.Person; -import javax.persistence.*; -import javax.xml.bind.annotation.XmlElement; -import java.util.*; - /** * Simple JavaBean domain object representing a veterinarian. * diff --git a/src/main/java/org/springframework/samples/petclinic/visit/Visit.java b/src/main/java/org/springframework/samples/petclinic/visit/Visit.java index 239d60597..df9f25fe0 100755 --- a/src/main/java/org/springframework/samples/petclinic/visit/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/visit/Visit.java @@ -15,14 +15,15 @@ */ package org.springframework.samples.petclinic.visit; -import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.samples.petclinic.model.BaseEntity; +import java.time.LocalDate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotEmpty; -import java.time.LocalDate; + +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.model.BaseEntity; /** * Simple JavaBean domain object representing a visit. diff --git a/src/main/resources/templates/pets/createOrUpdatePetForm.html b/src/main/resources/templates/pets/createOrUpdatePetForm.html index 5ab1f6c86..dd8a4dd1c 100644 --- a/src/main/resources/templates/pets/createOrUpdatePetForm.html +++ b/src/main/resources/templates/pets/createOrUpdatePetForm.html @@ -13,7 +13,7 @@
- +
+ th:text="${owner?.firstName + ' ' + owner?.lastName}"> diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index 28120afa1..adeaeacbe 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -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(Lists.newArrayList(george))); + .willReturn(new PageImpl(Lists.newArrayList(george()))); - given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl(Lists.newArrayList(george))); + given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl(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 tasks = new PageImpl(Lists.newArrayList(george, new Owner())); + Page tasks = new PageImpl(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 tasks = new PageImpl(Lists.newArrayList(george)); + Page tasks = new PageImpl(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)); diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java index 5efd55fad..cefdc283e 100755 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java @@ -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 diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java index 1c0c01b7e..0d50b44e7 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -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; diff --git a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java index a22317bfe..d331f3bc3 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java @@ -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")); } diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index cab001f11..83b353bb9 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -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 petTypes = this.pets.findPetTypes(); + Collection 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 types = this.pets.findPetTypes(); + Collection 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(); } diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java index e0b4964be..590c461b2 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -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(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(Lists.newArrayList(james(), helen()))); }