]> source.dussan.org Git - sonarqube.git/blob
a82ece85b5a34405e45af80c44b1c7f85744a9b3
[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.issue;
21
22 import org.junit.Test;
23 import org.sonar.api.rules.RuleType;
24 import org.sonar.api.utils.log.LogTester;
25 import org.sonar.api.utils.log.LoggerLevel;
26 import org.sonar.core.issue.DefaultIssue;
27 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
28 import org.sonar.server.computation.task.projectanalysis.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.scm.Changeset;
30 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepositoryRule;
31 import org.sonar.server.issue.IssueFieldsSetter;
32
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
37
38 public class IssueAssignerTest {
39
40   static final int FILE_REF = 1;
41   static final Component FILE = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").build();
42
43   @org.junit.Rule
44   public LogTester logTester = new LogTester();
45
46   @org.junit.Rule
47   public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
48
49   @org.junit.Rule
50   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule().setAnalysisDate(123456789L);
51
52   ScmAccountToUser scmAccountToUser = mock(ScmAccountToUser.class);
53   DefaultAssignee defaultAssignee = mock(DefaultAssignee.class);
54
55   IssueAssigner underTest = new IssueAssigner(analysisMetadataHolder, scmInfoRepository, scmAccountToUser, defaultAssignee, new IssueFieldsSetter());
56
57   @Test
58   public void nothing_to_do_if_no_changeset() throws Exception {
59     DefaultIssue issue = new DefaultIssue().setLine(1);
60
61     underTest.onIssue(FILE, issue);
62
63     assertThat(issue.authorLogin()).isNull();
64   }
65
66   @Test
67   public void set_author_to_issue() throws Exception {
68     setSingleChangeset("john", 123456789L, "rev-1");
69     DefaultIssue issue = new DefaultIssue().setLine(1);
70
71     underTest.onIssue(FILE, issue);
72
73     assertThat(issue.authorLogin()).isEqualTo("john");
74   }
75
76   @Test
77   public void does_not_set_author_to_issue_if_already_set() throws Exception {
78     setSingleChangeset("john", 123456789L, "rev-1");
79     DefaultIssue issue = new DefaultIssue()
80       .setLine(1)
81       .setAuthorLogin("j1234");
82
83     underTest.onIssue(FILE, issue);
84
85     assertThat(issue.authorLogin()).isEqualTo("j1234");
86   }
87
88   @Test
89   public void set_assignee_to_issue() throws Exception {
90     addScmUser("john", "John C");
91     setSingleChangeset("john", 123456789L, "rev-1");
92     DefaultIssue issue = new DefaultIssue().setLine(1);
93
94     underTest.onIssue(FILE, issue);
95
96     assertThat(issue.assignee()).isEqualTo("John C");
97   }
98
99   @Test
100   public void set_default_assignee_if_author_not_found() throws Exception {
101     addScmUser("john", null);
102     setSingleChangeset("john", 123456789L, "rev-1");
103     when(defaultAssignee.loadDefaultAssigneeLogin()).thenReturn("John C");
104     DefaultIssue issue = new DefaultIssue().setLine(1);
105
106     underTest.onIssue(FILE, issue);
107
108     assertThat(issue.assignee()).isEqualTo("John C");
109   }
110
111   @Test
112   public void doest_not_set_assignee_if_no_author() throws Exception {
113     addScmUser("john", "John C");
114     setSingleChangeset(null, 123456789L, "rev-1");
115     DefaultIssue issue = new DefaultIssue().setLine(1);
116
117     underTest.onIssue(FILE, issue);
118
119     assertThat(issue.authorLogin()).isNull();
120     assertThat(issue.assignee()).isNull();
121   }
122
123   @Test
124   public void doest_not_set_assignee_if_author_already_set_and_assignee_null() throws Exception {
125     addScmUser("john", "John C");
126     setSingleChangeset("john", 123456789L, "rev-1");
127     DefaultIssue issue = new DefaultIssue().setLine(1)
128       .setAuthorLogin("john")
129       .setAssignee(null);
130
131     underTest.onIssue(FILE, issue);
132
133     assertThat(issue.authorLogin()).isEqualTo("john");
134     assertThat(issue.assignee()).isNull();
135   }
136
137   @Test
138   public void set_last_committer_when_line_is_null() throws Exception {
139     addScmUser("henry", "Henry V");
140     Changeset changeset1 = Changeset.newChangesetBuilder()
141       .setAuthor("john")
142       .setDate(123456789L)
143       .setRevision("rev-1")
144       .build();
145     // Latest changeset
146     Changeset changeset2 = Changeset.newChangesetBuilder()
147       .setAuthor("henry")
148       .setDate(1234567810L)
149       .setRevision("rev-2")
150       .build();
151     scmInfoRepository.setScmInfo(FILE_REF, changeset1, changeset2, changeset1);
152
153     DefaultIssue issue = new DefaultIssue().setLine(null);
154
155     underTest.onIssue(FILE, issue);
156
157     assertThat(issue.assignee()).isEqualTo("Henry V");
158   }
159
160   @Test
161   public void set_last_committer_when_line_is_bigger_than_changeset_size() throws Exception {
162     addScmUser("john", "John C");
163     Changeset changeset = Changeset.newChangesetBuilder()
164       .setAuthor("john")
165       .setDate(123456789L)
166       .setRevision("rev-1")
167       .build();
168     scmInfoRepository.setScmInfo(FILE_REF, changeset, changeset);
169     DefaultIssue issue = new DefaultIssue().setLine(3);
170
171     underTest.onIssue(FILE, issue);
172
173     assertThat(issue.assignee()).isEqualTo("John C");
174   }
175
176   @Test
177   public void display_warning_when_line_is_above_max_size() throws Exception {
178     setSingleChangeset("john", 123456789L, "rev-1");
179     DefaultIssue issue = new DefaultIssue().setLine(2).setType(RuleType.VULNERABILITY);
180
181     underTest.onIssue(FILE, issue);
182
183     assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly(
184       "No SCM info has been found for issue DefaultIssue[key=<null>,type=VULNERABILITY,componentUuid=<null>,componentKey=<null>," +
185         "moduleUuid=<null>,moduleUuidPath=<null>,projectUuid=<null>,projectKey=<null>,ruleKey=<null>,language=<null>,severity=<null>," +
186         "manualSeverity=false,message=<null>,line=2,gap=<null>,effort=<null>,status=<null>,resolution=<null>," +
187         "assignee=<null>,checksum=<null>,attributes=<null>,authorLogin=<null>,comments=<null>,tags=<null>," +
188         "locations=<null>,creationDate=<null>,updateDate=<null>,closeDate=<null>,currentChange=<null>,changes=<null>,isNew=true," +
189         "beingClosed=false,onDisabledRule=false,isChanged=false,sendNotifications=false,selectedAt=<null>]");
190   }
191
192   private void setSingleChangeset(String author, Long date, String revision) {
193     scmInfoRepository.setScmInfo(FILE_REF,
194       Changeset.newChangesetBuilder()
195         .setAuthor(author)
196         .setDate(date)
197         .setRevision(revision)
198         .build());
199   }
200
201   private void addScmUser(String scmAccount, String userName) {
202     when(scmAccountToUser.getNullable(scmAccount)).thenReturn(userName);
203   }
204
205 }