3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.step;
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.List;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.TemporaryFolder;
30 import org.mockito.ArgumentCaptor;
31 import org.sonar.api.rule.RuleKey;
32 import org.sonar.api.rules.RuleType;
33 import org.sonar.api.utils.System2;
34 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
35 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
36 import org.sonar.ce.task.projectanalysis.issue.AdHocRuleCreator;
37 import org.sonar.ce.task.projectanalysis.issue.IssueCache;
38 import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryImpl;
39 import org.sonar.ce.task.projectanalysis.issue.UpdateConflictResolver;
40 import org.sonar.ce.task.projectanalysis.util.cache.DiskCache;
41 import org.sonar.ce.task.step.ComputationStep;
42 import org.sonar.ce.task.step.TestComputationStepContext;
43 import org.sonar.core.issue.DefaultIssue;
44 import org.sonar.core.issue.DefaultIssueComment;
45 import org.sonar.core.issue.FieldDiffs;
46 import org.sonar.db.DbClient;
47 import org.sonar.db.DbSession;
48 import org.sonar.db.DbTester;
49 import org.sonar.db.component.ComponentDto;
50 import org.sonar.db.issue.IssueChangeDto;
51 import org.sonar.db.issue.IssueDto;
52 import org.sonar.db.issue.IssueMapper;
53 import org.sonar.db.organization.OrganizationDto;
54 import org.sonar.db.rule.RuleDefinitionDto;
55 import org.sonar.db.rule.RuleTesting;
56 import org.sonar.scanner.protocol.output.ScannerReport;
57 import org.sonar.server.issue.IssueStorage;
59 import static java.util.Collections.singletonList;
60 import static org.assertj.core.api.Assertions.assertThat;
61 import static org.assertj.core.data.MapEntry.entry;
62 import static org.mockito.ArgumentMatchers.any;
63 import static org.mockito.ArgumentMatchers.eq;
64 import static org.mockito.Mockito.mock;
65 import static org.mockito.Mockito.verify;
66 import static org.mockito.Mockito.when;
67 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
68 import static org.sonar.api.issue.Issue.STATUS_CLOSED;
69 import static org.sonar.api.issue.Issue.STATUS_OPEN;
70 import static org.sonar.api.rule.Severity.BLOCKER;
71 import static org.sonar.db.component.ComponentTesting.newFileDto;
73 public class PersistIssuesStepTest extends BaseStepTest {
75 private static final long NOW = 1_400_000_000_000L;
78 public TemporaryFolder temp = new TemporaryFolder();
80 public DbTester db = DbTester.create(System2.INSTANCE);
82 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
84 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
85 .setOrganizationUuid("org-1", "qg-uuid-1");
87 private System2 system2 = mock(System2.class);
88 private DbSession session = db.getSession();
89 private DbClient dbClient = db.getDbClient();
90 private UpdateConflictResolver conflictResolver = mock(UpdateConflictResolver.class);
91 private IssueCache issueCache;
92 private ComputationStep underTest;
94 private AdHocRuleCreator adHocRuleCreator = mock(AdHocRuleCreator.class);
97 protected ComputationStep step() {
102 public void setup() throws Exception {
103 issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
104 reportReader.setMetadata(ScannerReport.Metadata.getDefaultInstance());
106 underTest = new PersistIssuesStep(dbClient, system2, conflictResolver, new RuleRepositoryImpl(adHocRuleCreator, dbClient, analysisMetadataHolder), issueCache,
111 public void tearDown() {
116 public void insert_copied_issue() {
117 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
118 db.rules().insert(rule);
119 OrganizationDto organizationDto = db.organizations().insert();
120 ComponentDto project = db.components().insertPrivateProject(organizationDto);
121 ComponentDto file = db.components().insertComponent(newFileDto(project, null));
122 when(system2.now()).thenReturn(NOW);
124 issueCache.newAppender().append(new DefaultIssue()
126 .setType(RuleType.CODE_SMELL)
127 .setRuleKey(rule.getKey())
128 .setComponentUuid(file.uuid())
129 .setProjectUuid(project.uuid())
130 .setSeverity(BLOCKER)
131 .setStatus(STATUS_OPEN)
134 .setType(RuleType.BUG)
136 .addComment(new DefaultIssueComment()
138 .setIssueKey("ISSUE")
139 .setUserUuid("john_uuid")
140 .setMarkdownText("Some text")
141 .setCreatedAt(new Date(NOW))
145 .setIssueKey("ISSUE")
146 .setUserUuid("john_uuid")
147 .setDiff("technicalDebt", null, 1L)
148 .setCreationDate(new Date(NOW))))
151 TestComputationStepContext context = new TestComputationStepContext();
152 underTest.execute(context);
154 IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
155 assertThat(result.getKey()).isEqualTo("ISSUE");
156 assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
157 assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
158 assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
159 assertThat(result.getSeverity()).isEqualTo(BLOCKER);
160 assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
161 assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
163 List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE"));
164 assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE);
165 assertThat(context.getStatistics().getAll()).containsOnly(
166 entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
170 public void insert_merged_issue() {
171 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
172 db.rules().insert(rule);
173 OrganizationDto organizationDto = db.organizations().insert();
174 ComponentDto project = db.components().insertPrivateProject(organizationDto);
175 ComponentDto file = db.components().insertComponent(newFileDto(project, null));
176 when(system2.now()).thenReturn(NOW);
178 issueCache.newAppender().append(new DefaultIssue()
180 .setType(RuleType.CODE_SMELL)
181 .setRuleKey(rule.getKey())
182 .setComponentUuid(file.uuid())
183 .setProjectUuid(project.uuid())
184 .setSeverity(BLOCKER)
185 .setStatus(STATUS_OPEN)
188 .setType(RuleType.BUG)
190 .addComment(new DefaultIssueComment()
192 .setIssueKey("ISSUE")
193 .setUserUuid("john_uuid")
194 .setMarkdownText("Some text")
195 .setCreatedAt(new Date(NOW))
197 .setCurrentChange(new FieldDiffs()
198 .setIssueKey("ISSUE")
199 .setUserUuid("john_uuid")
200 .setDiff("technicalDebt", null, 1L)
201 .setCreationDate(new Date(NOW))))
204 TestComputationStepContext context = new TestComputationStepContext();
205 underTest.execute(context);
207 IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
208 assertThat(result.getKey()).isEqualTo("ISSUE");
209 assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
210 assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
211 assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
212 assertThat(result.getSeverity()).isEqualTo(BLOCKER);
213 assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
214 assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
216 List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE"));
217 assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE);
218 assertThat(context.getStatistics().getAll()).containsOnly(
219 entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
223 public void update_conflicting_issue() {
224 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
225 db.rules().insert(rule);
226 OrganizationDto organizationDto = db.organizations().insert();
227 ComponentDto project = db.components().insertPrivateProject(organizationDto);
228 ComponentDto file = db.components().insertComponent(newFileDto(project, null));
229 IssueDto issue = db.issues().insert(rule, project, file,
230 i -> i.setStatus(STATUS_OPEN)
232 .setCreatedAt(NOW - 1_000_000_000L)
233 // simulate the issue has been updated after the analysis ran
234 .setUpdatedAt(NOW + 1_000_000_000L));
235 issue = dbClient.issueDao().selectByKey(db.getSession(), issue.getKey()).get();
236 DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
237 when(system2.now()).thenReturn(NOW);
239 DefaultIssue defaultIssue = issue.toDefaultIssue()
240 .setStatus(STATUS_CLOSED)
241 .setResolution(RESOLUTION_FIXED)
245 issueCacheAppender.append(defaultIssue).close();
247 TestComputationStepContext context = new TestComputationStepContext();
248 underTest.execute(context);
250 ArgumentCaptor<IssueDto> issueDtoCaptor = ArgumentCaptor.forClass(IssueDto.class);
251 verify(conflictResolver).resolve(eq(defaultIssue), issueDtoCaptor.capture(), any(IssueMapper.class));
252 assertThat(issueDtoCaptor.getValue().getId()).isEqualTo(issue.getId());
253 assertThat(context.getStatistics().getAll()).containsOnly(
254 entry("inserts", "0"), entry("updates", "1"), entry("merged", "1"), entry("untouched", "0"));
259 public void insert_new_issue() {
260 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
261 db.rules().insert(rule);
262 OrganizationDto organizationDto = db.organizations().insert();
263 ComponentDto project = db.components().insertPrivateProject(organizationDto);
264 ComponentDto file = db.components().insertComponent(newFileDto(project, null));
267 issueCache.newAppender().append(new DefaultIssue()
269 .setType(RuleType.CODE_SMELL)
270 .setRuleKey(rule.getKey())
271 .setComponentUuid(file.uuid())
272 .setProjectUuid(project.uuid())
273 .setSeverity(BLOCKER)
274 .setStatus(STATUS_OPEN)
276 .setType(RuleType.BUG)).close();
278 TestComputationStepContext context = new TestComputationStepContext();
279 underTest.execute(context);
281 IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
282 assertThat(result.getKey()).isEqualTo("ISSUE");
283 assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
284 assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
285 assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
286 assertThat(result.getSeverity()).isEqualTo(BLOCKER);
287 assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
288 assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
289 assertThat(context.getStatistics().getAll()).containsOnly(
290 entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
294 public void close_issue() {
295 ComponentDto project = db.components().insertPrivateProject();
296 ComponentDto file = db.components().insertComponent(newFileDto(project));
297 RuleDefinitionDto rule = db.rules().insert();
298 IssueDto issue = db.issues().insert(rule, project, file,
299 i -> i.setStatus(STATUS_OPEN)
301 .setCreatedAt(NOW - 1_000_000_000L)
302 .setUpdatedAt(NOW - 1_000_000_000L));
303 DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
305 issueCacheAppender.append(
306 issue.toDefaultIssue()
307 .setStatus(STATUS_CLOSED)
308 .setResolution(RESOLUTION_FIXED)
314 TestComputationStepContext context = new TestComputationStepContext();
315 underTest.execute(context);
317 IssueDto issueReloaded = db.getDbClient().issueDao().selectByKey(db.getSession(), issue.getKey()).get();
318 assertThat(issueReloaded.getStatus()).isEqualTo(STATUS_CLOSED);
319 assertThat(issueReloaded.getResolution()).isEqualTo(RESOLUTION_FIXED);
320 assertThat(context.getStatistics().getAll()).containsOnly(
321 entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"), entry("untouched", "0"));
325 public void add_comment() {
326 ComponentDto project = db.components().insertPrivateProject();
327 ComponentDto file = db.components().insertComponent(newFileDto(project));
328 RuleDefinitionDto rule = db.rules().insert();
329 IssueDto issue = db.issues().insert(rule, project, file,
330 i -> i.setStatus(STATUS_OPEN)
332 .setCreatedAt(NOW - 1_000_000_000L)
333 .setUpdatedAt(NOW - 1_000_000_000L));
334 DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
336 issueCacheAppender.append(
337 issue.toDefaultIssue()
338 .setStatus(STATUS_CLOSED)
339 .setResolution(RESOLUTION_FIXED)
343 .addComment(new DefaultIssueComment()
345 .setIssueKey(issue.getKey())
346 .setUserUuid("john_uuid")
347 .setMarkdownText("Some text")
348 .setCreatedAt(new Date(NOW))
352 TestComputationStepContext context = new TestComputationStepContext();
353 underTest.execute(context);
355 IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
356 assertThat(issueChangeDto)
357 .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserUuid, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey,
358 IssueChangeDto::getIssueChangeCreationDate)
359 .containsOnly(IssueChangeDto.TYPE_COMMENT, "john_uuid", "Some text", issue.getKey(), NOW);
360 assertThat(context.getStatistics().getAll()).containsOnly(
361 entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"), entry("untouched", "0"));
365 public void add_change() {
366 ComponentDto project = db.components().insertPrivateProject();
367 ComponentDto file = db.components().insertComponent(newFileDto(project));
368 RuleDefinitionDto rule = db.rules().insert();
369 IssueDto issue = db.issues().insert(rule, project, file,
370 i -> i.setStatus(STATUS_OPEN)
372 .setCreatedAt(NOW - 1_000_000_000L)
373 .setUpdatedAt(NOW - 1_000_000_000L));
374 DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
376 issueCacheAppender.append(
377 issue.toDefaultIssue()
378 .setStatus(STATUS_CLOSED)
379 .setResolution(RESOLUTION_FIXED)
383 .setCurrentChange(new FieldDiffs()
384 .setIssueKey("ISSUE")
385 .setUserUuid("john_uuid")
386 .setDiff("technicalDebt", null, 1L)
387 .setCreationDate(new Date(NOW))))
390 TestComputationStepContext context = new TestComputationStepContext();
391 underTest.execute(context);
393 IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
394 assertThat(issueChangeDto)
395 .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserUuid, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey,
396 IssueChangeDto::getIssueChangeCreationDate)
397 .containsOnly(IssueChangeDto.TYPE_FIELD_CHANGE, "john_uuid", "technicalDebt=1", issue.getKey(), NOW);
398 assertThat(context.getStatistics().getAll()).containsOnly(
399 entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"), entry("untouched", "0"));