Change PetTypeFormatter and Validator to use DTO

This commit is contained in:
PEDSF 2020-10-10 09:12:52 +02:00
parent 6d5b55fc10
commit 4dd08b9c5b
8 changed files with 62 additions and 45 deletions

View file

@ -15,7 +15,6 @@
<name>petclinic</name> <name>petclinic</name>
<properties> <properties>
<!-- Generic properties --> <!-- Generic properties -->
<java.version>1.8</java.version> <java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View file

@ -15,13 +15,9 @@
*/ */
package org.springframework.samples.petclinic.controller; package org.springframework.samples.petclinic.controller;
import org.springframework.samples.petclinic.dto.OwnerDTO; import org.springframework.samples.petclinic.dto.*;
import org.springframework.samples.petclinic.dto.PetDTO; import org.springframework.samples.petclinic.validator.PetValidator;
import org.springframework.samples.petclinic.dto.PetTypeDTO; import org.springframework.samples.petclinic.service.*;
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.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -93,8 +89,7 @@ class PetController {
if (result.hasErrors()) { if (result.hasErrors()) {
model.put("pet", pet); model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM; return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} } else {
else {
this.petService.save(pet); this.petService.save(pet);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }

View file

@ -21,7 +21,8 @@ import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter; 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; import org.springframework.stereotype.Component;
/** /**
@ -35,24 +36,24 @@ import org.springframework.stereotype.Component;
* @author Michael Isvy * @author Michael Isvy
*/ */
@Component @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(PetTypeService petTypeService) {
public PetTypeFormatter(PetRepository pets) { this.petTypeService = petTypeService;
this.pets = pets;
} }
@Override @Override
public String print(PetType petType, Locale locale) { public String print(PetTypeDTO petType, Locale locale) {
return petType.getName(); return petType.getName();
} }
@Override @Override
public PetType parse(String text, Locale locale) throws ParseException { public PetTypeDTO parse(String text, Locale locale) throws ParseException {
Collection<PetType> findPetTypes = this.pets.findPetTypes(); Collection<PetTypeDTO> findPetTypes = this.petTypeService.findPetTypes();
for (PetType type : findPetTypes) { for (PetTypeDTO type : findPetTypes) {
if (type.getName().equals(text)) { if (type.getName().equals(text)) {
return type; return type;
} }

View file

@ -4,12 +4,32 @@ import java.util.Collection;
public interface BaseService<E, D> { public interface BaseService<E, D> {
/**
* Convert Data Transfert Object to Entity Model
* @param dto DTO
* @return Entity Model
*/
public E dtoToEntity(D dto); public E dtoToEntity(D dto);
/**
* Convert Entity Model to Data Transfert Object
* @param entity Entity Model
* @return DTO
*/
public D entityToDTO(E entity); 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); 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); public Collection<E> dtosToEntities(Collection<D> dtos);
} }

View file

@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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.util.StringUtils;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
@ -35,7 +37,7 @@ public class PetValidator implements Validator {
@Override @Override
public void validate(Object obj, Errors errors) { public void validate(Object obj, Errors errors) {
Pet pet = (Pet) obj; PetDTO pet = (PetDTO) obj;
String name = pet.getName(); String name = pet.getName();
// name validation // name validation
if (!StringUtils.hasLength(name)) { if (!StringUtils.hasLength(name)) {

View file

@ -1,6 +1,7 @@
<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"> <group name="petclinic">
<css>classpath:META-INF/resources/webjars/bootstrap/3.3.6/less/bootstrap.less</css> <css>classpath:META-INF/resources/webjars/bootstrap/3.3.6/less/bootstrap.less</css>
<css>/petclinic.less</css> <css>/petclinic.less</css>
</group> </group>
</groups> </groups>

View file

@ -31,16 +31,10 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType; 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.OwnerDTO;
import org.springframework.samples.petclinic.dto.PetDTO; import org.springframework.samples.petclinic.dto.PetDTO;
import org.springframework.samples.petclinic.dto.PetTypeDTO; 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.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.OwnerService;
import org.springframework.samples.petclinic.service.PetService; import org.springframework.samples.petclinic.service.PetService;
import org.springframework.samples.petclinic.service.PetTypeService; import org.springframework.samples.petclinic.service.PetTypeService;
@ -97,11 +91,13 @@ class PetControllerTests {
@Test @Test
void testProcessCreationFormHasErrors() throws Exception { void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty").param("birthDate", mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
"2015-02-12")).andExpect(model().attributeHasNoErrors("owner")) .param("name", "Betty")
.andExpect(model().attributeHasErrors("pet")).andExpect(model().attributeHasFieldErrors("pet", "type")) .param("birthDate","2015-02-12"))
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")).andExpect(status().isOk()) .andExpect(model().attributeHasNoErrors("owner"))
.andExpect(view().name("pets/createOrUpdatePetForm")); .andExpect(model().attributeHasErrors("pet")).andExpect(model().attributeHasFieldErrors("pet", "type"))
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")).andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm"));
} }
@Test @Test

View file

@ -28,7 +28,10 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; 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.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.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
@ -42,18 +45,18 @@ import static org.mockito.BDDMockito.given;
class PetTypeFormatterTests { class PetTypeFormatterTests {
@Mock @Mock
private PetRepository pets; private PetTypeService petTypeService;
private PetTypeFormatter petTypeFormatter; private PetTypeFormatter petTypeFormatter;
@BeforeEach @BeforeEach
void setup() { void setup() {
this.petTypeFormatter = new PetTypeFormatter(pets); this.petTypeFormatter = new PetTypeFormatter(petTypeService);
} }
@Test @Test
void testPrint() { void testPrint() {
PetType petType = new PetType(); PetTypeDTO petType = new PetTypeDTO();
petType.setName("Hamster"); petType.setName("Hamster");
String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH); String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH);
assertThat(petTypeName).isEqualTo("Hamster"); assertThat(petTypeName).isEqualTo("Hamster");
@ -61,14 +64,14 @@ class PetTypeFormatterTests {
@Test @Test
void shouldParse() throws ParseException { void shouldParse() throws ParseException {
given(this.pets.findPetTypes()).willReturn(makePetTypes()); given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
assertThat(petType.getName()).isEqualTo("Bird"); assertThat(petType.getName()).isEqualTo("Bird");
} }
@Test @Test
void shouldThrowParseException() throws ParseException { void shouldThrowParseException() throws ParseException {
given(this.pets.findPetTypes()).willReturn(makePetTypes()); given(this.petTypeService.findPetTypes()).willReturn(makePetTypes());
Assertions.assertThrows(ParseException.class, () -> { Assertions.assertThrows(ParseException.class, () -> {
petTypeFormatter.parse("Fish", Locale.ENGLISH); petTypeFormatter.parse("Fish", Locale.ENGLISH);
}); });
@ -78,14 +81,14 @@ class PetTypeFormatterTests {
* Helper method to produce some sample pet types just for test purpose * Helper method to produce some sample pet types just for test purpose
* @return {@link Collection} of {@link PetType} * @return {@link Collection} of {@link PetType}
*/ */
private List<PetType> makePetTypes() { private List<PetTypeDTO> makePetTypes() {
List<PetType> petTypes = new ArrayList<>(); List<PetTypeDTO> petTypes = new ArrayList<>();
petTypes.add(new PetType() { petTypes.add(new PetTypeDTO() {
{ {
setName("Dog"); setName("Dog");
} }
}); });
petTypes.add(new PetType() { petTypes.add(new PetTypeDTO() {
{ {
setName("Bird"); setName("Bird");
} }