mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:25:50 +00:00
Extracted findPetTypes
- Extracted find by findPetTypes because of failing tests - Extracting helped to keep one repository responsible for an entity type - Seems like Spring Data is more strict and treats PetType as DTO and generated a Costructor NEW query - Current approach treats it still like an entity Signed-off-by: Patrick Baumgartner <contact@patbaumgartner.com>
This commit is contained in:
parent
b21890c126
commit
142321aa3e
7 changed files with 69 additions and 21 deletions
|
@ -25,7 +25,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
/**
|
||||
* Repository class for <code>Owner</code> domain objects All method names are compliant
|
||||
* Repository class for <code>Owner</code> 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<Owner, Integer> {
|
||||
|
||||
/**
|
||||
* 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<PetType> findPetTypes();
|
||||
|
||||
/**
|
||||
* Retrieve {@link Owner}s from the data store by last name, returning all owners
|
||||
* whose last name <i>starts</i> with the given name.
|
||||
|
|
|
@ -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<PetType> populatePetTypes() {
|
||||
return this.owners.findPetTypes();
|
||||
return this.types.findPetTypes();
|
||||
}
|
||||
|
||||
@ModelAttribute("owner")
|
||||
|
|
|
@ -35,10 +35,10 @@ import java.util.Locale;
|
|||
@Component
|
||||
public class PetTypeFormatter implements Formatter<PetType> {
|
||||
|
||||
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<PetType> {
|
|||
|
||||
@Override
|
||||
public PetType parse(String text, Locale locale) throws ParseException {
|
||||
Collection<PetType> findPetTypes = this.owners.findPetTypes();
|
||||
Collection<PetType> findPetTypes = this.types.findPetTypes();
|
||||
for (PetType type : findPetTypes) {
|
||||
if (type.getName().equals(text)) {
|
||||
return type;
|
||||
|
|
|
@ -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 <code>PetType</code> domain objects.
|
||||
*
|
||||
* @author Patrick Baumgartner
|
||||
*/
|
||||
|
||||
public interface PetTypeRepository extends JpaRepository<PetType, Integer> {
|
||||
|
||||
/**
|
||||
* 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<PetType> findPetTypes();
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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<PetType> petTypes = this.owners.findPetTypes();
|
||||
Collection<PetType> 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<PetType> types = this.owners.findPetTypes();
|
||||
Collection<PetType> types = this.types.findPetTypes();
|
||||
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
||||
pet.setBirthDate(LocalDate.now());
|
||||
owner6.addPet(pet);
|
||||
|
|
Loading…
Reference in a new issue