/** * Given: Nobody wants to receive notifications. * * <p>When: Freddy adds comment to review created by Evgeny and assigned to Simon. * * <p>Then: No notifications. */ @Test public void scenario4() { setUpMocks(); service.start(); service.stop(); verify(emailChannel, never()).deliver(any(Notification.class), anyString()); verify(gtalkChannel, never()).deliver(any(Notification.class), anyString()); }
@Test public void shouldNotAddNullAsUser() { setUpMocks(); doAnswer(addUser(null, gtalkChannel)) .when(commentOnIssueCreatedByMe) .dispatch(same(notification), any(NotificationDispatcher.Context.class)); service.start(); service.stop(); verify(emailChannel, never()).deliver(any(Notification.class), anyString()); verify(gtalkChannel, never()).deliver(any(Notification.class), anyString()); }
/** * Given: Simon wants to receive notifications by Email and GTLak on comments for reviews assigned * to him. * * <p>When: Freddy adds comment to review created by Evgeny and assigned to Simon. * * <p>Then: Two notifications should be delivered to Simon - one by Email and another by GTalk. */ @Test public void scenario3() { setUpMocks(); doAnswer(addUser(ASSIGNEE_SIMON, new NotificationChannel[] {emailChannel, gtalkChannel})) .when(commentOnIssueAssignedToMe) .dispatch(same(notification), any(NotificationDispatcher.Context.class)); service.start(); verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); verify(gtalkChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); service.stop(); verify(emailChannel, never()).deliver(notification, CREATOR_EVGENY); verify(gtalkChannel, never()).deliver(notification, CREATOR_EVGENY); }
@Test public void shouldLogEvery10Minutes() { setUpMocks(); // Emulate 2 notifications in DB when(manager.getFromQueue()).thenReturn(notification).thenReturn(notification).thenReturn(null); when(manager.count()).thenReturn(1L).thenReturn(0L); service = spy(service); // Emulate processing of each notification take 10 min to have a log each time when(service.now()) .thenReturn(0L) .thenReturn(10 * 60 * 1000 + 1L) .thenReturn(20 * 60 * 1000 + 2L); service.start(); verify(service, timeout(200)).log(1, 1, 10); verify(service, timeout(200)).log(2, 0, 20); service.stop(); }
// SONAR-4548 @Test public void shouldNotStopWhenException() { setUpMocks(); when(manager.getFromQueue()) .thenThrow(new RuntimeException("Unexpected exception")) .thenReturn(notification) .thenReturn(null); doAnswer(addUser(ASSIGNEE_SIMON, emailChannel)) .when(commentOnIssueAssignedToMe) .dispatch(same(notification), any(NotificationDispatcher.Context.class)); doAnswer(addUser(CREATOR_SIMON, emailChannel)) .when(commentOnIssueCreatedByMe) .dispatch(same(notification), any(NotificationDispatcher.Context.class)); service.start(); verify(emailChannel, timeout(2000)).deliver(notification, ASSIGNEE_SIMON); service.stop(); verify(gtalkChannel, never()).deliver(notification, ASSIGNEE_SIMON); }
@Test public void shouldPeriodicallyProcessQueue() throws Exception { NotificationQueueElement queueElement = mock(NotificationQueueElement.class); Notification notification = mock(Notification.class); when(queueElement.getNotification()).thenReturn(notification); when(manager.getFromQueue()) .thenReturn(queueElement) .thenReturn(null) .thenReturn(queueElement) .thenReturn(null) .thenReturn(queueElement) .thenReturn(null); doNothing().when(service).deliver(any(Notification.class)); service.start(); Thread.sleep(1500); // sleep 1.5 second to process queue service.stop(); verify(service, times(3)) .deliver(notification); // 3 times - 1 on start, 1 after delay, 1 on stop }
public void start() { notificationService.start(); }