]> source.dussan.org Git - sonarqube.git/blob
6a692d8794e25549d29fbd445ea754f095a7dcb4
[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.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;
45
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;
55
56 public class SendIssueNotificationsStepTest extends BaseStepTest {
57
58   static final String PROJECT_UUID = "PROJECT_UUID";
59   static final String PROJECT_KEY = "PROJECT_KEY";
60   static final String PROJECT_NAME = "PROJECT_NAME";
61
62   static final long ANALYSE_DATE = 123L;
63
64   static final Duration ISSUE_DURATION = Duration.create(100L);
65   static final String ISSUE_ASSIGNEE = "John";
66
67   static final Component PROJECT = builder(Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).setName(PROJECT_NAME).build();
68
69   @Rule
70   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
71     .setRoot(PROJECT);
72
73   @Rule
74   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
75     .setAnalysisDate(new Date(ANALYSE_DATE));
76
77   @Rule
78   public TemporaryFolder temp = new TemporaryFolder();
79
80   NotificationService notificationService = mock(NotificationService.class);
81   NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class);
82   NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
83   MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
84
85   IssueCache issueCache;
86   SendIssueNotificationsStep underTest;
87
88   @Before
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);
93
94     when(newIssuesNotificationFactory.newNewIssuesNotication()).thenReturn(newIssuesNotificationMock);
95     when(newIssuesNotificationFactory.newMyNewIssuesNotification()).thenReturn(myNewIssuesNotificationMock);
96   }
97
98   @Test
99   public void do_not_send_notifications_if_no_subscribers() {
100     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(false);
101
102     underTest.execute();
103
104     verify(notificationService, never()).deliver(any(Notification.class));
105   }
106
107   @Test
108   public void send_global_new_issues_notification() throws Exception {
109     issueCache.newAppender().append(
110       new DefaultIssue().setSeverity(Severity.BLOCKER).setEffort(ISSUE_DURATION)
111       ).close();
112
113     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
114
115     underTest.execute();
116
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);
122   }
123
124   @Test
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)
128       ).close();
129
130     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
131
132     underTest.execute();
133
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);
140   }
141
142   @Test
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();
146
147     when(notificationService.hasProjectSubscribersForTypes(PROJECT_UUID, SendIssueNotificationsStep.NOTIF_TYPES)).thenReturn(true);
148
149     underTest.execute();
150
151     verify(notificationService).deliver(any(IssueChangeNotification.class));
152   }
153
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);
160     return notification;
161   }
162
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);
170     return notification;
171   }
172
173   @Override
174   protected ComputationStep step() {
175     return underTest;
176   }
177 }