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:
Oliver Drotbohm 2022-01-06 11:00:14 +01:00 committed by Dave Syer
parent b559077f14
commit 472575378c
3 changed files with 33 additions and 8 deletions

View file

@ -31,6 +31,7 @@ import javax.validation.constraints.NotEmpty;
import org.springframework.core.style.ToStringCreator; import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person; import org.springframework.samples.petclinic.model.Person;
import org.springframework.util.Assert;
/** /**
* Simple JavaBean domain object representing an owner. * Simple JavaBean domain object representing an owner.
@ -39,6 +40,7 @@ import org.springframework.samples.petclinic.model.Person;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
* @author Michael Isvy * @author Michael Isvy
* @author Oliver Drotbohm
*/ */
@Entity @Entity
@Table(name = "owners") @Table(name = "owners")
@ -149,4 +151,23 @@ public class Owner extends Person {
.toString(); .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;
}
} }

View file

@ -78,13 +78,13 @@ class VisitController {
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is
// called // called
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new") @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) { BindingResult result) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm"; return "pets/createOrUpdateVisitForm";
} }
else { else {
owner.getPet(petId).addVisit(visit); owner.addVisit(petId, visit);
this.owners.save(owner); this.owners.save(owner);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }

View file

@ -199,13 +199,16 @@ class ClinicServiceTests {
Pet pet7 = owner6.getPet(7); Pet pet7 = owner6.getPet(7);
int found = pet7.getVisits().size(); int found = pet7.getVisits().size();
Visit visit = new Visit(); Visit visit = new Visit();
pet7.addVisit(visit);
visit.setDescription("test"); visit.setDescription("test");
owner6.addVisit(pet7.getId(), visit);
this.owners.save(owner6); this.owners.save(owner6);
owner6 = this.owners.findById(6); 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 @Test
@ -213,9 +216,10 @@ class ClinicServiceTests {
Owner owner6 = this.owners.findById(6); Owner owner6 = this.owners.findById(6);
Pet pet7 = owner6.getPet(7); Pet pet7 = owner6.getPet(7);
Collection<Visit> visits = pet7.getVisits(); Collection<Visit> visits = pet7.getVisits();
assertThat(visits).hasSize(2);
Visit[] visitArr = visits.toArray(new Visit[visits.size()]); assertThat(visits) //
assertThat(visitArr[0].getDate()).isNotNull(); .hasSize(2) //
.element(0).extracting(Visit::getDate).isNotNull();
} }
} }