]> source.dussan.org Git - sonarqube.git/blob
9c32306534089d68d770d1d420bfc28b77a413da
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.issue.notification;
21
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.server.notification.NotificationDispatcher;
29 import org.sonar.server.notification.NotificationDispatcherMetadata;
30 import org.sonar.server.notification.NotificationManager;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.*;
34
35 public class DoNotFixNotificationDispatcherTest {
36   NotificationManager notifications = mock(NotificationManager.class);
37   NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class);
38   NotificationChannel emailChannel = mock(NotificationChannel.class);
39   NotificationChannel twitterChannel = mock(NotificationChannel.class);
40   DoNotFixNotificationDispatcher underTest = new DoNotFixNotificationDispatcher(notifications);;
41
42   @Test
43   public void test_metadata() throws Exception {
44     NotificationDispatcherMetadata metadata = DoNotFixNotificationDispatcher.newMetadata();
45     assertThat(metadata.getDispatcherKey()).isEqualTo(underTest.getKey());
46     assertThat(metadata.getProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)).isEqualTo("true");
47     assertThat(metadata.getProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)).isEqualTo("true");
48   }
49
50   @Test
51   public void should_not_dispatch_if_other_notification_type() {
52     Notification notification = new Notification("other");
53     underTest.performDispatch(notification, context);
54
55     verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
56   }
57
58   @Test
59   public void should_dispatch_to_subscribers() {
60     Multimap<String, NotificationChannel> recipients = HashMultimap.create();
61     recipients.put("simon", emailChannel);
62     recipients.put("freddy", twitterChannel);
63     recipients.put("godin", twitterChannel);
64     when(notifications.findNotificationSubscribers(underTest, "struts")).thenReturn(recipients);
65
66     Notification fpNotif = new IssueChangeNotification().setFieldValue("projectKey", "struts")
67       .setFieldValue("changeAuthor", "godin")
68       .setFieldValue("new.resolution", Issue.RESOLUTION_FALSE_POSITIVE)
69       .setFieldValue("assignee", "freddy");
70     underTest.performDispatch(fpNotif, context);
71
72     verify(context).addUser("simon", emailChannel);
73     verify(context).addUser("freddy", twitterChannel);
74     // do not notify the person who flagged the issue as false-positive
75     verify(context, never()).addUser("godin", twitterChannel);
76     verifyNoMoreInteractions(context);
77   }
78
79   /**
80    * Only false positive and won't fix resolutions
81    */
82   @Test
83   public void ignore_other_resolutions() {
84     Multimap<String, NotificationChannel> recipients = HashMultimap.create();
85     recipients.put("simon", emailChannel);
86     recipients.put("freddy", twitterChannel);
87     when(notifications.findNotificationSubscribers(underTest, "struts")).thenReturn(recipients);
88
89     Notification fixedNotif = new IssueChangeNotification().setFieldValue("projectKey", "struts")
90       .setFieldValue("changeAuthor", "godin")
91       .setFieldValue("new.resolution", Issue.RESOLUTION_FIXED)
92       .setFieldValue("assignee", "freddy");
93     underTest.performDispatch(fixedNotif, context);
94
95     verifyZeroInteractions(context);
96   }
97 }