mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 11:22:48 +00:00
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:
parent
778161f018
commit
c9534421c8
21 changed files with 178 additions and 202 deletions
44
pom.xml
44
pom.xml
|
@ -1,7 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.samples</groupId>
|
||||
<artifactId>spring-petclinic</artifactId>
|
||||
|
@ -62,12 +60,12 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Databases - Uses H2 by default -->
|
||||
|
@ -317,20 +315,20 @@
|
|||
<artifactId>libsass-maven-plugin</artifactId>
|
||||
<version>0.2.26</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-resources</phase>
|
||||
<?m2e execute onConfiguration,onIncremental?>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<phase>generate-resources</phase>
|
||||
<?m2e execute onConfiguration,onIncremental?>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<inputPath>${basedir}/src/main/scss/</inputPath>
|
||||
<outputPath>${basedir}/src/main/resources/static/resources/css/</outputPath>
|
||||
<includePath>${project.build.directory}/webjars/META-INF/resources/webjars/bootstrap/${webjars-bootstrap.version}/scss/</includePath>
|
||||
<inputPath>${basedir}/src/main/scss/</inputPath>
|
||||
<outputPath>${basedir}/src/main/resources/static/resources/css/</outputPath>
|
||||
<includePath>${project.build.directory}/webjars/META-INF/resources/webjars/bootstrap/${webjars-bootstrap.version}/scss/</includePath>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
@ -363,7 +361,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
<pluginExecution>
|
||||
|
@ -376,7 +374,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
<pluginExecution>
|
||||
|
@ -389,7 +387,7 @@
|
|||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore/>
|
||||
<ignore />
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<Pet> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Owner, Integer> {
|
||||
|
||||
/**
|
||||
* 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<PetType> findPetTypes();
|
||||
|
||||
/**
|
||||
* Retrieve {@link Owner}s from the data store by last name, returning all owners
|
||||
* whose last name <i>starts</i> with the given name.
|
||||
|
|
|
@ -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<Visit> 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<Visit> getVisitsInternal() {
|
||||
if (this.visits == null) {
|
||||
this.visits = new HashSet<>();
|
||||
|
|
|
@ -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<PetType> 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}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <code>Pet</code> 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<Pet, Integer> {
|
||||
|
||||
/**
|
||||
* 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<PetType> 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);
|
||||
|
||||
}
|
|
@ -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...
|
||||
*/
|
||||
|
|
|
@ -36,11 +36,11 @@ import java.util.Locale;
|
|||
@Component
|
||||
public class PetTypeFormatter implements Formatter<PetType> {
|
||||
|
||||
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<PetType> {
|
|||
|
||||
@Override
|
||||
public PetType parse(String text, Locale locale) throws ParseException {
|
||||
Collection<PetType> findPetTypes = this.pets.findPetTypes();
|
||||
Collection<PetType> findPetTypes = this.owners.findPetTypes();
|
||||
for (PetType type : findPetTypes) {
|
||||
if (type.getName().equals(text)) {
|
||||
return type;
|
||||
|
|
|
@ -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<String, Object> model) {
|
||||
Pet pet = this.pets.findById(petId);
|
||||
public Visit loadPetWithVisit(@PathVariable("ownerId") int ownerId, @PathVariable("petId") int petId,
|
||||
Map<String, Object> 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<String, Object> 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()) {
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">Owner</label>
|
||||
<div class="col-sm-10">
|
||||
<span th:text="${pet.owner?.firstName + ' ' + pet.owner?.lastName}" />
|
||||
<span th:text="${owner?.firstName + ' ' + owner?.lastName}" />
|
||||
</div>
|
||||
</div>
|
||||
<input
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
th:text="${#temporals.format(pet.birthDate, 'yyyy-MM-dd')}"></td>
|
||||
<td th:text="${pet.type}"></td>
|
||||
<td
|
||||
th:text="${pet.owner?.firstName + ' ' + pet.owner?.lastName}"></td>
|
||||
th:text="${owner?.firstName + ' ' + owner?.lastName}"></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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())));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue