]> source.dussan.org Git - sonarqube.git/blob
9db49085e2f825a29ee87a048d3af65be82c69b2
[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.server.computation.task.projectanalysis.step;
21
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;
47
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;
57
58 public class SendIssueNotificationsStepTest extends BaseStepTest {
59
60   static final String PROJECT_UUID = "PROJECT_UUID";
61   static final String PROJECT_KEY = "PROJECT_KEY";
62   static final String PROJECT_NAME = "PROJECT_NAME";
63
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";
68
69   static final long ANALYSE_DATE = 123L;
70
71   static final Duration ISSUE_DURATION = Duration.create(100L);
72   static final String ISSUE_ASSIGNEE = "John";
73
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();
76
77   @Rule
78   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
79     .setRoot(PROJECT);
80
81   @Rule
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));
86
87   @Rule
88   public TemporaryFolder temp = new TemporaryFolder();
89
90   NotificationService notificationService = mock(NotificationService.class);
91   NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class);
92   NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
93   MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
94
95   IssueCache issueCache;
96   SendIssueNotificationsStep underTest;
97
98   @Before
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);
103
104     when(newIssuesNotificationFactory.newNewIssuesNotication()).thenReturn(newIssuesNotificationMock);
105     when(newIssuesNotificationFactory.newMyNewIssuesNotification()).thenReturn(myNewIssuesNotificationMock);
106   }
107
108   @Test
109   public void do_not_send_notifications_if_no_subscribers() {
110     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(false);
111
112     underTest.execute();
113
114     verify(notificationService, never()).deliver(any(Notification.class));
115   }
116
117   @Test
118   public void send_global_new_issues_notification() throws Exception {
119     issueCache.newAppender().append(
120       new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION)
121       ).close();
122
123     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
124
125     underTest.execute();
126
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);
132   }
133
134   @Test
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)
138       ).close();
139
140     when(notificationService.hasProjectSubscribersForTypes(BRANCH_COMPONENT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
141
142     analysisMetadataHolder.setBranch(new DefaultBranchImpl(BRANCH_NAME) {
143       @Override
144       public boolean isMain() {
145         return false;
146       }
147     });
148     treeRootHolder.setRoot(BRANCH);
149
150     underTest.execute();
151
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);
157   }
158
159   @Test
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)
163       ).close();
164
165     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
166
167     underTest.execute();
168
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);
175   }
176
177   @Test
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();
181
182     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
183
184     underTest.execute();
185
186     verify(notificationService).deliver(any(IssueChangeNotification.class));
187   }
188
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);
195     return notification;
196   }
197
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);
205     return notification;
206   }
207
208   @Override
209   protected ComputationStep step() {
210     return underTest;
211   }
212 }