]> source.dussan.org Git - sonarqube.git/blob
a3b3843bd83075e673545689a5faae4f34b53a9e
[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.qualitygate.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.email.EmailNotificationChannel;
34
35 import static java.util.Collections.emptySet;
36 import static java.util.stream.Collectors.toSet;
37 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoInteractions;
42 import static org.mockito.Mockito.verifyNoMoreInteractions;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION;
45 import static org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION;
46 import static org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER;
47
48 public class QGChangeNotificationHandlerTest {
49   private static final String QG_CHANGE_DISPATCHER_KEY = "NewAlerts";
50   private NotificationManager notificationManager = mock(NotificationManager.class);
51   private EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class);
52   private QGChangeNotificationHandler underTest = new QGChangeNotificationHandler(notificationManager, emailNotificationChannel);
53
54   @Test
55   public void getMetadata_returns_same_instance_as_static_method() {
56     assertThat(underTest.getMetadata()).containsSame(QGChangeNotificationHandler.newMetadata());
57   }
58
59   @Test
60   public void verify_qgChange_notification_dispatcher_key() {
61     NotificationDispatcherMetadata metadata = QGChangeNotificationHandler.newMetadata();
62
63     assertThat(metadata.getDispatcherKey()).isEqualTo(QG_CHANGE_DISPATCHER_KEY);
64   }
65
66   @Test
67   public void qgChange_notification_is_enable_at_global_level() {
68     NotificationDispatcherMetadata metadata = QGChangeNotificationHandler.newMetadata();
69
70     assertThat(metadata.getProperty(GLOBAL_NOTIFICATION)).isEqualTo("true");
71   }
72
73   @Test
74   public void qgChange_notification_is_enable_at_project_level() {
75     NotificationDispatcherMetadata metadata = QGChangeNotificationHandler.newMetadata();
76
77     assertThat(metadata.getProperty(PER_PROJECT_NOTIFICATION)).isEqualTo("true");
78   }
79
80   @Test
81   public void getNotificationClass_is_QGChangeNotification() {
82     assertThat(underTest.getNotificationClass()).isEqualTo(QGChangeNotification.class);
83   }
84
85   @Test
86   public void deliver_has_no_effect_if_notifications_is_empty() {
87     when(emailNotificationChannel.isActivated()).thenReturn(true);
88     int deliver = underTest.deliver(Collections.emptyList());
89
90     assertThat(deliver).isZero();
91     verifyNoInteractions(notificationManager, emailNotificationChannel);
92   }
93
94   @Test
95   public void deliver_has_no_effect_if_emailNotificationChannel_is_disabled() {
96     when(emailNotificationChannel.isActivated()).thenReturn(false);
97     Set<QGChangeNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
98       .mapToObj(i -> mock(QGChangeNotification.class))
99       .collect(toSet());
100
101     int deliver = underTest.deliver(notifications);
102
103     assertThat(deliver).isZero();
104     verifyNoInteractions(notificationManager);
105     verify(emailNotificationChannel).isActivated();
106     verifyNoMoreInteractions(emailNotificationChannel);
107     notifications.forEach(Mockito::verifyNoInteractions);
108   }
109
110   @Test
111   public void deliver_has_no_effect_if_no_notification_has_projectKey() {
112     when(emailNotificationChannel.isActivated()).thenReturn(true);
113     Set<QGChangeNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10))
114       .mapToObj(i -> newNotification(null))
115       .collect(toSet());
116
117     int deliver = underTest.deliver(notifications);
118
119     assertThat(deliver).isZero();
120     verifyNoInteractions(notificationManager);
121     verify(emailNotificationChannel).isActivated();
122     verifyNoMoreInteractions(emailNotificationChannel);
123     notifications.forEach(notification -> {
124       verify(notification).getProjectKey();
125       verifyNoMoreInteractions(notification);
126     });
127   }
128
129   @Test
130   public void deliver_has_no_effect_if_no_notification_has_subscribed_recipients_to_QGChange_notifications() {
131     String projectKey = randomAlphabetic(12);
132     QGChangeNotification notification = newNotification(projectKey);
133     when(emailNotificationChannel.isActivated()).thenReturn(true);
134     when(notificationManager.findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER))
135       .thenReturn(emptySet());
136
137     int deliver = underTest.deliver(Collections.singleton(notification));
138
139     assertThat(deliver).isZero();
140     verify(notificationManager).findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER);
141     verifyNoMoreInteractions(notificationManager);
142     verify(emailNotificationChannel).isActivated();
143     verifyNoMoreInteractions(emailNotificationChannel);
144   }
145
146   @Test
147   public void deliver_ignores_notification_without_projectKey() {
148     String projectKey = randomAlphabetic(10);
149     Set<QGChangeNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
150       .mapToObj(i -> newNotification(projectKey))
151       .collect(toSet());
152     Set<QGChangeNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5))
153       .mapToObj(i -> newNotification(null))
154       .collect(toSet());
155     Set<NotificationManager.EmailRecipient> emailRecipients = IntStream.range(0, 1 + new Random().nextInt(10))
156       .mapToObj(i -> "user_" + i)
157       .map(login -> new NotificationManager.EmailRecipient(login, emailOf(login)))
158       .collect(toSet());
159     Set<EmailNotificationChannel.EmailDeliveryRequest> expectedRequests = emailRecipients.stream()
160       .flatMap(emailRecipient -> withProjectKey.stream().map(notif -> new EmailNotificationChannel.EmailDeliveryRequest(emailRecipient.email(), notif)))
161       .collect(toSet());
162     when(emailNotificationChannel.isActivated()).thenReturn(true);
163     when(notificationManager.findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER))
164       .thenReturn(emailRecipients);
165
166     Set<QGChangeNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream())
167       .flatMap(t -> t)
168       .collect(toSet());
169     int deliver = underTest.deliver(notifications);
170
171     assertThat(deliver).isZero();
172     verify(notificationManager).findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER);
173     verifyNoMoreInteractions(notificationManager);
174     verify(emailNotificationChannel).isActivated();
175     verify(emailNotificationChannel).deliverAll(expectedRequests);
176     verifyNoMoreInteractions(emailNotificationChannel);
177   }
178
179   @Test
180   public void deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_QGChange_notifications() {
181     String projectKey1 = randomAlphabetic(10);
182     String projectKey2 = randomAlphabetic(11);
183     Set<QGChangeNotification> notifications1 = randomSetOfNotifications(projectKey1);
184     Set<QGChangeNotification> notifications2 = randomSetOfNotifications(projectKey2);
185     when(emailNotificationChannel.isActivated()).thenReturn(true);
186
187     Set<NotificationManager.EmailRecipient> emailRecipients1 = IntStream.range(0, 1 + new Random().nextInt(10))
188       .mapToObj(i -> "user1_" + i)
189       .map(login -> new NotificationManager.EmailRecipient(login, emailOf(login)))
190       .collect(toSet());
191     Set<NotificationManager.EmailRecipient> emailRecipients2 = IntStream.range(0, 1 + new Random().nextInt(10))
192       .mapToObj(i -> "user2_" + i)
193       .map(login -> new NotificationManager.EmailRecipient(login, emailOf(login)))
194       .collect(toSet());
195     when(notificationManager.findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey1, ALL_MUST_HAVE_ROLE_USER))
196       .thenReturn(emailRecipients1);
197     when(notificationManager.findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey2, ALL_MUST_HAVE_ROLE_USER))
198       .thenReturn(emailRecipients2);
199     Set<EmailNotificationChannel.EmailDeliveryRequest> expectedRequests = Stream.concat(
200       emailRecipients1.stream()
201         .flatMap(emailRecipient -> notifications1.stream().map(notif -> new EmailNotificationChannel.EmailDeliveryRequest(emailRecipient.email(), notif))),
202       emailRecipients2.stream()
203         .flatMap(emailRecipient -> notifications2.stream().map(notif -> new EmailNotificationChannel.EmailDeliveryRequest(emailRecipient.email(), notif))))
204       .collect(toSet());
205
206     int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
207
208     assertThat(deliver).isZero();
209     verify(notificationManager).findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey1, ALL_MUST_HAVE_ROLE_USER);
210     verify(notificationManager).findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey2, ALL_MUST_HAVE_ROLE_USER);
211     verifyNoMoreInteractions(notificationManager);
212     verify(emailNotificationChannel).isActivated();
213     verify(emailNotificationChannel).deliverAll(expectedRequests);
214     verifyNoMoreInteractions(emailNotificationChannel);
215   }
216
217   @Test
218   public void deliver_send_notifications_to_all_subscribers_of_all_projects() {
219     String projectKey1 = randomAlphabetic(10);
220     String projectKey2 = randomAlphabetic(11);
221     Set<QGChangeNotification> notifications1 = randomSetOfNotifications(projectKey1);
222     Set<QGChangeNotification> notifications2 = randomSetOfNotifications(projectKey2);
223     when(emailNotificationChannel.isActivated()).thenReturn(true);
224     when(notificationManager.findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey1, ALL_MUST_HAVE_ROLE_USER))
225       .thenReturn(emptySet());
226     when(notificationManager.findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey2, ALL_MUST_HAVE_ROLE_USER))
227       .thenReturn(emptySet());
228
229     int deliver = underTest.deliver(Stream.concat(notifications1.stream(), notifications2.stream()).collect(toSet()));
230
231     assertThat(deliver).isZero();
232     verify(notificationManager).findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey1, ALL_MUST_HAVE_ROLE_USER);
233     verify(notificationManager).findSubscribedEmailRecipients(QG_CHANGE_DISPATCHER_KEY, projectKey2, ALL_MUST_HAVE_ROLE_USER);
234     verifyNoMoreInteractions(notificationManager);
235     verify(emailNotificationChannel).isActivated();
236     verifyNoMoreInteractions(emailNotificationChannel);
237   }
238
239   private static Set<QGChangeNotification> randomSetOfNotifications(@Nullable String projectKey) {
240     return IntStream.range(0, 1 + new Random().nextInt(5))
241       .mapToObj(i -> newNotification(projectKey))
242       .collect(Collectors.toSet());
243   }
244
245   private static QGChangeNotification newNotification(@Nullable String projectKey) {
246     QGChangeNotification notification = mock(QGChangeNotification.class);
247     when(notification.getProjectKey()).thenReturn(projectKey);
248     return notification;
249   }
250
251   private static String emailOf(String assignee1) {
252     return assignee1 + "@giraffe";
253   }
254
255 }