]> source.dussan.org Git - sonarqube.git/blob
f145e54bc169bc894ac7768ff2cf55d00de42188
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.ce.task.projectanalysis.step;
21
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolderRule;
29 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
30 import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepositoryRule;
31 import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
32 import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
33 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
34 import org.sonar.ce.task.step.ComputationStep;
35 import org.sonar.ce.task.step.TestComputationStepContext;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.component.ComponentDto;
38 import org.sonar.db.measure.LiveMeasureDto;
39 import org.sonar.db.metric.MetricDto;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA_KEY;
43 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
44 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
45 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
46
47 public class PersistDuplicationDataStepIT extends BaseStepTest {
48
49   private static final int ROOT_REF = 1;
50   private static final String PROJECT_KEY = "PROJECT_KEY";
51   private static final String PROJECT_UUID = "u1";
52
53   private static final int FILE_1_REF = 2;
54   private static final String FILE_1_KEY = "FILE_1_KEY";
55   private static final String FILE_1_UUID = "u2";
56
57   private static final int FILE_2_REF = 3;
58   private static final String FILE_2_KEY = "FILE_2_KEY";
59   private static final String FILE_2_UUID = "u3";
60
61   @Rule
62   public DbTester db = DbTester.create(System2.INSTANCE);
63   @Rule
64   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
65     .setRoot(
66       builder(PROJECT, ROOT_REF).setKey(PROJECT_KEY).setUuid(PROJECT_UUID)
67         .addChildren(
68           builder(FILE, FILE_1_REF).setKey(FILE_1_KEY).setUuid(FILE_1_UUID)
69             .build(),
70           builder(FILE, FILE_2_REF).setKey(FILE_2_KEY).setUuid(FILE_2_UUID)
71             .build())
72         .build());
73
74   @Rule
75   public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
76   @Rule
77   public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
78   @Rule
79   public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
80
81   @Before
82   public void setUp() {
83     MetricDto metric = db.measures().insertMetric(m -> m.setKey(DUPLICATIONS_DATA_KEY).setValueType(Metric.ValueType.STRING.name()));
84     insertComponent(PROJECT_KEY, PROJECT_UUID);
85     insertComponent(FILE_1_KEY, FILE_1_UUID);
86     insertComponent(FILE_2_KEY, FILE_2_UUID);
87     db.commit();
88     metricRepository.add(metric.getUuid(), new Metric.Builder(DUPLICATIONS_DATA_KEY, DUPLICATIONS_DATA_KEY, Metric.ValueType.STRING).create());
89   }
90
91   @Override
92   protected ComputationStep step() {
93     return underTest();
94   }
95
96   @Test
97   public void nothing_to_persist_when_no_duplication() {
98     TestComputationStepContext context = new TestComputationStepContext();
99
100     underTest().execute(context);
101
102     assertThatNothingPersisted();
103     verifyStatistics(context, 0);
104   }
105
106   @Test
107   public void compute_duplications_on_same_file() {
108     duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), new TextBlock(6, 10));
109     TestComputationStepContext context = new TestComputationStepContext();
110
111     underTest().execute(context);
112
113     assertThat(selectMeasureData(FILE_1_UUID)).hasValue("<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
114         + FILE_1_KEY + "\"/></g></duplications>");
115     assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
116     assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
117   }
118
119   @Test
120   public void compute_duplications_on_different_files() {
121     duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
122     TestComputationStepContext context = new TestComputationStepContext();
123
124     underTest().execute(context);
125
126     assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
127       "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
128         + FILE_2_KEY + "\"/></g></duplications>");
129     assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
130     assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
131   }
132
133   @Test
134   public void compute_duplications_on_unchanged_file() {
135     duplicationRepository.addExtendedProjectDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
136     TestComputationStepContext context = new TestComputationStepContext();
137
138     underTest().execute(context);
139
140     assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
141       "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"true\" r=\""
142         + FILE_2_KEY + "\"/></g></duplications>");
143     assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
144     assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
145   }
146
147   @Test
148   public void compute_duplications_on_different_projects() {
149     String fileKeyFromOtherProject = "PROJECT2_KEY:file2";
150     duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 5), fileKeyFromOtherProject, new TextBlock(6, 10));
151     TestComputationStepContext context = new TestComputationStepContext();
152
153     underTest().execute(context);
154
155     assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
156       "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
157         + fileKeyFromOtherProject + "\"/></g></duplications>");
158     assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
159     assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
160   }
161
162   private PersistDuplicationDataStep underTest() {
163     return new PersistDuplicationDataStep(db.getDbClient(), treeRootHolder, metricRepository, duplicationRepository,
164       new MeasureToMeasureDto(analysisMetadataHolder, treeRootHolder));
165   }
166
167   private void assertThatNothingPersisted() {
168     assertThat(db.countRowsOfTable(db.getSession(), "live_measures")).isZero();
169   }
170
171   private Optional<String> selectMeasureData(String componentUuid) {
172       return db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), componentUuid, "duplications_data")
173       .map(LiveMeasureDto::getTextValue);
174   }
175
176   private ComponentDto insertComponent(String key, String uuid) {
177     ComponentDto componentDto = new ComponentDto()
178       .setKey(key)
179       .setUuid(uuid)
180       .setUuidPath(uuid + ".")
181       .setBranchUuid(uuid);
182     db.components().insertComponent(componentDto);
183     return componentDto;
184   }
185
186   private static void verifyStatistics(TestComputationStepContext context, int expectedInsertsOrUpdates) {
187     context.getStatistics().assertValue("insertsOrUpdates", expectedInsertsOrUpdates);
188   }
189 }