]> source.dussan.org Git - sonarqube.git/blob
e58a9a71a60966dc2af2c33b33835c60c13cf50c
[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.api.web.UserRole;
32 import org.sonar.server.notification.NotificationDispatcher;
33 import org.sonar.server.notification.NotificationDispatcherMetadata;
34 import org.sonar.server.notification.NotificationManager;
35
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;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class ChangesOnMyIssueNotificationDispatcherTest {
45
46   @Mock
47   NotificationManager notifications;
48
49   @Mock
50   NotificationDispatcher.Context context;
51
52   @Mock
53   NotificationChannel emailChannel;
54
55   @Mock
56   NotificationChannel twitterChannel;
57
58   ChangesOnMyIssueNotificationDispatcher dispatcher;
59
60   @Before
61   public void setUp() {
62     dispatcher = new ChangesOnMyIssueNotificationDispatcher(notifications);
63   }
64
65   @Test
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");
71   }
72
73   @Test
74   public void should_not_dispatch_if_other_notification_type() {
75     Notification notification = new Notification("other-notif");
76     dispatcher.performDispatch(notification, context);
77
78     verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
79   }
80
81   @Test
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);
89
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);
96
97     verify(context).addUser("freddy", twitterChannel);
98     verify(context, never()).addUser("godin", twitterChannel);
99     verifyNoMoreInteractions(context);
100   }
101
102   @Test
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);
109
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);
117
118     // no change author
119     dispatcher.performDispatch(new IssueChangeNotification().setFieldValue("projectKey", "struts")
120       .setFieldValue("new.resolution", "FIXED"), context);
121
122     verifyNoMoreInteractions(context);
123   }
124 }