]> source.dussan.org Git - sonarqube.git/blob
5bcb7421a66ee7d53bc9f228366470842000be0f
[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.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;
34
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;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class ChangesOnMyIssueNotificationDispatcherTest {
44
45   @Mock
46   NotificationManager notifications;
47
48   @Mock
49   NotificationDispatcher.Context context;
50
51   @Mock
52   NotificationChannel emailChannel;
53
54   @Mock
55   NotificationChannel twitterChannel;
56
57   ChangesOnMyIssueNotificationDispatcher dispatcher;
58
59   @Before
60   public void setUp() {
61     dispatcher = new ChangesOnMyIssueNotificationDispatcher(notifications);
62   }
63
64   @Test
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");
70   }
71
72   @Test
73   public void should_not_dispatch_if_other_notification_type() {
74     Notification notification = new Notification("other-notif");
75     dispatcher.performDispatch(notification, context);
76
77     verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
78   }
79
80   @Test
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);
87
88     Notification notification = new IssueChangeNotification().setFieldValue("projectKey", "struts")
89       .setFieldValue("changeAuthor", "olivier")
90       .setFieldValue("assignee", "freddy");
91     dispatcher.performDispatch(notification, context);
92
93     verify(context).addUser("freddy", twitterChannel);
94     verify(context, never()).addUser("godin", twitterChannel);
95     verifyNoMoreInteractions(context);
96   }
97
98   @Test
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);
105
106     // change author is the assignee
107     dispatcher.performDispatch(new IssueChangeNotification().setFieldValue("projectKey", "struts")
108       .setFieldValue("changeAuthor", "simon").setFieldValue("assignee", "simon"), context);
109
110     // no change author
111     dispatcher.performDispatch(new IssueChangeNotification().setFieldValue("projectKey", "struts")
112       .setFieldValue("new.resolution", "FIXED"), context);
113
114     verifyNoMoreInteractions(context);
115   }
116 }