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