2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.step;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.TemporaryFolder;
26 import org.mockito.Mockito;
27 import org.sonar.api.notifications.Notification;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.api.utils.System2;
30 import org.sonar.batch.protocol.Constants;
31 import org.sonar.batch.protocol.output.BatchReport;
32 import org.sonar.core.issue.DefaultIssue;
33 import org.sonar.server.computation.batch.BatchReportReaderRule;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.Component;
36 import org.sonar.server.computation.component.ReportComponent;
37 import org.sonar.server.computation.issue.IssueCache;
38 import org.sonar.server.computation.issue.RuleRepository;
39 import org.sonar.server.issue.notification.IssueChangeNotification;
40 import org.sonar.server.issue.notification.NewIssuesNotification;
41 import org.sonar.server.issue.notification.NewIssuesNotificationFactory;
42 import org.sonar.server.notification.NotificationService;
44 import static org.mockito.Mockito.any;
45 import static org.mockito.Mockito.atLeastOnce;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.never;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.when;
51 public class SendIssueNotificationsStepTest extends BaseStepTest {
53 private static final String PROJECT_UUID = "PROJECT_UUID";
54 private static final String PROJECT_KEY = "PROJECT_KEY";
57 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
60 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
63 public TemporaryFolder temp = new TemporaryFolder();
65 NotificationService notifService = mock(NotificationService.class);
66 IssueCache issueCache;
67 SendIssueNotificationsStep underTest;
70 public void setUp() throws Exception {
71 issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
72 NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class, Mockito.RETURNS_DEEP_STUBS);
73 underTest = new SendIssueNotificationsStep(issueCache, mock(RuleRepository.class), treeRootHolder, notifService, reportReader, newIssuesNotificationFactory);
75 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build());
77 reportReader.setMetadata(BatchReport.Metadata.newBuilder()
78 .setRootComponentRef(1)
80 reportReader.putComponent(BatchReport.Component.newBuilder()
82 .setType(Constants.ComponentType.PROJECT)
84 .setName("Project name")
89 public void do_not_send_notifications_if_no_subscribers() {
90 when(notifService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(false);
94 verify(notifService, never()).deliver(any(Notification.class));
98 public void send_notifications_if_subscribers() {
99 issueCache.newAppender().append(new DefaultIssue()
100 .setSeverity(Severity.BLOCKER)).close();
102 when(notifService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
106 verify(notifService).deliver(any(NewIssuesNotification.class));
107 verify(notifService, atLeastOnce()).deliver(any(IssueChangeNotification.class));
111 protected ComputationStep step() {