]> source.dussan.org Git - sonarqube.git/blob
f5ff4dd9a432f956b367a3e4ff2b8d60d627c1d6
[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.issue.notification;
21
22 import com.google.common.collect.Lists;
23 import java.util.Date;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.sonar.api.rule.RuleKey;
27 import org.sonar.api.rule.Severity;
28 import org.sonar.api.utils.DateUtils;
29 import org.sonar.api.utils.Duration;
30 import org.sonar.api.utils.Durations;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.rule.RuleDefinitionDto;
35 import org.sonar.server.user.index.UserIndex;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.ASSIGNEE;
43 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.COMPONENT;
44 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.EFFORT;
45 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE;
46 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.SEVERITY;
47 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.TAG;
48
49 public class NewIssuesNotificationTest {
50
51   NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
52   UserIndex userIndex = mock(UserIndex.class);
53   DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
54   Durations durations = mock(Durations.class);
55   NewIssuesNotification underTest = new NewIssuesNotification(userIndex, dbClient, durations);
56
57   @Test
58   public void set_project_without_branch() {
59     underTest.setProject("project-key", "project-uuid", "project-long-name", null);
60
61     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_NAME)).isEqualTo("project-long-name");
62     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_UUID)).isEqualTo("project-uuid");
63     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_KEY)).isEqualTo("project-key");
64     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_BRANCH)).isNull();
65   }
66
67   @Test
68   public void set_project_with_branch() {
69     underTest.setProject("project-key", "project-uuid", "project-long-name", "feature");
70
71     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_NAME)).isEqualTo("project-long-name");
72     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_UUID)).isEqualTo("project-uuid");
73     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_KEY)).isEqualTo("project-key");
74     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_BRANCH)).isEqualTo("feature");
75   }
76
77   @Test
78   public void set_date() {
79     Date date = new Date();
80
81     underTest.setAnalysisDate(date);
82
83     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_DATE)).isEqualTo(DateUtils.formatDateTime(date));
84   }
85
86   @Test
87   public void set_statistics() {
88     addIssueNTimes(newIssue1(), 5);
89     addIssueNTimes(newIssue2(), 3);
90     when(dbClient.componentDao().selectOrFailByUuid(any(DbSession.class), eq("file-uuid")).name()).thenReturn("file-name");
91     when(dbClient.componentDao().selectOrFailByUuid(any(DbSession.class), eq("directory-uuid")).name()).thenReturn("directory-name");
92     when(dbClient.ruleDao().selectOrFailDefinitionByKey(any(DbSession.class), eq(RuleKey.of("SonarQube", "rule-the-world")))).thenReturn(newRule("Rule the World", "Java"));
93     when(dbClient.ruleDao().selectOrFailDefinitionByKey(any(DbSession.class), eq(RuleKey.of("SonarQube", "rule-the-universe")))).thenReturn(newRule("Rule the Universe", "Clojure"));
94
95     underTest.setStatistics("project-long-name", stats);
96
97     assertThat(underTest.getFieldValue(SEVERITY + ".INFO.count")).isEqualTo("5");
98     assertThat(underTest.getFieldValue(SEVERITY + ".BLOCKER.count")).isEqualTo("3");
99     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo("maynard");
100     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("5");
101     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isEqualTo("keenan");
102     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isEqualTo("3");
103     assertThat(underTest.getFieldValue(TAG + ".1.label")).isEqualTo("owasp");
104     assertThat(underTest.getFieldValue(TAG + ".1.count")).isEqualTo("8");
105     assertThat(underTest.getFieldValue(TAG + ".2.label")).isEqualTo("bug");
106     assertThat(underTest.getFieldValue(TAG + ".2.count")).isEqualTo("5");
107     assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isEqualTo("file-name");
108     assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isEqualTo("5");
109     assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isEqualTo("directory-name");
110     assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isEqualTo("3");
111     assertThat(underTest.getFieldValue(RULE + ".1.label")).isEqualTo("Rule the World (Java)");
112     assertThat(underTest.getFieldValue(RULE + ".1.count")).isEqualTo("5");
113     assertThat(underTest.getFieldValue(RULE + ".2.label")).isEqualTo("Rule the Universe (Clojure)");
114     assertThat(underTest.getFieldValue(RULE + ".2.count")).isEqualTo("3");
115     assertThat(underTest.getDefaultMessage()).startsWith("8 new issues on project-long-name");
116   }
117
118   @Test
119   public void set_debt() {
120     when(durations.format(any(Duration.class))).thenReturn("55 min");
121
122     underTest.setDebt(Duration.create(55));
123
124     assertThat(underTest.getFieldValue(EFFORT + ".count")).isEqualTo("55 min");
125   }
126
127   private void addIssueNTimes(DefaultIssue issue, int times) {
128     for (int i = 0; i < times; i++) {
129       stats.add(issue);
130     }
131   }
132
133   private DefaultIssue newIssue1() {
134     return new DefaultIssue()
135       .setAssignee("maynard")
136       .setComponentUuid("file-uuid")
137       .setSeverity(Severity.INFO)
138       .setTags(Lists.newArrayList("bug", "owasp"))
139       .setRuleKey(RuleKey.of("SonarQube", "rule-the-world"))
140       .setEffort(Duration.create(5L));
141   }
142
143   private DefaultIssue newIssue2() {
144     return new DefaultIssue()
145       .setAssignee("keenan")
146       .setComponentUuid("directory-uuid")
147       .setSeverity(Severity.BLOCKER)
148       .setTags(Lists.newArrayList("owasp"))
149       .setRuleKey(RuleKey.of("SonarQube", "rule-the-universe"))
150       .setEffort(Duration.create(10L));
151   }
152
153   private RuleDefinitionDto newRule(String name, String language) {
154     return new RuleDefinitionDto()
155       .setName(name)
156       .setLanguage(language);
157   }
158 }