mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:25:50 +00:00
Additional tweaks in aggregate model.
Introduced Owner.addVisit(…) to avoid that state transition to live in controller code. Slightly polished some assertions in ClinicServiceTests.
This commit is contained in:
parent
b559077f14
commit
472575378c
3 changed files with 33 additions and 8 deletions
|
@ -31,6 +31,7 @@ import javax.validation.constraints.NotEmpty;
|
|||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.samples.petclinic.model.Person;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object representing an owner.
|
||||
|
@ -39,6 +40,7 @@ import org.springframework.samples.petclinic.model.Person;
|
|||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Michael Isvy
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "owners")
|
||||
|
@ -149,4 +151,23 @@ public class Owner extends Person {
|
|||
.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given {@link Visit} to the {@link Pet} with the given identifier.
|
||||
* @param petId the identifier of the {@link Pet}, must not be {@literal null}.
|
||||
* @param visit the visit to add, must not be {@literal null}.
|
||||
*/
|
||||
public Owner addVisit(Integer petId, Visit visit) {
|
||||
|
||||
Assert.notNull(petId, "Pet identifier must not be null!");
|
||||
Assert.notNull(visit, "Visit must not be null!");
|
||||
|
||||
Pet pet = getPet(petId);
|
||||
|
||||
Assert.notNull(pet, "Invalid Pet identifier!");
|
||||
|
||||
pet.addVisit(visit);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -78,13 +78,13 @@ class VisitController {
|
|||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is
|
||||
// called
|
||||
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
|
||||
public String processNewVisitForm(@ModelAttribute Owner owner, @PathVariable("petId") int petId, @Valid Visit visit,
|
||||
public String processNewVisitForm(@ModelAttribute Owner owner, @PathVariable int petId, @Valid Visit visit,
|
||||
BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return "pets/createOrUpdateVisitForm";
|
||||
}
|
||||
else {
|
||||
owner.getPet(petId).addVisit(visit);
|
||||
owner.addVisit(petId, visit);
|
||||
this.owners.save(owner);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
|
|
|
@ -199,13 +199,16 @@ class ClinicServiceTests {
|
|||
Pet pet7 = owner6.getPet(7);
|
||||
int found = pet7.getVisits().size();
|
||||
Visit visit = new Visit();
|
||||
pet7.addVisit(visit);
|
||||
visit.setDescription("test");
|
||||
|
||||
owner6.addVisit(pet7.getId(), visit);
|
||||
this.owners.save(owner6);
|
||||
|
||||
owner6 = this.owners.findById(6);
|
||||
assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
|
||||
assertThat(pet7.getVisits()).allMatch(value -> value.getId() != null);
|
||||
|
||||
assertThat(pet7.getVisits()) //
|
||||
.hasSize(found + 1) //
|
||||
.allMatch(value -> value.getId() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -213,9 +216,10 @@ class ClinicServiceTests {
|
|||
Owner owner6 = this.owners.findById(6);
|
||||
Pet pet7 = owner6.getPet(7);
|
||||
Collection<Visit> visits = pet7.getVisits();
|
||||
assertThat(visits).hasSize(2);
|
||||
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
|
||||
assertThat(visitArr[0].getDate()).isNotNull();
|
||||
|
||||
assertThat(visits) //
|
||||
.hasSize(2) //
|
||||
.element(0).extracting(Visit::getDate).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue