diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetController.java b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java index 867d0b5f40f..304b3cf124e 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java @@ -18,8 +18,6 @@ import java.util.List; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -35,15 +33,15 @@ @Controller class VetController { - private final VetRepository vetRepository; + private final VetDirectory vetDirectory; - public VetController(VetRepository vetRepository) { - this.vetRepository = vetRepository; + public VetController(VetDirectory vetDirectory) { + this.vetDirectory = vetDirectory; } @GetMapping("/vets.html") public String showVetList(@RequestParam(defaultValue = "1") int page, Model model) { - Page paginated = findPaginated(page); + Page paginated = this.vetDirectory.findPage(page); return addPaginationModel(page, paginated, model); } @@ -56,18 +54,12 @@ private String addPaginationModel(int page, Page paginated, Model model) { return "vets/vetList"; } - private Page findPaginated(int page) { - int pageSize = 5; - Pageable pageable = PageRequest.of(page - 1, pageSize); - return vetRepository.findAll(pageable); - } - @GetMapping({ "/vets" }) public @ResponseBody Vets showResourcesVetList() { // Here we are returning an object of type 'Vets' rather than a collection of Vet // objects so it is simpler for JSon/Object mapping Vets vets = new Vets(); - vets.getVetList().addAll(this.vetRepository.findAll()); + vets.getVetList().addAll(this.vetDirectory.findAll()); return vets; } diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetDirectory.java b/src/main/java/org/springframework/samples/petclinic/vet/VetDirectory.java new file mode 100644 index 00000000000..5c8b89a8123 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetDirectory.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2025 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.vet; + +import java.util.Collection; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +/** + * Provides veterinarian directory queries for the HTML and JSON endpoints. + */ +@Service +class VetDirectory { + + private final VetRepository vetRepository; + + VetDirectory(VetRepository vetRepository) { + this.vetRepository = vetRepository; + } + + Page findPage(int page) { + return this.vetRepository.findAll(PageRequest.of(page - 1, 5)); + } + + Collection findAll() { + return this.vetRepository.findAll(); + } + +} 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 208758cecf9..0099d62d8ac 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -22,7 +22,9 @@ import org.junit.jupiter.api.condition.DisabledInNativeImage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; +import org.springframework.context.annotation.Import; import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.http.MediaType; import org.springframework.test.context.aot.DisabledInAotMode; @@ -33,6 +35,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -41,6 +44,7 @@ */ @WebMvcTest(VetController.class) +@Import(VetDirectory.class) @DisabledInNativeImage @DisabledInAotMode class VetControllerTests { @@ -87,6 +91,16 @@ void showVetListHtml() throws Exception { .andExpect(model().attributeExists("listVets")) .andExpect(view().name("vets/vetList")); + verify(this.vets).findAll(PageRequest.of(0, 5)); + } + + @Test + void showSecondVetPage() throws Exception { + mockMvc.perform(get("/vets.html?page=2")) + .andExpect(status().isOk()) + .andExpect(model().attribute("currentPage", 2)) + .andExpect(view().name("vets/vetList")); + verify(this.vets).findAll(PageRequest.of(1, 5)); } @Test