3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.issue.notification;
22 import com.google.common.collect.HashMultimap;
23 import com.google.common.collect.Multimap;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.sonar.api.notifications.Notification;
30 import org.sonar.api.notifications.NotificationChannel;
31 import org.sonar.server.notification.NotificationDispatcher;
32 import org.sonar.server.notification.NotificationDispatcherMetadata;
33 import org.sonar.server.notification.NotificationManager;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.any;
37 import static org.mockito.Mockito.never;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 import static org.mockito.Mockito.when;
42 @RunWith(MockitoJUnitRunner.class)
43 public class ChangesOnMyIssueNotificationDispatcherTest {
46 NotificationManager notifications;
49 NotificationDispatcher.Context context;
52 NotificationChannel emailChannel;
55 NotificationChannel twitterChannel;
57 ChangesOnMyIssueNotificationDispatcher dispatcher;
61 dispatcher = new ChangesOnMyIssueNotificationDispatcher(notifications);
65 public void test_metadata() throws Exception {
66 NotificationDispatcherMetadata metadata = ChangesOnMyIssueNotificationDispatcher.newMetadata();
67 assertThat(metadata.getDispatcherKey()).isEqualTo(dispatcher.getKey());
68 assertThat(metadata.getProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)).isEqualTo("true");
69 assertThat(metadata.getProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)).isEqualTo("true");
73 public void should_not_dispatch_if_other_notification_type() {
74 Notification notification = new Notification("other-notif");
75 dispatcher.performDispatch(notification, context);
77 verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
81 public void should_dispatch_to_assignee() {
82 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
83 recipients.put("simon", emailChannel);
84 recipients.put("freddy", twitterChannel);
85 recipients.put("godin", twitterChannel);
86 when(notifications.findNotificationSubscribers(dispatcher, "struts")).thenReturn(recipients);
88 Notification notification = new IssueChangeNotification().setFieldValue("projectKey", "struts")
89 .setFieldValue("changeAuthor", "olivier")
90 .setFieldValue("assignee", "freddy");
91 dispatcher.performDispatch(notification, context);
93 verify(context).addUser("freddy", twitterChannel);
94 verify(context, never()).addUser("godin", twitterChannel);
95 verifyNoMoreInteractions(context);
99 public void should_not_dispatch_to_author_of_changes() {
100 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
101 recipients.put("simon", emailChannel);
102 recipients.put("freddy", twitterChannel);
103 recipients.put("godin", twitterChannel);
104 when(notifications.findNotificationSubscribers(dispatcher, "struts")).thenReturn(recipients);
106 // change author is the assignee
107 dispatcher.performDispatch(new IssueChangeNotification().setFieldValue("projectKey", "struts")
108 .setFieldValue("changeAuthor", "simon").setFieldValue("assignee", "simon"), context);
111 dispatcher.performDispatch(new IssueChangeNotification().setFieldValue("projectKey", "struts")
112 .setFieldValue("new.resolution", "FIXED"), context);
114 verifyNoMoreInteractions(context);