start including security in tests

This commit is contained in:
paul-emmanuel.dos-sa 2020-11-24 13:09:37 +01:00
parent 55da71a36a
commit aebd441e8e
16 changed files with 188 additions and 15 deletions

View file

@ -67,7 +67,15 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
/**
* Save an {@link Owner} to the data store, either inserting or updating it.
* @param owner the {@link Owner} to save
* @return the {@link Owner} saved or updated
*/
Owner save(Owner owner);
/**
* Delete an {@link Owner} from the data store.
* @param owner the {@link Owner} to delete
* @return the {@link Owner} deleted
*/
Owner delete(Owner owner);
}

View file

@ -131,6 +131,13 @@ public class OwnerService implements BaseService<Owner, OwnerDTO> {
return entityToDTO(owner);
}
public OwnerDTO delete(OwnerDTO ownerDTO) {
Owner owner = dtoToEntity(ownerDTO);
owner = ownerRepository.delete(owner);
return entityToDTO(owner);
}
public List<OwnerDTO> findByLastName(String lastName) {
Collection<Owner> owners = ownerRepository.findByLastName(lastName);
return entitiesToDTOS(Lists.from(owners.iterator()));

View file

@ -5,6 +5,7 @@ import org.junit.jupiter.api.*;
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.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;
@ -13,6 +14,8 @@ 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.business.OwnerService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@ -38,6 +41,9 @@ class OwnerControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@Autowired
private OwnerService ownerService;
@ -63,6 +69,7 @@ class OwnerControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initCreationForm")
@DisplayName("Verify that the view for new Owner is initialised with new OwnerDTO")
void whenGetNewOwner_thenReturnCreationViewWithNewOwner() throws Exception {
@ -77,6 +84,7 @@ class OwnerControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
void givenNewOwner_whenPostNewOwner_thenSaveOwnerAndRedirectToOwnerView() throws Exception {
@ -92,6 +100,7 @@ class OwnerControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processFindForm")
@DisplayName("Verify that we get the right view and all Owners")
void whenGetFindOwner_thenReturnFindViewWithAllOwners() throws Exception {

View file

@ -27,9 +27,12 @@ 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.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.samples.petclinic.common.CommonAttribute;
import org.springframework.samples.petclinic.common.CommonEndPoint;
@ -42,6 +45,16 @@ import org.springframework.samples.petclinic.dto.PetTypeDTO;
import org.springframework.samples.petclinic.dto.VisitDTO;
import org.springframework.samples.petclinic.service.business.OwnerService;
import org.springframework.samples.petclinic.service.business.VisitService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.empty;
@ -62,6 +75,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Colin But
* @author Paul-Emmanuel DOS SANTOS FACAO
*/
@RunWith(SpringRunner.class)
@WebMvcTest(OwnerController.class)
class OwnerControllerTest extends WebSocketSender {
@ -90,6 +104,9 @@ class OwnerControllerTest extends WebSocketSender {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@MockBean
SimpMessagingTemplate simpMessagingTemplate;
@ -128,6 +145,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initCreationForm")
@DisplayName("Verify that we get the right creation view and the right attribute name")
void whenGetNewOwner_thenReturnCreationViewWithNewOwner() throws Exception {
@ -137,6 +155,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that call the right view with parameters when attempt to create Owner")
void givenNewOwner_whenPostNewOwner_thenSaveOwnerAndRedirectToOwnerView() throws Exception {
@ -151,6 +170,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Owner creation form when Owner has no firstName")
void givenNewOwnerWithoutFirstName_whenPostNewOwner_thenRedirectToOwnerUpdateView() throws Exception {
@ -164,6 +184,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Owner creation form when Owner has no lastName")
void givenNewOwnerWithoutLastName_whenPostNewOwner_thenRedirectToOwnerUpdateView() throws Exception {
@ -177,6 +198,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Owner creation form when Owner has no address")
void givenNewOwnerWithoutAddress_whenPostNewOwner_thenRedirectToOwnerUpdateView() throws Exception {
@ -190,6 +212,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Owner creation form when Owner has no phone")
void givenNewOwnerWithoutPhone_whenPostNewOwner_thenRedirectToOwnerUpdateView() throws Exception {
@ -203,6 +226,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initFindForm")
@DisplayName("Verify that we get the right find view and the right attribute name")
void whenGetFindOwner_thenReturnFindViewWithNewOwner() throws Exception {
@ -212,6 +236,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processFindForm")
@DisplayName("Verify that we get the right view and all Owners list")
void whenGetFindOwner_thenReturnFindViewWithAllOwners() throws Exception {
@ -222,6 +247,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processFindForm")
@DisplayName("Verify that we get the right view and the Owner with specified firstName")
void givenOwnerLastName_whenGetFindOwner_thenReturnViewWithRightOwner() throws Exception {
@ -232,6 +258,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processFindForm")
@DisplayName("Verify that we get empty view and errors with specified wrong firstName")
void givenWrongOwnerLastName_whenGetFindOwner_thenReturnViewWithoutOwner() throws Exception {
@ -244,6 +271,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initUpdateOwnerForm")
@DisplayName("Verify that we get the right update view and the right Owner")
void whenGetUpdateOwner_thenReturnUpdateViewWithRightOwner() throws Exception {
@ -263,6 +291,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateOwnerForm")
@DisplayName("Verify that call the right view with parameters when attempt to update Owner")
void givenUpdatedOwner_whenPostOwner_thenSaveOwnerAndRedirectToOwnerView() throws Exception {
@ -275,6 +304,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateOwnerForm")
@DisplayName("Verify that we return to update view if the Owner firsName is wrong")
void givenUpdatedOwnerWithoutFirstName_whenPostOwner_thenRedirectToUpdateOwnerView() throws Exception {
@ -288,6 +318,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateOwnerForm")
@DisplayName("Verify that we return to update view if the Owner lastName is wrong")
void givenUpdatedOwnerWithoutLastName_whenPostOwner_thenRedirectToUpdateOwnerView() throws Exception {
@ -301,6 +332,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateOwnerForm")
@DisplayName("Verify that we return to update view if the Owner address is wrong")
void givenUpdatedOwnerWithoutAddress_whenPostOwner_thenRedirectToUpdateOwnerView() throws Exception {
@ -314,6 +346,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateOwnerForm")
@DisplayName("Verify that we return to update view if the Owner phone is wrong")
void givenUpdatedOwnerWithoutPhone_whenPostOwner_thenRedirectToUpdateOwnerView() throws Exception {
@ -327,6 +360,7 @@ class OwnerControllerTest extends WebSocketSender {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateOwnerForm")
@DisplayName("Verify that we display view with right Owner")
void givenOwnerId_whenGetOwner_thenShowOwnerView() throws Exception {

View file

@ -5,6 +5,7 @@ import org.junit.jupiter.api.*;
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.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;
@ -13,6 +14,8 @@ import org.springframework.samples.petclinic.dto.PetDTO;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.service.business.OwnerService;
import org.springframework.samples.petclinic.service.business.PetService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@ -42,6 +45,9 @@ class PetControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@Autowired
private OwnerService ownerService;
@ -62,6 +68,7 @@ class PetControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initCreationForm")
@DisplayName("Verify that the view for new Pet is initialised with new PetDTO")
void givenOwnerId_whenGetNewPet_thenReturnCreationViewWithNewPet() throws Exception {
@ -78,6 +85,7 @@ class PetControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that save new Pet and display the view")
void givenNewPet_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
@ -94,6 +102,7 @@ class PetControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initUpdateForm")
@DisplayName("Verify that the view to update Pet is initialised with right Pet")
void givenPetId_whenGetUpdatePet_thenReturnUpdateViewWithPet() throws Exception {
@ -110,6 +119,7 @@ class PetControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateForm")
@DisplayName("Verify that Pet is updated and the right view is displayed")
void givenUpdatePet_whenPostUpdatePet_thenUpdatePetAndRedirectToOwnerView() throws Exception {

View file

@ -28,6 +28,7 @@ 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.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
@ -45,6 +46,9 @@ import org.springframework.samples.petclinic.formatter.PetTypeFormatter;
import org.springframework.samples.petclinic.service.business.OwnerService;
import org.springframework.samples.petclinic.service.business.PetService;
import org.springframework.samples.petclinic.service.business.PetTypeService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
/**
@ -55,6 +59,7 @@ import org.springframework.test.web.servlet.MockMvc;
*/
@WebMvcTest(value = PetController.class,
includeFilters = @ComponentScan.Filter(value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE))
@RunWith(SpringRunner.class)
class PetControllerTest {
private static final int TEST_OWNER_ID = 1;
@ -64,6 +69,9 @@ class PetControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@MockBean
SimpMessagingTemplate simpMessagingTemplate;
@ -88,6 +96,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initCreationForm")
@DisplayName("Verify that Pet creation form is initialized")
void givenOwnerId_whenAskToCreatePet_thenDisplayCreationViewWithRightPet() throws Exception {
@ -97,6 +106,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that call the right view with parameters when attempt to create Pet")
void givenNewPet_whenPostNewPet_thenSavePetAndRedirectToOwnerView() throws Exception {
@ -107,6 +117,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Pet creation form when pet has no name")
void givenNewPetWithoutName_whenPostNewPet_thenRedirectToPetUpdate() throws Exception {
@ -121,6 +132,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Pet creation form when pet has no type")
void givenNewPetWithoutType_whenPostNewPet_thenRedirectToPetUpdate() throws Exception {
@ -135,6 +147,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Pet creation form when pet has wrong Owner ID")
void givenNewPetWithWrongOwner_whenPostNewPet_thenRedirectToPetUpdate() throws Exception {
@ -148,6 +161,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processCreationForm")
@DisplayName("Verify that return to Pet creation form when pet has no birth date")
void givenNewPetWithoutBirthDate_whenPostNewPet_thenRedirectToPetUpdate() throws Exception {
@ -163,6 +177,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initUpdateForm")
@DisplayName("Verify that Pet update form is initialized with the right Pet")
void givenPetId_whenGetUpdatePet_thenReturnUpdateViewWithPet() throws Exception {
@ -172,6 +187,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateForm")
void givenOwnerAndModifiedPet_whenAskToUpdatePet_thenUpdatePetAndDisplayOwnerView() throws Exception {
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID)
@ -181,6 +197,7 @@ class PetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processUpdateForm")
void testProcessUpdateFormHasErrors() throws Exception {
mockMvc.perform(post(CommonEndPoint.OWNERS_ID + CommonEndPoint.PETS_ID_EDIT, TEST_OWNER_ID, TEST_PET_ID)

View file

@ -10,12 +10,15 @@ import org.junit.jupiter.api.Test;
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.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.VetsDTO;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.service.business.VetService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@ -39,6 +42,9 @@ class VetControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@Autowired
private VetService vetService;
@ -53,6 +59,7 @@ class VetControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("showVetList")
@DisplayName("When asking vets get String containing Vets")
void whenGetVets_thenReturnStringOfVets() throws Exception {
@ -67,6 +74,7 @@ class VetControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("showResourcesVetList")
@DisplayName("When asking vets get Vets DTO object containing Vets")
void whenGetVets_thenReturnVetsDTO() throws Exception {

View file

@ -28,6 +28,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
@ -40,6 +41,9 @@ import org.springframework.samples.petclinic.dto.SpecialtyDTO;
import org.springframework.samples.petclinic.dto.VetDTO;
import org.springframework.samples.petclinic.dto.VetsDTO;
import org.springframework.samples.petclinic.service.business.VetService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@ -52,12 +56,16 @@ import java.util.Objects;
*
* @author Paul-Emmanuel DOS SANTOS FACAO
*/
@RunWith(SpringRunner.class)
@WebMvcTest(VetController.class)
class VetControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@MockBean
SimpMessagingTemplate simpMessagingTemplate;
@ -90,6 +98,7 @@ class VetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("showVetList")
@DisplayName("When asking vets get String containing Vets")
void whenGetVets_thenReturnStringOfVets() throws Exception {
@ -103,6 +112,7 @@ class VetControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("showResourcesVetList")
@DisplayName("When asking vets get Vets DTO object containing Vets")
void whenGetVets_thenReturnVetsDTO() throws Exception {

View file

@ -5,6 +5,7 @@ import org.junit.jupiter.api.*;
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.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;
@ -13,6 +14,8 @@ import org.springframework.samples.petclinic.dto.VisitDTO;
import org.springframework.samples.petclinic.model.business.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.business.PetService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@ -40,6 +43,9 @@ class VisitControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@Autowired
private VisitRepository visitRepository;
@ -68,6 +74,7 @@ class VisitControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initNewVisitForm")
@DisplayName("Verify that return form for new Visit with right Pet")
void givenPetId_whenGetNewVisit_thenReturnCreationViewWithNewVisit() throws Exception {
@ -88,6 +95,7 @@ class VisitControllerIntegrationTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processNewVisitForm")
@DisplayName("Verify that save Visit")
void givenVisitAndOwnerIDAndPetId_whenSaveVisit_thenSaveVisit() throws Exception {

View file

@ -42,6 +42,8 @@ import org.springframework.samples.petclinic.dto.VisitDTO;
import org.springframework.samples.petclinic.model.business.Visit;
import org.springframework.samples.petclinic.service.business.PetService;
import org.springframework.samples.petclinic.service.business.VisitService;
import org.springframework.samples.petclinic.service.common.UserDetailsServiceImpl;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@ -75,6 +77,9 @@ class VisitControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserDetailsServiceImpl userDetailsService;
@MockBean
SimpMessagingTemplate simpMessagingTemplate;
@ -111,6 +116,7 @@ class VisitControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("initNewVisitForm")
@DisplayName("Verify that return form for new Visit with right Pet")
void testInitNewVisitForm() throws Exception {
@ -132,6 +138,7 @@ class VisitControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processNewVisitForm")
@DisplayName("Verify that save Visit")
void givenVisitAndOwnerIDAndPetId_whenSaveVisit_thenSaveVisit() throws Exception {
@ -147,6 +154,7 @@ class VisitControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processNewVisitForm")
@DisplayName("Verify that return to update Visit view if Visit has no date")
void givenVisitAndOwnerIDAndPetIdAndVisitWithoutDate_whenSaveVisit_thenReturnToCreationView() throws Exception {
@ -158,6 +166,7 @@ class VisitControllerTest {
}
@Test
@WithMockUser(value = WebSecurityConfig.TEST_USER)
@Tag("processNewVisitForm")
@DisplayName("Verify that return to update Visit view if Visit has no description")
void givenVisitAndOwnerIDAndPetIdAndVisitWithoutDescription_whenSaveVisit_thenReturnToCreationView()

View file

@ -0,0 +1,40 @@
package org.springframework.samples.petclinic.controller;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* Web securuty configuration for controllers tests
*
* @author Paul-Emmanuel DOS SANTOS FACAO
*/
@TestConfiguration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public static final String TEST_USER = "petclinicuser";
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth.inMemoryAuthentication()
.passwordEncoder(encoder)
.withUser(TEST_USER)
.password(encoder.encode("secret"))
.roles("ROLE_USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/owners/**", "/pets/**", "/users/**", "/visits/**")
.authenticated()
.antMatchers("/**")
.permitAll()
.and()
.httpBasic();
}
}

View file

@ -22,6 +22,7 @@ import java.time.LocalDate;
import java.util.Collection;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
@ -35,6 +36,7 @@ import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.model.business.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
/**
@ -66,7 +68,8 @@ import org.springframework.transaction.annotation.Transactional;
* @author Michael Isvy
* @author Dave Syer
*/
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
@DataJpaTest
@RunWith(SpringRunner.class)
class ClinicServiceTest {
@Autowired

View file

@ -5,9 +5,9 @@ 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.junit.runner.RunWith;
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.common.CommonAttribute;
import org.springframework.samples.petclinic.dto.OwnerDTO;
import org.springframework.samples.petclinic.dto.PetDTO;
@ -22,7 +22,7 @@ import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.business.OwnerService;
import org.springframework.samples.petclinic.service.business.PetService;
import org.springframework.samples.petclinic.service.business.PetTypeService;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.LocalDate;
import java.util.ArrayList;
@ -33,7 +33,8 @@ import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@Slf4j
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
@DataJpaTest
@RunWith(SpringRunner.class)
class OwnerServiceTest {
private final static Integer OWNER_ID = 11;
@ -211,7 +212,7 @@ class OwnerServiceTest {
List<OwnerDTO> expected = ownerService.findAll();
assertThat(expected).doesNotContain(ownerDTO);
ownerService.save(ownerDTO);
OwnerDTO saved = ownerService.save(ownerDTO);
List<OwnerDTO> found = ownerService.findAll();
@ -219,6 +220,7 @@ class OwnerServiceTest {
.usingElementComparatorOnFields("lastName", "firstName", "address", "city", "telephone")
.contains(ownerDTO).containsAnyElementsOf(expected);
ownerService.delete(saved);
}
@Test
@ -230,9 +232,11 @@ class OwnerServiceTest {
OwnerDTO saved = ownerService.save(ownerDTO);
List<OwnerDTO> found = ownerService.findAll();
assertThat(saved).isEqualToIgnoringGivenFields(ownerDTO, CommonAttribute.OWNER_ID, CommonAttribute.OWNER_PETS);
assertThat(saved).isEqualToIgnoringGivenFields(ownerDTO, CommonAttribute.ID, CommonAttribute.OWNER_PETS);
assertThat(found).usingElementComparatorOnFields("lastName", "firstName", "address", "city", "telephone")
.contains(ownerDTO);
OwnerDTO deleted = ownerService.delete(saved);
}
}

View file

@ -1,26 +1,24 @@
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.junit.jupiter.api.*;
import org.junit.runner.RunWith;
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.business.Owner;
import org.springframework.samples.petclinic.model.business.Pet;
import org.springframework.samples.petclinic.model.business.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.samples.petclinic.service.business.OwnerService;
import org.springframework.samples.petclinic.service.business.PetService;
import org.springframework.samples.petclinic.service.business.PetTypeService;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.LocalDate;
import java.util.Collection;
@ -29,7 +27,8 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@Slf4j
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
@DataJpaTest
@RunWith(SpringRunner.class)
class PetServiceTest {
private final static Integer OWNER_ID = 5;
@ -45,6 +44,8 @@ class PetServiceTest {
private final static String PET_TYPE_NAME = "dinausor";
@Autowired
private OwnerRepository ownerRepository;
private OwnerService ownerService;
@Autowired
@ -67,6 +68,7 @@ class PetServiceTest {
@BeforeEach
void beforeEach() {
this.petService = new PetService(petRepository, petTypeRepository, visitRepository);
this.ownerService = new OwnerService(ownerRepository, petRepository, petTypeRepository, visitRepository);
PetTypeService petTypeService = new PetTypeService(petTypeRepository);
Collection<PetTypeDTO> petTypeDTOS = petService.findPetTypes();

View file

@ -5,6 +5,7 @@ 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.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
@ -14,6 +15,7 @@ import org.springframework.samples.petclinic.repository.SpecialtyRepository;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.service.business.VetService;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Collection;
@ -22,7 +24,9 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@Slf4j
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
// @DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
@DataJpaTest
@RunWith(SpringRunner.class)
class VetServiceTest {
private final static Integer VET_ID = 11;

View file

@ -52,7 +52,7 @@ class ValidatorTest {
Validator validator = createValidator();
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
assertThat(constraintViolations).hasSize(1);
assertThat(constraintViolations).hasSize(2);
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
assertThat(violation.getMessage()).isEqualTo("must not be empty");