3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.step;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.api.utils.System2;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbTester;
33 import org.sonar.scanner.protocol.output.ScannerReport;
34 import org.sonar.server.computation.batch.BatchReportReaderRule;
35 import org.sonar.server.computation.batch.TreeRootHolderRule;
36 import org.sonar.server.computation.component.Component;
37 import org.sonar.server.computation.component.DbIdsRepositoryImpl;
38 import org.sonar.server.computation.component.ReportComponent;
39 import org.sonar.server.computation.duplication.CrossProjectDuplicationStatusHolder;
41 import static java.util.Collections.singletonList;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 public class PersistCrossProjectDuplicationIndexStepTest {
48 static final int FILE_REF = 2;
49 static final Component FILE = ReportComponent.builder(Component.Type.FILE, FILE_REF).build();
50 static final long FILE_SNAPSHOT_ID = 11L;
52 static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1)
55 static final long PROJECT_SNAPSHOT_ID = 10L;
57 static final ScannerReport.CpdTextBlock CPD_TEXT_BLOCK = ScannerReport.CpdTextBlock.newBuilder()
58 .setHash("a8998353e96320ec")
64 public DbTester dbTester = DbTester.create(System2.INSTANCE);
67 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
70 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
72 CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
74 DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
76 DbClient dbClient = dbTester.getDbClient();
78 ComputationStep underTest = new PersistCrossProjectDuplicationIndexStep(dbClient, dbIdsRepository, treeRootHolder, reportReader, crossProjectDuplicationStatusHolder);
81 public void setUp() throws Exception {
82 dbIdsRepository.setSnapshotId(PROJECT, PROJECT_SNAPSHOT_ID);
83 dbIdsRepository.setSnapshotId(FILE, FILE_SNAPSHOT_ID);
87 public void persist_cpd_text_block() throws Exception {
88 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
89 reportReader.putDuplicationBlocks(FILE_REF, singletonList(CPD_TEXT_BLOCK));
93 Map<String, Object> dto = dbTester.selectFirst("select hash as \"hash\", start_line as \"startLine\", end_line as \"endLine\", index_in_file as \"indexInFile\", " +
94 "snapshot_id as \"snapshotId\", project_snapshot_id as \"projectSnapshotId\" from duplications_index");
95 assertThat(dto.get("hash")).isEqualTo(CPD_TEXT_BLOCK.getHash());
96 assertThat(dto.get("startLine")).isEqualTo(30L);
97 assertThat(dto.get("endLine")).isEqualTo(45L);
98 assertThat(dto.get("indexInFile")).isEqualTo(0L);
99 assertThat(dto.get("snapshotId")).isEqualTo(FILE_SNAPSHOT_ID);
100 assertThat(dto.get("projectSnapshotId")).isEqualTo(PROJECT_SNAPSHOT_ID);
104 public void persist_many_cpd_text_blocks() throws Exception {
105 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
106 reportReader.putDuplicationBlocks(FILE_REF, Arrays.asList(
108 ScannerReport.CpdTextBlock.newBuilder()
109 .setHash("b1234353e96320ff")
116 List<Map<String, Object>> dtos = dbTester.select("select hash as \"hash\", start_line as \"startLine\", end_line as \"endLine\", index_in_file as \"indexInFile\", " +
117 "snapshot_id as \"snapshotId\", project_snapshot_id as \"projectSnapshotId\" from duplications_index");
118 assertThat(dtos).extracting("hash").containsOnly(CPD_TEXT_BLOCK.getHash(), "b1234353e96320ff");
119 assertThat(dtos).extracting("startLine").containsOnly(30L, 20L);
120 assertThat(dtos).extracting("endLine").containsOnly(45L, 15L);
121 assertThat(dtos).extracting("indexInFile").containsOnly(0L, 1L);
122 assertThat(dtos).extracting("snapshotId").containsOnly(FILE_SNAPSHOT_ID);
123 assertThat(dtos).extracting("projectSnapshotId").containsOnly(PROJECT_SNAPSHOT_ID);
127 public void nothing_to_persist_when_no_cpd_text_blocks_in_report() throws Exception {
128 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
129 reportReader.putDuplicationBlocks(FILE_REF, Collections.<ScannerReport.CpdTextBlock>emptyList());
133 assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
137 public void nothing_to_do_when_cross_project_duplication_is_disabled() throws Exception {
138 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
139 reportReader.putDuplicationBlocks(FILE_REF, singletonList(CPD_TEXT_BLOCK));
143 assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);