]> source.dussan.org Git - sonarqube.git/blob
e0571fe8d0c43f150c0b5079568500fdffcb2e7f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.ce.task.projectanalysis.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.api.web.UserRole;
32 import org.sonar.server.notification.NotificationDispatcherMetadata;
33 import org.sonar.server.notification.NotificationManager;
34 import org.sonar.server.notification.NotificationManager.EmailRecipient;
35 import org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject;
36 import org.sonar.server.notification.email.EmailNotificationChannel;
37 import org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest;
38
39 import static java.util.Collections.emptySet;
40 import static java.util.stream.Collectors.toSet;
41 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.verifyNoMoreInteractions;
46 import static org.mockito.Mockito.verifyZeroInteractions;
47 import static org.mockito.Mockito.when;
48 import static org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION;
49 import static org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION;
50
51 public class ReportAnalysisFailureNotificationHandlerTest {
52   private static final String REPORT_FAILURE_DISPATCHER_KEY = "CeReportTaskFailure";
53   private static final SubscriberPermissionsOnProject REQUIRED_SUBSCRIBER_PERMISSIONS = new SubscriberPermissionsOnProject(UserRole.ADMIN, UserRole.USER);
54   private NotificationManager notificationManager = mock(NotificationManager.class);
55   private EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class);
56   private ReportAnalysisFailureNotificationHandler underTest = new ReportAnalysisFailureNotificationHandler(notificationManager, emailNotificationChannel);
57
58   @Test
59   public void getMetadata_returns_same_instance_as_static_method() {
60     assertThat(underTest.getMetadata().get()).isSameAs(ReportAnalysisFailureNotificationHandler.newMetadata());
61   }
62
63   @Test
64   public void verify_reportFailures_notification_dispatcher_key() {
65     NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationHandler.newMetadata();
66
67     assertThat(metadata.getDispatcherKey()).isEqualTo(REPORT_FAILURE_DISPATCHER_KEY);
68   }
69
70   @Test
71   public void reportFailures_notification_is_enable_at_global_level() {
72     NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationHandler.newMetadata();
73
74     assertThat(metadata.getProperty(GLOBAL_NOTIFICATION)).isEqualTo("true");
75   }
76
77   @Test
78   public void reportFailures_notification_is_enable_at_project_level() {
79     NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationHandler.newMetadata();
80
81     assertThat(metadata.getProperty(PER_PROJECT_NOTIFICATION)).isEqualTo("true");
82   }
83
84   @Test
85   public void getNotificationClass_is_ReportAnalysisFailureNotification() {
86     assertThat(underTest.getNotificationClass()).isEqualTo(ReportAnalysisFailureNotification.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     verifyZeroInteractions(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<ReportAnalysisFailureNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
102       .mapToObj(i -> mock(ReportAnalysisFailureNotification.class))
103       .collect(toSet());
104
105     int deliver = underTest.deliver(notifications);
106
107     assertThat(deliver).isZero();
108     verifyZeroInteractions(notificationManager);
109     verify(emailNotificationChannel).isActivated();
110     verifyNoMoreInteractions(emailNotificationChannel);
111     notifications.forEach(Mockito::verifyZeroInteractions);
112   }
113
114   @Test
115   public void deliver_has_no_effect_if_no_notification_has_projectKey() {
116     when(emailNotificationChannel.isActivated()).thenReturn(true);
117     Set<ReportAnalysisFailureNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
118       .mapToObj(i -> newNotification(null))
119       .collect(toSet());
120
121     int deliver = underTest.deliver(notifications);
122
123     assertThat(deliver).isZero();
124     verifyZeroInteractions(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_subscribed_recipients_to_ReportFailure_notifications() {
135     String projectKey = randomAlphabetic(12);
136     ReportAnalysisFailureNotification notification = newNotification(projectKey);
137     when(emailNotificationChannel.isActivated()).thenReturn(true);
138     when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS))
139       .thenReturn(emptySet());
140
141     int deliver = underTest.deliver(Collections.singleton(notification));
142
143     assertThat(deliver).isZero();
144     verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS);
145     verifyNoMoreInteractions(notificationManager);
146     verify(emailNotificationChannel).isActivated();
147     verifyNoMoreInteractions(emailNotificationChannel);
148   }
149
150   @Test
151   public void deliver_ignores_notification_without_projectKey() {
152     String projectKey = randomAlphabetic(10);
153     Set<ReportAnalysisFailureNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
154       .mapToObj(i -> newNotification(projectKey))
155       .collect(toSet());
156     Set<ReportAnalysisFailureNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
157       .mapToObj(i -> newNotification(null))
158       .collect(toSet());
159     Set<EmailRecipient> emailRecipients = IntStream.range(0, 1 + new Random().nextInt(10))
160       .mapToObj(i -> "user_" + i)
161       .map(login -> new EmailRecipient(login, emailOf(login)))
162       .collect(toSet());
163     Set<EmailDeliveryRequest> expectedRequests = emailRecipients.stream()
164       .flatMap(emailRecipient -> withProjectKey.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.getEmail(), notif)))
165       .collect(toSet());
166     when(emailNotificationChannel.isActivated()).thenReturn(true);
167     when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS))
168       .thenReturn(emailRecipients);
169
170     Set<ReportAnalysisFailureNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream())
171       .flatMap(t -> t)
172       .collect(toSet());
173     int deliver = underTest.deliver(notifications);
174
175     assertThat(deliver).isZero();
176     verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS);
177     verifyNoMoreInteractions(notificationManager);
178     verify(emailNotificationChannel).isActivated();
179     verify(emailNotificationChannel).deliverAll(expectedRequests);
180     verifyNoMoreInteractions(emailNotificationChannel);
181   }
182
183   @Test
184   public void deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ReportFailure_notifications() {
185     String projectKey1 = randomAlphabetic(10);
186     String projectKey2 = randomAlphabetic(11);
187     Set<ReportAnalysisFailureNotification> notifications1 = randomSetOfNotifications(projectKey1);
188     Set<ReportAnalysisFailureNotification> notifications2 = randomSetOfNotifications(projectKey2);
189     when(emailNotificationChannel.isActivated()).thenReturn(true);
190
191     Set<EmailRecipient> emailRecipients1 = IntStream.range(0, 1 + new Random().nextInt(10))
192       .mapToObj(i -> "user1_" + i)
193       .map(login -> new EmailRecipient(login, emailOf(login)))
194       .collect(toSet());
195     Set<EmailRecipient> emailRecipients2 = IntStream.range(0, 1 + new Random().nextInt(10))
196       .mapToObj(i -> "user2_" + i)
197       .map(login -> new EmailRecipient(login, emailOf(login)))
198       .collect(toSet());
199     when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey1, REQUIRED_SUBSCRIBER_PERMISSIONS))
200       .thenReturn(emailRecipients1);
201     when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey2, REQUIRED_SUBSCRIBER_PERMISSIONS))
202       .thenReturn(emailRecipients2);
203     Set<EmailDeliveryRequest> expectedRequests = Stream.concat(
204       emailRecipients1.stream()
205         .flatMap(emailRecipient -> notifications1.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.getEmail(), notif))),
206       emailRecipients2.stream()
207         .flatMap(emailRecipient -> notifications2.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.getEmail(), notif))))
208       .collect(toSet());
209
210     int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
211
212     assertThat(deliver).isZero();
213     verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey1, REQUIRED_SUBSCRIBER_PERMISSIONS);
214     verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey2, REQUIRED_SUBSCRIBER_PERMISSIONS);
215     verifyNoMoreInteractions(notificationManager);
216     verify(emailNotificationChannel).isActivated();
217     verify(emailNotificationChannel).deliverAll(expectedRequests);
218     verifyNoMoreInteractions(emailNotificationChannel);
219   }
220
221   @Test
222   public void deliver_send_notifications_to_all_subscribers_of_all_projects() {
223     String projectKey1 = randomAlphabetic(10);
224     String projectKey2 = randomAlphabetic(11);
225     Set<ReportAnalysisFailureNotification> notifications1 = randomSetOfNotifications(projectKey1);
226     Set<ReportAnalysisFailureNotification> notifications2 = randomSetOfNotifications(projectKey2);
227     when(emailNotificationChannel.isActivated()).thenReturn(true);
228     when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey1, REQUIRED_SUBSCRIBER_PERMISSIONS))
229       .thenReturn(emptySet());
230     when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey2, REQUIRED_SUBSCRIBER_PERMISSIONS))
231       .thenReturn(emptySet());
232
233     int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
234
235     assertThat(deliver).isZero();
236     verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey1, REQUIRED_SUBSCRIBER_PERMISSIONS);
237     verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey2, REQUIRED_SUBSCRIBER_PERMISSIONS);
238     verifyNoMoreInteractions(notificationManager);
239     verify(emailNotificationChannel).isActivated();
240     verifyNoMoreInteractions(emailNotificationChannel);
241   }
242
243   private static Set<ReportAnalysisFailureNotification> randomSetOfNotifications(@Nullable String projectKey) {
244     return IntStream.range(0, 1 + new Random().nextInt(5))
245       .mapToObj(i -> newNotification(projectKey))
246       .collect(Collectors.toSet());
247   }
248
249   private static ReportAnalysisFailureNotification newNotification(@Nullable String projectKey) {
250     ReportAnalysisFailureNotification notification = mock(ReportAnalysisFailureNotification.class);
251     when(notification.getProjectKey()).thenReturn(projectKey);
252     return notification;
253   }
254   private static String emailOf(String assignee1) {
255     return assignee1 + "@house";
256   }
257
258 }