]> source.dussan.org Git - sonarqube.git/blob
5eb3e51d4a308db2879d203f21e29fdb77120ec8
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
21
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
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;
44
45 import static java.util.Collections.singletonList;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.mockito.Mockito.when;
48
49 public class PersistCrossProjectDuplicationIndexStepTest {
50
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";
54
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();
58
59   private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1)
60     .addChildren(FILE_1)
61     .addChildren(FILE_2)
62     .build();
63
64   private static final ScannerReport.CpdTextBlock CPD_TEXT_BLOCK = ScannerReport.CpdTextBlock.newBuilder()
65     .setHash("a8998353e96320ec")
66     .setStartLine(30)
67     .setEndLine(45)
68     .build();
69   private static final String ANALYSIS_UUID = "analysis uuid";
70   private static final String BASE_ANALYSIS_UUID = "base analysis uuid";
71
72   @Rule
73   public DbTester dbTester = DbTester.create(System2.INSTANCE);
74   @Rule
75   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
76   @Rule
77   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
78   @Rule
79   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
80
81   @Mock
82   CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder;
83   @Mock
84   Analysis baseAnalysis;
85
86   DbClient dbClient = dbTester.getDbClient();
87
88   ComputationStep underTest;
89
90   @Before
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);
97   }
98
99   @Test
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));
103
104     underTest.execute();
105
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);
113   }
114
115   @Test
116   public void persist_many_cpd_text_blocks() throws Exception {
117     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
118     reportReader.putDuplicationBlocks(FILE_1_REF, Arrays.asList(
119       CPD_TEXT_BLOCK,
120       ScannerReport.CpdTextBlock.newBuilder()
121         .setHash("b1234353e96320ff")
122         .setStartLine(20)
123         .setEndLine(15)
124         .build()));
125
126     underTest.execute();
127
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);
135   }
136
137   @Test
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());
141
142     underTest.execute();
143
144     assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
145   }
146
147   @Test
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));
151
152     underTest.execute();
153
154     assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
155   }
156
157 }