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.Test;
25 import org.sonar.api.issue.Issue;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.api.notifications.NotificationChannel;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.server.notification.NotificationDispatcher;
30 import org.sonar.server.notification.NotificationDispatcherMetadata;
31 import org.sonar.server.notification.NotificationManager;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.*;
36 public class DoNotFixNotificationDispatcherTest {
37 NotificationManager notifications = mock(NotificationManager.class);
38 NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class);
39 NotificationChannel emailChannel = mock(NotificationChannel.class);
40 NotificationChannel twitterChannel = mock(NotificationChannel.class);
41 DoNotFixNotificationDispatcher underTest = new DoNotFixNotificationDispatcher(notifications);;
44 public void test_metadata() throws Exception {
45 NotificationDispatcherMetadata metadata = DoNotFixNotificationDispatcher.newMetadata();
46 assertThat(metadata.getDispatcherKey()).isEqualTo(underTest.getKey());
47 assertThat(metadata.getProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)).isEqualTo("true");
48 assertThat(metadata.getProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)).isEqualTo("true");
52 public void should_not_dispatch_if_other_notification_type() {
53 Notification notification = new Notification("other");
54 underTest.performDispatch(notification, context);
56 verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
60 public void should_dispatch_to_subscribers() {
61 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
62 recipients.put("simon", emailChannel);
63 recipients.put("freddy", twitterChannel);
64 recipients.put("godin", twitterChannel);
65 when(notifications.findSubscribedRecipientsForDispatcher(underTest, "uuid1", new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER))).thenReturn(recipients);
67 Notification fpNotif = new IssueChangeNotification().setFieldValue("projectKey", "struts")
68 .setFieldValue("projectUuid", "uuid1")
69 .setFieldValue("changeAuthor", "godin")
70 .setFieldValue("new.resolution", Issue.RESOLUTION_FALSE_POSITIVE)
71 .setFieldValue("assignee", "freddy");
72 underTest.performDispatch(fpNotif, context);
74 verify(context).addUser("simon", emailChannel);
75 verify(context).addUser("freddy", twitterChannel);
76 // do not notify the person who flagged the issue as false-positive
77 verify(context, never()).addUser("godin", twitterChannel);
78 verifyNoMoreInteractions(context);
82 * Only false positive and won't fix resolutions
85 public void ignore_other_resolutions() {
86 Notification fixedNotif = new IssueChangeNotification().setFieldValue("projectKey", "struts")
87 .setFieldValue("changeAuthor", "godin")
88 .setFieldValue("new.resolution", Issue.RESOLUTION_FIXED)
89 .setFieldValue("assignee", "freddy");
90 underTest.performDispatch(fixedNotif, context);
92 verifyZeroInteractions(context);