3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.issue;
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;
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;
38 public class IssueAssignerTest {
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();
44 public LogTester logTester = new LogTester();
47 public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
50 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule().setAnalysisDate(123456789L);
52 ScmAccountToUser scmAccountToUser = mock(ScmAccountToUser.class);
53 DefaultAssignee defaultAssignee = mock(DefaultAssignee.class);
55 IssueAssigner underTest = new IssueAssigner(analysisMetadataHolder, scmInfoRepository, scmAccountToUser, defaultAssignee, new IssueFieldsSetter());
58 public void nothing_to_do_if_no_changeset() throws Exception {
59 DefaultIssue issue = new DefaultIssue().setLine(1);
61 underTest.onIssue(FILE, issue);
63 assertThat(issue.authorLogin()).isNull();
67 public void set_author_to_issue() throws Exception {
68 setSingleChangeset("john", 123456789L, "rev-1");
69 DefaultIssue issue = new DefaultIssue().setLine(1);
71 underTest.onIssue(FILE, issue);
73 assertThat(issue.authorLogin()).isEqualTo("john");
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()
81 .setAuthorLogin("j1234");
83 underTest.onIssue(FILE, issue);
85 assertThat(issue.authorLogin()).isEqualTo("j1234");
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);
94 underTest.onIssue(FILE, issue);
96 assertThat(issue.assignee()).isEqualTo("John C");
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);
106 underTest.onIssue(FILE, issue);
108 assertThat(issue.assignee()).isEqualTo("John C");
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);
117 underTest.onIssue(FILE, issue);
119 assertThat(issue.authorLogin()).isNull();
120 assertThat(issue.assignee()).isNull();
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")
131 underTest.onIssue(FILE, issue);
133 assertThat(issue.authorLogin()).isEqualTo("john");
134 assertThat(issue.assignee()).isNull();
138 public void set_last_committer_when_line_is_null() throws Exception {
139 addScmUser("henry", "Henry V");
140 Changeset changeset1 = Changeset.newChangesetBuilder()
143 .setRevision("rev-1")
146 Changeset changeset2 = Changeset.newChangesetBuilder()
148 .setDate(1234567810L)
149 .setRevision("rev-2")
151 scmInfoRepository.setScmInfo(FILE_REF, changeset1, changeset2, changeset1);
153 DefaultIssue issue = new DefaultIssue().setLine(null);
155 underTest.onIssue(FILE, issue);
157 assertThat(issue.assignee()).isEqualTo("Henry V");
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()
166 .setRevision("rev-1")
168 scmInfoRepository.setScmInfo(FILE_REF, changeset, changeset);
169 DefaultIssue issue = new DefaultIssue().setLine(3);
171 underTest.onIssue(FILE, issue);
173 assertThat(issue.assignee()).isEqualTo("John C");
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);
181 underTest.onIssue(FILE, issue);
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>]");
192 private void setSingleChangeset(String author, Long date, String revision) {
193 scmInfoRepository.setScmInfo(FILE_REF,
194 Changeset.newChangesetBuilder()
197 .setRevision(revision)
201 private void addScmUser(String scmAccount, String userName) {
202 when(scmAccountToUser.getNullable(scmAccount)).thenReturn(userName);