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.analysis.Project;
34 import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
35 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
36 import org.sonar.server.computation.task.projectanalysis.component.Component;
37 import org.sonar.server.computation.task.projectanalysis.component.Component.Type;
38 import org.sonar.server.computation.task.projectanalysis.issue.IssueCache;
39 import org.sonar.server.computation.task.projectanalysis.issue.RuleRepository;
40 import org.sonar.server.computation.task.step.ComputationStep;
41 import org.sonar.server.issue.notification.IssueChangeNotification;
42 import org.sonar.server.issue.notification.MyNewIssuesNotification;
43 import org.sonar.server.issue.notification.NewIssuesNotification;
44 import org.sonar.server.issue.notification.NewIssuesNotificationFactory;
45 import org.sonar.server.issue.notification.NewIssuesStatistics;
46 import org.sonar.server.notification.NotificationService;
48 import static org.mockito.Matchers.anyString;
49 import static org.mockito.Matchers.eq;
50 import static org.mockito.Mockito.any;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.never;
53 import static org.mockito.Mockito.times;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.when;
56 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
58 public class SendIssueNotificationsStepTest extends BaseStepTest {
60 static final String PROJECT_UUID = "PROJECT_UUID";
61 static final String PROJECT_KEY = "PROJECT_KEY";
62 static final String PROJECT_NAME = "PROJECT_NAME";
64 static final String BRANCH_COMPONENT_UUID = "BRANCH_UUID";
65 static final String BRANCH_COMPONENT_KEY = "BRANCH_KEY";
66 static final String BRANCH_COMPONENT_NAME = "BRANCH_NAME";
67 static final String BRANCH_NAME = "feature";
69 static final long ANALYSE_DATE = 123L;
71 static final Duration ISSUE_DURATION = Duration.create(100L);
72 static final String ISSUE_ASSIGNEE = "John";
74 static final Component PROJECT = builder(Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).setName(PROJECT_NAME).build();
75 static final Component BRANCH = builder(Type.PROJECT, 2).setUuid(BRANCH_COMPONENT_UUID).setKey(BRANCH_COMPONENT_KEY).setName(BRANCH_COMPONENT_NAME).build();
78 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
82 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
83 .setProject(new Project(PROJECT_UUID, PROJECT_KEY, PROJECT_NAME))
84 .setBranch(new DefaultBranchImpl())
85 .setAnalysisDate(new Date(ANALYSE_DATE));
88 public TemporaryFolder temp = new TemporaryFolder();
90 NotificationService notificationService = mock(NotificationService.class);
91 NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class);
92 NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
93 MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
95 IssueCache issueCache;
96 SendIssueNotificationsStep underTest;
99 public void setUp() throws Exception {
100 issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
101 underTest = new SendIssueNotificationsStep(issueCache, mock(RuleRepository.class), treeRootHolder, notificationService, analysisMetadataHolder,
102 newIssuesNotificationFactory);
104 when(newIssuesNotificationFactory.newNewIssuesNotication()).thenReturn(newIssuesNotificationMock);
105 when(newIssuesNotificationFactory.newMyNewIssuesNotification()).thenReturn(myNewIssuesNotificationMock);
109 public void do_not_send_notifications_if_no_subscribers() {
110 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(false);
114 verify(notificationService, never()).deliver(any(Notification.class));
118 public void send_global_new_issues_notification() throws Exception {
119 issueCache.newAppender().append(
120 new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION)
123 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
127 verify(notificationService).deliver(any(NewIssuesNotification.class));
128 verify(newIssuesNotificationMock).setProject(PROJECT_KEY, PROJECT_UUID, PROJECT_NAME, null);
129 verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
130 verify(newIssuesNotificationMock).setStatistics(eq(PROJECT_NAME), any(NewIssuesStatistics.Stats.class));
131 verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION);
135 public void send_global_new_issues_notification_on_branch() throws Exception {
136 issueCache.newAppender().append(
137 new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION)
140 when(notificationService.hasProjectSubscribersForTypes(BRANCH_COMPONENT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
142 analysisMetadataHolder.setBranch(new DefaultBranchImpl(BRANCH_NAME) {
144 public boolean isMain() {
148 treeRootHolder.setRoot(BRANCH);
152 verify(notificationService).deliver(any(NewIssuesNotification.class));
153 verify(newIssuesNotificationMock).setProject(PROJECT_KEY, BRANCH_COMPONENT_UUID, BRANCH_COMPONENT_NAME, BRANCH_NAME);
154 verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
155 verify(newIssuesNotificationMock).setStatistics(eq(BRANCH_COMPONENT_NAME), any(NewIssuesStatistics.Stats.class));
156 verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION);
160 public void send_new_issues_notification_to_user() throws Exception {
161 issueCache.newAppender().append(
162 new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION).setAssignee(ISSUE_ASSIGNEE)
165 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
169 verify(notificationService, times(2)).deliver(any(Notification.class));
170 verify(myNewIssuesNotificationMock).setAssignee(ISSUE_ASSIGNEE);
171 verify(myNewIssuesNotificationMock).setProject(PROJECT_KEY, PROJECT_UUID, PROJECT_NAME, null);
172 verify(myNewIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
173 verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT_NAME), any(NewIssuesStatistics.Stats.class));
174 verify(myNewIssuesNotificationMock).setDebt(ISSUE_DURATION);
178 public void send_issues_change_notification() throws Exception {
179 DefaultIssue issue = new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION).setChanged(true).setSendNotifications(true);
180 issueCache.newAppender().append(issue).close();
182 when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
186 verify(notificationService).deliver(any(IssueChangeNotification.class));
189 private NewIssuesNotification createNewIssuesNotificationMock() {
190 NewIssuesNotification notification = mock(NewIssuesNotification.class);
191 when(notification.setProject(anyString(), anyString(), anyString(), anyString())).thenReturn(notification);
192 when(notification.setAnalysisDate(any(Date.class))).thenReturn(notification);
193 when(notification.setStatistics(anyString(), any(NewIssuesStatistics.Stats.class))).thenReturn(notification);
194 when(notification.setDebt(any(Duration.class))).thenReturn(notification);
198 private MyNewIssuesNotification createMyNewIssuesNotificationMock() {
199 MyNewIssuesNotification notification = mock(MyNewIssuesNotification.class);
200 when(notification.setAssignee(anyString())).thenReturn(notification);
201 when(notification.setProject(anyString(), anyString(), anyString(), anyString())).thenReturn(notification);
202 when(notification.setAnalysisDate(any(Date.class))).thenReturn(notification);
203 when(notification.setStatistics(anyString(), any(NewIssuesStatistics.Stats.class))).thenReturn(notification);
204 when(notification.setDebt(any(Duration.class))).thenReturn(notification);
209 protected ComputationStep step() {