mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
add VetControllerIntegrationTest
This commit is contained in:
parent
93cde700ad
commit
6b931913ec
3 changed files with 33 additions and 13 deletions
|
@ -17,7 +17,6 @@ package org.springframework.samples.petclinic.dto;
|
||||||
|
|
||||||
import org.springframework.beans.support.MutableSortDefinition;
|
import org.springframework.beans.support.MutableSortDefinition;
|
||||||
import org.springframework.beans.support.PropertyComparator;
|
import org.springframework.beans.support.PropertyComparator;
|
||||||
import org.springframework.samples.petclinic.model.Specialty;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
package org.springframework.samples.petclinic.controller;
|
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 lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Tag;
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.modelmapper.internal.util.Lists;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.samples.petclinic.common.CommonAttribute;
|
import org.springframework.samples.petclinic.common.CommonAttribute;
|
||||||
import org.springframework.samples.petclinic.common.CommonEndPoint;
|
import org.springframework.samples.petclinic.common.CommonEndPoint;
|
||||||
import org.springframework.samples.petclinic.common.CommonView;
|
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.dto.VetsDTO;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
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.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
@ -45,22 +46,22 @@ public class VetControllerIntegrationTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private VetRepository vetRepository;
|
private VetRepository vetRepository;
|
||||||
|
|
||||||
|
private VetsDTO expected;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void beforeEach() {
|
void beforeEach() {
|
||||||
|
Collection<Vet> vets = vetRepository.findAll();
|
||||||
|
expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("showVetList")
|
@Tag("showVetList")
|
||||||
void testShowVetListHtml() throws Exception {
|
@DisplayName("When asking vets get String containing Vets")
|
||||||
Collection<Vet> vets = vetRepository.findAll();
|
void whenGetVets_thenReturnStringOfVets() throws Exception {
|
||||||
|
|
||||||
VetsDTO expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets)));
|
|
||||||
|
|
||||||
final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS_HTML))
|
final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS_HTML))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().is2xxSuccessful())
|
||||||
.andExpect(model().attributeExists(CommonAttribute.VETS))
|
.andExpect(model().attributeExists(CommonAttribute.VETS))
|
||||||
.andExpect(view().name(CommonView.VET_VETS_LIST))
|
.andExpect(view().name(CommonView.VET_VETS_LIST))
|
||||||
.andReturn();
|
.andReturn();
|
||||||
|
@ -70,5 +71,22 @@ public class VetControllerIntegrationTest {
|
||||||
assertThat(found).isEqualToComparingFieldByField(expected);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
|
|
||||||
import org.assertj.core.util.Lists;
|
import org.assertj.core.util.Lists;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Tag;
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -75,7 +76,8 @@ class VetControllerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("showVetList")
|
@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())
|
mockMvc.perform(get(CommonEndPoint.VETS_HTML)).andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists(CommonAttribute.VETS))
|
.andExpect(model().attributeExists(CommonAttribute.VETS))
|
||||||
.andExpect(view().name(CommonView.VET_VETS_LIST));
|
.andExpect(view().name(CommonView.VET_VETS_LIST));
|
||||||
|
@ -83,7 +85,8 @@ class VetControllerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("showResourcesVetList")
|
@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))
|
ResultActions actions = mockMvc.perform(get(CommonEndPoint.VETS).accept(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue