diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index ae4659114..eac7a6ecf 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -25,7 +25,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; /** - * Repository class for Owner domain objects All method names are compliant + * Repository class for Owner 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 @@ -38,13 +38,6 @@ import org.springframework.data.jpa.repository.Query; */ public interface OwnerRepository extends JpaRepository { - /** - * 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") - List findPetTypes(); - /** * Retrieve {@link Owner}s from the data store by last name, returning all owners * whose last name starts with the given name. diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 3234e39d6..fde83fe81 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -48,13 +48,16 @@ class PetController { private final OwnerRepository owners; - public PetController(OwnerRepository owners) { + private final PetTypeRepository types; + + public PetController(OwnerRepository owners, PetTypeRepository types) { this.owners = owners; + this.types = types; } @ModelAttribute("types") public Collection populatePetTypes() { - return this.owners.findPetTypes(); + return this.types.findPetTypes(); } @ModelAttribute("owner") diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java index 73bfa1ae1..7ea68631f 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java @@ -35,10 +35,10 @@ import java.util.Locale; @Component public class PetTypeFormatter implements Formatter { - private final OwnerRepository owners; + private final PetTypeRepository types; - public PetTypeFormatter(OwnerRepository owners) { - this.owners = owners; + public PetTypeFormatter(PetTypeRepository types) { + this.types = types; } @Override @@ -48,7 +48,7 @@ public class PetTypeFormatter implements Formatter { @Override public PetType parse(String text, Locale locale) throws ParseException { - Collection findPetTypes = this.owners.findPetTypes(); + Collection findPetTypes = this.types.findPetTypes(); for (PetType type : findPetTypes) { if (type.getName().equals(text)) { return type; diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetTypeRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeRepository.java new file mode 100644 index 000000000..73a95cbe3 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeRepository.java @@ -0,0 +1,43 @@ +/* + * 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.owner; + +import java.util.List; +import java.util.Optional; + +import jakarta.annotation.Nonnull; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +/** + * Repository class for PetType domain objects. + * + * @author Patrick Baumgartner + */ + +public interface PetTypeRepository extends JpaRepository { + + /** + * 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") + List findPetTypes(); + +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java index 6fc9a849e..cb7c23faf 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java @@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; +import org.springframework.samples.petclinic.owner.PetTypeRepository; +import org.springframework.samples.petclinic.owner.OwnerRepository; import org.springframework.test.context.aot.DisabledInAotMode; import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; @@ -61,12 +63,15 @@ class PetControllerTests { @MockitoBean private OwnerRepository owners; + @MockitoBean + private PetTypeRepository types; + @BeforeEach void setup() { PetType cat = new PetType(); cat.setId(3); cat.setName("hamster"); - given(this.owners.findPetTypes()).willReturn(List.of(cat)); + given(this.types.findPetTypes()).willReturn(List.of(cat)); Owner owner = new Owner(); Pet pet = new Pet(); diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java index 0295b4788..33e4ea8df 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -43,13 +43,13 @@ import org.mockito.junit.jupiter.MockitoExtension; class PetTypeFormatterTests { @Mock - private OwnerRepository pets; + private PetTypeRepository types; private PetTypeFormatter petTypeFormatter; @BeforeEach void setup() { - this.petTypeFormatter = new PetTypeFormatter(pets); + this.petTypeFormatter = new PetTypeFormatter(types); } @Test @@ -62,14 +62,14 @@ class PetTypeFormatterTests { @Test void shouldParse() throws ParseException { - given(this.pets.findPetTypes()).willReturn(makePetTypes()); + given(types.findPetTypes()).willReturn(makePetTypes()); PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); assertThat(petType.getName()).isEqualTo("Bird"); } @Test void shouldThrowParseException() { - given(this.pets.findPetTypes()).willReturn(makePetTypes()); + given(types.findPetTypes()).willReturn(makePetTypes()); Assertions.assertThrows(ParseException.class, () -> { petTypeFormatter.parse("Fish", Locale.ENGLISH); }); diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index 17360278f..978c95396 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -31,6 +31,7 @@ import org.springframework.data.domain.Page; 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.PetTypeRepository; import org.springframework.samples.petclinic.owner.Pet; import org.springframework.samples.petclinic.owner.PetType; import org.springframework.samples.petclinic.owner.Visit; @@ -75,6 +76,9 @@ class ClinicServiceTests { @Autowired protected OwnerRepository owners; + @Autowired + protected PetTypeRepository types; + @Autowired protected VetRepository vets; @@ -140,7 +144,7 @@ class ClinicServiceTests { @Test void shouldFindAllPetTypes() { - Collection petTypes = this.owners.findPetTypes(); + Collection petTypes = this.types.findPetTypes(); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); assertThat(petType1.getName()).isEqualTo("cat"); @@ -159,7 +163,7 @@ class ClinicServiceTests { Pet pet = new Pet(); pet.setName("bowser"); - Collection types = this.owners.findPetTypes(); + Collection types = this.types.findPetTypes(); pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setBirthDate(LocalDate.now()); owner6.addPet(pet);