]> source.dussan.org Git - sonarqube.git/blob
2824982e79217ac15eb27f586cdd13ab976bb7b6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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;
54
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;
66
67 public class LoadCrossProjectDuplicationsRepositoryStepTest {
68
69
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";
74
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))
78     .build();
79
80   @Rule
81   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
82     ReportComponent.builder(PROJECT, PROJECT_REF)
83       .addChildren(CURRENT_FILE).build());
84
85   @Rule
86   public BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
87
88   @Rule
89   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
90
91   private CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
92
93   @Rule
94   public DbTester dbTester = DbTester.create(System2.INSTANCE);
95
96   private DbClient dbClient = dbTester.getDbClient();
97   private DbSession dbSession = dbTester.getSession();
98   private IntegrateCrossProjectDuplications integrateCrossProjectDuplications = mock(IntegrateCrossProjectDuplications.class);
99   private Analysis baseProjectAnalysis;
100
101   private ComputationStep underTest = new LoadCrossProjectDuplicationsRepositoryStep(treeRootHolder, batchReportReader, analysisMetadataHolder, crossProjectDuplicationStatusHolder,
102     integrateCrossProjectDuplications, dbClient);
103
104   @Before
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);
110     dbSession.commit();
111
112     baseProjectAnalysis = new Analysis.Builder()
113       .setUuid(projectSnapshot.getUuid())
114       .setCreatedAt(projectSnapshot.getCreatedAt())
115       .build();
116   }
117
118   @Test
119   public void call_compute_cpd_on_one_duplication() {
120     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
121     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
122
123     ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
124     SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
125
126     ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
127
128     String hash = "a8998353e96320ec";
129     DuplicationUnitDto duplicate = new DuplicationUnitDto()
130       .setHash(hash)
131       .setStartLine(40)
132       .setEndLine(55)
133       .setIndexInFile(0)
134       .setAnalysisUuid(otherProjectSnapshot.getUuid())
135       .setComponentUuid(otherFile.uuid());
136     dbClient.duplicationDao().insert(dbSession, duplicate);
137     dbSession.commit();
138
139     ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
140       .setHash(hash)
141       .setStartLine(30)
142       .setEndLine(45)
143       .setStartTokenIndex(0)
144       .setEndTokenIndex(10)
145       .build();
146     batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock));
147
148     underTest.execute(new TestComputationStepContext());
149
150     verify(integrateCrossProjectDuplications).computeCpd(CURRENT_FILE,
151       singletonList(
152         new Block.Builder()
153           .setResourceId(CURRENT_FILE_KEY)
154           .setBlockHash(new ByteArray(hash))
155           .setIndexInFile(0)
156           .setLines(originBlock.getStartLine(), originBlock.getEndLine())
157           .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex())
158           .build()),
159       singletonList(
160         new Block.Builder()
161           .setResourceId(otherFile.getDbKey())
162           .setBlockHash(new ByteArray(hash))
163           .setIndexInFile(duplicate.getIndexInFile())
164           .setLines(duplicate.getStartLine(), duplicate.getEndLine())
165           .build()));
166   }
167
168   @Test
169   public void call_compute_cpd_on_many_duplication() {
170     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
171     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
172
173     ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
174     SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
175
176     ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
177
178     ScannerReport.CpdTextBlock originBlock1 = ScannerReport.CpdTextBlock.newBuilder()
179       .setHash("a8998353e96320ec")
180       .setStartLine(30)
181       .setEndLine(45)
182       .setStartTokenIndex(0)
183       .setEndTokenIndex(10)
184       .build();
185     ScannerReport.CpdTextBlock originBlock2 = ScannerReport.CpdTextBlock.newBuilder()
186       .setHash("b1234353e96320ff")
187       .setStartLine(10)
188       .setEndLine(25)
189       .setStartTokenIndex(5)
190       .setEndTokenIndex(15)
191       .build();
192     batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock1, originBlock2));
193
194     DuplicationUnitDto duplicate1 = new DuplicationUnitDto()
195       .setHash(originBlock1.getHash())
196       .setStartLine(40)
197       .setEndLine(55)
198       .setIndexInFile(0)
199       .setAnalysisUuid(otherProjectSnapshot.getUuid())
200       .setComponentUuid(otherFile.uuid());
201
202     DuplicationUnitDto duplicate2 = new DuplicationUnitDto()
203       .setHash(originBlock2.getHash())
204       .setStartLine(20)
205       .setEndLine(35)
206       .setIndexInFile(1)
207       .setAnalysisUuid(otherProjectSnapshot.getUuid())
208       .setComponentUuid(otherFile.uuid());
209     dbClient.duplicationDao().insert(dbSession, duplicate1);
210     dbClient.duplicationDao().insert(dbSession, duplicate2);
211     dbSession.commit();
212
213     underTest.execute(new TestComputationStepContext());
214
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);
218
219     verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture());
220
221     Map<Integer, Block> originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue());
222     assertThat(originBlocksByIndex.get(0)).isEqualTo(
223       new Block.Builder()
224         .setResourceId(CURRENT_FILE_KEY)
225         .setBlockHash(new ByteArray(originBlock1.getHash()))
226         .setIndexInFile(0)
227         .setLines(originBlock1.getStartLine(), originBlock1.getEndLine())
228         .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex())
229         .build());
230     assertThat(originBlocksByIndex.get(1)).isEqualTo(
231       new Block.Builder()
232         .setResourceId(CURRENT_FILE_KEY)
233         .setBlockHash(new ByteArray(originBlock2.getHash()))
234         .setIndexInFile(1)
235         .setLines(originBlock2.getStartLine(), originBlock2.getEndLine())
236         .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex())
237         .build());
238
239     Map<Integer, Block> duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue());
240     assertThat(duplicationBlocksByIndex.get(0)).isEqualTo(
241       new Block.Builder()
242         .setResourceId(otherFile.getDbKey())
243         .setBlockHash(new ByteArray(originBlock1.getHash()))
244         .setIndexInFile(duplicate1.getIndexInFile())
245         .setLines(duplicate1.getStartLine(), duplicate1.getEndLine())
246         .build());
247     assertThat(duplicationBlocksByIndex.get(1)).isEqualTo(
248       new Block.Builder()
249         .setResourceId(otherFile.getDbKey())
250         .setBlockHash(new ByteArray(originBlock2.getHash()))
251         .setIndexInFile(duplicate2.getIndexInFile())
252         .setLines(duplicate2.getStartLine(), duplicate2.getEndLine())
253         .build());
254   }
255
256   @Test
257   public void nothing_to_do_when_cross_project_duplication_is_disabled() {
258     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
259     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
260
261     ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
262     SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
263
264     ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
265
266     String hash = "a8998353e96320ec";
267     DuplicationUnitDto duplicate = new DuplicationUnitDto()
268       .setHash(hash)
269       .setStartLine(40)
270       .setEndLine(55)
271       .setIndexInFile(0)
272       .setAnalysisUuid(otherProjectSnapshot.getUuid())
273       .setComponentUuid(otherFIle.uuid());
274     dbClient.duplicationDao().insert(dbSession, duplicate);
275     dbSession.commit();
276
277     ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
278       .setHash(hash)
279       .setStartLine(30)
280       .setEndLine(45)
281       .setStartTokenIndex(0)
282       .setEndTokenIndex(10)
283       .build();
284     batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock));
285
286     underTest.execute(new TestComputationStepContext());
287
288     verifyZeroInteractions(integrateCrossProjectDuplications);
289   }
290
291   @Test
292   public void nothing_to_do_when_no_cpd_text_blocks_found() {
293     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
294     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
295
296     batchReportReader.putDuplicationBlocks(FILE_REF, Collections.emptyList());
297
298     underTest.execute(new TestComputationStepContext());
299
300     verifyNoInteractions(integrateCrossProjectDuplications);
301   }
302
303   @Test
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);
307
308     ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
309       .setHash("a8998353e96320ec")
310       .setStartLine(30)
311       .setEndLine(45)
312       .setStartTokenIndex(0)
313       .setEndTokenIndex(10)
314       .build();
315     batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock));
316
317     underTest.execute(new TestComputationStepContext());
318
319     verifyNoInteractions(integrateCrossProjectDuplications);
320   }
321
322   private ComponentDto createProject(String projectKey) {
323     ComponentDto project = ComponentTesting.newPrivateProjectDto().setDbKey(projectKey);
324     return dbTester.components().insertComponent(project);
325   }
326
327   private SnapshotDto createProjectSnapshot(ComponentDto project) {
328     SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(project);
329     dbClient.snapshotDao().insert(dbSession, projectSnapshot);
330     dbSession.commit();
331     return projectSnapshot;
332   }
333
334   private ComponentDto createFile(String fileKey, ComponentDto project) {
335     ComponentDto file = ComponentTesting.newFileDto(project, null)
336       .setDbKey(fileKey)
337       .setLanguage(XOO_LANGUAGE);
338     dbClient.componentDao().insert(dbSession, file);
339     dbSession.commit();
340     return file;
341   }
342
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);
347     }
348     return blocksByIndexInFile;
349   }
350
351 }