|
| 1 | +package br.com.developers.controller.user; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 5 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 7 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 8 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Order; |
| 11 | +import org.junit.jupiter.params.ParameterizedTest; |
| 12 | +import org.junit.jupiter.params.provider.ArgumentsSource; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.http.HttpStatus; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.test.context.ActiveProfiles; |
| 18 | +import org.springframework.test.web.servlet.MockMvc; |
| 19 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 20 | +import org.springframework.web.context.WebApplicationContext; |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import br.com.developers.constants.UserConstantsForTests; |
| 24 | +import br.com.developers.domain.repository.UserRepository; |
| 25 | +import br.com.developers.login.dto.LoginDTO; |
| 26 | +import br.com.developers.login.dto.RegisterDTO; |
| 27 | +import br.com.developers.login.http.request.AccessToken; |
| 28 | +import br.com.developers.provider.LoginDTOProviderTest; |
| 29 | +import br.com.developers.provider.RegisterDTOProviderTests; |
| 30 | + |
| 31 | +@SpringBootTest |
| 32 | +@ActiveProfiles("staging") |
| 33 | +public class UserControllerIntegrationTests implements UserConstantsForTests { |
| 34 | + |
| 35 | + private static final String PATH = "/api/user/"; |
| 36 | + private static final String PATH_LOGIN = "/api/auth/"; |
| 37 | + |
| 38 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private UserRepository userRepository; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private WebApplicationContext webApplicationContext; |
| 45 | + |
| 46 | + private MockMvc mockMvc; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() { |
| 50 | + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
| 51 | + } |
| 52 | + |
| 53 | + @Order(1) |
| 54 | + @ParameterizedTest |
| 55 | + @ArgumentsSource(RegisterDTOProviderTests.class) |
| 56 | + void shouldRegisterWithSucess(RegisterDTO registerData) throws Exception { |
| 57 | + this.mockMvc |
| 58 | + .perform(post(PATH).content(createJsonRegisterData(registerData)) |
| 59 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 60 | + .andExpect(status().isCreated()).andExpect(header().exists("Location")); |
| 61 | + } |
| 62 | + |
| 63 | + @Order(2) |
| 64 | + @ParameterizedTest |
| 65 | + @ArgumentsSource(RegisterDTOProviderTests.class) |
| 66 | + void shouldTryRegisterAndReturnEmailExistingException(RegisterDTO registerData) throws Exception { |
| 67 | + this.mockMvc |
| 68 | + .perform(post(PATH).content(createJsonRegisterData(registerData)) |
| 69 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 70 | + .andExpect(status().isConflict()) |
| 71 | + .andExpect(jsonPath("$.message", is("Failed -> Email is already in use!"))) |
| 72 | + .andExpect(jsonPath("$.status", is(HttpStatus.CONFLICT.value()))); |
| 73 | + } |
| 74 | + |
| 75 | + @Order(3) |
| 76 | + @ParameterizedTest |
| 77 | + @ArgumentsSource(LoginDTOProviderTest.class) |
| 78 | + void shouldTryLoginAndReturnUserNotFound(LoginDTO loginDTO) throws Exception { |
| 79 | + loginDTO.setEmail("teste@teste.com.br"); |
| 80 | + this.mockMvc |
| 81 | + .perform(post(PATH_LOGIN).content(createJsonLoginDTO(loginDTO)) |
| 82 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 83 | + .andExpect(jsonPath("$.message", is("User Not Found with -> email : teste@teste.com.br"))) |
| 84 | + .andExpect(jsonPath("status", is(HttpStatus.NOT_FOUND.value()))); |
| 85 | + } |
| 86 | + |
| 87 | + @Order(4) |
| 88 | + @ParameterizedTest |
| 89 | + @ArgumentsSource(RegisterDTOProviderTests.class) |
| 90 | + void shouldLoginAndUpdateUserWithSucess(RegisterDTO registerData) throws Exception { |
| 91 | + |
| 92 | + LoginDTO loginDTO = new LoginDTO(); |
| 93 | + loginDTO.setEmail(EMAIL); |
| 94 | + loginDTO.setPassword(PASSWORD); |
| 95 | + |
| 96 | + String response = this.mockMvc |
| 97 | + .perform(post(PATH_LOGIN).content(createJsonLoginDTO(loginDTO)) |
| 98 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 99 | + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); |
| 100 | + |
| 101 | + AccessToken token = MAPPER.convertValue(response, AccessToken.class); |
| 102 | + |
| 103 | + mockMvc |
| 104 | + .perform(put(PATH).contentType(MediaType.APPLICATION_JSON_VALUE) |
| 105 | + .content(createJsonRegisterData(registerData)) |
| 106 | + .header("Authorization", "Bearer " + token.getAccessToken())) |
| 107 | + .andExpect(status().isCreated()).andExpect(header().exists("Location"));; |
| 108 | + } |
| 109 | + |
| 110 | + private String createJsonRegisterData(RegisterDTO registerData) throws JsonProcessingException { |
| 111 | + return MAPPER.writeValueAsString(registerData); |
| 112 | + } |
| 113 | + |
| 114 | + private String createJsonLoginDTO(LoginDTO loginDTO) throws JsonProcessingException { |
| 115 | + return MAPPER.writeValueAsString(loginDTO); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments