From 6b931913ec71c6bae437fcc19e958af6fcc65ab9 Mon Sep 17 00:00:00 2001 From: PEDSF Date: Thu, 29 Oct 2020 18:18:12 +0100 Subject: [PATCH] add VetControllerIntegrationTest --- .../samples/petclinic/dto/VetDTO.java | 1 - .../VetControllerIntegrationTest.java | 38 ++++++++++++++----- .../controller/VetControllerTest.java | 7 +++- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java index 0b1520b56..a4aa14636 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/VetDTO.java @@ -17,7 +17,6 @@ package org.springframework.samples.petclinic.dto; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; -import org.springframework.samples.petclinic.model.Specialty; import javax.xml.bind.annotation.XmlElement; import java.util.*; diff --git a/src/test/java/org/springframework/samples/petclinic/controller/VetControllerIntegrationTest.java b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerIntegrationTest.java index c32f4b954..2cdca1f48 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/VetControllerIntegrationTest.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerIntegrationTest.java @@ -1,17 +1,19 @@ package org.springframework.samples.petclinic.controller; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; 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.modelmapper.internal.util.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; 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.VetDTO; import org.springframework.samples.petclinic.dto.VetsDTO; import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.repository.VetRepository; @@ -19,10 +21,9 @@ import org.springframework.samples.petclinic.service.VetService; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -45,22 +46,22 @@ public class VetControllerIntegrationTest { @Autowired private VetRepository vetRepository; + private VetsDTO expected; @BeforeEach void beforeEach() { - + Collection vets = vetRepository.findAll(); + expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets))); } @Test @Tag("showVetList") - void testShowVetListHtml() throws Exception { - Collection vets = vetRepository.findAll(); - - VetsDTO expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets))); + @DisplayName("When asking vets get String containing Vets") + void whenGetVets_thenReturnStringOfVets() throws Exception { final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS_HTML)) - .andExpect(status().isOk()) + .andExpect(status().is2xxSuccessful()) .andExpect(model().attributeExists(CommonAttribute.VETS)) .andExpect(view().name(CommonView.VET_VETS_LIST)) .andReturn(); @@ -70,5 +71,22 @@ public class VetControllerIntegrationTest { assertThat(found).isEqualToComparingFieldByField(expected); } + @Test + @Tag("showResourcesVetList") + @DisplayName("When asking vets get Vets DTO object containing Vets") + void whenGetVets_thenReturnVetsDTO() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS)) + .andExpect(status().is2xxSuccessful()) + .andReturn(); + + String json = result.getResponse().getContentAsString(StandardCharsets.UTF_8); + + VetsDTO found = mapper.readValue(json, VetsDTO.class); + + assertThat(found).isEqualToComparingFieldByField(expected); + } } diff --git a/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTest.java b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTest.java index c29de530e..74ae5b20a 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/VetControllerTest.java @@ -26,6 +26,7 @@ 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.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -75,7 +76,8 @@ class VetControllerTest { @Test @Tag("showVetList") - void testShowVetListHtml() throws Exception { + @DisplayName("When asking vets get String containing Vets") + void whenGetVets_thenReturnStringOfVets() throws Exception { mockMvc.perform(get(CommonEndPoint.VETS_HTML)).andExpect(status().isOk()) .andExpect(model().attributeExists(CommonAttribute.VETS)) .andExpect(view().name(CommonView.VET_VETS_LIST)); @@ -83,7 +85,8 @@ class VetControllerTest { @Test @Tag("showResourcesVetList") - void testShowResourcesVetList() throws Exception { + @DisplayName("When asking vets get Vets DTO object containing Vets") + void whenGetVets_thenReturnVetsDTO() throws Exception { ResultActions actions = mockMvc.perform(get(CommonEndPoint.VETS).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk());