]> source.dussan.org Git - sonarqube.git/blob
2262e3e80f18ac4f8df61f1c70328aab5e101772
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 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.plugins.core.issue.notification;
21
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.sonar.api.batch.SensorContext;
27 import org.sonar.api.component.Component;
28 import org.sonar.api.issue.Issue;
29 import org.sonar.api.resources.Project;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.api.rules.Rule;
32 import org.sonar.api.rules.RuleFinder;
33 import org.sonar.api.utils.DateUtils;
34 import org.sonar.batch.issue.IssueCache;
35 import org.sonar.core.issue.DefaultIssue;
36 import org.sonar.core.issue.IssueChangeContext;
37 import org.sonar.core.issue.IssueNotifications;
38
39 import java.util.Arrays;
40
41 import static org.mockito.Mockito.*;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class SendIssueNotificationsPostJobTest {
45   @Mock
46   Project project;
47
48   @Mock
49   IssueCache issueCache;
50
51   @Mock
52   IssueNotifications notifications;
53
54   @Mock
55   RuleFinder ruleFinder;
56
57   @Mock
58   SensorContext sensorContext;
59
60   @Test
61   public void should_send_notif_if_new_issues() throws Exception {
62     when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
63     when(issueCache.all()).thenReturn(Arrays.asList(
64       new DefaultIssue().setNew(true),
65       new DefaultIssue().setNew(false)
66     ));
67
68     SendIssueNotificationsPostJob job = new SendIssueNotificationsPostJob(issueCache, notifications, ruleFinder);
69     job.executeOn(project, sensorContext);
70
71     verify(notifications).sendNewIssues(project, 1);
72   }
73
74   @Test
75   public void should_not_send_notif_if_no_new_issues() throws Exception {
76     when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
77     when(issueCache.all()).thenReturn(Arrays.asList(
78       new DefaultIssue().setNew(false)
79     ));
80
81     SendIssueNotificationsPostJob job = new SendIssueNotificationsPostJob(issueCache, notifications, ruleFinder);
82     job.executeOn(project, sensorContext);
83
84     verifyZeroInteractions(notifications);
85   }
86
87   @Test
88   public void should_send_notif_if_issue_change() throws Exception {
89
90     when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
91     RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
92     Rule rule = new Rule("squid", "AvoidCycles");
93     DefaultIssue issue = new DefaultIssue()
94       .setChanged(true)
95       .setFieldDiff(mock(IssueChangeContext.class), "severity", "MINOR", "BLOCKER")
96       .setRuleKey(ruleKey);
97     when(issueCache.all()).thenReturn(Arrays.asList(issue));
98     when(ruleFinder.findByKey(ruleKey)).thenReturn(rule);
99
100     SendIssueNotificationsPostJob job = new SendIssueNotificationsPostJob(issueCache, notifications, ruleFinder);
101     job.executeOn(project, sensorContext);
102
103     verify(notifications).sendChanges(eq(issue), any(IssueChangeContext.class), eq(rule), any(Component.class), (Component)isNull());
104   }
105
106   @Test
107   public void should_not_send_notif_if_issue_change_on_removed_rule() throws Exception {
108     IssueChangeContext changeContext = mock(IssueChangeContext.class);
109
110     when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
111     RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
112     DefaultIssue issue = new DefaultIssue()
113       .setChanged(true)
114       .setFieldDiff(changeContext, "severity", "MINOR", "BLOCKER")
115       .setRuleKey(ruleKey);
116     when(issueCache.all()).thenReturn(Arrays.asList(issue));
117     when(ruleFinder.findByKey(ruleKey)).thenReturn(null);
118
119     SendIssueNotificationsPostJob job = new SendIssueNotificationsPostJob(issueCache, notifications, ruleFinder);
120     job.executeOn(project, sensorContext);
121
122     verify(notifications, never()).sendChanges(eq(issue), eq(changeContext), any(Rule.class), any(Component.class), any(Component.class));
123   }
124 }