Advertisement
VladNitu

MatchingControllerTest

Dec 19th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.47 KB | None | 0 0
  1. package com.example.micro.controllers;
  2.  
  3. import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
  4. import static org.hamcrest.Matchers.containsString;
  5. import static org.mockito.ArgumentMatchers.any;
  6. import static org.mockito.ArgumentMatchers.anyLong;
  7. import static org.mockito.ArgumentMatchers.anyString;
  8. import static org.mockito.Mockito.doNothing;
  9. import static org.mockito.Mockito.lenient;
  10. import static org.mockito.Mockito.never;
  11. import static org.mockito.Mockito.verify;
  12. import static org.mockito.Mockito.when;
  13. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  14. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  15. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  16. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  17.  
  18. import com.example.micro.domain.Matching;
  19. import com.example.micro.publishers.ActivityPublisher;
  20. import com.example.micro.publishers.NotificationPublisher;
  21. import com.example.micro.services.MatchingServiceImpl;
  22. import com.example.micro.utils.Pair;
  23. import com.example.micro.utils.TimeSlot;
  24. import com.fasterxml.jackson.databind.ObjectMapper;
  25.  
  26. import java.time.LocalDateTime;
  27. import java.util.List;
  28. import java.util.Optional;
  29.  
  30. import org.junit.jupiter.api.BeforeEach;
  31. import org.junit.jupiter.api.Test;
  32. import org.junit.jupiter.api.extension.ExtendWith;
  33. import org.mockito.Mock;
  34. import org.mockito.junit.jupiter.MockitoExtension;
  35. import org.springframework.http.MediaType;
  36. import org.springframework.test.web.servlet.MockMvc;
  37. import org.springframework.test.web.servlet.MvcResult;
  38. import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
  39. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  40.  
  41. @ExtendWith(MockitoExtension.class)
  42. public class MatchingControllerTest {
  43. MockMvc mockMvc;
  44. @Mock
  45. private MatchingServiceImpl matchingServiceImpl;
  46. @Mock
  47. private ActivityPublisher activityPublisher;
  48. @Mock
  49. private NotificationPublisher notificationPublisher;
  50. private final ObjectMapper objectMapper = new ObjectMapper();
  51.  
  52.  
  53. private MatchingController matchingController;
  54. private Matching matching;
  55. private String userId;
  56. private Long activityId;
  57. private List<TimeSlot> timeSlots;
  58.  
  59. @BeforeEach
  60. void setUp() {
  61. userId = "Vlad";
  62. activityId = 1L;
  63.  
  64. timeSlots = List.of(new TimeSlot(
  65. LocalDateTime.of(2003, 12, 1, 23, 15),
  66. LocalDateTime.of(2002, 11, 2, 15, 00)
  67. ));
  68. matching = new Matching("Vlad", 1L, "rower", false);
  69. this.matchingController = new MatchingController(matchingServiceImpl,
  70. activityPublisher,
  71. notificationPublisher);
  72. mockMvc = MockMvcBuilders
  73. .standaloneSetup(matchingController)
  74. .build();
  75. }
  76.  
  77. @Test
  78. public void getAvailableActivitiesTest() throws Exception {
  79. when(matchingServiceImpl.findActivitiesByUserId(userId)).thenReturn(List.of());
  80. when(activityPublisher.getTimeSlots(List.of())).thenReturn(List.of());
  81. // when(FunctionUtils.filterTimeSlots(List.of(), List.of())).thenReturn(List.of());
  82. when(activityPublisher.getAvailableActivities(userId, List.of())).thenReturn(List.of());
  83.  
  84. MvcResult result = mockMvc
  85. .perform(post("/getAvailableActivities/Vlad")
  86. .contentType(MediaType.APPLICATION_JSON)
  87. .content(objectMapper.writeValueAsString(List.of()))
  88. )
  89. .andExpect(status().isOk())
  90. .andReturn();
  91.  
  92. String content = result.getResponse().getContentAsString();
  93. assertThat(content).isEqualTo("[]"); // Empty list
  94. }
  95.  
  96. @Test
  97. public void chooseActivityFail1() throws Exception {
  98. when(activityPublisher.check(any(Matching.class))).thenReturn(false);
  99. MvcResult mvcResult = mockMvc
  100. .perform(post("/chooseActivity")
  101. .contentType(MediaType.APPLICATION_JSON)
  102. .content(objectMapper.writeValueAsString(matching))
  103. )
  104. .andExpect(status().isBadRequest())
  105. .andReturn();
  106.  
  107. assertThat(mvcResult.getResponse().getContentAsString()).isEmpty(); // no response received
  108. }
  109.  
  110. @Test
  111. public void chooseActivityFail2() throws Exception {
  112. when(activityPublisher.check(any(Matching.class))).thenReturn(true);
  113. when(matchingServiceImpl.findMatchingWithPendingFalse(anyString(), anyLong())).thenReturn(Optional.of(matching));
  114.  
  115. MvcResult mvcResult = mockMvc
  116. .perform(post("/chooseActivity")
  117. .contentType(MediaType.APPLICATION_JSON)
  118. .content(objectMapper.writeValueAsString(matching))
  119. )
  120. .andExpect(status().isBadRequest())
  121. .andReturn();
  122.  
  123. assertThat(mvcResult.getResponse().getContentAsString()).isEmpty(); // no response received
  124. }
  125.  
  126. @Test
  127. public void chooseActivity() throws Exception {
  128. Matching savedMatching = new Matching("Niq", 2L, "side", true);
  129.  
  130. when(activityPublisher.check(any(Matching.class))).thenReturn(true);
  131. when(matchingServiceImpl.findMatchingWithPendingFalse(anyString(), anyLong())).thenReturn(Optional.empty());
  132. lenient().when(matchingServiceImpl.save(any(Matching.class))).thenReturn(savedMatching);
  133. when(activityPublisher.getOwnerId(anyLong())).thenReturn("dummyString");
  134. doNothing().when(notificationPublisher).notifyUser(anyString(), anyString(), anyLong(), anyString(), anyString());
  135.  
  136.  
  137. MvcResult mvcResult = mockMvc
  138. .perform(post("/chooseActivity")
  139. .contentType(MediaType.APPLICATION_JSON)
  140. .content(objectMapper.writeValueAsString(matching))
  141. )
  142. .andDo(MockMvcResultHandlers.print())
  143. .andExpect(status().isOk())
  144. .andReturn();
  145.  
  146. String contentAsString = mvcResult.getResponse().getContentAsString();
  147. Matching obtained = objectMapper.readValue(contentAsString, Matching.class);
  148.  
  149. assertThat(savedMatching).isEqualTo(obtained);
  150. }
  151.  
  152. @Test
  153. public void getUserActivitiesTest() throws Exception {
  154.  
  155. when(matchingServiceImpl.findActivitiesByUserId("Vlad")).thenReturn(List.of(1L));
  156.  
  157. MvcResult mvcResult = mockMvc
  158. .perform(get("/getUserActivities/Vlad"))
  159. .andExpect(status().isOk())
  160. .andReturn();
  161.  
  162. //JSON String representation of List<Long> object.
  163. String contentAsString = mvcResult.getResponse().getContentAsString();
  164. assertThat(contentAsString).contains("1");
  165. }
  166.  
  167. @Test
  168. public void unenrollFail() throws Exception {
  169. // Empty matching found -> badRequest
  170. Pair<String, Long> expected = new Pair<String, Long>(userId, activityId);
  171. when(matchingServiceImpl.findMatchingWithPendingFalse(userId, activityId))
  172. .thenReturn(Optional.empty());
  173. MvcResult mvcResult = mockMvc
  174. .perform(post("/unenroll")
  175. .contentType(MediaType.APPLICATION_JSON)
  176. .content(objectMapper.writeValueAsString(expected)))
  177. .andExpect(status().isBadRequest())
  178. .andReturn();
  179.  
  180. assertThat(mvcResult.getResponse().getContentAsString()).isEmpty();
  181. }
  182.  
  183. @Test
  184. public void unenrollTest() throws Exception {
  185. Pair<String, Long> expected = new Pair<String, Long>(userId, activityId);
  186. when(matchingServiceImpl.findMatchingWithPendingFalse(userId, activityId))
  187. .thenReturn(Optional.of(matching));
  188.  
  189. MvcResult mvcResult = mockMvc
  190. .perform(post("/unenroll")
  191. .contentType(MediaType.APPLICATION_JSON)
  192. .content(objectMapper.writeValueAsString(expected)))
  193. .andExpect(status().isOk())
  194. .andReturn();
  195.  
  196. String contentAsString = mvcResult.getResponse().getContentAsString();
  197. Pair<String, Long> obtained = objectMapper.readValue(contentAsString, expected.getClass());
  198. assertThat(obtained.getFirst()).isEqualTo(expected.getFirst());
  199. }
  200.  
  201. @Test
  202. public void findAllTest() throws Exception {
  203. List<Matching> expected = List.of(matching);
  204. when(matchingServiceImpl.findAll()).thenReturn(expected);
  205. MvcResult mvcResult = mockMvc
  206. .perform(get("/findAll")
  207. .contentType(MediaType.APPLICATION_JSON)
  208. .content(objectMapper.writeValueAsString(expected))
  209. )
  210. .andExpect(status().isOk())
  211. .andReturn();
  212.  
  213. String contentAsString = mvcResult.getResponse().getContentAsString();
  214. List<Matching> obtained = objectMapper.readValue(contentAsString, List.class);
  215. assertThat(obtained.toString()).contains("Vlad", "1", "false");
  216. assertThat(obtained.size()).isEqualTo(1);
  217. }
  218.  
  219. @Test
  220. public void saveMatchingTest() throws Exception {
  221. Matching savedMatching = new Matching("Niq", 2L, "side", true);
  222. when(matchingServiceImpl.save(matching))
  223. .thenReturn(savedMatching);
  224.  
  225. MvcResult mvcResult = mockMvc
  226. .perform(post("/save")
  227. .contentType(MediaType.APPLICATION_JSON)
  228. .content(objectMapper.writeValueAsString(matching))
  229. )
  230. .andDo(MockMvcResultHandlers.print())
  231. .andExpect(status().isOk())
  232. .andExpect(content().string(containsString("Niq")))
  233. .andReturn();
  234.  
  235. String contentAsString = mvcResult.getResponse().getContentAsString();
  236. Matching obtained = objectMapper.readValue(contentAsString, Matching.class);
  237. assertThat(obtained).isEqualTo(savedMatching);
  238.  
  239. }
  240.  
  241. @Test
  242. public void decideMatchAcceptFails1() throws Exception {
  243.  
  244. String ownerId = "Vlad";
  245. lenient()
  246. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  247.  
  248. MvcResult mvcResult = mockMvc
  249. .perform(post("/decideMatchAccept/Vlad"))
  250. .andExpect(status().isBadRequest())
  251. .andReturn();
  252.  
  253. String contentAsString = mvcResult.getResponse().getContentAsString();
  254. assertThat(contentAsString).isEqualTo(""); // no response
  255.  
  256. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  257. }
  258.  
  259. @Test
  260. public void decideMatchAcceptPasses() throws Exception {
  261. String ownerId = "Radu";
  262. lenient()
  263. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  264. lenient()
  265. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(true);
  266.  
  267. when(matchingServiceImpl.save(matching)).thenReturn(matching);
  268.  
  269. doNothing().when(matchingServiceImpl).deleteById(anyString(), anyLong(), anyString());
  270. doNothing().when(activityPublisher).takeAvailableSpot(anyLong(), anyString());
  271. doNothing().when(notificationPublisher).notifyUser(anyString(), anyString(), anyLong(), anyString(), anyString());
  272.  
  273.  
  274. MvcResult mvcResult = mockMvc
  275. .perform(post("/decideMatchAccept/Vlad")
  276. .contentType(MediaType.APPLICATION_JSON)
  277. .content(objectMapper.writeValueAsString(matching)))
  278. .andDo(MockMvcResultHandlers.print())
  279. .andExpect(status().isOk())
  280. .andReturn();
  281.  
  282. String contentAsString = mvcResult.getResponse().getContentAsString();
  283. Matching obtained = objectMapper.readValue(contentAsString, Matching.class);
  284. assertThat(obtained).isEqualTo(matching);
  285. }
  286.  
  287. @Test
  288. public void decideMatchAcceptFailsOwnerEqualsSender() throws Exception {
  289. String ownerId = "Radu";
  290. lenient()
  291. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  292.  
  293. lenient()
  294. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(true);
  295.  
  296. MvcResult mvcResult = mockMvc
  297. .perform(post("/decideMatchAccept/Radu")
  298. .contentType(MediaType.APPLICATION_JSON)
  299. .content(objectMapper.writeValueAsString(matching)))
  300. .andExpect(status().isBadRequest())
  301. .andReturn();
  302.  
  303. String contentAsString = mvcResult.getResponse().getContentAsString();
  304. assertThat(contentAsString).isEqualTo(""); // no response
  305.  
  306. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  307. }
  308.  
  309. @Test
  310. public void decideMatchAcceptFailsCheckIdFalse() throws Exception {
  311. String ownerId = "Radu";
  312. lenient()
  313. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  314.  
  315. lenient()
  316. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(false);
  317.  
  318. MvcResult mvcResult = mockMvc
  319. .perform(post("/decideMatchAccept/Vlad")
  320. .contentType(MediaType.APPLICATION_JSON)
  321. .content(objectMapper.writeValueAsString(matching)))
  322. .andExpect(status().isBadRequest())
  323. .andReturn();
  324.  
  325. String contentAsString = mvcResult.getResponse().getContentAsString();
  326. assertThat(contentAsString).isEqualTo(""); // no response
  327.  
  328. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  329. }
  330.  
  331. @Test
  332. public void decideMatchAcceptBoth() throws Exception {
  333. String ownerId = "Radu";
  334. lenient()
  335. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  336.  
  337. lenient()
  338. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(false);
  339.  
  340. MvcResult mvcResult = mockMvc
  341. .perform(post("/decideMatchAccept/Radu")
  342. .contentType(MediaType.APPLICATION_JSON)
  343. .content(objectMapper.writeValueAsString(matching)))
  344.  
  345. .andExpect(status().isBadRequest())
  346. .andReturn();
  347.  
  348. String contentAsString = mvcResult.getResponse().getContentAsString();
  349. assertThat(contentAsString).isEqualTo(""); // no response
  350.  
  351. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  352. }
  353.  
  354. @Test
  355. public void decideMatchDeclineFailsOwnerEqualsSender() throws Exception {
  356. String ownerId = "Radu";
  357. lenient()
  358. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  359.  
  360. lenient()
  361. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(true);
  362.  
  363. MvcResult mvcResult = mockMvc
  364. .perform(post("/decideMatchDecline/Radu")
  365. .contentType(MediaType.APPLICATION_JSON)
  366. .content(objectMapper.writeValueAsString(matching)))
  367. .andExpect(status().isBadRequest())
  368. .andReturn();
  369.  
  370. String contentAsString = mvcResult.getResponse().getContentAsString();
  371. assertThat(contentAsString).isEqualTo(""); // no response
  372.  
  373. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  374. }
  375.  
  376. @Test
  377. public void decideMatchDeclineFailsCheckIdFalse() throws Exception {
  378. String ownerId = "Radu";
  379. lenient()
  380. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  381.  
  382. lenient()
  383. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(false);
  384.  
  385. MvcResult mvcResult = mockMvc
  386. .perform(post("/decideMatchDecline/Vlad")
  387. .contentType(MediaType.APPLICATION_JSON)
  388. .content(objectMapper.writeValueAsString(matching)))
  389. .andExpect(status().isBadRequest())
  390. .andReturn();
  391.  
  392. String contentAsString = mvcResult.getResponse().getContentAsString();
  393. assertThat(contentAsString).isEqualTo(""); // no response
  394.  
  395. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  396. }
  397.  
  398. @Test
  399. public void decideMatchDeclineBoth() throws Exception {
  400. String ownerId = "Radu";
  401. lenient()
  402. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  403.  
  404. lenient()
  405. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(false);
  406.  
  407. MvcResult mvcResult = mockMvc
  408. .perform(post("/decideMatchDecline/Radu")
  409. .contentType(MediaType.APPLICATION_JSON)
  410. .content(objectMapper.writeValueAsString(matching)))
  411.  
  412. .andExpect(status().isBadRequest())
  413. .andReturn();
  414.  
  415. String contentAsString = mvcResult.getResponse().getContentAsString();
  416. assertThat(contentAsString).isEqualTo(""); // no response
  417.  
  418. verify(matchingServiceImpl, never()).deleteById(anyString(), anyLong(), anyString());
  419. }
  420.  
  421. @Test
  422. public void decideMatchDeclinePass() throws Exception {
  423.  
  424. String ownerId = "Radu";
  425. lenient()
  426. .when(activityPublisher.getOwnerId(matching.getActivityId())).thenReturn(ownerId);
  427. lenient()
  428. .when(matchingServiceImpl.checkId(anyString(), anyLong(), anyString())).thenReturn(true);
  429.  
  430. doNothing().when(matchingServiceImpl).deleteById(anyString(), anyLong(), anyString());
  431.  
  432.  
  433. MvcResult mvcResult = mockMvc
  434. .perform(post("/decideMatchDecline/Vlad")
  435. .contentType(MediaType.APPLICATION_JSON)
  436. .content(objectMapper.writeValueAsString(matching)))
  437. .andDo(MockMvcResultHandlers.print())
  438. .andExpect(status().isOk())
  439. .andReturn();
  440.  
  441. matching.setPending(true);
  442.  
  443. String contentAsString = mvcResult.getResponse().getContentAsString();
  444. Matching obtained = objectMapper.readValue(contentAsString, Matching.class);
  445. assertThat(obtained).isEqualTo(matching);
  446. }
  447.  
  448. @Test
  449. void validationTest() throws Exception {
  450. Matching matchingInvalid = new Matching("", null, "", false);
  451. MvcResult mvcResult = mockMvc
  452. .perform(post("/save")
  453. .contentType(MediaType.APPLICATION_JSON)
  454. .content(objectMapper.writeValueAsString(matchingInvalid))
  455. )
  456. .andExpect(status().isBadRequest())
  457. .andReturn();
  458.  
  459. String contentAsString = mvcResult.getResponse().getContentAsString();
  460. assertThat(contentAsString).contains("userId is mandatory, thus it cannot be blank.");
  461. assertThat(contentAsString).contains("activityId is mandatory, thus it cannot be null.");
  462. assertThat(contentAsString).contains("position is mandatory, thus it cannot be blank.");
  463. }
  464.  
  465. }
  466.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement