Advertisement
Guest User

Untitled

a guest
May 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. package com.capgemini.jstk.springAplication.User;
  2.  
  3. import com.capgemini.jstk.springAplication.Aplication.SpringAplication;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.mockito.Mock;
  7. import org.mockito.Mockito;
  8. import static org.hamcrest.Matchers.is;
  9. import static org.mockito.Mockito.times;
  10. import static org.mockito.Mockito.when;
  11. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  12. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  13. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  14. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  15. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  16.  
  17. import org.junit.runner.RunWith;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.boot.test.context.SpringBootTest;
  20. import org.springframework.http.MediaType;
  21. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  22. import org.springframework.test.web.servlet.MockMvc;
  23. import org.springframework.test.web.servlet.ResultActions;
  24. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  25. import org.springframework.web.servlet.View;
  26. import com.fasterxml.jackson.databind.ObjectMapper;
  27.  
  28. import java.util.ArrayList;
  29. import java.util.HashSet;
  30. import java.util.List;
  31.  
  32. @RunWith(SpringJUnit4ClassRunner.class)
  33. @SpringBootTest(classes = SpringAplication.class)
  34. public class UserAPITest {
  35.  
  36. private MockMvc mockMvc;
  37.  
  38. private View view;
  39.  
  40. @Autowired
  41. private ObjectMapper objectMapper;
  42.  
  43. @Mock
  44. private UserService userService = Mockito.mock(UserService.class);
  45.  
  46.  
  47. @Before
  48. public void setup() {
  49. mockMvc = MockMvcBuilders.standaloneSetup(new UserAPI(userService)).setSingleView(view).build();
  50. }
  51.  
  52. @Test
  53. public void shouldReturnAllUsers() throws Exception {
  54.  
  55. //given
  56. List<UserDTO> users = new ArrayList<>();
  57.  
  58. UserDTO user1 = new UserDTO.UserDTOBuilder()
  59. .idUser(1L)
  60. .firstName("Arek")
  61. .surName("Nowak")
  62. .eMail("blabla@wp.pl")
  63. .password("qwerqwer11")
  64. .lifeMotto("Carpe diem")
  65. .myGames(new HashSet<>())
  66. .previousGames(new ArrayList<>())
  67. .availabilityTime(new ArrayList<>())
  68. .build();
  69.  
  70. users.add(user1);
  71.  
  72. //when
  73. when(userService.getAllUsers()).thenReturn(users);
  74. ResultActions resultActions = mockMvc.perform(get("/getUsers"));
  75.  
  76. // then
  77. Mockito.verify(userService, times(1)).getAllUsers();
  78. resultActions.andDo(print())//
  79. .andExpect(status().isOk())
  80. .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  81. .andExpect(jsonPath("$[0].firstName", is(user1.getFirstName())))
  82. .andExpect(jsonPath("$[0].surName", is(user1.getSurName())))
  83. .andExpect(jsonPath("$[0].email", is(user1.getEMail())));
  84. }
  85.  
  86. @Test
  87. public void shouldReturnUserByID() throws Exception {
  88.  
  89. //given
  90. List<UserDTO> users = new ArrayList<>();
  91.  
  92. UserDTO user2 = new UserDTO.UserDTOBuilder()
  93. .idUser(0L)
  94. .firstName("Marcin")
  95. .surName("Kowalski")
  96. .eMail("kawakawa@wp.pl")
  97. .password("123456789")
  98. .lifeMotto(":)")
  99. .myGames(new HashSet<>())
  100. .previousGames(new ArrayList<>())
  101. .availabilityTime(new ArrayList<>())
  102. .build();
  103.  
  104. users.add(user2);
  105.  
  106. //when
  107. when(userService.getUserById(0L)).thenReturn(user2);
  108. String json = objectMapper.writeValueAsString(user2);
  109.  
  110. //then
  111. mockMvc.perform(get("/getUser/0")
  112. .accept(MediaType.APPLICATION_JSON)
  113. .contentType(MediaType.APPLICATION_JSON).content(json))
  114. .andExpect(status().isOk())
  115. .andExpect(jsonPath("$.firstName", is("Marcin")))
  116. .andExpect(jsonPath("$.surName", is("Kowalski")));
  117. }
  118.  
  119. @Test
  120. public void shouldfindUsersByParameters() throws Exception {
  121.  
  122. //given
  123. List<UserDTO> users = new ArrayList<>();
  124.  
  125. UserDTO user = new UserDTO.UserDTOBuilder()
  126. .idUser(1L)
  127. .firstName("Arek")
  128. .surName("Nowak")
  129. .eMail("blabla@wp.pl")
  130. .password("qwerqwer11")
  131. .lifeMotto("Carpe diem")
  132. .myGames(new HashSet<>())
  133. .previousGames(new ArrayList<>())
  134. .availabilityTime(new ArrayList<>())
  135. .build();
  136.  
  137. users.add(user);
  138.  
  139. //when
  140. when(userService.findUserByParameters(user)).thenReturn(users);
  141. String json = objectMapper.writeValueAsString(user);
  142.  
  143. //then
  144. mockMvc.perform(post("/filterUser")
  145. .accept(MediaType.APPLICATION_JSON)
  146. .contentType(MediaType.APPLICATION_JSON).content(json))
  147. .andExpect(status().isOk())
  148. .andExpect(jsonPath("$[0].firstName", is("Arek")))
  149. .andExpect(jsonPath("$[0].surName", is("Nowak")));
  150. }
  151.  
  152. @Test
  153. public void shouldAddUser() throws Exception {
  154.  
  155. //given
  156. UserDTO user = new UserDTO.UserDTOBuilder()
  157. .idUser(0L)
  158. .firstName("Arek")
  159. .surName("Nowak")
  160. .eMail("blabla@wp.pl")
  161. .password("qwerqwer11")
  162. .lifeMotto("Carpe diem")
  163. .myGames(new HashSet<>())
  164. .previousGames(new ArrayList<>())
  165. .availabilityTime(new ArrayList<>())
  166. .build();
  167.  
  168.  
  169. //when
  170. when(userService.addUserProfile(user)).thenReturn(user);
  171. String json = objectMapper.writeValueAsString(user);
  172.  
  173. //then
  174. mockMvc.perform(post("/addUserProfile")
  175. .accept(MediaType.APPLICATION_JSON)
  176. .contentType(MediaType.APPLICATION_JSON).content(json))
  177. .andExpect(status().isOk())
  178. .andExpect(jsonPath("$.idUser", is(0)))
  179. .andExpect(jsonPath("$.firstName", is("Arek")));
  180.  
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement