|
| 1 | +package com.sopromadze.blogapi.controllerTest.todoController; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.sopromadze.blogapi.configurationSecurity.SpringSecurityTestConfig; |
| 5 | +import com.sopromadze.blogapi.model.Todo; |
| 6 | +import com.sopromadze.blogapi.service.TodoService; |
| 7 | +import lombok.extern.java.Log; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | +import org.springframework.security.test.context.support.WithMockUser; |
| 15 | +import org.springframework.test.web.servlet.MockMvc; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | + |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | + |
| 22 | +@Log |
| 23 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {SpringSecurityTestConfig.class}) |
| 24 | +@AutoConfigureMockMvc |
| 25 | +public class AddTodo { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private MockMvc mockMvc; |
| 29 | + @Autowired |
| 30 | + private ObjectMapper objectMapper; |
| 31 | + @MockBean |
| 32 | + private TodoService todoService; |
| 33 | + |
| 34 | + Todo todo; |
| 35 | + @BeforeEach |
| 36 | + void init(){ |
| 37 | + todo = new Todo(); |
| 38 | + todo.setId(1L); |
| 39 | + todo.setTitle("Todo para luego"); |
| 40 | + todo.setCreatedAt(Instant.now()); |
| 41 | + todo.setUpdatedAt(Instant.now()); |
| 42 | + } |
| 43 | + //Test: Comprobar que devuelve 201 |
| 44 | + //Entrada: post("/api/todos") |
| 45 | + //Salida esperada: El test se realiza con exito y devuelve 201 |
| 46 | + @Test |
| 47 | + @WithMockUser(authorities = {"ROLE_USER"}) |
| 48 | + void addTodo_success() throws Exception{ |
| 49 | + |
| 50 | + mockMvc.perform(post("/api/todos") |
| 51 | + .contentType("application/json") |
| 52 | + .content(objectMapper.writeValueAsString(todo))) |
| 53 | + .andExpect(status().isCreated()); |
| 54 | + } |
| 55 | + //Test: Comprobar que devuelve 401 |
| 56 | + //Entrada: post("/api/todos") |
| 57 | + //Salida esperada: El test se realiza con exito y devuelve 401 |
| 58 | + @Test |
| 59 | + void addTodo_return403() throws Exception{ |
| 60 | + |
| 61 | + mockMvc.perform(post("/api/todos") |
| 62 | + .contentType("application/json") |
| 63 | + .content(objectMapper.writeValueAsString(todo))) |
| 64 | + .andExpect(status().isUnauthorized()); |
| 65 | + } |
| 66 | + //Test: Comprobar que devuelve 400 |
| 67 | + //Entrada: post("/api/todos") |
| 68 | + //Salida esperada: El test se realiza con exito y devuelve 400 |
| 69 | + @Test |
| 70 | + @WithMockUser(authorities = {"ROLE_USER"}) |
| 71 | + void addTodo_return400() throws Exception{ |
| 72 | + |
| 73 | + mockMvc.perform(post("/api/todos") |
| 74 | + .contentType("application/json")) |
| 75 | + .andExpect(status().isBadRequest()); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments