]> source.dussan.org Git - sonarqube.git/blob
f0d088d383cdb60404ead413e9962222f2ae51a6
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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.server.computation.step;
21
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;
43
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;
50
51 public class SendIssueNotificationsStepTest extends BaseStepTest {
52
53   private static final String PROJECT_UUID = "PROJECT_UUID";
54   private static final String PROJECT_KEY = "PROJECT_KEY";
55
56   @Rule
57   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
58
59   @Rule
60   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
61
62   @Rule
63   public TemporaryFolder temp = new TemporaryFolder();
64
65   NotificationService notifService = mock(NotificationService.class);
66   IssueCache issueCache;
67   SendIssueNotificationsStep underTest;
68
69   @Before
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);
74
75     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build());
76
77     reportReader.setMetadata(BatchReport.Metadata.newBuilder()
78       .setRootComponentRef(1)
79       .build());
80     reportReader.putComponent(BatchReport.Component.newBuilder()
81       .setRef(1)
82       .setType(Constants.ComponentType.PROJECT)
83       .setKey(PROJECT_KEY)
84       .setName("Project name")
85       .build());
86   }
87
88   @Test
89   public void do_not_send_notifications_if_no_subscribers() {
90     when(notifService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(false);
91
92     underTest.execute();
93
94     verify(notifService, never()).deliver(any(Notification.class));
95   }
96
97   @Test
98   public void send_notifications_if_subscribers() {
99     issueCache.newAppender().append(new DefaultIssue()
100       .setSeverity(Severity.BLOCKER)).close();
101
102     when(notifService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
103
104     underTest.execute();
105
106     verify(notifService).deliver(any(NewIssuesNotification.class));
107     verify(notifService, atLeastOnce()).deliver(any(IssueChangeNotification.class));
108   }
109
110   @Override
111   protected ComputationStep step() {
112     return underTest;
113   }
114 }