3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.sonar.api.utils.System2;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbTester;
34 import org.sonar.scanner.protocol.output.ScannerReport;
35 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
36 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
37 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
38 import org.sonar.server.computation.task.projectanalysis.component.Component;
39 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
40 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
41 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
42 import org.sonar.server.computation.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
43 import org.sonar.server.computation.task.step.ComputationStep;
45 import static java.util.Collections.singletonList;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.mockito.Mockito.when;
49 public class PersistCrossProjectDuplicationIndexStepTest {
51 private static final int FILE_1_REF = 2;
52 private static final int FILE_2_REF = 3;
53 private static final String FILE_2_UUID = "file2";
55 private static final Component FILE_1 = ReportComponent.builder(Component.Type.FILE, FILE_1_REF).build();
56 private static final Component FILE_2 = ReportComponent.builder(Component.Type.FILE, FILE_2_REF)
57 .setStatus(Status.SAME).setUuid(FILE_2_UUID).build();
59 private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1)
64 private static final ScannerReport.CpdTextBlock CPD_TEXT_BLOCK = ScannerReport.CpdTextBlock.newBuilder()
65 .setHash("a8998353e96320ec")
69 private static final String ANALYSIS_UUID = "analysis uuid";
70 private static final String BASE_ANALYSIS_UUID = "base analysis uuid";
73 public DbTester dbTester = DbTester.create(System2.INSTANCE);
75 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
77 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
79 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
82 CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
84 Analysis baseAnalysis;
86 DbClient dbClient = dbTester.getDbClient();
88 ComputationStep underTest;
91 public void setUp() throws Exception {
92 MockitoAnnotations.initMocks(this);
93 when(baseAnalysis.getUuid()).thenReturn(BASE_ANALYSIS_UUID);
94 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
95 analysisMetadataHolder.setBaseAnalysis(baseAnalysis);
96 underTest = new PersistCrossProjectDuplicationIndexStep(crossProjectDuplicationStatusHolder, dbClient, treeRootHolder, analysisMetadataHolder, reportReader);
100 public void persist_cpd_text_block() throws Exception {
101 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
102 reportReader.putDuplicationBlocks(FILE_1_REF, singletonList(CPD_TEXT_BLOCK));
106 Map<String, Object> dto = dbTester.selectFirst("select HASH, START_LINE, END_LINE, INDEX_IN_FILE, COMPONENT_UUID, ANALYSIS_UUID from duplications_index");
107 assertThat(dto.get("HASH")).isEqualTo(CPD_TEXT_BLOCK.getHash());
108 assertThat(dto.get("START_LINE")).isEqualTo(30L);
109 assertThat(dto.get("END_LINE")).isEqualTo(45L);
110 assertThat(dto.get("INDEX_IN_FILE")).isEqualTo(0L);
111 assertThat(dto.get("COMPONENT_UUID")).isEqualTo(FILE_1.getUuid());
112 assertThat(dto.get("ANALYSIS_UUID")).isEqualTo(ANALYSIS_UUID);
116 public void persist_many_cpd_text_blocks() throws Exception {
117 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
118 reportReader.putDuplicationBlocks(FILE_1_REF, Arrays.asList(
120 ScannerReport.CpdTextBlock.newBuilder()
121 .setHash("b1234353e96320ff")
128 List<Map<String, Object>> dtos = dbTester.select("select HASH, START_LINE, END_LINE, INDEX_IN_FILE, COMPONENT_UUID, ANALYSIS_UUID from duplications_index");
129 assertThat(dtos).extracting("HASH").containsOnly(CPD_TEXT_BLOCK.getHash(), "b1234353e96320ff");
130 assertThat(dtos).extracting("START_LINE").containsOnly(30L, 20L);
131 assertThat(dtos).extracting("END_LINE").containsOnly(45L, 15L);
132 assertThat(dtos).extracting("INDEX_IN_FILE").containsOnly(0L, 1L);
133 assertThat(dtos).extracting("COMPONENT_UUID").containsOnly(FILE_1.getUuid());
134 assertThat(dtos).extracting("ANALYSIS_UUID").containsOnly(ANALYSIS_UUID);
138 public void nothing_to_persist_when_no_cpd_text_blocks_in_report() throws Exception {
139 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
140 reportReader.putDuplicationBlocks(FILE_1_REF, Collections.<ScannerReport.CpdTextBlock>emptyList());
144 assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
148 public void nothing_to_do_when_cross_project_duplication_is_disabled() throws Exception {
149 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
150 reportReader.putDuplicationBlocks(FILE_1_REF, singletonList(CPD_TEXT_BLOCK));
154 assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);