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