mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 07:15:49 +00:00
Start VetControllerIntegrationTest
This commit is contained in:
parent
6b116509b1
commit
93cde700ad
6 changed files with 134 additions and 14 deletions
|
@ -27,6 +27,13 @@ public class VetsDTO {
|
|||
|
||||
private List<VetDTO> vets;
|
||||
|
||||
public VetsDTO() {
|
||||
}
|
||||
|
||||
public VetsDTO(List<VetDTO> vets) {
|
||||
this.vets = vets;
|
||||
}
|
||||
|
||||
public List<VetDTO> getVetList() {
|
||||
if (vets == null) {
|
||||
vets = new ArrayList<>();
|
||||
|
|
|
@ -12,9 +12,9 @@ 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.PetType;
|
||||
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.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
|
@ -27,12 +27,16 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
|||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
/**
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
class PetControllerIntegrationTest {
|
||||
|
||||
private static final int TEST_OWNER_ID = 5;
|
||||
private static final int TEST_PET_ID = 6;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
@ -40,6 +44,9 @@ class PetControllerIntegrationTest {
|
|||
@Autowired
|
||||
private OwnerService ownerService;
|
||||
|
||||
@Autowired
|
||||
private PetService petService;
|
||||
|
||||
@Autowired
|
||||
private PetRepository petRepository;
|
||||
|
||||
|
@ -63,7 +70,7 @@ class PetControllerIntegrationTest {
|
|||
@Test
|
||||
@Tag("initCreationForm")
|
||||
@DisplayName("Verify that the view for new Pet is initialised with new PetDTO")
|
||||
void testInitCreationForm() throws Exception {
|
||||
void givenOwnerId_whenGetNewPet_thenReturnCreationViewWithNewPet() throws Exception {
|
||||
|
||||
final MvcResult result = mockMvc
|
||||
.perform(get(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||
|
@ -78,7 +85,7 @@ class PetControllerIntegrationTest {
|
|||
@Test
|
||||
@Tag("processCreationForm")
|
||||
@DisplayName("Verify that save new Pet and display the view")
|
||||
void testProcessCreationFormSuccess() throws Exception {
|
||||
void givenNewPet_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
|
||||
|
||||
final MvcResult result = mockMvc
|
||||
.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||
|
@ -91,4 +98,37 @@ class PetControllerIntegrationTest {
|
|||
assertThat(found).contains(petDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Tag("initUpdateForm")
|
||||
@DisplayName("Verify that the view to update Pet is initialised with right Pet")
|
||||
void givenPetId_whenGetUpdatePet_thenReturnUpdateViewWithPet() throws Exception {
|
||||
PetDTO expected = petService.entityToDTO(petRepository.findById(TEST_PET_ID));
|
||||
|
||||
final MvcResult result = 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)).andReturn();
|
||||
|
||||
PetDTO petFound = (PetDTO) Objects.requireNonNull(result.getModelAndView()).getModel().get(CommonAttribute.PET);
|
||||
|
||||
assertThat(petFound).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Tag("processUpdateForm")
|
||||
@DisplayName("Verify that Pet is updated and the right view is displayed")
|
||||
void givenUpdatePet_whenPostUpdatePet_thenUpdatePetAndRedirectToOwnerView() throws Exception {
|
||||
PetDTO petExpected = petService.entityToDTO(petRepository.findById(TEST_PET_ID));
|
||||
OwnerDTO ownerExpected = ownerService.findById(petExpected.getOwner().getId());
|
||||
petExpected.setName("Nabucho");
|
||||
petExpected.setBirthDate(LocalDate.now());
|
||||
|
||||
final MvcResult result = mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, ownerExpected.getId(), petExpected.getId())
|
||||
.flashAttr(CommonAttribute.OWNER, ownerExpected).flashAttr(CommonAttribute.PET, petExpected))
|
||||
.andExpect(view().name(CommonView.OWNER_OWNERS_ID_R))
|
||||
.andReturn();
|
||||
|
||||
PetDTO petFound = petService.entityToDTO(petRepository.findById(TEST_PET_ID));
|
||||
|
||||
assertThat(petFound).isEqualTo(petExpected);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ class PetControllerTest {
|
|||
@Test
|
||||
@Tag("initCreationForm")
|
||||
@DisplayName("Verify that Pet creation form is initialized")
|
||||
void testInitCreationForm() throws Exception {
|
||||
void givenOwnerId_whenAskToCreatePet_thenDisplayCreationViewWithRightPet() 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));
|
||||
|
@ -94,7 +94,7 @@ class PetControllerTest {
|
|||
@Test
|
||||
@Tag("processCreationForm")
|
||||
@DisplayName("Verify that call the right view with parameters when attempt to create Pet")
|
||||
void givenPet_WhenPostPet_thenRedirectToOwnerForm() throws Exception {
|
||||
void givenNewPet_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
|
||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||
.param(CommonAttribute.PET_NAME, "Betty")
|
||||
.param(CommonAttribute.PET_TYPE, "hamster")
|
||||
|
@ -106,7 +106,7 @@ class PetControllerTest {
|
|||
@Test
|
||||
@Tag("processCreationForm")
|
||||
@DisplayName("Verify that return to Pet creation form when pet has no name")
|
||||
void givenPetWithNoName_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||
void givenNewPetWithoutName_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
|
||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||
.param(CommonAttribute.PET_TYPE, "hamster")
|
||||
.param(CommonAttribute.PET_BIRTH_DATE, "2015-02-12"))
|
||||
|
@ -121,7 +121,7 @@ class PetControllerTest {
|
|||
@Test
|
||||
@Tag("processCreationForm")
|
||||
@DisplayName("Verify that return to Pet creation form when pet has no type")
|
||||
void givenPetWithNoType_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||
void givenNewPetWithoutType_whenPostNewPet_thenSavePetAndRedirectToOwnerView() 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"))
|
||||
|
@ -136,7 +136,7 @@ class PetControllerTest {
|
|||
@Test
|
||||
@Tag("processCreationForm")
|
||||
@DisplayName("Verify that return to Pet creation form when pet has wrong Owner ID")
|
||||
void givenPetWithWrongOwnerId_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||
void givenNewPetWithWrongOwner_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
|
||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, 22)
|
||||
.param(CommonAttribute.PET_NAME, "Betty")
|
||||
.param(CommonAttribute.PET_TYPE, "hamster")
|
||||
|
@ -150,7 +150,7 @@ class PetControllerTest {
|
|||
@Test
|
||||
@Tag("processCreationForm")
|
||||
@DisplayName("Verify that return to Pet creation form when pet has no birth date")
|
||||
void givenPetWithNoBirthDate_WhenPostPet_thenReturnToCreationForm() throws Exception {
|
||||
void givenNewPetWithoutBirthDate_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
|
||||
|
||||
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_NEW, TEST_OWNER_ID)
|
||||
.param(CommonAttribute.PET_NAME, "Betty")
|
||||
|
@ -165,7 +165,8 @@ class PetControllerTest {
|
|||
|
||||
@Test
|
||||
@Tag("initUpdateForm")
|
||||
void testInitUpdateForm() throws Exception {
|
||||
@DisplayName("Verify that Pet update form is initialized with the right Pet")
|
||||
void givenPetId_whenGetUpdatePet_thenReturnUpdateViewWithPet() 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));
|
||||
|
@ -173,7 +174,7 @@ class PetControllerTest {
|
|||
|
||||
@Test
|
||||
@Tag("processUpdateForm")
|
||||
void testProcessUpdateFormSuccess() throws Exception {
|
||||
void givenOwnerAndModifiedPet_whenAskToUpdatePet_thenUpdatePetAndDisplayOwnerView() 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())
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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;
|
||||
import org.springframework.samples.petclinic.service.VetService;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
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;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
/**
|
||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class VetControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private VetService vetService;
|
||||
|
||||
@Autowired
|
||||
private VetRepository vetRepository;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Tag("showVetList")
|
||||
void testShowVetListHtml() throws Exception {
|
||||
Collection<Vet> vets = vetRepository.findAll();
|
||||
|
||||
VetsDTO expected = new VetsDTO(vetService.entitiesToDTOS(new ArrayList<>(vets)));
|
||||
|
||||
final MvcResult result = mockMvc.perform(get(CommonEndPoint.VETS_HTML))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attributeExists(CommonAttribute.VETS))
|
||||
.andExpect(view().name(CommonView.VET_VETS_LIST))
|
||||
.andReturn();
|
||||
|
||||
VetsDTO found = (VetsDTO) result.getModelAndView().getModel().get(CommonAttribute.VETS);
|
||||
|
||||
assertThat(found).isEqualToComparingFieldByField(expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -57,7 +57,7 @@ class VetControllerTest {
|
|||
private VetService vetService;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
void beforeEach() {
|
||||
VetDTO james = new VetDTO();
|
||||
james.setFirstName("James");
|
||||
james.setLastName("Carter");
|
||||
|
|
|
@ -16,14 +16,12 @@ 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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue