|
| 1 | +package com.sopromadze.blogapi.controllerTest.userController; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.sopromadze.blogapi.configurationSecurity.SpringSecurityTestConfig; |
| 5 | +import com.sopromadze.blogapi.model.user.User; |
| 6 | +import com.sopromadze.blogapi.payload.InfoRequest; |
| 7 | +import com.sopromadze.blogapi.payload.UserProfile; |
| 8 | +import com.sopromadze.blogapi.service.impl.UserServiceImpl; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.mockito.Mockito; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 17 | +import org.springframework.security.test.context.support.WithUserDetails; |
| 18 | +import org.springframework.test.web.servlet.MockMvc; |
| 19 | +import org.springframework.test.web.servlet.MvcResult; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
| 24 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 25 | + |
| 26 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {SpringSecurityTestConfig.class}) |
| 27 | +@AutoConfigureMockMvc |
| 28 | +public class SetAddress { |
| 29 | + @Autowired |
| 30 | + private MockMvc mockMvc; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private ObjectMapper objectMapper; |
| 34 | + |
| 35 | + @MockBean |
| 36 | + private UserServiceImpl userService; |
| 37 | + |
| 38 | + private UserProfile user; |
| 39 | + private InfoRequest infoRequest; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void init(){ |
| 43 | + user = new UserProfile(); |
| 44 | + user.setUsername("username"); |
| 45 | + user.setId(2L); |
| 46 | + user.setLastName("lastname"); |
| 47 | + user.setFirstName("user"); |
| 48 | + user.setEmail("user@user.com"); |
| 49 | + |
| 50 | + infoRequest = new InfoRequest(); |
| 51 | + infoRequest.setCity("Seville"); |
| 52 | + infoRequest.setStreet("Condes"); |
| 53 | + infoRequest.setZipcode("41013"); |
| 54 | + infoRequest.setSuite("Suite"); |
| 55 | + |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + /*Test:Check if Controller add user works |
| 60 | + * Input:User |
| 61 | + * Output:ResponseEntity<User> with status 200 ok |
| 62 | + * */ |
| 63 | + @Test |
| 64 | + @DisplayName("Add user success") |
| 65 | + @WithUserDetails("admin") |
| 66 | + void addUser_success() throws Exception{ |
| 67 | + when(userService.setOrUpdateInfo(Mockito.any(),Mockito.any())).thenReturn(user); |
| 68 | + |
| 69 | + MvcResult mvcResult = mockMvc.perform(put("/api/users/setOrUpdateInfo") |
| 70 | + .contentType("application/json") |
| 71 | + .content(objectMapper.writeValueAsString(infoRequest))) |
| 72 | + .andExpect(status().isOk()).andReturn(); |
| 73 | + |
| 74 | + User actual = objectMapper.readValue(mvcResult.getResponse().getContentAsString(),User.class); |
| 75 | + |
| 76 | + assertTrue(actual.getUsername().equals(user.getUsername())); |
| 77 | + } |
| 78 | + |
| 79 | + /*Test:Check if Controller add user throws unauthorized |
| 80 | + * Input:User |
| 81 | + * Output:ResponseEntity<User> with status 401 unauthorized |
| 82 | + * */ |
| 83 | + @Test |
| 84 | + @DisplayName("Add user unauthorized") |
| 85 | + void addUser_unauthorized() throws Exception{ |
| 86 | + when(userService.setOrUpdateInfo(Mockito.any(),Mockito.any())).thenReturn(user); |
| 87 | + |
| 88 | + MvcResult mvcResult = mockMvc.perform(put("/api/users/setOrUpdateInfo") |
| 89 | + .contentType("application/json") |
| 90 | + .content(objectMapper.writeValueAsString(infoRequest))) |
| 91 | + .andExpect(status().isUnauthorized()).andReturn(); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments