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.server.computation.task.projectanalysis.step;
22 import java.util.Date;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.TemporaryFolder;
27 import org.sonar.api.notifications.Notification;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.api.utils.Duration;
30 import org.sonar.api.utils.System2;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
34 import org.sonar.server.computation.task.projectanalysis.component.Component;
35 import org.sonar.server.computation.task.projectanalysis.component.Component.Type;
36 import org.sonar.server.computation.task.projectanalysis.issue.IssueCache;
37 import org.sonar.server.computation.task.projectanalysis.issue.RuleRepository;
38 import org.sonar.server.computation.task.step.ComputationStep;
39 import org.sonar.server.issue.notification.IssueChangeNotification;
40 import org.sonar.server.issue.notification.MyNewIssuesNotification;
41 import org.sonar.server.issue.notification.NewIssuesNotification;
42 import org.sonar.server.issue.notification.NewIssuesNotificationFactory;
43 import org.sonar.server.issue.notification.NewIssuesStatistics;
44 import org.sonar.server.notification.NotificationService;
46 import static org.mockito.Matchers.anyString;
47 import static org.mockito.Matchers.eq;
48 import static org.mockito.Mockito.any;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.never;
51 import static org.mockito.Mockito.times;
52 import static org.mockito.Mockito.verify;
53 import static org.mockito.Mockito.when;
54 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
56 public class SendIssueNotificationsStepTest extends BaseStepTest {
58 static final String PROJECT_UUID = "PROJECT_UUID";
59 static final String PROJECT_KEY = "PROJECT_KEY";
60 static final String PROJECT_NAME = "PROJECT_NAME";
62 static final long ANALYSE_DATE = 123L;
64 static final Duration ISSUE_DURATION = Duration.create(100L);
65 static final String ISSUE_ASSIGNEE = "John";
67 static final Component PROJECT = builder(Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).setName(PROJECT_NAME).build();
70 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
74 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
75 .setAnalysisDate(new Date(ANALYSE_DATE));
78 public TemporaryFolder temp = new TemporaryFolder();
80 NotificationService notificationService = mock(NotificationService.class);
81 NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class);
82 NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
83 MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
85 IssueCache issueCache;
86 SendIssueNotificationsStep underTest;
89 public void setUp() throws Exception {
90 issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
91 underTest = new SendIssueNotificationsStep(issueCache, mock(RuleRepository.class), treeRootHolder, notificationService, analysisMetadataHolder,
92 newIssuesNotificationFactory);
94 when(newIssuesNotificationFactory.newNewIssuesNotication()).thenReturn(newIssuesNotificationMock);
95 when(newIssuesNotificationFactory.newMyNewIssuesNotification()).thenReturn(myNewIssuesNotificationMock);
99 public void do_not_send_notifications_if_no_subscribers() {
100 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(false);
104 verify(notificationService, never()).deliver(any(Notification.class));
108 public void send_global_new_issues_notification() throws Exception {
109 issueCache.newAppender().append(
110 new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION)
113 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
117 verify(notificationService).deliver(any(NewIssuesNotification.class));
118 verify(newIssuesNotificationMock).setProject(PROJECT_KEY, PROJECT_UUID, PROJECT_NAME);
119 verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
120 verify(newIssuesNotificationMock).setStatistics(eq(PROJECT_NAME), any(NewIssuesStatistics.Stats.class));
121 verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION);
125 public void send_new_issues_notification_to_user() throws Exception {
126 issueCache.newAppender().append(
127 new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION).setAssignee(ISSUE_ASSIGNEE)
130 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
134 verify(notificationService, times(2)).deliver(any(Notification.class));
135 verify(myNewIssuesNotificationMock).setAssignee(ISSUE_ASSIGNEE);
136 verify(myNewIssuesNotificationMock).setProject(PROJECT_KEY, PROJECT_UUID, PROJECT_NAME);
137 verify(myNewIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
138 verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT_NAME), any(NewIssuesStatistics.Stats.class));
139 verify(myNewIssuesNotificationMock).setDebt(ISSUE_DURATION);
143 public void send_issues_change_notification() throws Exception {
144 DefaultIssue issue = new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION).setChanged(true).setSendNotifications(true);
145 issueCache.newAppender().append(issue).close();
147 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
151 verify(notificationService).deliver(any(IssueChangeNotification.class));
154 private NewIssuesNotification createNewIssuesNotificationMock() {
155 NewIssuesNotification notification = mock(NewIssuesNotification.class);
156 when(notification.setProject(anyString(), anyString(), anyString())).thenReturn(notification);
157 when(notification.setAnalysisDate(any(Date.class))).thenReturn(notification);
158 when(notification.setStatistics(anyString(), any(NewIssuesStatistics.Stats.class))).thenReturn(notification);
159 when(notification.setDebt(any(Duration.class))).thenReturn(notification);
163 private MyNewIssuesNotification createMyNewIssuesNotificationMock() {
164 MyNewIssuesNotification notification = mock(MyNewIssuesNotification.class);
165 when(notification.setAssignee(anyString())).thenReturn(notification);
166 when(notification.setProject(anyString(), anyString(), anyString())).thenReturn(notification);
167 when(notification.setAnalysisDate(any(Date.class))).thenReturn(notification);
168 when(notification.setStatistics(anyString(), any(NewIssuesStatistics.Stats.class))).thenReturn(notification);
169 when(notification.setDebt(any(Duration.class))).thenReturn(notification);
174 protected ComputationStep step() {