]> source.dussan.org Git - sonarqube.git/blob
92c233a8fc841e97b88f3482bd47d8cc6d3e4e9c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.notification;
21
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
31 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.verifyNoMoreInteractions;
36 import static org.mockito.Mockito.when;
37
38 public class ReportAnalysisFailureNotificationDispatcherTest {
39   private NotificationManager notificationManager = mock(NotificationManager.class);
40   private Notification notificationMock = mock(Notification.class);
41   private NotificationDispatcher.Context contextMock = mock(NotificationDispatcher.Context.class);
42   private ReportAnalysisFailureNotificationDispatcher underTest = new ReportAnalysisFailureNotificationDispatcher(notificationManager);
43
44   @Test
45   public void dispatcher_defines_key() {
46     assertThat(underTest.getKey()).isNotEmpty();
47   }
48
49   @Test
50   public void newMetadata_indicates_enabled_global_and_project_level_notifications() {
51     NotificationDispatcherMetadata metadata = ReportAnalysisFailureNotificationDispatcher.newMetadata();
52
53     assertThat(metadata.getProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)).isEqualTo("true");
54     assertThat(metadata.getProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)).isEqualTo("true");
55   }
56
57   @Test
58   public void performDispatch_has_no_effect_if_type_is_empty() {
59     when(notificationMock.getType()).thenReturn("");
60
61     underTest.performDispatch(notificationMock, contextMock);
62
63     verify(notificationMock).getType();
64     verifyNoMoreInteractions(notificationMock, contextMock);
65   }
66
67   @Test
68   public void performDispatch_has_no_effect_if_type_is_not_ReportAnalysisFailureNotification_TYPE() {
69     when(notificationMock.getType()).thenReturn(randomAlphanumeric(6));
70
71     underTest.performDispatch(notificationMock, contextMock);
72
73     verify(notificationMock).getType();
74     verifyNoMoreInteractions(notificationMock, contextMock);
75   }
76
77   @Test
78   public void performDispatch_adds_user_for_each_recipient_and_channel_for_the_component_uuid_in_the_notification() {
79     when(notificationMock.getType()).thenReturn(ReportAnalysisFailureNotification.TYPE);
80     String projectUuid = randomAlphanumeric(9);
81     when(notificationMock.getFieldValue("project.uuid")).thenReturn(projectUuid);
82     HashMultimap<String, NotificationChannel> multimap = HashMultimap.create();
83     String login1 = randomAlphanumeric(3);
84     String login2 = randomAlphanumeric(3);
85     NotificationChannel channel1 = mock(NotificationChannel.class);
86     NotificationChannel channel2 = mock(NotificationChannel.class);
87     NotificationChannel channel3 = mock(NotificationChannel.class);
88     multimap.put(login1, channel1);
89     multimap.put(login1, channel2);
90     multimap.put(login2, channel2);
91     multimap.put(login2, channel3);
92     when(notificationManager.findSubscribedRecipientsForDispatcher(underTest, projectUuid, new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER)))
93       .thenReturn(multimap);
94
95     underTest.performDispatch(notificationMock, contextMock);
96
97     verify(contextMock).addUser(login1, channel1);
98     verify(contextMock).addUser(login1, channel2);
99     verify(contextMock).addUser(login2, channel2);
100     verify(contextMock).addUser(login2, channel3);
101     verifyNoMoreInteractions(contextMock);
102   }
103
104   @Test
105   public void performDispatch_adds_no_user_if_notification_manager_returns_none() {
106     when(notificationMock.getType()).thenReturn(ReportAnalysisFailureNotification.TYPE);
107     String projectUuid = randomAlphanumeric(9);
108     when(notificationMock.getFieldValue("project.uuid")).thenReturn(projectUuid);
109     HashMultimap<String, NotificationChannel> multimap = HashMultimap.create();
110     when(notificationManager.findSubscribedRecipientsForDispatcher(underTest, projectUuid, new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER)))
111       .thenReturn(multimap);
112
113     underTest.performDispatch(notificationMock, contextMock);
114
115     verifyNoMoreInteractions(contextMock);
116   }
117 }