From 7c8e2875bcade491339483ac13132e78a2061edb Mon Sep 17 00:00:00 2001 From: "kaihang.xkh" Date: Fri, 28 Mar 2025 15:10:04 +0800 Subject: [PATCH] Add tests for Pet name search functionality --- .../petclinic/owner/OwnerControllerTests.java | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) 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 426ca5c24..28311d495 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -100,7 +100,17 @@ class OwnerControllerTests { Visit visit = new Visit(); visit.setDate(LocalDate.now()); george.getPet("Max").getVisits().add(visit); - + + // Set up for Pet name search tests + given(this.owners.findByPetNameContaining(eq("Max"), any(Pageable.class))) + .willReturn(new PageImpl<>(Lists.newArrayList(george))); + + given(this.owners.findByLastNameAndPetName(eq("Franklin"), eq("Max"), any(Pageable.class))) + .willReturn(new PageImpl<>(Lists.newArrayList(george))); + + // No results for unknown pet name + given(this.owners.findByPetNameContaining(eq("Unknown Pet"), any(Pageable.class))) + .willReturn(new PageImpl<>(Lists.newArrayList())); } @Test @@ -157,6 +167,26 @@ class OwnerControllerTests { .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); } + @Test + void testProcessFindFormByPetName() throws Exception { + Page tasks = new PageImpl<>(Lists.newArrayList(george())); + when(this.owners.findByPetNameContaining(eq("Max"), any(Pageable.class))).thenReturn(tasks); + mockMvc.perform(get("/owners?page=1").param("petName", "Max")) + .andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); + } + + @Test + void testProcessFindFormByLastNameAndPetName() throws Exception { + Page tasks = new PageImpl<>(Lists.newArrayList(george())); + when(this.owners.findByLastNameAndPetName(eq("Franklin"), eq("Max"), any(Pageable.class))).thenReturn(tasks); + mockMvc.perform(get("/owners?page=1") + .param("lastName", "Franklin") + .param("petName", "Max")) + .andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); + } + @Test void testProcessFindFormNoOwnersFound() throws Exception { Page tasks = new PageImpl<>(Lists.newArrayList()); @@ -166,7 +196,41 @@ class OwnerControllerTests { .andExpect(model().attributeHasFieldErrors("owner", "lastName")) .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound")) .andExpect(view().name("owners/findOwners")); + } + @Test + void testProcessFindFormNoPetsFound() throws Exception { + Page tasks = new PageImpl<>(Lists.newArrayList()); + when(this.owners.findByPetNameContaining(eq("Unknown Pet"), any(Pageable.class))).thenReturn(tasks); + mockMvc.perform(get("/owners?page=1").param("petName", "Unknown Pet")) + .andExpect(status().isOk()) + .andExpect(model().attributeHasFieldErrors("owner", "petName")) + .andExpect(model().attributeHasFieldErrorCode("owner", "petName", "notFound")) + .andExpect(view().name("owners/findOwners")); + } + + @Test + void testProcessFindFormMultipleOwnersByPetName() throws Exception { + Owner george = george(); + Owner anotherOwner = new Owner(); + anotherOwner.setId(2); + anotherOwner.setFirstName("Another"); + anotherOwner.setLastName("Owner"); + + Pet max = new Pet(); + PetType dog = new PetType(); + dog.setName("dog"); + max.setType(dog); + max.setName("Max"); + max.setBirthDate(LocalDate.now()); + anotherOwner.addPet(max); + + Page tasks = new PageImpl<>(Lists.newArrayList(george, anotherOwner)); + when(this.owners.findByPetNameContaining(eq("Max"), any(Pageable.class))).thenReturn(tasks); + + mockMvc.perform(get("/owners?page=1").param("petName", "Max")) + .andExpect(status().isOk()) + .andExpect(view().name("owners/ownersList")); } @Test @@ -250,4 +314,4 @@ class OwnerControllerTests { .andExpect(flash().attributeExists("error")); } -} +} \ No newline at end of file