]> source.dussan.org Git - sonarqube.git/blob
6fcabab19e8a469e16ee499b1ef69976e3d251cf
[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 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbTester;
32 import org.sonar.scanner.protocol.output.ScannerReport;
33 import org.sonar.server.computation.batch.BatchReportReaderRule;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.Component;
36 import org.sonar.server.computation.component.DbIdsRepositoryImpl;
37 import org.sonar.server.computation.component.ReportComponent;
38 import org.sonar.server.computation.duplication.CrossProjectDuplicationStatusHolder;
39
40 import static java.util.Collections.singletonList;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45 public class PersistCrossProjectDuplicationIndexStepTest {
46
47   static final int FILE_REF = 2;
48   static final Component FILE = ReportComponent.builder(Component.Type.FILE, FILE_REF).build();
49   static final long FILE_SNAPSHOT_ID = 11L;
50
51   static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1)
52     .addChildren(FILE)
53     .build();
54   static final long PROJECT_SNAPSHOT_ID = 10L;
55
56   static final ScannerReport.CpdTextBlock CPD_TEXT_BLOCK = ScannerReport.CpdTextBlock.newBuilder()
57     .setHash("a8998353e96320ec")
58     .setStartLine(30)
59     .setEndLine(45)
60     .build();
61
62   @Rule
63   public DbTester dbTester = DbTester.create(System2.INSTANCE);
64
65   @Rule
66   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
67
68   @Rule
69   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
70
71   CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
72
73   DbIdsRepositoryImpl dbIdsRepository = new DbIdsRepositoryImpl();
74
75   DbClient dbClient = dbTester.getDbClient();
76
77   ComputationStep underTest = new PersistCrossProjectDuplicationIndexStep(dbClient, dbIdsRepository, treeRootHolder, reportReader, crossProjectDuplicationStatusHolder);
78
79   @Before
80   public void setUp() throws Exception {
81     dbIdsRepository.setSnapshotId(PROJECT, PROJECT_SNAPSHOT_ID);
82     dbIdsRepository.setSnapshotId(FILE, FILE_SNAPSHOT_ID);
83   }
84
85   @Test
86   public void persist_cpd_text_block() throws Exception {
87     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
88     reportReader.putDuplicationBlocks(FILE_REF, singletonList(CPD_TEXT_BLOCK));
89
90     underTest.execute();
91
92     Map<String, Object> dto = dbTester.selectFirst("select hash as \"hash\", start_line as \"startLine\", end_line as \"endLine\", index_in_file as \"indexInFile\", " +
93       "snapshot_id as \"snapshotId\", component_uuid as \"componentUuid\", project_snapshot_id as \"projectSnapshotId\" from duplications_index");
94     assertThat(dto.get("hash")).isEqualTo(CPD_TEXT_BLOCK.getHash());
95     assertThat(dto.get("startLine")).isEqualTo(30L);
96     assertThat(dto.get("endLine")).isEqualTo(45L);
97     assertThat(dto.get("indexInFile")).isEqualTo(0L);
98     assertThat(dto.get("snapshotId")).isEqualTo(FILE_SNAPSHOT_ID);
99     assertThat(dto.get("componentUuid")).isEqualTo(FILE.getUuid());
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\", component_uuid as \"componentUuid\", 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("componentUuid").containsOnly(FILE.getUuid());
124     assertThat(dtos).extracting("projectSnapshotId").containsOnly(PROJECT_SNAPSHOT_ID);
125   }
126
127   @Test
128   public void nothing_to_persist_when_no_cpd_text_blocks_in_report() throws Exception {
129     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
130     reportReader.putDuplicationBlocks(FILE_REF, Collections.<ScannerReport.CpdTextBlock>emptyList());
131
132     underTest.execute();
133
134     assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
135   }
136
137   @Test
138   public void nothing_to_do_when_cross_project_duplication_is_disabled() throws Exception {
139     when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
140     reportReader.putDuplicationBlocks(FILE_REF, singletonList(CPD_TEXT_BLOCK));
141
142     underTest.execute();
143
144     assertThat(dbTester.countRowsOfTable("duplications_index")).isEqualTo(0);
145   }
146
147 }