mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
Change PetTypeFormatter and Validator to use DTO
This commit is contained in:
parent
6d5b55fc10
commit
4dd08b9c5b
8 changed files with 62 additions and 45 deletions
1
pom.xml
1
pom.xml
|
@ -15,7 +15,6 @@
|
|||
<name>petclinic</name>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- Generic properties -->
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
|
|
@ -15,13 +15,9 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||
import org.springframework.samples.petclinic.owner.PetValidator;
|
||||
import org.springframework.samples.petclinic.service.OwnerService;
|
||||
import org.springframework.samples.petclinic.service.PetService;
|
||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||
import org.springframework.samples.petclinic.dto.*;
|
||||
import org.springframework.samples.petclinic.validator.PetValidator;
|
||||
import org.springframework.samples.petclinic.service.*;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -93,8 +89,7 @@ class PetController {
|
|||
if (result.hasErrors()) {
|
||||
model.put("pet", pet);
|
||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
this.petService.save(pet);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
|
|
|
@ -21,7 +21,8 @@ import java.util.Locale;
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.Formatter;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
|
@ -35,24 +36,24 @@ import org.springframework.stereotype.Component;
|
|||
* @author Michael Isvy
|
||||
*/
|
||||
@Component
|
||||
public class PetTypeFormatter implements Formatter<PetType> {
|
||||
public class PetTypeFormatter implements Formatter<PetTypeDTO> {
|
||||
|
||||
private final PetRepository pets;
|
||||
// private final PetRepository pets;
|
||||
private final PetTypeService petTypeService;
|
||||
|
||||
@Autowired
|
||||
public PetTypeFormatter(PetRepository pets) {
|
||||
this.pets = pets;
|
||||
public PetTypeFormatter(PetTypeService petTypeService) {
|
||||
this.petTypeService = petTypeService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String print(PetType petType, Locale locale) {
|
||||
public String print(PetTypeDTO petType, Locale locale) {
|
||||
return petType.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PetType parse(String text, Locale locale) throws ParseException {
|
||||
Collection<PetType> findPetTypes = this.pets.findPetTypes();
|
||||
for (PetType type : findPetTypes) {
|
||||
public PetTypeDTO parse(String text, Locale locale) throws ParseException {
|
||||
Collection<PetTypeDTO> findPetTypes = this.petTypeService.findPetTypes();
|
||||
for (PetTypeDTO type : findPetTypes) {
|
||||
if (type.getName().equals(text)) {
|
||||
return type;
|
||||
}
|
||||
|
|
|
@ -4,12 +4,32 @@ import java.util.Collection;
|
|||
|
||||
public interface BaseService<E, D> {
|
||||
|
||||
/**
|
||||
* Convert Data Transfert Object to Entity Model
|
||||
* @param dto DTO
|
||||
* @return Entity Model
|
||||
*/
|
||||
public E dtoToEntity(D dto);
|
||||
|
||||
/**
|
||||
* Convert Entity Model to Data Transfert Object
|
||||
* @param entity Entity Model
|
||||
* @return DTO
|
||||
*/
|
||||
public D entityToDTO(E entity);
|
||||
|
||||
/**
|
||||
* Convert Entities Models Collection to Data Transfert Object Collection
|
||||
* @param entities Collection of Entity Model
|
||||
* @return Collection of DTO
|
||||
*/
|
||||
public Collection<D> entitiesToDTOS(Collection<E> entities);
|
||||
|
||||
/**
|
||||
* Convert Entities Models Collection to Data Transfert Object Collection
|
||||
* @param dtos Collection of DTO
|
||||
* @return Collection of Entity Model
|
||||
*/
|
||||
public Collection<E> dtosToEntities(Collection<D> dtos);
|
||||
|
||||
}
|
||||
|
|
|
@ -13,8 +13,10 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
package org.springframework.samples.petclinic.validator;
|
||||
|
||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||
import org.springframework.samples.petclinic.owner.Pet;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
|
@ -35,7 +37,7 @@ public class PetValidator implements Validator {
|
|||
|
||||
@Override
|
||||
public void validate(Object obj, Errors errors) {
|
||||
Pet pet = (Pet) obj;
|
||||
PetDTO pet = (PetDTO) obj;
|
||||
String name = pet.getName();
|
||||
// name validation
|
||||
if (!StringUtils.hasLength(name)) {
|
|
@ -1,4 +1,5 @@
|
|||
<groups xmlns="http://www.isdc.ro/wro">
|
||||
<groups xmlns="http://www.isdc.ro/wro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.isdc.ro/wro ">
|
||||
<group name="petclinic">
|
||||
<css>classpath:META-INF/resources/webjars/bootstrap/3.3.6/less/bootstrap.less</css>
|
||||
<css>/petclinic.less</css>
|
||||
|
|
|
@ -31,16 +31,10 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.samples.petclinic.controller.PetController;
|
||||
import org.springframework.samples.petclinic.dto.OwnerDTO;
|
||||
import org.springframework.samples.petclinic.dto.PetDTO;
|
||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||
import org.springframework.samples.petclinic.owner.Owner;
|
||||
import org.springframework.samples.petclinic.owner.Pet;
|
||||
import org.springframework.samples.petclinic.owner.PetType;
|
||||
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.service.OwnerService;
|
||||
import org.springframework.samples.petclinic.service.PetService;
|
||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||
|
@ -97,8 +91,10 @@ class PetControllerTests {
|
|||
|
||||
@Test
|
||||
void testProcessCreationFormHasErrors() throws Exception {
|
||||
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty").param("birthDate",
|
||||
"2015-02-12")).andExpect(model().attributeHasNoErrors("owner"))
|
||||
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
|
||||
.param("name", "Betty")
|
||||
.param("birthDate","2015-02-12"))
|
||||
.andExpect(model().attributeHasNoErrors("owner"))
|
||||
.andExpect(model().attributeHasErrors("pet")).andExpect(model().attributeHasFieldErrors("pet", "type"))
|
||||
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")).andExpect(status().isOk())
|
||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
||||
|
|
|
@ -28,7 +28,10 @@ import org.junit.jupiter.api.Test;
|
|||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.samples.petclinic.dto.PetTypeDTO;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.service.PetService;
|
||||
import org.springframework.samples.petclinic.service.PetTypeService;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
@ -42,18 +45,18 @@ import static org.mockito.BDDMockito.given;
|
|||
class PetTypeFormatterTests {
|
||||
|
||||
@Mock
|
||||
private PetRepository pets;
|
||||
private PetTypeService petTypeService;
|
||||
|
||||
private PetTypeFormatter petTypeFormatter;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.petTypeFormatter = new PetTypeFormatter(pets);
|
||||
this.petTypeFormatter = new PetTypeFormatter(petTypeService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPrint() {
|
||||
PetType petType = new PetType();
|
||||
PetTypeDTO petType = new PetTypeDTO();
|
||||
petType.setName("Hamster");
|
||||
String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH);
|
||||
assertThat(petTypeName).isEqualTo("Hamster");
|
||||
|
@ -61,14 +64,14 @@ class PetTypeFormatterTests {
|
|||
|
||||
@Test
|
||||
void shouldParse() throws ParseException {
|
||||
given(this.pets.findPetTypes()).willReturn(makePetTypes());
|
||||
PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
|
||||
given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
|
||||
PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
|
||||
assertThat(petType.getName()).isEqualTo("Bird");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowParseException() throws ParseException {
|
||||
given(this.pets.findPetTypes()).willReturn(makePetTypes());
|
||||
given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
|
||||
Assertions.assertThrows(ParseException.class, () -> {
|
||||
petTypeFormatter.parse("Fish", Locale.ENGLISH);
|
||||
});
|
||||
|
@ -78,14 +81,14 @@ class PetTypeFormatterTests {
|
|||
* Helper method to produce some sample pet types just for test purpose
|
||||
* @return {@link Collection} of {@link PetType}
|
||||
*/
|
||||
private List<PetType> makePetTypes() {
|
||||
List<PetType> petTypes = new ArrayList<>();
|
||||
petTypes.add(new PetType() {
|
||||
private List<PetTypeDTO> makePetTypes() {
|
||||
List<PetTypeDTO> petTypes = new ArrayList<>();
|
||||
petTypes.add(new PetTypeDTO() {
|
||||
{
|
||||
setName("Dog");
|
||||
}
|
||||
});
|
||||
petTypes.add(new PetType() {
|
||||
petTypes.add(new PetTypeDTO() {
|
||||
{
|
||||
setName("Bird");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue