]> source.dussan.org Git - sonarqube.git/blob
c9a698b37b47692a20fabce7eef2ea3ccc6fc6a5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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 java.util.Collections;
23 import java.util.Random;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import java.util.stream.IntStream;
27 import java.util.stream.Stream;
28 import javax.annotation.Nullable;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.sonar.server.notification.NotificationDispatcherMetadata;
32 import org.sonar.server.notification.NotificationManager;
33 import org.sonar.server.notification.NotificationManager.EmailRecipient;
34 import org.sonar.server.notification.email.EmailNotificationChannel;
35 import org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest;
36
37 import static com.google.common.collect.ImmutableSet.of;
38 import static java.util.Collections.emptySet;
39 import static java.util.stream.Collectors.toSet;
40 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.verifyNoInteractions;
45 import static org.mockito.Mockito.verifyNoMoreInteractions;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION;
48 import static org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION;
49 import static org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER;
50
51 public class MyNewIssuesNotificationHandlerTest {
52   private static final String MY_NEW_ISSUES_DISPATCHER_KEY = "SQ-MyNewIssues";
53   private NotificationManager notificationManager = mock(NotificationManager.class);
54   private EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class);
55
56   private MyNewIssuesNotificationHandler underTest = new MyNewIssuesNotificationHandler(notificationManager, emailNotificationChannel);
57
58   @Test
59   public void getMetadata_returns_same_instance_as_static_method() {
60     assertThat(underTest.getMetadata()).containsSame(MyNewIssuesNotificationHandler.newMetadata());
61   }
62
63   @Test
64   public void verify_myNewIssues_notification_dispatcher_key() {
65     NotificationDispatcherMetadata metadata = MyNewIssuesNotificationHandler.newMetadata();
66
67     assertThat(metadata.getDispatcherKey()).isEqualTo(MY_NEW_ISSUES_DISPATCHER_KEY);
68   }
69
70   @Test
71   public void myNewIssues_notification_is_enable_at_global_level() {
72     NotificationDispatcherMetadata metadata = MyNewIssuesNotificationHandler.newMetadata();
73
74     assertThat(metadata.getProperty(GLOBAL_NOTIFICATION)).isEqualTo("true");
75   }
76
77   @Test
78   public void myNewIssues_notification_is_enable_at_project_level() {
79     NotificationDispatcherMetadata metadata = MyNewIssuesNotificationHandler.newMetadata();
80
81     assertThat(metadata.getProperty(PER_PROJECT_NOTIFICATION)).isEqualTo("true");
82   }
83
84   @Test
85   public void getNotificationClass_is_MyNewIssuesNotification() {
86     assertThat(underTest.getNotificationClass()).isEqualTo(MyNewIssuesNotification.class);
87   }
88
89   @Test
90   public void deliver_has_no_effect_if_notifications_is_empty() {
91     when(emailNotificationChannel.isActivated()).thenReturn(true);
92     int deliver = underTest.deliver(Collections.emptyList());
93
94     assertThat(deliver).isZero();
95     verifyNoInteractions(notificationManager, emailNotificationChannel);
96   }
97
98   @Test
99   public void deliver_has_no_effect_if_emailNotificationChannel_is_disabled() {
100     when(emailNotificationChannel.isActivated()).thenReturn(false);
101     Set<MyNewIssuesNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
102       .mapToObj(i -> mock(MyNewIssuesNotification.class))
103       .collect(toSet());
104
105     int deliver = underTest.deliver(notifications);
106
107     assertThat(deliver).isZero();
108     verifyNoInteractions(notificationManager);
109     verify(emailNotificationChannel).isActivated();
110     verifyNoMoreInteractions(emailNotificationChannel);
111     notifications.forEach(Mockito::verifyNoInteractions);
112   }
113
114   @Test
115   public void deliver_has_no_effect_if_no_notification_has_projectKey() {
116     when(emailNotificationChannel.isActivated()).thenReturn(true);
117     Set<MyNewIssuesNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
118       .mapToObj(i -> newNotification(null, null))
119       .collect(toSet());
120
121     int deliver = underTest.deliver(notifications);
122
123     assertThat(deliver).isZero();
124     verifyNoInteractions(notificationManager);
125     verify(emailNotificationChannel).isActivated();
126     verifyNoMoreInteractions(emailNotificationChannel);
127     notifications.forEach(notification -> {
128       verify(notification).getProjectKey();
129       verifyNoMoreInteractions(notification);
130     });
131   }
132
133   @Test
134   public void deliver_has_no_effect_if_no_notification_has_assignee() {
135     when(emailNotificationChannel.isActivated()).thenReturn(true);
136     Set<MyNewIssuesNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
137       .mapToObj(i -> newNotification(randomAlphabetic(5 + i), null))
138       .collect(toSet());
139
140     int deliver = underTest.deliver(notifications);
141
142     assertThat(deliver).isZero();
143     verifyNoInteractions(notificationManager);
144     verify(emailNotificationChannel).isActivated();
145     verifyNoMoreInteractions(emailNotificationChannel);
146     notifications.forEach(notification -> {
147       verify(notification).getProjectKey();
148       verify(notification).getAssignee();
149       verifyNoMoreInteractions(notification);
150     });
151   }
152
153   @Test
154   public void deliver_has_no_effect_if_no_notification_has_subscribed_assignee_to_MyNewIssue_notifications() {
155     String projectKey = randomAlphabetic(12);
156     String assignee = randomAlphabetic(10);
157     MyNewIssuesNotification notification = newNotification(projectKey, assignee);
158     when(emailNotificationChannel.isActivated()).thenReturn(true);
159     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, of(assignee), ALL_MUST_HAVE_ROLE_USER))
160       .thenReturn(emptySet());
161
162     int deliver = underTest.deliver(Collections.singleton(notification));
163
164     assertThat(deliver).isZero();
165     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, of(assignee), ALL_MUST_HAVE_ROLE_USER);
166     verifyNoMoreInteractions(notificationManager);
167     verify(emailNotificationChannel).isActivated();
168     verifyNoMoreInteractions(emailNotificationChannel);
169   }
170
171   @Test
172   public void deliver_ignores_notification_without_projectKey() {
173     String projectKey = randomAlphabetic(10);
174     Set<MyNewIssuesNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
175       .mapToObj(i -> newNotification(projectKey, randomAlphabetic(11 + i)))
176       .collect(toSet());
177     Set<MyNewIssuesNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
178       .mapToObj(i -> newNotification(null, randomAlphabetic(11 + i)))
179       .collect(toSet());
180     Set<MyNewIssuesNotification> noProjectKeyNoAssignee = randomSetOfNotifications(null, null);
181     Set<EmailRecipient> authorizedRecipients = withProjectKey.stream()
182       .map(n -> new EmailRecipient(n.getAssignee(), n.getAssignee() + "@foo"))
183       .collect(toSet());
184     Set<EmailDeliveryRequest> expectedRequests = withProjectKey.stream()
185       .map(n -> new EmailDeliveryRequest(n.getAssignee() + "@foo", n))
186       .collect(toSet());
187     when(emailNotificationChannel.isActivated()).thenReturn(true);
188     Set<String> assignees = withProjectKey.stream().map(MyNewIssuesNotification::getAssignee).collect(toSet());
189     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER))
190       .thenReturn(authorizedRecipients);
191
192     Set<MyNewIssuesNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream(), noProjectKeyNoAssignee.stream())
193       .flatMap(t -> t)
194       .collect(toSet());
195     int deliver = underTest.deliver(notifications);
196
197     assertThat(deliver).isZero();
198     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
199     verifyNoMoreInteractions(notificationManager);
200     verify(emailNotificationChannel).isActivated();
201     verify(emailNotificationChannel).deliverAll(expectedRequests);
202     verifyNoMoreInteractions(emailNotificationChannel);
203   }
204
205   @Test
206   public void deliver_ignores_notification_without_assignee() {
207     String projectKey = randomAlphabetic(10);
208     Set<MyNewIssuesNotification> withAssignee = IntStream.range(0, 1 + new Random().nextInt(5))
209       .mapToObj(i -> newNotification(projectKey, randomAlphabetic(11 + i)))
210       .collect(toSet());
211     Set<MyNewIssuesNotification> noAssignee = randomSetOfNotifications(projectKey, null);
212     Set<MyNewIssuesNotification> noProjectKeyNoAssignee = randomSetOfNotifications(null, null);
213     Set<EmailRecipient> authorizedRecipients = withAssignee.stream()
214       .map(n -> new EmailRecipient(n.getAssignee(), n.getAssignee() + "@foo"))
215       .collect(toSet());
216     Set<EmailDeliveryRequest> expectedRequests = withAssignee.stream()
217       .map(n -> new EmailDeliveryRequest(n.getAssignee() + "@foo", n))
218       .collect(toSet());
219     when(emailNotificationChannel.isActivated()).thenReturn(true);
220     Set<String> assignees = withAssignee.stream().map(MyNewIssuesNotification::getAssignee).collect(toSet());
221     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER))
222       .thenReturn(authorizedRecipients);
223
224     Set<MyNewIssuesNotification> notifications = Stream.of(withAssignee.stream(), noAssignee.stream(), noProjectKeyNoAssignee.stream())
225       .flatMap(t -> t)
226       .collect(toSet());
227     int deliver = underTest.deliver(notifications);
228
229     assertThat(deliver).isZero();
230     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
231     verifyNoMoreInteractions(notificationManager);
232     verify(emailNotificationChannel).isActivated();
233     verify(emailNotificationChannel).deliverAll(expectedRequests);
234     verifyNoMoreInteractions(emailNotificationChannel);
235   }
236
237   @Test
238   public void deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_MyNewIssue_notifications() {
239     String projectKey1 = randomAlphabetic(10);
240     String assignee1 = randomAlphabetic(11);
241     String projectKey2 = randomAlphabetic(12);
242     String assignee2 = randomAlphabetic(13);
243     Set<MyNewIssuesNotification> notifications1 = randomSetOfNotifications(projectKey1, assignee1);
244     Set<MyNewIssuesNotification> notifications2 = randomSetOfNotifications(projectKey2, assignee2);
245     when(emailNotificationChannel.isActivated()).thenReturn(true);
246     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey1, of(assignee1), ALL_MUST_HAVE_ROLE_USER))
247       .thenReturn(emptySet());
248     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey2, of(assignee2),ALL_MUST_HAVE_ROLE_USER))
249       .thenReturn(emptySet());
250
251     int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
252
253     assertThat(deliver).isZero();
254     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey1, of(assignee1), ALL_MUST_HAVE_ROLE_USER);
255     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey2, of(assignee2), ALL_MUST_HAVE_ROLE_USER);
256     verifyNoMoreInteractions(notificationManager);
257     verify(emailNotificationChannel).isActivated();
258     verifyNoMoreInteractions(emailNotificationChannel);
259   }
260
261   @Test
262   public void deliver_ignores_notifications_which_assignee_has_no_subscribed_to_MyNewIssue_notifications() {
263     String projectKey = randomAlphabetic(5);
264     String assignee1 = randomAlphabetic(6);
265     String assignee2 = randomAlphabetic(7);
266     Set<String> assignees = of(assignee1, assignee2);
267     // assignee1 is not authorized
268     Set<MyNewIssuesNotification> assignee1Notifications = randomSetOfNotifications(projectKey, assignee1);
269     // assignee2 is authorized
270     Set<MyNewIssuesNotification> assignee2Notifications = randomSetOfNotifications(projectKey, assignee2);
271     when(emailNotificationChannel.isActivated()).thenReturn(true);
272     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER))
273       .thenReturn(of(emailRecipientOf(assignee2)));
274     Set<EmailDeliveryRequest> expectedRequests = assignee2Notifications.stream()
275       .map(t -> new EmailDeliveryRequest(emailOf(t.getAssignee()), t))
276       .collect(toSet());
277     int deliveredCount = new Random().nextInt(expectedRequests.size());
278     when(emailNotificationChannel.deliverAll(expectedRequests)).thenReturn(deliveredCount);
279
280     int deliver = underTest.deliver(Stream.concat(assignee1Notifications.stream(), assignee2Notifications.stream()).collect(toSet()));
281
282     assertThat(deliver).isEqualTo(deliveredCount);
283     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
284     verifyNoMoreInteractions(notificationManager);
285     verify(emailNotificationChannel).isActivated();
286     verify(emailNotificationChannel).deliverAll(expectedRequests);
287     verifyNoMoreInteractions(emailNotificationChannel);
288   }
289
290   @Test
291   public void deliver_returns_sum_of_delivery_counts_when_multiple_projects() {
292     String projectKey1 = randomAlphabetic(5);
293     String projectKey2 = randomAlphabetic(6);
294     String projectKey3 = randomAlphabetic(7);
295     String assignee1 = randomAlphabetic(8);
296     String assignee2 = randomAlphabetic(9);
297     String assignee3 = randomAlphabetic(10);
298     // assignee1 has subscribed to project1 only, no notification on project3
299     Set<MyNewIssuesNotification> assignee1Project1 = randomSetOfNotifications(projectKey1, assignee1);
300     Set<MyNewIssuesNotification> assignee1Project2 = randomSetOfNotifications(projectKey2, assignee1);
301     // assignee2 is subscribed to project1 and project2, notifications on all projects
302     Set<MyNewIssuesNotification> assignee2Project1 = randomSetOfNotifications(projectKey1, assignee2);
303     Set<MyNewIssuesNotification> assignee2Project2 = randomSetOfNotifications(projectKey2, assignee2);
304     Set<MyNewIssuesNotification> assignee2Project3 = randomSetOfNotifications(projectKey3, assignee2);
305     // assignee3 is subscribed to project2 only, no notification on project1
306     Set<MyNewIssuesNotification> assignee3Project2 = randomSetOfNotifications(projectKey2, assignee3);
307     Set<MyNewIssuesNotification> assignee3Project3 = randomSetOfNotifications(projectKey3, assignee3);
308     when(emailNotificationChannel.isActivated()).thenReturn(true);
309     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey1, of(assignee1, assignee2), ALL_MUST_HAVE_ROLE_USER))
310       .thenReturn(of(emailRecipientOf(assignee1), emailRecipientOf(assignee2)));
311     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey2, of(assignee1, assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER))
312       .thenReturn(of(emailRecipientOf(assignee2), emailRecipientOf(assignee3)));
313     when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey3, of(assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER))
314       .thenReturn(emptySet());
315     Set<EmailDeliveryRequest> expectedRequests = Stream.of(
316       assignee1Project1.stream(), assignee2Project1.stream(), assignee2Project2.stream(), assignee3Project2.stream())
317       .flatMap(t -> t)
318       .map(t -> new EmailDeliveryRequest(emailOf(t.getAssignee()), t))
319       .collect(toSet());
320     int deliveredCount = new Random().nextInt(expectedRequests.size());
321     when(emailNotificationChannel.deliverAll(expectedRequests)).thenReturn(deliveredCount);
322
323     Set<MyNewIssuesNotification> notifications = Stream.of(
324       assignee1Project1.stream(), assignee1Project2.stream(),
325       assignee2Project1.stream(), assignee2Project2.stream(),
326       assignee2Project3.stream(), assignee3Project2.stream(), assignee3Project3.stream())
327       .flatMap(t -> t)
328       .collect(toSet());
329     int deliver = underTest.deliver(notifications);
330
331     assertThat(deliver).isEqualTo(deliveredCount);
332     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey1, of(assignee1, assignee2), ALL_MUST_HAVE_ROLE_USER);
333     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey2, of(assignee1, assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER);
334     verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey3, of(assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER);
335     verifyNoMoreInteractions(notificationManager);
336     verify(emailNotificationChannel).isActivated();
337     verify(emailNotificationChannel).deliverAll(expectedRequests);
338     verifyNoMoreInteractions(emailNotificationChannel);
339   }
340
341   private static Set<MyNewIssuesNotification> randomSetOfNotifications(@Nullable String projectKey, @Nullable String assignee) {
342     return IntStream.range(0, 1 + new Random().nextInt(5))
343       .mapToObj(i -> newNotification(projectKey, assignee))
344       .collect(Collectors.toSet());
345   }
346
347   private static MyNewIssuesNotification newNotification(@Nullable String projectKey, @Nullable String assignee) {
348     MyNewIssuesNotification notification = mock(MyNewIssuesNotification.class);
349     when(notification.getProjectKey()).thenReturn(projectKey);
350     when(notification.getAssignee()).thenReturn(assignee);
351     return notification;
352   }
353
354   private static EmailRecipient emailRecipientOf(String assignee1) {
355     return new EmailRecipient(assignee1, emailOf(assignee1));
356   }
357
358   private static String emailOf(String assignee1) {
359     return assignee1 + "@bar";
360   }
361 }