diff --git a/pom.xml b/pom.xml index ed213a1ef..3636c8154 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,6 @@ petclinic - 1.8 UTF-8 diff --git a/src/main/java/org/springframework/samples/petclinic/controller/PetController.java b/src/main/java/org/springframework/samples/petclinic/controller/PetController.java index 64f3f5512..2bd6eb9a3 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/PetController.java @@ -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}"; } 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 d96062c49..f2ab9b542 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java @@ -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 { +public class PetTypeFormatter implements Formatter { - 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 findPetTypes = this.pets.findPetTypes(); - for (PetType type : findPetTypes) { + public PetTypeDTO parse(String text, Locale locale) throws ParseException { + Collection findPetTypes = this.petTypeService.findPetTypes(); + for (PetTypeDTO type : findPetTypes) { if (type.getName().equals(text)) { return type; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/BaseService.java b/src/main/java/org/springframework/samples/petclinic/service/BaseService.java index 40e0cc1f5..1f97e4d0b 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/BaseService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/BaseService.java @@ -4,12 +4,32 @@ import java.util.Collection; public interface BaseService { + /** + * 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 entitiesToDTOS(Collection entities); + /** + * Convert Entities Models Collection to Data Transfert Object Collection + * @param dtos Collection of DTO + * @return Collection of Entity Model + */ public Collection dtosToEntities(Collection dtos); } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetValidator.java b/src/main/java/org/springframework/samples/petclinic/validator/PetValidator.java similarity index 89% rename from src/main/java/org/springframework/samples/petclinic/owner/PetValidator.java rename to src/main/java/org/springframework/samples/petclinic/validator/PetValidator.java index e1370b428..6c96f217d 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetValidator.java +++ b/src/main/java/org/springframework/samples/petclinic/validator/PetValidator.java @@ -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)) { diff --git a/src/main/wro/wro.xml b/src/main/wro/wro.xml index 590156d7e..d4f23be37 100644 --- a/src/main/wro/wro.xml +++ b/src/main/wro/wro.xml @@ -1,6 +1,7 @@ - + classpath:META-INF/resources/webjars/bootstrap/3.3.6/less/bootstrap.less /petclinic.less - + diff --git a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java index 5594d7ec7..011842c87 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java @@ -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,11 +91,13 @@ 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")) - .andExpect(model().attributeHasErrors("pet")).andExpect(model().attributeHasFieldErrors("pet", "type")) - .andExpect(model().attributeHasFieldErrorCode("pet", "type", "required")).andExpect(status().isOk()) - .andExpect(view().name("pets/createOrUpdatePetForm")); + 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")); } @Test 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 44c051119..284955710 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -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 makePetTypes() { - List petTypes = new ArrayList<>(); - petTypes.add(new PetType() { + private List makePetTypes() { + List petTypes = new ArrayList<>(); + petTypes.add(new PetTypeDTO() { { setName("Dog"); } }); - petTypes.add(new PetType() { + petTypes.add(new PetTypeDTO() { { setName("Bird"); }