3 * Copyright (C) 2009-2017 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.notification;
22 import com.google.common.collect.HashMultimap;
23 import org.junit.Test;
24 import org.sonar.api.notifications.Notification;
25 import org.sonar.api.notifications.NotificationChannel;
26 import org.sonar.api.web.UserRole;
27 import org.sonar.server.notification.NotificationDispatcher;
28 import org.sonar.server.notification.NotificationDispatcherMetadata;
29 import org.sonar.server.notification.NotificationManager;
30 import org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject;
32 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.verifyNoMoreInteractions;
37 import static org.mockito.Mockito.when;
39 public class ReportAnalysisFailureNotificationDispatcherTest {
40 private NotificationManager notificationManager = mock(NotificationManager.class);
41 private Notification notificationMock = mock(Notification.class);
42 private NotificationDispatcher.Context contextMock = mock(NotificationDispatcher.Context.class);
43 private ReportAnalysisFailureNotificationDispatcher underTest = new ReportAnalysisFailureNotificationDispatcher(notificationManager);
46 public void dispatcher_defines_key() {
47 assertThat(underTest.getKey()).isNotEmpty();
51 public void newMetadata_indicates_enabled_global_and_project_level_notifications() {
52 NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationDispatcher.newMetadata();
54 assertThat(metadata.getProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)).isEqualTo("true");
55 assertThat(metadata.getProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)).isEqualTo("true");
59 public void performDispatch_has_no_effect_if_type_is_empty() {
60 when(notificationMock.getType()).thenReturn("");
62 underTest.performDispatch(notificationMock, contextMock);
64 verify(notificationMock).getType();
65 verifyNoMoreInteractions(notificationMock, contextMock);
69 public void performDispatch_has_no_effect_if_type_is_not_ReportAnalysisFailureNotification_TYPE() {
70 when(notificationMock.getType()).thenReturn(randomAlphanumeric(6));
72 underTest.performDispatch(notificationMock, contextMock);
74 verify(notificationMock).getType();
75 verifyNoMoreInteractions(notificationMock, contextMock);
79 public void performDispatch_adds_user_for_each_recipient_and_channel_for_the_component_uuid_in_the_notification() {
80 when(notificationMock.getType()).thenReturn(ReportAnalysisFailureNotification.TYPE);
81 String projectUuid = randomAlphanumeric(9);
82 when(notificationMock.getFieldValue("project.uuid")).thenReturn(projectUuid);
83 HashMultimap<String, NotificationChannel> multimap = HashMultimap.create();
84 String login1 = randomAlphanumeric(3);
85 String login2 = randomAlphanumeric(3);
86 NotificationChannel channel1 = mock(NotificationChannel.class);
87 NotificationChannel channel2 = mock(NotificationChannel.class);
88 NotificationChannel channel3 = mock(NotificationChannel.class);
89 multimap.put(login1, channel1);
90 multimap.put(login1, channel2);
91 multimap.put(login2, channel2);
92 multimap.put(login2, channel3);
93 when(notificationManager.findSubscribedRecipientsForDispatcher(underTest, projectUuid, new SubscriberPermissionsOnProject(UserRole.ADMIN, UserRole.USER)))
94 .thenReturn(multimap);
96 underTest.performDispatch(notificationMock, contextMock);
98 verify(contextMock).addUser(login1, channel1);
99 verify(contextMock).addUser(login1, channel2);
100 verify(contextMock).addUser(login2, channel2);
101 verify(contextMock).addUser(login2, channel3);
102 verifyNoMoreInteractions(contextMock);
106 public void performDispatch_adds_no_user_if_notification_manager_returns_none() {
107 when(notificationMock.getType()).thenReturn(ReportAnalysisFailureNotification.TYPE);
108 String projectUuid = randomAlphanumeric(9);
109 when(notificationMock.getFieldValue("project.uuid")).thenReturn(projectUuid);
110 HashMultimap<String, NotificationChannel> multimap = HashMultimap.create();
111 when(notificationManager.findSubscribedRecipientsForDispatcher(underTest, projectUuid, new SubscriberPermissionsOnProject(UserRole.ADMIN, UserRole.USER)))
112 .thenReturn(multimap);
114 underTest.performDispatch(notificationMock, contextMock);
116 verifyNoMoreInteractions(contextMock);