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