3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.notification;
22 import java.util.Collections;
23 import java.util.Random;
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;
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.verifyNoInteractions;
46 import static org.mockito.Mockito.verifyNoMoreInteractions;
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;
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);
59 public void getMetadata_returns_same_instance_as_static_method() {
60 assertThat(underTest.getMetadata()).containsSame(ReportAnalysisFailureNotificationHandler.newMetadata());
64 public void verify_reportFailures_notification_dispatcher_key() {
65 NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationHandler.newMetadata();
67 assertThat(metadata.getDispatcherKey()).isEqualTo(REPORT_FAILURE_DISPATCHER_KEY);
71 public void reportFailures_notification_is_enable_at_global_level() {
72 NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationHandler.newMetadata();
74 assertThat(metadata.getProperty(GLOBAL_NOTIFICATION)).isEqualTo("true");
78 public void reportFailures_notification_is_enable_at_project_level() {
79 NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationHandler.newMetadata();
81 assertThat(metadata.getProperty(PER_PROJECT_NOTIFICATION)).isEqualTo("true");
85 public void getNotificationClass_is_ReportAnalysisFailureNotification() {
86 assertThat(underTest.getNotificationClass()).isEqualTo(ReportAnalysisFailureNotification.class);
90 public void deliver_has_no_effect_if_notifications_is_empty() {
91 when(emailNotificationChannel.isActivated()).thenReturn(true);
92 int deliver = underTest.deliver(Collections.emptyList());
94 assertThat(deliver).isZero();
95 verifyNoInteractions(notificationManager, emailNotificationChannel);
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))
105 int deliver = underTest.deliver(notifications);
107 assertThat(deliver).isZero();
108 verifyNoInteractions(notificationManager);
109 verify(emailNotificationChannel).isActivated();
110 verifyNoMoreInteractions(emailNotificationChannel);
111 notifications.forEach(Mockito::verifyNoInteractions);
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))
121 int deliver = underTest.deliver(notifications);
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);
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());
141 int deliver = underTest.deliver(Collections.singleton(notification));
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);
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))
156 Set<ReportAnalysisFailureNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
157 .mapToObj(i -> newNotification(null))
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)))
163 Set<EmailDeliveryRequest> expectedRequests = emailRecipients.stream()
164 .flatMap(emailRecipient -> withProjectKey.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.email(), notif)))
166 when(emailNotificationChannel.isActivated()).thenReturn(true);
167 when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS))
168 .thenReturn(emailRecipients);
170 Set<ReportAnalysisFailureNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream())
173 int deliver = underTest.deliver(notifications);
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);
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);
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)))
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)))
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.email(), notif))),
206 emailRecipients2.stream()
207 .flatMap(emailRecipient -> notifications2.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.email(), notif))))
210 int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
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);
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());
233 int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
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);
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());
249 private static ReportAnalysisFailureNotification newNotification(@Nullable String projectKey) {
250 ReportAnalysisFailureNotification notification = mock(ReportAnalysisFailureNotification.class);
251 when(notification.getProjectKey()).thenReturn(projectKey);
254 private static String emailOf(String assignee1) {
255 return assignee1 + "@house";