3 * Copyright (C) 2009-2021 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.issue;
22 import java.time.Instant;
23 import java.time.temporal.ChronoUnit;
24 import java.util.Collections;
25 import java.util.Date;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.sonar.api.config.internal.MapSettings;
33 import org.sonar.api.issue.Issue;
34 import org.sonar.api.rule.RuleKey;
35 import org.sonar.api.utils.System2;
36 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
37 import org.sonar.ce.task.projectanalysis.analysis.Branch;
38 import org.sonar.ce.task.projectanalysis.component.SiblingComponentsWithOpenIssues;
39 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
40 import org.sonar.core.issue.DefaultIssue;
41 import org.sonar.core.issue.FieldDiffs;
42 import org.sonar.core.issue.tracking.SimpleTracker;
43 import org.sonar.db.DbClient;
44 import org.sonar.db.DbTester;
45 import org.sonar.db.component.BranchType;
46 import org.sonar.db.component.ComponentDto;
47 import org.sonar.db.issue.IssueDto;
48 import org.sonar.db.issue.IssueTesting;
49 import org.sonar.db.rule.RuleDefinitionDto;
50 import org.sonar.db.user.UserDto;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.ArgumentMatchers.eq;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.verifyNoInteractions;
56 import static org.mockito.Mockito.when;
57 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
58 import static org.sonar.db.component.ComponentTesting.newFileDto;
60 public class SiblingsIssueMergerTest {
62 private IssueLifecycle issueLifecycle;
65 private Branch branch;
68 public DbTester db = DbTester.create();
71 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
72 .setRoot(builder(org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT, PROJECT_REF).setKey(PROJECT_KEY).setUuid(PROJECT_UUID)
77 public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
79 private static final String PROJECT_KEY = "project";
80 private static final int PROJECT_REF = 1;
81 private static final String PROJECT_UUID = "projectUuid";
82 private static final int FILE_1_REF = 12341;
83 private static final String FILE_1_KEY = "fileKey";
84 private static final String FILE_1_UUID = "fileUuid";
86 private static final org.sonar.ce.task.projectanalysis.component.Component FILE_1 = builder(
87 org.sonar.ce.task.projectanalysis.component.Component.Type.FILE, FILE_1_REF)
92 private final SimpleTracker<DefaultIssue, SiblingIssue> tracker = new SimpleTracker<>();
93 private SiblingsIssueMerger copier;
94 private ComponentDto fileOnBranch1Dto;
95 private ComponentDto fileOnBranch2Dto;
96 private ComponentDto fileOnBranch3Dto;
97 private ComponentDto projectDto;
98 private ComponentDto branch1Dto;
99 private ComponentDto branch2Dto;
100 private ComponentDto branch3Dto;
101 private RuleDefinitionDto rule;
104 public void setUp() {
105 MockitoAnnotations.initMocks(this);
106 DbClient dbClient = db.getDbClient();
107 ComponentIssuesLoader componentIssuesLoader = new ComponentIssuesLoader(dbClient, null, null, new MapSettings().asConfig(), System2.INSTANCE);
108 copier = new SiblingsIssueMerger(new SiblingsIssuesLoader(new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, dbClient), dbClient, componentIssuesLoader),
111 projectDto = db.components().insertPublicProject(p -> p.setDbKey(PROJECT_KEY).setUuid(PROJECT_UUID));
112 branch1Dto = db.components().insertProjectBranch(projectDto, b -> b.setKey("myBranch1")
113 .setBranchType(BranchType.PULL_REQUEST)
114 .setMergeBranchUuid(projectDto.uuid()));
115 branch2Dto = db.components().insertProjectBranch(projectDto, b -> b.setKey("myBranch2")
116 .setBranchType(BranchType.PULL_REQUEST)
117 .setMergeBranchUuid(projectDto.uuid()));
118 branch3Dto = db.components().insertProjectBranch(projectDto, b -> b.setKey("myBranch3")
119 .setBranchType(BranchType.PULL_REQUEST)
120 .setMergeBranchUuid(projectDto.uuid()));
121 fileOnBranch1Dto = db.components().insertComponent(newFileDto(branch1Dto).setDbKey(FILE_1_KEY + ":PULL_REQUEST:myBranch1"));
122 fileOnBranch2Dto = db.components().insertComponent(newFileDto(branch2Dto).setDbKey(FILE_1_KEY + ":PULL_REQUEST:myBranch2"));
123 fileOnBranch3Dto = db.components().insertComponent(newFileDto(branch3Dto).setDbKey(FILE_1_KEY + ":PULL_REQUEST:myBranch3"));
124 rule = db.rules().insert();
125 when(branch.getReferenceBranchUuid()).thenReturn(projectDto.uuid());
126 metadataHolder.setBranch(branch);
130 public void do_nothing_if_no_match() {
131 DefaultIssue i = createIssue("issue1", rule.getKey(), Issue.STATUS_CONFIRMED, new Date());
132 copier.tryMerge(FILE_1, Collections.singleton(i));
134 verifyNoInteractions(issueLifecycle);
138 public void do_nothing_if_no_new_issue() {
139 db.issues().insert(IssueTesting.newIssue(rule, branch1Dto, fileOnBranch1Dto).setKee("issue1").setStatus(Issue.STATUS_CONFIRMED).setLine(1).setChecksum("checksum"));
140 copier.tryMerge(FILE_1, Collections.emptyList());
142 verifyNoInteractions(issueLifecycle);
146 public void merge_confirmed_issues() {
147 db.issues().insert(IssueTesting.newIssue(rule, branch1Dto, fileOnBranch1Dto).setKee("issue1").setStatus(Issue.STATUS_CONFIRMED).setLine(1).setChecksum("checksum"));
148 DefaultIssue newIssue = createIssue("issue2", rule.getKey(), Issue.STATUS_OPEN, new Date());
150 copier.tryMerge(FILE_1, Collections.singleton(newIssue));
152 ArgumentCaptor<DefaultIssue> issueToMerge = ArgumentCaptor.forClass(DefaultIssue.class);
153 verify(issueLifecycle).mergeConfirmedOrResolvedFromPr(eq(newIssue), issueToMerge.capture(), eq("myBranch1"));
155 assertThat(issueToMerge.getValue().key()).isEqualTo("issue1");
159 public void prefer_more_recently_updated_issues() {
160 Instant now = Instant.now();
161 db.issues().insert(IssueTesting.newIssue(rule, branch1Dto, fileOnBranch1Dto).setKee("issue1").setStatus(Issue.STATUS_REOPENED).setLine(1).setChecksum("checksum")
162 .setIssueUpdateDate(Date.from(now.plus(2, ChronoUnit.SECONDS))));
163 db.issues().insert(IssueTesting.newIssue(rule, branch2Dto, fileOnBranch2Dto).setKee("issue2").setStatus(Issue.STATUS_OPEN).setLine(1).setChecksum("checksum")
164 .setIssueUpdateDate(Date.from(now.plus(1, ChronoUnit.SECONDS))));
165 db.issues().insert(IssueTesting.newIssue(rule, branch3Dto, fileOnBranch3Dto).setKee("issue3").setStatus(Issue.STATUS_OPEN).setLine(1).setChecksum("checksum")
166 .setIssueUpdateDate(Date.from(now)));
167 DefaultIssue newIssue = createIssue("newIssue", rule.getKey(), Issue.STATUS_OPEN, new Date());
169 copier.tryMerge(FILE_1, Collections.singleton(newIssue));
171 ArgumentCaptor<DefaultIssue> issueToMerge = ArgumentCaptor.forClass(DefaultIssue.class);
172 verify(issueLifecycle).mergeConfirmedOrResolvedFromPr(eq(newIssue), issueToMerge.capture(), eq("myBranch1"));
174 assertThat(issueToMerge.getValue().key()).isEqualTo("issue1");
178 public void lazy_load_changes() {
179 UserDto user = db.users().insertUser();
180 IssueDto issue = db.issues()
181 .insert(IssueTesting.newIssue(rule, branch2Dto, fileOnBranch2Dto).setKee("issue").setStatus(Issue.STATUS_CONFIRMED).setLine(1).setChecksum("checksum"));
182 db.issues().insertComment(issue, user, "A comment 2");
183 db.issues().insertFieldDiffs(issue, FieldDiffs.parse("severity=BLOCKER|MINOR,assignee=foo|bar").setCreationDate(new Date()));
184 DefaultIssue newIssue = createIssue("newIssue", rule.getKey(), Issue.STATUS_OPEN, new Date());
186 copier.tryMerge(FILE_1, Collections.singleton(newIssue));
188 ArgumentCaptor<DefaultIssue> issueToMerge = ArgumentCaptor.forClass(DefaultIssue.class);
189 verify(issueLifecycle).mergeConfirmedOrResolvedFromPr(eq(newIssue), issueToMerge.capture(), eq("myBranch2"));
191 assertThat(issueToMerge.getValue().key()).isEqualTo("issue");
192 assertThat(issueToMerge.getValue().defaultIssueComments()).isNotEmpty();
193 assertThat(issueToMerge.getValue().changes()).isNotEmpty();
196 private static DefaultIssue createIssue(String key, RuleKey ruleKey, String status, Date creationDate) {
197 DefaultIssue issue = new DefaultIssue();
199 issue.setRuleKey(ruleKey);
200 issue.setMessage("msg");
202 issue.setStatus(status);
203 issue.setResolution(null);
204 issue.setCreationDate(creationDate);
205 issue.setChecksum("checksum");