Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package com.smart.notes.smart_notes;
  2.  
  3.  
  4. import com.smart.notes.smart_notes.configuration.security.TokenBuilder;
  5. import com.smart.notes.smart_notes.entity.user.UserDTO;
  6. import com.smart.notes.smart_notes.entity.user.UserModel;
  7. import com.smart.notes.smart_notes.entity.user.properities.EmailDTO;
  8. import com.smart.notes.smart_notes.service.user.UserServiceImpl;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  13. import org.springframework.boot.test.context.SpringBootTest;
  14. import org.springframework.boot.test.mock.mockito.MockBean;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.security.core.userdetails.UserDetails;
  17. import org.springframework.test.context.junit4.SpringRunner;
  18. import org.springframework.test.web.servlet.MockMvc;
  19.  
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import java.util.stream.Collectors;
  23.  
  24. import static org.hamcrest.core.Is.is;
  25. import static org.mockito.Mockito.*;
  26. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  27. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  28. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  29.  
  30.  
  31. @RunWith(SpringRunner.class)
  32. @AutoConfigureMockMvc
  33. @SpringBootTest
  34. public class UserServiceTest {
  35.  
  36.     @Autowired
  37.     private MockMvc mvc;
  38.  
  39.     @MockBean
  40.     private UserServiceImpl userServiceImpl;
  41.  
  42.  
  43.     private final String TOKEN = TokenBuilder.createToken("mahumba", 10000);
  44.  
  45.     @Test
  46.     public void testFindUserByIdAndExpectOk() throws Exception {
  47.  
  48.         UserDTO userDTO = UserDTO.convertUserModelToUserDTO(getUsersList().get(0));
  49.         when(userServiceImpl.findUserById("id")).thenReturn(userDTO);
  50.         mvc.perform(get("/users/{id}", "id").header("Authorization", TOKEN))
  51.                 .andExpect(status().isOk())
  52.                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
  53.                 .andExpect(jsonPath("$.email", is(userDTO.getEmail())));
  54.         verify(userServiceImpl, times(1)).findUserById("id");
  55.         verifyNoMoreInteractions(userServiceImpl);
  56.     }
  57.  
  58.     @Test
  59.     public void testFindAllUsers() throws Exception {
  60.         when(userServiceImpl.findAllUsers())
  61.                 .thenReturn(getUsersList()
  62.                         .stream()
  63.                         .map(userModel -> UserDTO.convertUserModelToUserDTO(userModel))
  64.                         .collect(Collectors.toList()));
  65.         mvc.perform(get("/users").header("Authorization", TOKEN))
  66.                 .andExpect(status().isOk())
  67.                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
  68.         verify(userServiceImpl, times(1)).findAllUsers();
  69.         verifyNoMoreInteractions(userServiceImpl);
  70.     }
  71.  
  72.  
  73.     @Test
  74.     public void testFindUserIdByEmail() throws Exception {
  75.         EmailDTO emailDTO = new EmailDTO();
  76.         emailDTO.setEmail(getUsersList().get(0).getEmail());
  77.  
  78.         when(userServiceImpl.findUserIdByEmail(emailDTO)).thenReturn("id");
  79.  
  80.         mvc.perform(get("/users/id").header("Authorization", TOKEN))
  81.                 .andExpect(status().isOk());
  82.     }
  83.  
  84.  
  85.     @Test
  86.     public void testAddUser() throws Exception {
  87.         UserDTO userDTO = new UserDTO();
  88.         userDTO.setEmail("test@mail.com");
  89.         userDTO.setPassword("password");
  90.  
  91.         mvc.perform(post("/registration")
  92.                 .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content(ConvertJavaObjectToJson.asJsonString(userDTO)))
  93.                 .andExpect(status().isOk());
  94.         verify(userServiceImpl, times(1)).addUser(userDTO);
  95.         verifyNoMoreInteractions(userServiceImpl);
  96.     }
  97.  
  98.  
  99.     public List<UserModel> getUsersList() {
  100.         List<UserModel> userList = Arrays.asList(
  101.                 UserDTO.convertUserDTOToUser(new UserDTO("jankowalski@mail.com", "password")),
  102.                 UserDTO.convertUserDTOToUser(new UserDTO("jannowak@mail.com", "password"))
  103.         );
  104.         return userList;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement