visitDTOS = new HashSet<>();
+
+ for (Visit visit : visits) {
+ visitDTOS.add(entityToDTO(visit));
+ }
+
+ return visitDTOS;
+ }
+
+}
diff --git a/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java b/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java
index 0a96582c9..51ba24f22 100755
--- a/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java
+++ b/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java
@@ -34,9 +34,7 @@ class CacheConfiguration {
@Bean
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
- return cm -> {
- cm.createCache("vets", cacheConfiguration());
- };
+ return cm -> cm.createCache("vets", cacheConfiguration());
}
/**
diff --git a/src/main/java/org/springframework/samples/petclinic/validator/PetDTOValidator.java b/src/main/java/org/springframework/samples/petclinic/validator/PetDTOValidator.java
new file mode 100644
index 000000000..ede0f9185
--- /dev/null
+++ b/src/main/java/org/springframework/samples/petclinic/validator/PetDTOValidator.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012-2019 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.validator;
+
+import org.springframework.samples.petclinic.common.CommonAttribute;
+import org.springframework.samples.petclinic.common.CommonError;
+import org.springframework.samples.petclinic.dto.PetDTO;
+import org.springframework.util.StringUtils;
+import org.springframework.validation.Errors;
+import org.springframework.validation.Validator;
+
+/**
+ * Validator
for PetDTO
forms.
+ *
+ * We're not using Bean Validation annotations here because it is easier to define such
+ * validation rule in Java.
+ *
+ *
+ * @author Ken Krebs
+ * @author Juergen Hoeller
+ * @author Paul-Emmanuel DOS SANTOS FACAO
+ */
+public class PetDTOValidator implements Validator {
+
+ @Override
+ public void validate(Object obj, Errors errors) {
+ PetDTO pet = (PetDTO) obj;
+ String name = pet.getName();
+ // name validation
+ if (!StringUtils.hasLength(name)) {
+ errors.rejectValue(CommonAttribute.PET_NAME, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
+ }
+
+ // type validation
+ if (pet.isNew() && pet.getType() == null) {
+ errors.rejectValue(CommonAttribute.PET_TYPE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
+ }
+
+ // birth date validation
+ if (pet.getBirthDate() == null) {
+ errors.rejectValue(CommonAttribute.PET_BIRTH_DATE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
+ }
+ }
+
+ /**
+ * This Validator validates *just* Pet instances
+ */
+ @Override
+ public boolean supports(Class> clazz) {
+ return PetDTO.class.isAssignableFrom(clazz);
+ }
+
+}
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 71%
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..98f4ab025 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,11 @@
* 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.common.CommonAttribute;
+import org.springframework.samples.petclinic.common.CommonError;
+import org.springframework.samples.petclinic.model.Pet;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
@@ -28,28 +31,27 @@ import org.springframework.validation.Validator;
*
* @author Ken Krebs
* @author Juergen Hoeller
+ * @author Paul-Emmanuel DOS SANTOS FACAO
*/
public class PetValidator implements Validator {
- private static final String REQUIRED = "required";
-
@Override
public void validate(Object obj, Errors errors) {
Pet pet = (Pet) obj;
String name = pet.getName();
// name validation
if (!StringUtils.hasLength(name)) {
- errors.rejectValue("name", REQUIRED, REQUIRED);
+ errors.rejectValue(CommonAttribute.PET_NAME, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
}
// type validation
if (pet.isNew() && pet.getType() == null) {
- errors.rejectValue("type", REQUIRED, REQUIRED);
+ errors.rejectValue(CommonAttribute.PET_TYPE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
}
// birth date validation
if (pet.getBirthDate() == null) {
- errors.rejectValue("birthDate", REQUIRED, REQUIRED);
+ errors.rejectValue(CommonAttribute.PET_BIRTH_DATE, CommonError.REQUIRED_ARGS, CommonError.REQUIRED_MESSAGE);
}
}
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/PetclinicIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java
index 226db01fe..90736badb 100644
--- a/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java
@@ -19,7 +19,7 @@ package org.springframework.samples.petclinic;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.samples.petclinic.vet.VetRepository;
+import org.springframework.samples.petclinic.repository.VetRepository;
@SpringBootTest
class PetclinicIntegrationTests {
diff --git a/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java
new file mode 100644
index 000000000..ac1018907
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/controller/OwnerControllerTests.java
@@ -0,0 +1,244 @@
+/*
+ * Copyright 2012-2019 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.controller;
+
+import java.time.LocalDate;
+import java.util.Collections;
+import java.util.List;
+
+import org.assertj.core.util.Lists;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.samples.petclinic.common.CommonAttribute;
+import org.springframework.samples.petclinic.common.CommonEndPoint;
+import org.springframework.samples.petclinic.common.CommonView;
+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.dto.VisitDTO;
+import org.springframework.samples.petclinic.service.OwnerService;
+import org.springframework.samples.petclinic.service.VisitService;
+import org.springframework.test.web.servlet.MockMvc;
+
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.hasProperty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+/**
+ * Test class for {@link OwnerController}
+ *
+ * @author Colin But
+ * @author Paul-Emmanuel DOS SANTOS FACAO
+ */
+@WebMvcTest(OwnerController.class)
+class OwnerControllerTests {
+
+ private static final int TEST_OWNER_ID = 1;
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockBean
+ private OwnerService owners;
+
+ @MockBean
+ private VisitService visits;
+
+ private OwnerDTO george;
+
+ @BeforeEach
+ void setup() {
+ george = new OwnerDTO();
+ george.setId(TEST_OWNER_ID);
+ george.setFirstName("George");
+ george.setLastName("Franklin");
+ george.setAddress("110 W. Liberty St.");
+ george.setCity("Madison");
+ george.setTelephone("6085551023");
+ PetDTO max = new PetDTO();
+ PetTypeDTO dog = new PetTypeDTO();
+ dog.setName("dog");
+ max.setId(1);
+ max.setType(dog);
+ max.setName("Max");
+ max.setBirthDate(LocalDate.now());
+ george.setPetsInternal(Collections.singleton(max));
+ given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
+ VisitDTO visit = new VisitDTO();
+ visit.setDate(LocalDate.now());
+ given(this.visits.findByPetId(max.getId())).willReturn(Collections.singletonList(visit));
+ }
+
+ @Test
+ @Tag("initCreationForm")
+ void testInitCreationForm() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS_NEW)).andExpect(status().isOk())
+ .andExpect(model().attributeExists(CommonAttribute.OWNER))
+ .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
+ }
+
+ @Test
+ @Tag("processCreationForm")
+ void testProcessCreationFormSuccess() throws Exception {
+ mockMvc.perform(post("/owners/new").param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
+ .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
+ .param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street").param(CommonAttribute.OWNER_CITY, "London")
+ .param(CommonAttribute.OWNER_PHONE, "01316761638")).andExpect(status().is3xxRedirection());
+ }
+
+ @Test
+ @Tag("processCreationForm")
+ void testProcessCreationFormHasErrors() throws Exception {
+ mockMvc.perform(post(CommonEndPoint.OWNERS_NEW).param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
+ .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs").param(CommonAttribute.OWNER_CITY, "London"))
+ .andExpect(status().isOk()).andExpect(model().attributeHasErrors(CommonAttribute.OWNER))
+ .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS))
+ .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE))
+ .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
+ }
+
+ @Test
+ @Tag("initFindForm")
+ void testInitFindForm() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS_FIND)).andExpect(status().isOk())
+ .andExpect(model().attributeExists(CommonAttribute.OWNER))
+ .andExpect(view().name(CommonView.OWNER_FIND_OWNERS));
+ }
+
+ @Test
+ @Tag("processFindForm")
+ void testProcessFindFormSuccess() throws Exception {
+ given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new OwnerDTO()));
+
+ mockMvc.perform(get(CommonEndPoint.OWNERS)).andExpect(status().isOk())
+ .andExpect(view().name(CommonView.OWNER_OWNERS_LIST));
+ }
+
+ @Test
+ @Tag("processFindForm")
+ void testProcessFindFormByLastName() throws Exception {
+ given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
+
+ mockMvc.perform(get(CommonEndPoint.OWNERS).param(CommonAttribute.OWNER_LAST_NAME, "Franklin"))
+ .andExpect(status().is3xxRedirection())
+ .andExpect(view().name(CommonView.OWNER_OWNERS_R + TEST_OWNER_ID));
+ }
+
+ @Test
+ @Tag("processFindForm")
+ void testProcessFindFormNoOwnersFound() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS).param(CommonAttribute.OWNER_LAST_NAME, "Unknown Surname"))
+ .andExpect(status().isOk())
+ .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME))
+ .andExpect(model().attributeHasFieldErrorCode(CommonAttribute.OWNER, CommonAttribute.OWNER_LAST_NAME,
+ "notFound"))
+ .andExpect(view().name(CommonView.OWNER_FIND_OWNERS));
+ }
+
+ @Test
+ @Tag("initUpdateOwnerForm")
+ void testInitUpdateOwnerForm() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID)).andExpect(status().isOk())
+ .andExpect(model().attributeExists(CommonAttribute.OWNER))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin"))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George"))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_ADDRESS, is("110 W. Liberty St."))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_CITY, is("Madison"))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_PHONE, is("6085551023"))))
+ .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
+ }
+
+ @Test
+ @Tag("processUpdateOwnerForm")
+ void testProcessUpdateOwnerFormSuccess() throws Exception {
+ mockMvc.perform(post(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID)
+ .param(CommonAttribute.OWNER_FIRST_NAME, "Joe").param(CommonAttribute.OWNER_LAST_NAME, "Bloggs")
+ .param(CommonAttribute.OWNER_ADDRESS, "123 Caramel Street").param(CommonAttribute.OWNER_CITY, "London")
+ .param(CommonAttribute.OWNER_PHONE, "01616291589")).andExpect(status().is3xxRedirection())
+ .andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
+ }
+
+ @Test
+ @Tag("processUpdateOwnerForm")
+ void testProcessUpdateOwnerFormHasErrors() throws Exception {
+ mockMvc.perform(
+ post(CommonEndPoint.OWNERS_ID_EDIT, TEST_OWNER_ID).param(CommonAttribute.OWNER_FIRST_NAME, "Joe")
+ .param(CommonAttribute.OWNER_LAST_NAME, "Bloggs").param(CommonAttribute.OWNER_CITY, "London"))
+ .andExpect(status().isOk()).andExpect(model().attributeHasErrors(CommonAttribute.OWNER))
+ .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_ADDRESS))
+ .andExpect(model().attributeHasFieldErrors(CommonAttribute.OWNER, CommonAttribute.OWNER_PHONE))
+ .andExpect(view().name(CommonView.OWNER_CREATE_OR_UPDATE));
+ }
+
+ @Test
+ @Tag("processUpdateOwnerForm")
+ void testShowOwner() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS_ID, TEST_OWNER_ID)).andExpect(status().isOk())
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_LAST_NAME, is("Franklin"))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_FIRST_NAME, is("George"))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_ADDRESS, is("110 W. Liberty St."))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_CITY, is("Madison"))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_PHONE, is("6085551023"))))
+ .andExpect(
+ model().attribute(CommonAttribute.OWNER, hasProperty(CommonAttribute.OWNER_PETS, not(empty()))))
+ .andExpect(model().attribute(CommonAttribute.OWNER,
+ hasProperty(CommonAttribute.OWNER_PETS, new BaseMatcher>() {
+
+ @Override
+ public boolean matches(Object item) {
+ @SuppressWarnings("unchecked")
+ List pets = (List) item;
+ PetDTO pet = pets.get(0);
+ if (pet.getVisits().isEmpty()) {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("Max did not have any visits");
+ }
+ })))
+ .andExpect(view().name(CommonView.OWNER_DETAILS));
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java
new file mode 100644
index 000000000..717507c5b
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/controller/PetControllerTests.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2012-2019 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.controller;
+
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+import org.assertj.core.util.Lists;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+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.common.CommonAttribute;
+import org.springframework.samples.petclinic.common.CommonEndPoint;
+import org.springframework.samples.petclinic.common.CommonError;
+import org.springframework.samples.petclinic.common.CommonView;
+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.formatter.PetTypeFormatter;
+import org.springframework.samples.petclinic.service.OwnerService;
+import org.springframework.samples.petclinic.service.PetService;
+import org.springframework.samples.petclinic.service.PetTypeService;
+import org.springframework.test.web.servlet.MockMvc;
+
+/**
+ * Test class for the {@link PetController}
+ *
+ * @author Colin But
+ * @author Paul-Emmanuel DOS SANTOS FACAO
+ */
+@WebMvcTest(value = PetController.class,
+ includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
+class PetControllerTests {
+
+ private static final int TEST_OWNER_ID = 1;
+
+ private static final int TEST_PET_ID = 1;
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockBean
+ private PetService petService;
+
+ @MockBean
+ private PetTypeService petTypeService;
+
+ @MockBean
+ private OwnerService ownerService;
+
+ @BeforeEach
+ void setup() {
+ PetTypeDTO cat = new PetTypeDTO();
+ cat.setId(3);
+ cat.setName("hamster");
+
+ given(this.ownerService.findById(TEST_OWNER_ID)).willReturn(new OwnerDTO());
+ given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO());
+ given(this.petService.findPetTypes()).willReturn(Lists.newArrayList(cat));
+ }
+
+ @Test
+ @Tag("initCreationForm")
+ void testInitCreationForm() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID))
+ .andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE))
+ .andExpect(model().attributeExists(CommonAttribute.PET));
+ }
+
+ @Test
+ @Tag("processCreationForm")
+ void testProcessCreationFormSuccess() throws Exception {
+ mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
+ .param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_TYPE, "hamster")
+ .param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12")).andExpect(status().is3xxRedirection())
+ .andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
+ }
+
+ @Test
+ @Tag("processCreationForm")
+ void testProcessCreationFormHasErrors() throws Exception {
+ mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
+ .param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
+ .andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
+ .andExpect(model().attributeHasErrors(CommonAttribute.PET))
+ .andExpect(model().attributeHasFieldErrors(CommonAttribute.PET, CommonAttribute.PET_TYPE))
+ .andExpect(model().attributeHasFieldErrorCode(CommonAttribute.PET, CommonAttribute.PET_TYPE,
+ CommonError.REQUIRED_ARGS))
+ .andExpect(status().isOk()).andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
+ }
+
+ @Test
+ @Tag("initUpdateForm")
+ void testInitUpdateForm() throws Exception {
+ mockMvc.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID))
+ .andExpect(status().isOk()).andExpect(model().attributeExists(CommonAttribute.PET))
+ .andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
+ }
+
+ @Test
+ @Tag("processUpdateForm")
+ void testProcessUpdateFormSuccess() throws Exception {
+ mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID)
+ .param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_TYPE, "hamster")
+ .param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12")).andExpect(status().is3xxRedirection())
+ .andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
+ }
+
+ @Test
+ @Tag("processUpdateForm")
+ void testProcessUpdateFormHasErrors() throws Exception {
+ mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID)
+ .param(CommonAttribute.PET_NAME, "Betty").param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
+ .andExpect(model().attributeHasNoErrors(CommonAttribute.OWNER))
+ .andExpect(model().attributeHasErrors(CommonAttribute.PET)).andExpect(status().isOk())
+ .andExpect(view().name(CommonView.PET_CREATE_OR_UPDATE));
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java
similarity index 67%
rename from src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java
rename to src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java
index fd537bee2..c3eef01b1 100644
--- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTests.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.samples.petclinic.vet;
+package org.springframework.samples.petclinic.controller;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -26,16 +26,26 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
+import org.springframework.samples.petclinic.common.CommonAttribute;
+import org.springframework.samples.petclinic.common.CommonEndPoint;
+import org.springframework.samples.petclinic.common.CommonView;
+import org.springframework.samples.petclinic.dto.SpecialtyDTO;
+import org.springframework.samples.petclinic.dto.VetDTO;
+import org.springframework.samples.petclinic.service.VetService;
+import org.springframework.samples.petclinic.model.Specialty;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
/**
* Test class for the {@link VetController}
+ *
+ * @author Paul-Emmanuel DOS SANTOS FACAO
*/
@WebMvcTest(VetController.class)
class VetControllerTests {
@@ -44,35 +54,39 @@ class VetControllerTests {
private MockMvc mockMvc;
@MockBean
- private VetRepository vets;
+ private VetService vetService;
@BeforeEach
void setup() {
- Vet james = new Vet();
+ VetDTO james = new VetDTO();
james.setFirstName("James");
james.setLastName("Carter");
james.setId(1);
- Vet helen = new Vet();
+ VetDTO helen = new VetDTO();
helen.setFirstName("Helen");
helen.setLastName("Leary");
helen.setId(2);
- Specialty radiology = new Specialty();
+ SpecialtyDTO radiology = new SpecialtyDTO();
radiology.setId(1);
radiology.setName("radiology");
helen.addSpecialty(radiology);
- given(this.vets.findAll()).willReturn(Lists.newArrayList(james, helen));
+ given(this.vetService.findAll()).willReturn(Lists.newArrayList(james, helen));
}
@Test
+ @Tag("showVetList")
void testShowVetListHtml() throws Exception {
- mockMvc.perform(get("/vets.html")).andExpect(status().isOk()).andExpect(model().attributeExists("vets"))
- .andExpect(view().name("vets/vetList"));
+ mockMvc.perform(get(CommonEndPoint.VETS_HTML)).andExpect(status().isOk())
+ .andExpect(model().attributeExists(CommonAttribute.VETS))
+ .andExpect(view().name(CommonView.VET_VETS_LIST));
}
@Test
+ @Tag("showResourcesVetList")
void testShowResourcesVetList() throws Exception {
- ResultActions actions = mockMvc.perform(get("/vets").accept(MediaType.APPLICATION_JSON))
+ ResultActions actions = mockMvc.perform(get(CommonEndPoint.VETS).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
+
actions.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.vetList[0].id").value(1));
}
diff --git a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/controller/VisitControllerTests.java
similarity index 58%
rename from src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java
rename to src/test/java/org/springframework/samples/petclinic/controller/VisitControllerTests.java
index 84bee72df..718bab0f0 100644
--- a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/controller/VisitControllerTests.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.samples.petclinic.owner;
+package org.springframework.samples.petclinic.controller;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -24,11 +24,17 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
-import org.springframework.samples.petclinic.visit.VisitRepository;
+import org.springframework.samples.petclinic.common.CommonAttribute;
+import org.springframework.samples.petclinic.common.CommonEndPoint;
+import org.springframework.samples.petclinic.common.CommonView;
+import org.springframework.samples.petclinic.dto.PetDTO;
+import org.springframework.samples.petclinic.service.PetService;
+import org.springframework.samples.petclinic.service.VisitService;
import org.springframework.test.web.servlet.MockMvc;
/**
@@ -45,34 +51,37 @@ class VisitControllerTests {
private MockMvc mockMvc;
@MockBean
- private VisitRepository visits;
+ private VisitService visitService;
@MockBean
- private PetRepository pets;
+ private PetService petService;
@BeforeEach
void init() {
- given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
+ given(this.petService.findById(TEST_PET_ID)).willReturn(new PetDTO());
}
@Test
+ @Tag("initNewVisitForm")
void testInitNewVisitForm() throws Exception {
- mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)).andExpect(status().isOk())
- .andExpect(view().name("pets/createOrUpdateVisitForm"));
+ mockMvc.perform(get(CommonEndPoint.VISITS_NEW, TEST_PET_ID)).andExpect(status().isOk())
+ .andExpect(view().name(CommonView.VISIT_CREATE_OR_UPDATE));
}
@Test
+ @Tag("processNewVisitForm")
void testProcessNewVisitFormSuccess() throws Exception {
- mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George")
- .param("description", "Visit Description")).andExpect(status().is3xxRedirection())
- .andExpect(view().name("redirect:/owners/{ownerId}"));
+ mockMvc.perform(post(CommonEndPoint.VISITS_NEW, TEST_PET_ID).param(CommonAttribute.NAME, "George")
+ .param(CommonAttribute.DESCRIPTION, "Visit Description")).andExpect(status().is3xxRedirection())
+ .andExpect(view().name(CommonView.OWNER_OWNERS_ID_R));
}
@Test
+ @Tag("processNewVisitForm")
void testProcessNewVisitFormHasErrors() throws Exception {
- mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID).param("name", "George"))
- .andExpect(model().attributeHasErrors("visit")).andExpect(status().isOk())
- .andExpect(view().name("pets/createOrUpdateVisitForm"));
+ mockMvc.perform(post(CommonEndPoint.VISITS_NEW, TEST_PET_ID).param(CommonAttribute.NAME, "George"))
+ .andExpect(model().attributeHasErrors(CommonAttribute.VISIT)).andExpect(status().isOk())
+ .andExpect(view().name(CommonView.VISIT_CREATE_OR_UPDATE));
}
}
diff --git a/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java
new file mode 100644
index 000000000..a9e8f2e72
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeDTOFormatterTests.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2012-2019 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.formater;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+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.formatter.PetTypeFormatter;
+import org.springframework.samples.petclinic.model.PetType;
+import org.springframework.samples.petclinic.service.PetService;
+import org.springframework.samples.petclinic.service.PetTypeService;
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.BDDMockito.given;
+
+/**
+ * Test class for {@link PetTypeFormatter}
+ *
+ * @author Colin But
+ */
+@ExtendWith(MockitoExtension.class)
+class PetTypeDTOFormatterTests {
+
+ @Mock
+ private PetService petService;
+
+ private PetTypeFormatter petTypeFormatter;
+
+ @BeforeEach
+ void setup() {
+ this.petTypeFormatter = new PetTypeFormatter(petService);
+ }
+
+ @Test
+ void testPrint() {
+ PetTypeDTO petType = new PetTypeDTO();
+ petType.setName("Hamster");
+ String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH);
+ assertThat(petTypeName).isEqualTo("Hamster");
+ }
+
+ @Test
+ void shouldParse() throws ParseException {
+ given(this.petService.findPetTypes()).willReturn(makePetTypes());
+ PetTypeDTO petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
+ assertThat(petType.getName()).isEqualTo("Bird");
+ }
+
+ @Test
+ void shouldThrowParseException() {
+ given(this.petService.findPetTypes()).willReturn(makePetTypes());
+ Assertions.assertThrows(ParseException.class, () -> petTypeFormatter.parse("Fish", Locale.ENGLISH));
+ }
+
+ /**
+ * 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 PetTypeDTO() {
+ {
+ setName("Dog");
+ }
+ });
+ petTypes.add(new PetTypeDTO() {
+ {
+ setName("Bird");
+ }
+ });
+ return petTypes;
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java
similarity index 64%
rename from src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java
rename to src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java
index adb96b69d..902048f72 100644
--- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/formater/PetTypeFormatterTests.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.samples.petclinic.owner;
+package org.springframework.samples.petclinic.formater;
import java.text.ParseException;
import java.util.ArrayList;
@@ -28,6 +28,11 @@ 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.formatter.PetTypeFormatter;
+import org.springframework.samples.petclinic.model.PetType;
+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;
@@ -41,18 +46,18 @@ import static org.mockito.BDDMockito.given;
class PetTypeFormatterTests {
@Mock
- private PetRepository pets;
+ private PetService petService;
private PetTypeFormatter petTypeFormatter;
@BeforeEach
void setup() {
- this.petTypeFormatter = new PetTypeFormatter(pets);
+ this.petTypeFormatter = new PetTypeFormatter(petService);
}
@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");
@@ -60,31 +65,29 @@ class PetTypeFormatterTests {
@Test
void shouldParse() throws ParseException {
- given(this.pets.findPetTypes()).willReturn(makePetTypes());
- PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
+ given(this.petService.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());
- Assertions.assertThrows(ParseException.class, () -> {
- petTypeFormatter.parse("Fish", Locale.ENGLISH);
- });
+ void shouldThrowParseException() {
+ given(this.petService.findPetTypes()).willReturn(makePetTypes());
+ Assertions.assertThrows(ParseException.class, () -> petTypeFormatter.parse("Fish", Locale.ENGLISH));
}
/**
* 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");
}
diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java b/src/test/java/org/springframework/samples/petclinic/model/VetTests.java
similarity index 95%
rename from src/test/java/org/springframework/samples/petclinic/vet/VetTests.java
rename to src/test/java/org/springframework/samples/petclinic/model/VetTests.java
index d8df78b85..45066fe9c 100644
--- a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/model/VetTests.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.springframework.samples.petclinic.vet;
+package org.springframework.samples.petclinic.model;
import org.junit.jupiter.api.Test;
import org.springframework.util.SerializationUtils;
diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java
deleted file mode 100644
index 1d6249c5d..000000000
--- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java
+++ /dev/null
@@ -1,199 +0,0 @@
-/*
- * Copyright 2012-2019 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.time.LocalDate;
-import java.util.Collections;
-import java.util.List;
-
-import org.assertj.core.util.Lists;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
-import org.springframework.boot.test.mock.mockito.MockBean;
-import org.springframework.samples.petclinic.visit.Visit;
-import org.springframework.samples.petclinic.visit.VisitRepository;
-import org.springframework.test.web.servlet.MockMvc;
-
-import static org.hamcrest.Matchers.empty;
-import static org.hamcrest.Matchers.hasProperty;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.not;
-import static org.mockito.BDDMockito.given;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
-
-/**
- * Test class for {@link OwnerController}
- *
- * @author Colin But
- */
-@WebMvcTest(OwnerController.class)
-class OwnerControllerTests {
-
- private static final int TEST_OWNER_ID = 1;
-
- @Autowired
- private MockMvc mockMvc;
-
- @MockBean
- private OwnerRepository owners;
-
- @MockBean
- private VisitRepository visits;
-
- private Owner george;
-
- @BeforeEach
- void setup() {
- george = new Owner();
- george.setId(TEST_OWNER_ID);
- george.setFirstName("George");
- george.setLastName("Franklin");
- george.setAddress("110 W. Liberty St.");
- george.setCity("Madison");
- george.setTelephone("6085551023");
- Pet max = new Pet();
- PetType dog = new PetType();
- dog.setName("dog");
- max.setId(1);
- max.setType(dog);
- max.setName("Max");
- max.setBirthDate(LocalDate.now());
- george.setPetsInternal(Collections.singleton(max));
- given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
- Visit visit = new Visit();
- visit.setDate(LocalDate.now());
- given(this.visits.findByPetId(max.getId())).willReturn(Collections.singletonList(visit));
- }
-
- @Test
- void testInitCreationForm() throws Exception {
- mockMvc.perform(get("/owners/new")).andExpect(status().isOk()).andExpect(model().attributeExists("owner"))
- .andExpect(view().name("owners/createOrUpdateOwnerForm"));
- }
-
- @Test
- void testProcessCreationFormSuccess() throws Exception {
- mockMvc.perform(post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs")
- .param("address", "123 Caramel Street").param("city", "London").param("telephone", "01316761638"))
- .andExpect(status().is3xxRedirection());
- }
-
- @Test
- void testProcessCreationFormHasErrors() throws Exception {
- mockMvc.perform(
- post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs").param("city", "London"))
- .andExpect(status().isOk()).andExpect(model().attributeHasErrors("owner"))
- .andExpect(model().attributeHasFieldErrors("owner", "address"))
- .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
- .andExpect(view().name("owners/createOrUpdateOwnerForm"));
- }
-
- @Test
- void testInitFindForm() throws Exception {
- mockMvc.perform(get("/owners/find")).andExpect(status().isOk()).andExpect(model().attributeExists("owner"))
- .andExpect(view().name("owners/findOwners"));
- }
-
- @Test
- void testProcessFindFormSuccess() throws Exception {
- given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
- mockMvc.perform(get("/owners")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
- }
-
- @Test
- void testProcessFindFormByLastName() throws Exception {
- given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
- mockMvc.perform(get("/owners").param("lastName", "Franklin")).andExpect(status().is3xxRedirection())
- .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
- }
-
- @Test
- void testProcessFindFormNoOwnersFound() throws Exception {
- mockMvc.perform(get("/owners").param("lastName", "Unknown Surname")).andExpect(status().isOk())
- .andExpect(model().attributeHasFieldErrors("owner", "lastName"))
- .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
- .andExpect(view().name("owners/findOwners"));
- }
-
- @Test
- void testInitUpdateOwnerForm() throws Exception {
- mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)).andExpect(status().isOk())
- .andExpect(model().attributeExists("owner"))
- .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
- .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
- .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
- .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
- .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
- .andExpect(view().name("owners/createOrUpdateOwnerForm"));
- }
-
- @Test
- void testProcessUpdateOwnerFormSuccess() throws Exception {
- mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe")
- .param("lastName", "Bloggs").param("address", "123 Caramel Street").param("city", "London")
- .param("telephone", "01616291589")).andExpect(status().is3xxRedirection())
- .andExpect(view().name("redirect:/owners/{ownerId}"));
- }
-
- @Test
- void testProcessUpdateOwnerFormHasErrors() throws Exception {
- mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe")
- .param("lastName", "Bloggs").param("city", "London")).andExpect(status().isOk())
- .andExpect(model().attributeHasErrors("owner"))
- .andExpect(model().attributeHasFieldErrors("owner", "address"))
- .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
- .andExpect(view().name("owners/createOrUpdateOwnerForm"));
- }
-
- @Test
- void testShowOwner() throws Exception {
- mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)).andExpect(status().isOk())
- .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
- .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
- .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
- .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
- .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
- .andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))
- .andExpect(model().attribute("owner", hasProperty("pets", new BaseMatcher>() {
-
- @Override
- public boolean matches(Object item) {
- @SuppressWarnings("unchecked")
- List pets = (List) item;
- Pet pet = pets.get(0);
- if (pet.getVisits().isEmpty()) {
- return false;
- }
- return true;
- }
-
- @Override
- public void describeTo(Description description) {
- description.appendText("Max did not have any visits");
- }
- }))).andExpect(view().name("owners/ownerDetails"));
- }
-
-}
diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java
deleted file mode 100755
index 47c444a78..000000000
--- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Copyright 2012-2019 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 static org.mockito.BDDMockito.given;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
-
-import org.assertj.core.util.Lists;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-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.test.web.servlet.MockMvc;
-
-/**
- * Test class for the {@link PetController}
- *
- * @author Colin But
- */
-@WebMvcTest(value = PetController.class,
- includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
-class PetControllerTests {
-
- private static final int TEST_OWNER_ID = 1;
-
- private static final int TEST_PET_ID = 1;
-
- @Autowired
- private MockMvc mockMvc;
-
- @MockBean
- private PetRepository pets;
-
- @MockBean
- private OwnerRepository owners;
-
- @BeforeEach
- void setup() {
- PetType cat = new PetType();
- cat.setId(3);
- cat.setName("hamster");
- given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat));
- given(this.owners.findById(TEST_OWNER_ID)).willReturn(new Owner());
- given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
-
- }
-
- @Test
- void testInitCreationForm() throws Exception {
- mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID)).andExpect(status().isOk())
- .andExpect(view().name("pets/createOrUpdatePetForm")).andExpect(model().attributeExists("pet"));
- }
-
- @Test
- void testProcessCreationFormSuccess() throws Exception {
- mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty")
- .param("type", "hamster").param("birthDate", "2015-02-12")).andExpect(status().is3xxRedirection())
- .andExpect(view().name("redirect:/owners/{ownerId}"));
- }
-
- @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"));
- }
-
- @Test
- void testInitUpdateForm() throws Exception {
- mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
- .andExpect(status().isOk()).andExpect(model().attributeExists("pet"))
- .andExpect(view().name("pets/createOrUpdatePetForm"));
- }
-
- @Test
- void testProcessUpdateFormSuccess() throws Exception {
- mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty")
- .param("type", "hamster").param("birthDate", "2015-02-12")).andExpect(status().is3xxRedirection())
- .andExpect(view().name("redirect:/owners/{ownerId}"));
- }
-
- @Test
- void testProcessUpdateFormHasErrors() throws Exception {
- mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty")
- .param("birthDate", "2015/02/12")).andExpect(model().attributeHasNoErrors("owner"))
- .andExpect(model().attributeHasErrors("pet")).andExpect(status().isOk())
- .andExpect(view().name("pets/createOrUpdatePetForm"));
- }
-
-}
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 a7f3d9d24..6088c8ae0 100644
--- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java
@@ -25,15 +25,15 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
-import org.springframework.samples.petclinic.owner.Owner;
-import org.springframework.samples.petclinic.owner.OwnerRepository;
-import org.springframework.samples.petclinic.owner.Pet;
-import org.springframework.samples.petclinic.owner.PetRepository;
-import org.springframework.samples.petclinic.owner.PetType;
-import org.springframework.samples.petclinic.vet.Vet;
-import org.springframework.samples.petclinic.vet.VetRepository;
-import org.springframework.samples.petclinic.visit.Visit;
-import org.springframework.samples.petclinic.visit.VisitRepository;
+import org.springframework.samples.petclinic.model.Owner;
+import org.springframework.samples.petclinic.repository.OwnerRepository;
+import org.springframework.samples.petclinic.model.Pet;
+import org.springframework.samples.petclinic.repository.PetRepository;
+import org.springframework.samples.petclinic.model.PetType;
+import org.springframework.samples.petclinic.model.Vet;
+import org.springframework.samples.petclinic.repository.VetRepository;
+import org.springframework.samples.petclinic.model.Visit;
+import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -112,10 +112,10 @@ class ClinicServiceTests {
owner.setCity("Wollongong");
owner.setTelephone("4444444444");
this.owners.save(owner);
- assertThat(owner.getId().longValue()).isNotEqualTo(0);
+ assertThat(owner.getId().longValue()).isNotZero();
owners = this.owners.findByLastName("Schultz");
- assertThat(owners.size()).isEqualTo(found + 1);
+ assertThat(owners).hasSize(found + 1);
}
@Test
@@ -176,7 +176,7 @@ class ClinicServiceTests {
@Test
@Transactional
- void shouldUpdatePetName() throws Exception {
+ void shouldUpdatePetName() {
Pet pet7 = this.pets.findById(7);
String oldName = pet7.getName();
@@ -216,7 +216,7 @@ class ClinicServiceTests {
}
@Test
- void shouldFindVisitsByPetId() throws Exception {
+ void shouldFindVisitsByPetId() {
Collection visits = this.visits.findByPetId(7);
assertThat(visits).hasSize(2);
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
diff --git a/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java
new file mode 100644
index 000000000..0857538ab
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/service/OwnerServiceTest.java
@@ -0,0 +1,231 @@
+package org.springframework.samples.petclinic.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+import org.springframework.context.annotation.ComponentScan;
+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.model.Owner;
+import org.springframework.samples.petclinic.model.Pet;
+import org.springframework.samples.petclinic.model.PetType;
+import org.springframework.samples.petclinic.repository.OwnerRepository;
+import org.springframework.samples.petclinic.repository.PetRepository;
+import org.springframework.samples.petclinic.repository.PetTypeRepository;
+import org.springframework.samples.petclinic.repository.VisitRepository;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Slf4j
+@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
+class OwnerServiceTest {
+
+ private final static Integer OWNER_ID = 11;
+
+ private final static String OWNER_FIRST_NAME = "Sam";
+
+ private final static String OWNER_LAST_NAME = "Schultz";
+
+ private final static String OWNER_ADDRESS = "4, Evans Street";
+
+ private final static String OWNER_CITY = "Wollongong";
+
+ private final static String OWNER_PHONE = "1234567890";
+
+ private final static Integer PET_ID = 11;
+
+ private final static String PET_NAME = "bowser";
+
+ private final static String PET_BIRTH_DATE = "2020-07-11";
+
+ @Autowired
+ private OwnerRepository ownerRepository;
+
+ @Autowired
+ private PetRepository petRepository;
+
+ @Autowired
+ private PetTypeRepository petTypeRepository;
+
+ @Autowired
+ private VisitRepository visitRepository;
+
+ private PetService petService;
+
+ private OwnerService ownerService;
+
+ private static Owner owner;
+
+ private static OwnerDTO ownerDTO;
+
+ private static Pet pet;
+
+ private static PetDTO petDTO;
+
+ @BeforeEach
+ void beforeEach() {
+ petService = new PetService(petRepository, petTypeRepository, visitRepository);
+ ownerService = new OwnerService(ownerRepository, petRepository, petTypeRepository, visitRepository);
+ PetTypeService petTypeService = new PetTypeService(petTypeRepository);
+ Collection petTypeDTOS = petService.findPetTypes();
+ PetTypeDTO petTypeDTO = petTypeDTOS.stream().findFirst().get();
+ PetType petType = petTypeService.dtoToEntity(petTypeDTO);
+ pet = new Pet();
+ pet.setId(PET_ID);
+ pet.setName(PET_NAME);
+ pet.setType(petType);
+ pet.setBirthDate(LocalDate.parse(PET_BIRTH_DATE));
+ petDTO = new PetDTO();
+ petDTO.setId(PET_ID);
+ petDTO.setName(PET_NAME);
+ petDTO.setType(petTypeDTO);
+ petDTO.setBirthDate(LocalDate.parse(PET_BIRTH_DATE));
+
+ owner = new Owner();
+ owner.setId(OWNER_ID);
+ owner.setFirstName(OWNER_FIRST_NAME);
+ owner.setLastName(OWNER_LAST_NAME);
+ owner.setAddress(OWNER_ADDRESS);
+ owner.setCity(OWNER_CITY);
+ owner.setTelephone(OWNER_PHONE);
+ owner.addPet(pet);
+ ownerDTO = new OwnerDTO();
+ ownerDTO.setId(OWNER_ID);
+ ownerDTO.setFirstName(OWNER_FIRST_NAME);
+ ownerDTO.setLastName(OWNER_LAST_NAME);
+ ownerDTO.setAddress(OWNER_ADDRESS);
+ ownerDTO.setCity(OWNER_CITY);
+ ownerDTO.setTelephone(OWNER_PHONE);
+ ownerDTO.addPet(petDTO);
+ }
+
+ @Test
+ @Tag("dtoToEntity")
+ @DisplayName("Verify the convertion from DTO to Entity")
+ void dtoToEntity() {
+ Owner found = ownerService.dtoToEntity(ownerDTO);
+
+ assertThat(found.getId()).isEqualTo(owner.getId());
+ assertThat(found.getFirstName()).isEqualTo(owner.getFirstName());
+ assertThat(found.getLastName()).isEqualTo(owner.getLastName());
+ assertThat(found.getAddress()).isEqualTo(owner.getAddress());
+ assertThat(found.getCity()).isEqualTo(owner.getCity());
+ assertThat(found.getTelephone()).isEqualTo(owner.getTelephone());
+
+ assertThat(found.getPets().size()).isEqualTo(owner.getPets().size());
+
+ for (Pet pet : found.getPets()) {
+ assertThat(owner.getPets()).extracting("id").contains(pet.getId());
+ }
+
+ }
+
+ @Test
+ @Tag("entityToDTO")
+ @DisplayName("Verify the convertion from Entity to DTO")
+ void entityToDTO() {
+ OwnerDTO found = ownerService.entityToDTO(owner);
+
+ assertThat(found.getId()).isEqualTo(ownerDTO.getId());
+ assertThat(found.getFirstName()).isEqualTo(ownerDTO.getFirstName());
+ assertThat(found.getLastName()).isEqualTo(ownerDTO.getLastName());
+ assertThat(found.getAddress()).isEqualTo(ownerDTO.getAddress());
+ assertThat(found.getCity()).isEqualTo(ownerDTO.getCity());
+ assertThat(found.getTelephone()).isEqualTo(ownerDTO.getTelephone());
+ assertThat(found.getPets().size()).isEqualTo(ownerDTO.getPets().size());
+
+ for (PetDTO petDTO : found.getPets()) {
+ assertThat(ownerDTO.getPets()).extracting("id").contains(petDTO.getId());
+ }
+ }
+
+ @Test
+ @Tag("dtosToEntities")
+ @DisplayName("Verify the convertion from DTOs list to Entities list")
+ void dtosToEntities() {
+ List ownerDTOS = ownerService.findAll();
+ List expected = new ArrayList<>();
+ ownerDTOS.forEach(dto -> expected.add(ownerService.dtoToEntity(dto)));
+
+ List found = ownerService.dtosToEntities(ownerDTOS);
+
+ assertThat(found).hasSameSizeAs(expected).containsAll(expected);
+ }
+
+ @Test
+ @Tag("entitiesToDTOS")
+ @DisplayName("Verify the convertion from Entities list to DTOs list")
+ void entitiesToDTOS() {
+ List expected = ownerService.findAll();
+ List owners = new ArrayList<>();
+ expected.forEach(dto -> owners.add(ownerService.dtoToEntity(dto)));
+
+ List found = ownerService.entitiesToDTOS(owners);
+
+ assertThat(found).hasSameSizeAs(expected).containsAll(expected);
+ }
+
+ @Test
+ @Tag("findById")
+ @DisplayName("Verify that we get OwnerDTO by his ID")
+ void findById() {
+ List allDTO = ownerService.findAll();
+ OwnerDTO expected = allDTO.get(2);
+
+ assertThat(ownerService.findById(expected.getId())).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("findByLastName")
+ @DisplayName("Verify that we get OwnerDTO by his LastName")
+ void findByLastName() {
+ OwnerDTO expected = ownerService.findById(1);
+
+ Optional found = ownerService.findByLastName(expected.getLastName()).stream().findFirst();
+
+ found.ifPresent(dto -> assertThat(dto).isEqualToComparingFieldByField(expected));
+ }
+
+ @Test
+ @Tag("findAll")
+ @DisplayName("Verify that the OwnerDTO list contain all previous elements and the new saved one")
+ void findAll() {
+ List expected = ownerService.findAll();
+
+ assertThat(expected).doesNotContain(ownerDTO);
+ ownerService.save(ownerDTO);
+
+ List found = ownerService.findAll();
+
+ assertThat(found).hasSize(expected.size() + 1)
+ .usingElementComparatorOnFields("lastName", "firstName", "address", "city", "telephone")
+ .contains(ownerDTO).containsAnyElementsOf(expected);
+
+ }
+
+ @Test
+ @Tag("save")
+ @DisplayName("Verify that all OwnerDTO list contain the new saved one")
+ void save() {
+ assertThat(ownerService.findAll()).doesNotContain(ownerDTO);
+
+ ownerService.save(ownerDTO);
+ List found = ownerService.findAll();
+
+ assertThat(found).usingElementComparatorOnFields("lastName", "firstName", "address", "city", "telephone")
+ .contains(ownerDTO);
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/service/PetServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/PetServiceTest.java
new file mode 100644
index 000000000..1eab22b6f
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/service/PetServiceTest.java
@@ -0,0 +1,176 @@
+package org.springframework.samples.petclinic.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+import org.springframework.context.annotation.ComponentScan;
+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.model.Owner;
+import org.springframework.samples.petclinic.model.Pet;
+import org.springframework.samples.petclinic.model.PetType;
+import org.springframework.samples.petclinic.repository.PetRepository;
+import org.springframework.samples.petclinic.repository.PetTypeRepository;
+import org.springframework.samples.petclinic.repository.VetRepository;
+import org.springframework.samples.petclinic.repository.VisitRepository;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDate;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Slf4j
+@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
+class PetServiceTest {
+
+ private final static Integer OWNER_ID = 5;
+
+ private final static Integer PET_ID = 14;
+
+ private final static String PET_NAME = "bowser";
+
+ private final static String PET_BIRTH_DATE = "2020-07-11";
+
+ @Autowired
+ private OwnerService ownerService;
+
+ @Autowired
+ private PetRepository petRepository;
+
+ @Autowired
+ private PetTypeRepository petTypeRepository;
+
+ @Autowired
+ private VisitRepository visitRepository;
+
+ private PetService petService;
+
+ private static Owner owner;
+
+ private static Pet pet;
+
+ private static PetDTO petDTO;
+
+ @BeforeEach
+ void beforeEach() {
+ this.petService = new PetService(petRepository, petTypeRepository, visitRepository);
+
+ PetTypeService petTypeService = new PetTypeService(petTypeRepository);
+ Collection petTypeDTOS = petService.findPetTypes();
+ PetTypeDTO petTypeDTO = petTypeDTOS.stream().findFirst().get();
+ PetType petType = petTypeService.dtoToEntity(petTypeDTO);
+ pet = new Pet();
+ pet.setId(PET_ID);
+ pet.setName(PET_NAME);
+ pet.setType(petType);
+ pet.setBirthDate(LocalDate.parse(PET_BIRTH_DATE));
+ petDTO = new PetDTO();
+ petDTO.setId(PET_ID);
+ petDTO.setName(PET_NAME);
+ petDTO.setType(petTypeDTO);
+ petDTO.setBirthDate(LocalDate.parse(PET_BIRTH_DATE));
+
+ OwnerDTO ownerDTO = ownerService.findById(OWNER_ID);
+ ownerDTO.addPet(petDTO);
+
+ pet.setOwner(ownerService.dtoToEntity(ownerDTO));
+ petDTO.setOwner(ownerDTO);
+ }
+
+ @Test
+ @Tag("dtoToEntity")
+ @DisplayName("Verify the convertion from DTO to Entity")
+ void dtoToEntity() {
+ Pet found = petService.dtoToEntity(petDTO);
+
+ assertThat(found.getId()).isEqualTo(pet.getId());
+ assertThat(found.getName()).isEqualTo(pet.getName());
+ assertThat(found.getBirthDate()).isEqualTo(pet.getBirthDate());
+ assertThat(found.getType()).isEqualTo(pet.getType());
+ assertThat(found.getOwner().getId()).isEqualTo(pet.getOwner().getId());
+ assertThat(found.getVisits()).isEqualTo(pet.getVisits());
+ }
+
+ @Test
+ @Tag("entityToDTO")
+ @DisplayName("Verify the convertion from Entity to DTO")
+ void entityToDTO() {
+ PetDTO found = petService.entityToDTO(pet);
+
+ assertThat(found.getId()).isEqualTo(petDTO.getId());
+ assertThat(found.getName()).isEqualTo(petDTO.getName());
+ assertThat(found.getBirthDate()).isEqualTo(petDTO.getBirthDate());
+ assertThat(found.getType()).isEqualTo(petDTO.getType());
+ assertThat(found.getOwner().getId()).isEqualTo(petDTO.getOwner().getId());
+ assertThat(found.getVisits()).isEqualTo(petDTO.getVisits());
+ }
+
+ @Test
+ @Tag("dtosToEntities")
+ @DisplayName("Verify the convertion from DTOs list to Entities list")
+ void dtosToEntities() {
+ List expected = petRepository.findAll();
+ List allDTO = petService.findAll();
+
+ List found = petService.dtosToEntities(allDTO);
+
+ assertThat(found).hasSameSizeAs(expected).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("entitiesToDTOS")
+ @DisplayName("Verify the convertion from Entity to DTO")
+ void entitiesToDTOS() {
+ List allEntity = petRepository.findAll();
+ List expected = petService.findAll();
+
+ List found = petService.entitiesToDTOS(allEntity);
+
+ assertThat(found).hasSameSizeAs(expected).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("findById")
+ @DisplayName("Verify that we get PetDTO by his ID")
+ void findById() {
+ List allDTO = petService.findAll();
+ PetDTO expected = allDTO.get(2);
+
+ assertThat(petService.findById(expected.getId())).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("findAll")
+ @DisplayName("Verify that the PetDTO list contain all previous elements and the new saved one")
+ void findAll() {
+ List expected = petService.findAll();
+
+ assertThat(expected).doesNotContain(petDTO);
+ petService.save(petDTO);
+
+ List found = petService.findAll();
+
+ assertThat(found).hasSize(expected.size() + 1).contains(petDTO).containsAll(expected);
+
+ }
+
+ @Test
+ @Tag("save")
+ @DisplayName("Verify that all PetDTO list contain the new saved one")
+ void save() {
+ assertThat(petService.findAll()).doesNotContain(petDTO);
+
+ petService.save(petDTO);
+
+ assertThat(petService.findAll()).containsAnyOf(petDTO);
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java
new file mode 100644
index 000000000..f1c833820
--- /dev/null
+++ b/src/test/java/org/springframework/samples/petclinic/service/VetServiceTest.java
@@ -0,0 +1,142 @@
+package org.springframework.samples.petclinic.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.samples.petclinic.dto.VetDTO;
+import org.springframework.samples.petclinic.model.Vet;
+import org.springframework.samples.petclinic.repository.SpecialtyRepository;
+import org.springframework.samples.petclinic.repository.VetRepository;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Slf4j
+@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
+class VetServiceTest {
+
+ private final static Integer VET_ID = 11;
+
+ private final static String VET_FIRST_NAME = "Sam";
+
+ private final static String VET_LAST_NAME = "Schultz";
+
+ @Autowired
+ private VetRepository vetRepository;
+
+ @Autowired
+ private SpecialtyRepository specialtyRepository;
+
+ private VetService vetService;
+
+ private static Vet vet;
+
+ private static VetDTO vetDTO;
+
+ @BeforeEach
+ void beforeEach() {
+ vetService = new VetService(vetRepository, specialtyRepository);
+ vet = new Vet();
+ vet.setId(VET_ID);
+ vet.setFirstName(VET_FIRST_NAME);
+ vet.setLastName(VET_LAST_NAME);
+ vetDTO = new VetDTO();
+ vetDTO.setId(VET_ID);
+ vetDTO.setFirstName(VET_FIRST_NAME);
+ vetDTO.setLastName(VET_LAST_NAME);
+
+ }
+
+ @Test
+ @Tag("dtoToEntity")
+ @DisplayName("Verify the convertion from DTO to Entity")
+ void dtoToEntity() {
+ Vet found = vetService.dtoToEntity(vetDTO);
+
+ assertThat(found.getId()).isEqualTo(vet.getId());
+ assertThat(found.getFirstName()).isEqualTo(vet.getFirstName());
+ assertThat(found.getLastName()).isEqualTo(vet.getLastName());
+ }
+
+ @Test
+ @Tag("entityToDTO")
+ @DisplayName("Verify the convertion from Entity to DTO")
+ void entityToDTO() {
+ VetDTO found = vetService.entityToDTO(vet);
+
+ assertThat(found.getId()).isEqualTo(vetDTO.getId());
+ assertThat(found.getFirstName()).isEqualTo(vetDTO.getFirstName());
+ assertThat(found.getLastName()).isEqualTo(vetDTO.getLastName());
+ }
+
+ @Test
+ @DisplayName("Verify the convertion from DTOs list to Entities list")
+ @Tag("dtosToEntities")
+ void dtosToEntities() {
+ List vetDTOS = vetService.findAll();
+ List expected = new ArrayList<>();
+ vetDTOS.forEach(dto -> expected.add(vetService.dtoToEntity(dto)));
+
+ Collection found = vetService.dtosToEntities(vetDTOS);
+
+ assertThat(found).hasSameSizeAs(expected).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("entitiesToDTOS")
+ @DisplayName("Verify the convertion from Entities list to DTOs list")
+ void entitiesToDTOS() {
+ List expected = vetService.findAll();
+ List vets = new ArrayList<>();
+ expected.forEach(dto -> vets.add(vetService.dtoToEntity(dto)));
+
+ List found = vetService.entitiesToDTOS(vets);
+
+ assertThat(found).hasSameSizeAs(expected).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("findById")
+ @DisplayName("Verify that we get VetDTO by his ID")
+ void findById() {
+ List allDTO = vetService.findAll();
+ VetDTO expected = allDTO.get(2);
+
+ assertThat(vetService.findById(expected.getId())).isEqualTo(expected);
+ }
+
+ @Test
+ @Tag("findAll")
+ @DisplayName("Verify that the VetDTO list contain all previous elements and the new saved one")
+ void findAll() {
+ List expected = vetService.findAll();
+
+ assertThat(expected).doesNotContain(vetDTO);
+ vetService.save(vetDTO);
+
+ List found = vetService.findAll();
+
+ assertThat(found).contains(vetDTO).containsAll(expected);
+ }
+
+ @Test
+ @Tag("save")
+ @DisplayName("Verify that all VetDTO list contain the new saved one")
+ void save() {
+ assertThat(vetService.findAll()).doesNotContain(vetDTO);
+
+ vetService.save(vetDTO);
+
+ assertThat(vetService.findAll()).contains(vetDTO);
+ }
+
+}
diff --git a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/validator/ValidatorTests.java
similarity index 94%
rename from src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java
rename to src/test/java/org/springframework/samples/petclinic/validator/ValidatorTests.java
index 8d754900d..f22b9d6b4 100644
--- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/validator/ValidatorTests.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.samples.petclinic.model;
+package org.springframework.samples.petclinic.validator;
import java.util.Locale;
import java.util.Set;
@@ -24,6 +24,7 @@ import javax.validation.Validator;
import org.junit.jupiter.api.Test;
import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.samples.petclinic.model.Person;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import static org.assertj.core.api.Assertions.assertThat;