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.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.junit.rules.ExpectedException;
31 import org.mockito.ArgumentCaptor;
32 import org.sonar.api.utils.System2;
33 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
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.component.Component;
37 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
38 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
39 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
40 import org.sonar.ce.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
41 import org.sonar.ce.task.projectanalysis.duplication.IntegrateCrossProjectDuplications;
42 import org.sonar.ce.task.step.ComputationStep;
43 import org.sonar.ce.task.step.TestComputationStepContext;
44 import org.sonar.db.DbClient;
45 import org.sonar.db.DbSession;
46 import org.sonar.db.DbTester;
47 import org.sonar.db.component.ComponentDto;
48 import org.sonar.db.component.ComponentTesting;
49 import org.sonar.db.component.SnapshotDto;
50 import org.sonar.db.component.SnapshotTesting;
51 import org.sonar.db.duplication.DuplicationUnitDto;
52 import org.sonar.duplications.block.Block;
53 import org.sonar.duplications.block.ByteArray;
54 import org.sonar.scanner.protocol.output.ScannerReport;
56 import static java.util.Arrays.asList;
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.verifyZeroInteractions;
62 import static org.mockito.Mockito.when;
63 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
64 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
66 public class LoadCrossProjectDuplicationsRepositoryStepTest {
69 public ExpectedException thrown = ExpectedException.none();
71 private static final String XOO_LANGUAGE = "xoo";
72 private static final int PROJECT_REF = 1;
73 private static final int FILE_REF = 2;
74 private static final String CURRENT_FILE_KEY = "FILE_KEY";
76 private static final Component CURRENT_FILE = ReportComponent.builder(FILE, FILE_REF)
77 .setKey(CURRENT_FILE_KEY)
78 .setFileAttributes(new FileAttributes(false, XOO_LANGUAGE, 1))
82 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
83 ReportComponent.builder(PROJECT, PROJECT_REF)
84 .addChildren(CURRENT_FILE).build());
87 public BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
90 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
92 private CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
95 public DbTester dbTester = DbTester.create(System2.INSTANCE);
97 private DbClient dbClient = dbTester.getDbClient();
98 private DbSession dbSession = dbTester.getSession();
99 private IntegrateCrossProjectDuplications integrateCrossProjectDuplications = mock(IntegrateCrossProjectDuplications.class);
100 private Analysis baseProjectAnalysis;
102 private ComputationStep underTest = new LoadCrossProjectDuplicationsRepositoryStep(treeRootHolder, batchReportReader, analysisMetadataHolder, crossProjectDuplicationStatusHolder,
103 integrateCrossProjectDuplications, dbClient);
106 public void setUp() {
107 ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert());
108 dbClient.componentDao().insert(dbSession, project);
109 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(project);
110 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
113 baseProjectAnalysis = new Analysis.Builder()
114 .setId(projectSnapshot.getId())
115 .setUuid(projectSnapshot.getUuid())
116 .setCreatedAt(projectSnapshot.getCreatedAt())
121 public void call_compute_cpd_on_one_duplication() {
122 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
123 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
125 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
126 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
128 ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
130 String hash = "a8998353e96320ec";
131 DuplicationUnitDto duplicate = new DuplicationUnitDto()
136 .setAnalysisUuid(otherProjectSnapshot.getUuid())
137 .setComponentUuid(otherFile.uuid());
138 dbClient.duplicationDao().insert(dbSession, duplicate);
141 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
145 .setStartTokenIndex(0)
146 .setEndTokenIndex(10)
148 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
150 underTest.execute(new TestComputationStepContext());
152 verify(integrateCrossProjectDuplications).computeCpd(CURRENT_FILE,
155 .setResourceId(CURRENT_FILE_KEY)
156 .setBlockHash(new ByteArray(hash))
158 .setLines(originBlock.getStartLine(), originBlock.getEndLine())
159 .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex())
163 .setResourceId(otherFile.getDbKey())
164 .setBlockHash(new ByteArray(hash))
165 .setIndexInFile(duplicate.getIndexInFile())
166 .setLines(duplicate.getStartLine(), duplicate.getEndLine())
171 public void call_compute_cpd_on_many_duplication() {
172 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
173 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
175 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
176 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
178 ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
180 ScannerReport.CpdTextBlock originBlock1 = ScannerReport.CpdTextBlock.newBuilder()
181 .setHash("a8998353e96320ec")
184 .setStartTokenIndex(0)
185 .setEndTokenIndex(10)
187 ScannerReport.CpdTextBlock originBlock2 = ScannerReport.CpdTextBlock.newBuilder()
188 .setHash("b1234353e96320ff")
191 .setStartTokenIndex(5)
192 .setEndTokenIndex(15)
194 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock1, originBlock2));
196 DuplicationUnitDto duplicate1 = new DuplicationUnitDto()
197 .setHash(originBlock1.getHash())
201 .setAnalysisUuid(otherProjectSnapshot.getUuid())
202 .setComponentUuid(otherFile.uuid());
204 DuplicationUnitDto duplicate2 = new DuplicationUnitDto()
205 .setHash(originBlock2.getHash())
209 .setAnalysisUuid(otherProjectSnapshot.getUuid())
210 .setComponentUuid(otherFile.uuid());
211 dbClient.duplicationDao().insert(dbSession, duplicate1);
212 dbClient.duplicationDao().insert(dbSession, duplicate2);
215 underTest.execute(new TestComputationStepContext());
217 Class<ArrayList<Block>> listClass = (Class<ArrayList<Block>>) (Class) ArrayList.class;
218 ArgumentCaptor<ArrayList<Block>> originBlocks = ArgumentCaptor.forClass(listClass);
219 ArgumentCaptor<ArrayList<Block>> duplicationBlocks = ArgumentCaptor.forClass(listClass);
221 verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture());
223 Map<Integer, Block> originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue());
224 assertThat(originBlocksByIndex.get(0)).isEqualTo(
226 .setResourceId(CURRENT_FILE_KEY)
227 .setBlockHash(new ByteArray(originBlock1.getHash()))
229 .setLines(originBlock1.getStartLine(), originBlock1.getEndLine())
230 .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex())
232 assertThat(originBlocksByIndex.get(1)).isEqualTo(
234 .setResourceId(CURRENT_FILE_KEY)
235 .setBlockHash(new ByteArray(originBlock2.getHash()))
237 .setLines(originBlock2.getStartLine(), originBlock2.getEndLine())
238 .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex())
241 Map<Integer, Block> duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue());
242 assertThat(duplicationBlocksByIndex.get(0)).isEqualTo(
244 .setResourceId(otherFile.getDbKey())
245 .setBlockHash(new ByteArray(originBlock1.getHash()))
246 .setIndexInFile(duplicate1.getIndexInFile())
247 .setLines(duplicate1.getStartLine(), duplicate1.getEndLine())
249 assertThat(duplicationBlocksByIndex.get(1)).isEqualTo(
251 .setResourceId(otherFile.getDbKey())
252 .setBlockHash(new ByteArray(originBlock2.getHash()))
253 .setIndexInFile(duplicate2.getIndexInFile())
254 .setLines(duplicate2.getStartLine(), duplicate2.getEndLine())
259 public void nothing_to_do_when_cross_project_duplication_is_disabled() {
260 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
261 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
263 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
264 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
266 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
268 String hash = "a8998353e96320ec";
269 DuplicationUnitDto duplicate = new DuplicationUnitDto()
274 .setAnalysisUuid(otherProjectSnapshot.getUuid())
275 .setComponentUuid(otherFIle.uuid());
276 dbClient.duplicationDao().insert(dbSession, duplicate);
279 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
283 .setStartTokenIndex(0)
284 .setEndTokenIndex(10)
286 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
288 underTest.execute(new TestComputationStepContext());
290 verifyZeroInteractions(integrateCrossProjectDuplications);
294 public void nothing_to_do_when_no_cpd_text_blocks_found() {
295 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
296 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
298 batchReportReader.putDuplicationBlocks(FILE_REF, Collections.emptyList());
300 underTest.execute(new TestComputationStepContext());
302 verifyZeroInteractions(integrateCrossProjectDuplications);
306 public void nothing_to_do_when_cpd_text_blocks_exists_but_no_duplicated_found() {
307 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
308 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
310 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
311 .setHash("a8998353e96320ec")
314 .setStartTokenIndex(0)
315 .setEndTokenIndex(10)
317 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
319 underTest.execute(new TestComputationStepContext());
321 verifyZeroInteractions(integrateCrossProjectDuplications);
324 private ComponentDto createProject(String projectKey) {
325 ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()).setDbKey(projectKey);
326 dbClient.componentDao().insert(dbSession, project);
331 private SnapshotDto createProjectSnapshot(ComponentDto project) {
332 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(project);
333 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
335 return projectSnapshot;
338 private ComponentDto createFile(String fileKey, ComponentDto project) {
339 ComponentDto file = ComponentTesting.newFileDto(project, null)
341 .setLanguage(XOO_LANGUAGE);
342 dbClient.componentDao().insert(dbSession, file);
347 private static Map<Integer, Block> blocksByIndexInFile(List<Block> blocks) {
348 Map<Integer, Block> blocksByIndexInFile = new HashMap<>();
349 for (Block block : blocks) {
350 blocksByIndexInFile.put(block.getIndexInFile(), block);
352 return blocksByIndexInFile;