]> source.dussan.org Git - sonarqube.git/blob
cfee41ef09a6e98c700f189c4fb4dc765f774518
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.step;
21
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
26
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;
40
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;
45
46 public class PersistCrossProjectDuplicationIndexStepTest {
47
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;
51
52   static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1)
53     .addChildren(FILE)
54     .build();
55   static final long PROJECT_SNAPSHOT_ID = 10L;
56
57   static final ScannerReport.CpdTextBlock CPD_TEXT_BLOCK = ScannerReport.CpdTextBlock.newBuilder()
58     .setHash("a8998353e96320ec")
59     .setStartLine(30)
60     .setEndLine(45)
61     .build();
62
63   @Rule
64   public DbTester dbTester = DbTester.create(System2.INSTANCE);
65
66   @Rule
67   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
68
69   @Rule
70   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
71
72   CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
73
74   DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
75
76   DbClient dbClient = dbTester.getDbClient();
77
78   ComputationStep underTest = new PersistCrossProjectDuplicationIndexStep(dbClient, dbIdsRepository, treeRootHolder, reportReader, crossProjectDuplicationStatusHolder);
79
80   @Before
81   public void setUp() throws Exception {
82     dbIdsRepository.setSnapshotId(PROJECT, PROJECT_SNAPSHOT_ID);
83     dbIdsRepository.setSnapshotId(FILE, FILE_SNAPSHOT_ID);
84   }
85
86   @Test
87   public void persist_cpd_text_block() throws Exception {
88     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
89     reportReader.putDuplicationBlocks(FILE_REF, singletonList(CPD_TEXT_BLOCK));
90
91     underTest.execute();
92
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);
101   }
102
103   @Test
104   public void persist_many_cpd_text_blocks() throws Exception {
105     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
106     reportReader.putDuplicationBlocks(FILE_REF, Arrays.asList(
107       CPD_TEXT_BLOCK,
108       ScannerReport.CpdTextBlock.newBuilder()
109         .setHash("b1234353e96320ff")
110         .setStartLine(20)
111         .setEndLine(15)
112         .build()));
113
114     underTest.execute();
115
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);
124   }
125
126   @Test
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());
130
131     underTest.execute();
132
133     assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
134   }
135
136   @Test
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));
140
141     underTest.execute();
142
143     assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
144   }
145
146 }