You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationMediumTest.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.notification;
  21. import org.junit.Test;
  22. import org.mockito.invocation.InvocationOnMock;
  23. import org.mockito.stubbing.Answer;
  24. import org.sonar.api.config.PropertyDefinitions;
  25. import org.sonar.api.config.internal.Settings;
  26. import org.sonar.api.config.internal.MapSettings;
  27. import org.sonar.api.notifications.Notification;
  28. import org.sonar.api.notifications.NotificationChannel;
  29. import org.sonar.db.DbClient;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.mockito.ArgumentMatchers.any;
  32. import static org.mockito.ArgumentMatchers.anyString;
  33. import static org.mockito.ArgumentMatchers.same;
  34. import static org.mockito.Mockito.doAnswer;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.never;
  37. import static org.mockito.Mockito.spy;
  38. import static org.mockito.Mockito.timeout;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. public class NotificationMediumTest {
  42. private static String CREATOR_SIMON = "simon";
  43. private static String CREATOR_EVGENY = "evgeny";
  44. private static String ASSIGNEE_SIMON = "simon";
  45. private DefaultNotificationManager manager = mock(DefaultNotificationManager.class);
  46. private Notification notification = mock(Notification.class);
  47. private NotificationChannel emailChannel = mock(NotificationChannel.class);
  48. private NotificationChannel gtalkChannel = mock(NotificationChannel.class);
  49. private NotificationDispatcher commentOnIssueAssignedToMe = mock(NotificationDispatcher.class);
  50. private NotificationDispatcher commentOnIssueCreatedByMe = mock(NotificationDispatcher.class);
  51. private NotificationDispatcher qualityGateChange = mock(NotificationDispatcher.class);
  52. private DbClient dbClient = mock(DbClient.class);
  53. private NotificationService service = new NotificationService(dbClient, new NotificationDispatcher[] {commentOnIssueAssignedToMe, commentOnIssueCreatedByMe, qualityGateChange});
  54. private NotificationDaemon underTest = null;
  55. private void setUpMocks() {
  56. when(emailChannel.getKey()).thenReturn("email");
  57. when(gtalkChannel.getKey()).thenReturn("gtalk");
  58. when(commentOnIssueAssignedToMe.getKey()).thenReturn("CommentOnIssueAssignedToMe");
  59. when(commentOnIssueAssignedToMe.getType()).thenReturn("issue-changes");
  60. when(commentOnIssueCreatedByMe.getKey()).thenReturn("CommentOnIssueCreatedByMe");
  61. when(commentOnIssueCreatedByMe.getType()).thenReturn("issue-changes");
  62. when(qualityGateChange.getKey()).thenReturn("QGateChange");
  63. when(qualityGateChange.getType()).thenReturn("qgate-changes");
  64. when(manager.getFromQueue()).thenReturn(notification).thenReturn(null);
  65. MapSettings settings = new MapSettings(new PropertyDefinitions(NotificationDaemon.class)).setProperty("sonar.notifications.delay", 1L);
  66. underTest = new NotificationDaemon(settings.asConfig(), manager, service);
  67. }
  68. /**
  69. * Given:
  70. * Simon wants to receive notifications by email on comments for reviews assigned to him or created by him.
  71. * <p/>
  72. * When:
  73. * Freddy adds comment to review created by Simon and assigned to Simon.
  74. * <p/>
  75. * Then:
  76. * Only one notification should be delivered to Simon by Email.
  77. */
  78. @Test
  79. public void scenario1() {
  80. setUpMocks();
  81. doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  82. doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  83. underTest.start();
  84. verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON);
  85. underTest.stop();
  86. verify(gtalkChannel, never()).deliver(notification, ASSIGNEE_SIMON);
  87. }
  88. /**
  89. * Given:
  90. * Evgeny wants to receive notification by GTalk on comments for reviews created by him.
  91. * Simon wants to receive notification by Email on comments for reviews assigned to him.
  92. * <p/>
  93. * When:
  94. * Freddy adds comment to review created by Evgeny and assigned to Simon.
  95. * <p/>
  96. * Then:
  97. * Two notifications should be delivered - one to Simon by Email and another to Evgeny by GTalk.
  98. */
  99. @Test
  100. public void scenario2() {
  101. setUpMocks();
  102. doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  103. doAnswer(addUser(CREATOR_EVGENY, gtalkChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  104. underTest.start();
  105. verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON);
  106. verify(gtalkChannel, timeout(2000)).deliver(notification, CREATOR_EVGENY);
  107. underTest.stop();
  108. verify(emailChannel, never()).deliver(notification, CREATOR_EVGENY);
  109. verify(gtalkChannel, never()).deliver(notification, ASSIGNEE_SIMON);
  110. }
  111. /**
  112. * Given:
  113. * Simon wants to receive notifications by Email and GTLak on comments for reviews assigned to him.
  114. * <p/>
  115. * When:
  116. * Freddy adds comment to review created by Evgeny and assigned to Simon.
  117. * <p/>
  118. * Then:
  119. * Two notifications should be delivered to Simon - one by Email and another by GTalk.
  120. */
  121. @Test
  122. public void scenario3() {
  123. setUpMocks();
  124. doAnswer(addUser(ASSIGNEE_SIMON, new NotificationChannel[] {emailChannel, gtalkChannel}))
  125. .when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  126. underTest.start();
  127. verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON);
  128. verify(gtalkChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON);
  129. underTest.stop();
  130. verify(emailChannel, never()).deliver(notification, CREATOR_EVGENY);
  131. verify(gtalkChannel, never()).deliver(notification, CREATOR_EVGENY);
  132. }
  133. /**
  134. * Given:
  135. * Nobody wants to receive notifications.
  136. * <p/>
  137. * When:
  138. * Freddy adds comment to review created by Evgeny and assigned to Simon.
  139. * <p/>
  140. * Then:
  141. * No notifications.
  142. */
  143. @Test
  144. public void scenario4() {
  145. setUpMocks();
  146. underTest.start();
  147. underTest.stop();
  148. verify(emailChannel, never()).deliver(any(Notification.class), anyString());
  149. verify(gtalkChannel, never()).deliver(any(Notification.class), anyString());
  150. }
  151. // SONAR-4548
  152. @Test
  153. public void shouldNotStopWhenException() {
  154. setUpMocks();
  155. when(manager.getFromQueue()).thenThrow(new RuntimeException("Unexpected exception")).thenReturn(notification).thenReturn(null);
  156. doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)).when(commentOnIssueAssignedToMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  157. doAnswer(addUser(CREATOR_SIMON, emailChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  158. underTest.start();
  159. verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON);
  160. underTest.stop();
  161. verify(gtalkChannel, never()).deliver(notification, ASSIGNEE_SIMON);
  162. }
  163. @Test
  164. public void shouldNotAddNullAsUser() {
  165. setUpMocks();
  166. doAnswer(addUser(null, gtalkChannel)).when(commentOnIssueCreatedByMe).dispatch(same(notification), any(NotificationDispatcher.Context.class));
  167. underTest.start();
  168. underTest.stop();
  169. verify(emailChannel, never()).deliver(any(Notification.class), anyString());
  170. verify(gtalkChannel, never()).deliver(any(Notification.class), anyString());
  171. }
  172. @Test
  173. public void getDispatchers() {
  174. setUpMocks();
  175. assertThat(service.getDispatchers()).containsOnly(commentOnIssueAssignedToMe, commentOnIssueCreatedByMe, qualityGateChange);
  176. }
  177. @Test
  178. public void getDispatchers_empty() {
  179. Settings settings = new MapSettings().setProperty("sonar.notifications.delay", 1L);
  180. service = new NotificationService(dbClient);
  181. assertThat(service.getDispatchers()).hasSize(0);
  182. }
  183. @Test
  184. public void shouldLogEvery10Minutes() {
  185. setUpMocks();
  186. // Emulate 2 notifications in DB
  187. when(manager.getFromQueue()).thenReturn(notification).thenReturn(notification).thenReturn(null);
  188. when(manager.count()).thenReturn(1L).thenReturn(0L);
  189. underTest = spy(underTest);
  190. // Emulate processing of each notification take 10 min to have a log each time
  191. when(underTest.now()).thenReturn(0L).thenReturn(10 * 60 * 1000 + 1L).thenReturn(20 * 60 * 1000 + 2L);
  192. underTest.start();
  193. verify(underTest, timeout(200)).log(0, 1, 10);
  194. verify(underTest, timeout(200)).log(0, 0, 20);
  195. underTest.stop();
  196. }
  197. private static Answer<Object> addUser(final String user, final NotificationChannel channel) {
  198. return addUser(user, new NotificationChannel[] {channel});
  199. }
  200. private static Answer<Object> addUser(final String user, final NotificationChannel[] channels) {
  201. return new Answer<Object>() {
  202. public Object answer(InvocationOnMock invocation) {
  203. for (NotificationChannel channel : channels) {
  204. ((NotificationDispatcher.Context) invocation.getArguments()[1]).addUser(user, channel);
  205. }
  206. return null;
  207. }
  208. };
  209. }
  210. }