3 * Copyright (C) 2009-2022 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.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.mockito.ArgumentCaptor;
31 import org.sonar.api.utils.System2;
32 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
33 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
35 import org.sonar.ce.task.projectanalysis.component.Component;
36 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
37 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
38 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
39 import org.sonar.ce.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
40 import org.sonar.ce.task.projectanalysis.duplication.IntegrateCrossProjectDuplications;
41 import org.sonar.ce.task.step.ComputationStep;
42 import org.sonar.ce.task.step.TestComputationStepContext;
43 import org.sonar.db.DbClient;
44 import org.sonar.db.DbSession;
45 import org.sonar.db.DbTester;
46 import org.sonar.db.component.ComponentDto;
47 import org.sonar.db.component.ComponentTesting;
48 import org.sonar.db.component.SnapshotDto;
49 import org.sonar.db.component.SnapshotTesting;
50 import org.sonar.db.duplication.DuplicationUnitDto;
51 import org.sonar.duplications.block.Block;
52 import org.sonar.duplications.block.ByteArray;
53 import org.sonar.scanner.protocol.output.ScannerReport;
55 import static java.util.Arrays.asList;
56 import static java.util.Collections.singletonList;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.mockito.Mockito.eq;
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.verify;
61 import static org.mockito.Mockito.verifyNoInteractions;
62 import static org.mockito.Mockito.verifyZeroInteractions;
63 import static org.mockito.Mockito.when;
64 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
65 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
67 public class LoadCrossProjectDuplicationsRepositoryStepTest {
70 private static final String XOO_LANGUAGE = "xoo";
71 private static final int PROJECT_REF = 1;
72 private static final int FILE_REF = 2;
73 private static final String CURRENT_FILE_KEY = "FILE_KEY";
75 private static final Component CURRENT_FILE = ReportComponent.builder(FILE, FILE_REF)
76 .setKey(CURRENT_FILE_KEY)
77 .setFileAttributes(new FileAttributes(false, XOO_LANGUAGE, 1))
81 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
82 ReportComponent.builder(PROJECT, PROJECT_REF)
83 .addChildren(CURRENT_FILE).build());
86 public BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
89 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
91 private CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
94 public DbTester dbTester = DbTester.create(System2.INSTANCE);
96 private DbClient dbClient = dbTester.getDbClient();
97 private DbSession dbSession = dbTester.getSession();
98 private IntegrateCrossProjectDuplications integrateCrossProjectDuplications = mock(IntegrateCrossProjectDuplications.class);
99 private Analysis baseProjectAnalysis;
101 private ComputationStep underTest = new LoadCrossProjectDuplicationsRepositoryStep(treeRootHolder, batchReportReader, analysisMetadataHolder, crossProjectDuplicationStatusHolder,
102 integrateCrossProjectDuplications, dbClient);
105 public void setUp() {
106 ComponentDto project = ComponentTesting.newPrivateProjectDto();
107 dbTester.components().insertComponent(project);
108 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(project);
109 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
112 baseProjectAnalysis = new Analysis.Builder()
113 .setUuid(projectSnapshot.getUuid())
114 .setCreatedAt(projectSnapshot.getCreatedAt())
119 public void call_compute_cpd_on_one_duplication() {
120 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
121 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
123 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
124 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
126 ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
128 String hash = "a8998353e96320ec";
129 DuplicationUnitDto duplicate = new DuplicationUnitDto()
134 .setAnalysisUuid(otherProjectSnapshot.getUuid())
135 .setComponentUuid(otherFile.uuid());
136 dbClient.duplicationDao().insert(dbSession, duplicate);
139 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
143 .setStartTokenIndex(0)
144 .setEndTokenIndex(10)
146 batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock));
148 underTest.execute(new TestComputationStepContext());
150 verify(integrateCrossProjectDuplications).computeCpd(CURRENT_FILE,
153 .setResourceId(CURRENT_FILE_KEY)
154 .setBlockHash(new ByteArray(hash))
156 .setLines(originBlock.getStartLine(), originBlock.getEndLine())
157 .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex())
161 .setResourceId(otherFile.getDbKey())
162 .setBlockHash(new ByteArray(hash))
163 .setIndexInFile(duplicate.getIndexInFile())
164 .setLines(duplicate.getStartLine(), duplicate.getEndLine())
169 public void call_compute_cpd_on_many_duplication() {
170 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
171 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
173 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
174 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
176 ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
178 ScannerReport.CpdTextBlock originBlock1 = ScannerReport.CpdTextBlock.newBuilder()
179 .setHash("a8998353e96320ec")
182 .setStartTokenIndex(0)
183 .setEndTokenIndex(10)
185 ScannerReport.CpdTextBlock originBlock2 = ScannerReport.CpdTextBlock.newBuilder()
186 .setHash("b1234353e96320ff")
189 .setStartTokenIndex(5)
190 .setEndTokenIndex(15)
192 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock1, originBlock2));
194 DuplicationUnitDto duplicate1 = new DuplicationUnitDto()
195 .setHash(originBlock1.getHash())
199 .setAnalysisUuid(otherProjectSnapshot.getUuid())
200 .setComponentUuid(otherFile.uuid());
202 DuplicationUnitDto duplicate2 = new DuplicationUnitDto()
203 .setHash(originBlock2.getHash())
207 .setAnalysisUuid(otherProjectSnapshot.getUuid())
208 .setComponentUuid(otherFile.uuid());
209 dbClient.duplicationDao().insert(dbSession, duplicate1);
210 dbClient.duplicationDao().insert(dbSession, duplicate2);
213 underTest.execute(new TestComputationStepContext());
215 Class<ArrayList<Block>> listClass = (Class<ArrayList<Block>>) (Class) ArrayList.class;
216 ArgumentCaptor<ArrayList<Block>> originBlocks = ArgumentCaptor.forClass(listClass);
217 ArgumentCaptor<ArrayList<Block>> duplicationBlocks = ArgumentCaptor.forClass(listClass);
219 verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture());
221 Map<Integer, Block> originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue());
222 assertThat(originBlocksByIndex.get(0)).isEqualTo(
224 .setResourceId(CURRENT_FILE_KEY)
225 .setBlockHash(new ByteArray(originBlock1.getHash()))
227 .setLines(originBlock1.getStartLine(), originBlock1.getEndLine())
228 .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex())
230 assertThat(originBlocksByIndex.get(1)).isEqualTo(
232 .setResourceId(CURRENT_FILE_KEY)
233 .setBlockHash(new ByteArray(originBlock2.getHash()))
235 .setLines(originBlock2.getStartLine(), originBlock2.getEndLine())
236 .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex())
239 Map<Integer, Block> duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue());
240 assertThat(duplicationBlocksByIndex.get(0)).isEqualTo(
242 .setResourceId(otherFile.getDbKey())
243 .setBlockHash(new ByteArray(originBlock1.getHash()))
244 .setIndexInFile(duplicate1.getIndexInFile())
245 .setLines(duplicate1.getStartLine(), duplicate1.getEndLine())
247 assertThat(duplicationBlocksByIndex.get(1)).isEqualTo(
249 .setResourceId(otherFile.getDbKey())
250 .setBlockHash(new ByteArray(originBlock2.getHash()))
251 .setIndexInFile(duplicate2.getIndexInFile())
252 .setLines(duplicate2.getStartLine(), duplicate2.getEndLine())
257 public void nothing_to_do_when_cross_project_duplication_is_disabled() {
258 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
259 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
261 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
262 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
264 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
266 String hash = "a8998353e96320ec";
267 DuplicationUnitDto duplicate = new DuplicationUnitDto()
272 .setAnalysisUuid(otherProjectSnapshot.getUuid())
273 .setComponentUuid(otherFIle.uuid());
274 dbClient.duplicationDao().insert(dbSession, duplicate);
277 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
281 .setStartTokenIndex(0)
282 .setEndTokenIndex(10)
284 batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock));
286 underTest.execute(new TestComputationStepContext());
288 verifyZeroInteractions(integrateCrossProjectDuplications);
292 public void nothing_to_do_when_no_cpd_text_blocks_found() {
293 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
294 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
296 batchReportReader.putDuplicationBlocks(FILE_REF, Collections.emptyList());
298 underTest.execute(new TestComputationStepContext());
300 verifyNoInteractions(integrateCrossProjectDuplications);
304 public void nothing_to_do_when_cpd_text_blocks_exists_but_no_duplicated_found() {
305 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
306 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
308 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
309 .setHash("a8998353e96320ec")
312 .setStartTokenIndex(0)
313 .setEndTokenIndex(10)
315 batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock));
317 underTest.execute(new TestComputationStepContext());
319 verifyNoInteractions(integrateCrossProjectDuplications);
322 private ComponentDto createProject(String projectKey) {
323 ComponentDto project = ComponentTesting.newPrivateProjectDto().setDbKey(projectKey);
324 return dbTester.components().insertComponent(project);
327 private SnapshotDto createProjectSnapshot(ComponentDto project) {
328 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(project);
329 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
331 return projectSnapshot;
334 private ComponentDto createFile(String fileKey, ComponentDto project) {
335 ComponentDto file = ComponentTesting.newFileDto(project, null)
337 .setLanguage(XOO_LANGUAGE);
338 dbClient.componentDao().insert(dbSession, file);
343 private static Map<Integer, Block> blocksByIndexInFile(List<Block> blocks) {
344 Map<Integer, Block> blocksByIndexInFile = new HashMap<>();
345 for (Block block : blocks) {
346 blocksByIndexInFile.put(block.getIndexInFile(), block);
348 return blocksByIndexInFile;