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.api.web.UserRole;
32 import org.sonar.server.notification.NotificationDispatcher;
33 import org.sonar.server.notification.NotificationDispatcherMetadata;
34 import org.sonar.server.notification.NotificationManager;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.any;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.verifyNoMoreInteractions;
41 import static org.mockito.Mockito.when;
43 @RunWith(MockitoJUnitRunner.class)
44 public class ChangesOnMyIssueNotificationDispatcherTest {
47 NotificationManager notifications;
50 NotificationDispatcher.Context context;
53 NotificationChannel emailChannel;
56 NotificationChannel twitterChannel;
58 ChangesOnMyIssueNotificationDispatcher dispatcher;
62 dispatcher = new ChangesOnMyIssueNotificationDispatcher(notifications);
66 public void test_metadata() throws Exception {
67 NotificationDispatcherMetadata metadata = ChangesOnMyIssueNotificationDispatcher.newMetadata();
68 assertThat(metadata.getDispatcherKey()).isEqualTo(dispatcher.getKey());
69 assertThat(metadata.getProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)).isEqualTo("true");
70 assertThat(metadata.getProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)).isEqualTo("true");
74 public void should_not_dispatch_if_other_notification_type() {
75 Notification notification = new Notification("other-notif");
76 dispatcher.performDispatch(notification, context);
78 verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
82 public void should_dispatch_to_assignee() {
83 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
84 recipients.put("simon", emailChannel);
85 recipients.put("freddy", twitterChannel);
86 recipients.put("godin", twitterChannel);
87 when(notifications.findSubscribedRecipientsForDispatcher(dispatcher, "uuid1",
88 new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER))).thenReturn(recipients);
90 Notification notification = new IssueChangeNotification()
91 .setFieldValue("projectKey", "struts")
92 .setFieldValue("projectUuid", "uuid1")
93 .setFieldValue("changeAuthor", "olivier")
94 .setFieldValue("assignee", "freddy");
95 dispatcher.performDispatch(notification, context);
97 verify(context).addUser("freddy", twitterChannel);
98 verify(context, never()).addUser("godin", twitterChannel);
99 verifyNoMoreInteractions(context);
103 public void should_not_dispatch_to_author_of_changes() {
104 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
105 recipients.put("simon", emailChannel);
106 recipients.put("freddy", twitterChannel);
107 recipients.put("godin", twitterChannel);
108 when(notifications.findSubscribedRecipientsForDispatcher(dispatcher, "uuid1", new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER))).thenReturn(recipients);
110 // change author is the assignee
111 dispatcher.performDispatch(
112 new IssueChangeNotification()
113 .setFieldValue("projectKey", "struts")
114 .setFieldValue("projectUuid", "uuid1")
115 .setFieldValue("changeAuthor", "simon")
116 .setFieldValue("assignee", "simon"), context);
119 dispatcher.performDispatch(new IssueChangeNotification().setFieldValue("projectKey", "struts")
120 .setFieldValue("new.resolution", "FIXED"), context);
122 verifyNoMoreInteractions(context);