]> source.dussan.org Git - sonarqube.git/blob
e681f8448e5485de6c83c7dfc67c1db5b7037905
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.projectexport.steps;
21
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.utils.log.LogTester;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.ce.task.projectexport.component.ComponentRepositoryImpl;
31 import org.sonar.ce.task.step.TestComputationStepContext;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.component.BranchDto;
34 import org.sonar.db.component.BranchType;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.component.SnapshotDto;
37 import org.sonar.db.measure.MeasureDto;
38 import org.sonar.db.metric.MetricDto;
39
40 import static com.google.common.collect.Lists.newArrayList;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
45 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
46 import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
47 import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED;
48
49 public class ExportMeasuresStepTest {
50
51   private static final ComponentDto PROJECT = new ComponentDto()
52     .setDbKey("project_key")
53     .setUuid("project_uuid")
54     .setRootUuid("project_uuid")
55     .setProjectUuid("project_uuid")
56     .setUuidPath(UUID_PATH_OF_ROOT)
57     .setEnabled(true);
58   private static final ComponentDto FILE = new ComponentDto()
59     .setDbKey("file_key")
60     .setUuid("file_uuid")
61     .setRootUuid("project_uuid")
62     .setProjectUuid("project_uuid")
63     .setUuidPath(UUID_PATH_OF_ROOT + PROJECT.uuid() + UUID_PATH_SEPARATOR)
64     .setEnabled(true);
65   private static final ComponentDto ANOTHER_PROJECT = new ComponentDto()
66     .setDbKey("another_project_key")
67     .setUuid("another_project_uuid")
68     .setRootUuid("another_project_uuid")
69     .setProjectUuid("another_project_uuid")
70     .setUuidPath(UUID_PATH_OF_ROOT)
71     .setEnabled(true);
72
73   private static final MetricDto NCLOC = new MetricDto()
74     .setUuid("3")
75     .setKey("ncloc")
76     .setShortName("Lines of code")
77     .setEnabled(true);
78
79   private static final MetricDto DISABLED_METRIC = new MetricDto()
80     .setUuid("4")
81     .setKey("coverage")
82     .setShortName("Coverage")
83     .setEnabled(false);
84
85   private static final List<BranchDto> BRANCHES = newArrayList(
86     new BranchDto()
87       .setBranchType(BranchType.BRANCH)
88       .setKey("master")
89       .setUuid(PROJECT.uuid())
90       .setProjectUuid(PROJECT.uuid()));
91
92
93   @Rule
94   public LogTester logTester = new LogTester();
95
96   @Rule
97   public DbTester dbTester = DbTester.create(System2.INSTANCE);
98
99   private ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl();
100   private MutableMetricRepository metricRepository = new MutableMetricRepositoryImpl();
101   private ProjectHolder projectHolder = mock(ProjectHolder.class);
102   private FakeDumpWriter dumpWriter = new FakeDumpWriter();
103   private ExportMeasuresStep underTest = new ExportMeasuresStep(dbTester.getDbClient(), projectHolder, componentRepository, metricRepository, dumpWriter);
104
105   @Before
106   public void setUp() {
107     String projectUuid = dbTester.components().insertPublicProject(PROJECT).uuid();
108     componentRepository.register(1, projectUuid, false);
109     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, ANOTHER_PROJECT);
110     dbTester.getDbClient().metricDao().insert(dbTester.getSession(), NCLOC, DISABLED_METRIC);
111     dbTester.commit();
112     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
113     when(projectHolder.branches()).thenReturn(BRANCHES);
114   }
115
116   @Test
117   public void export_zero_measures() {
118     underTest.execute(new TestComputationStepContext());
119
120     assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES)).isEmpty();
121     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 measures exported");
122     assertThat(metricRepository.getRefByUuid()).isEmpty();
123   }
124
125   @Test
126   public void export_measures() {
127     SnapshotDto firstAnalysis = insertSnapshot("U_1", PROJECT, STATUS_PROCESSED);
128     insertMeasure(firstAnalysis, PROJECT, new MeasureDto().setValue(100.0).setMetricUuid(NCLOC.getUuid()));
129     SnapshotDto secondAnalysis = insertSnapshot("U_2", PROJECT, STATUS_PROCESSED);
130     insertMeasure(secondAnalysis, PROJECT, new MeasureDto().setValue(110.0).setMetricUuid(NCLOC.getUuid()));
131     SnapshotDto anotherProjectAnalysis = insertSnapshot("U_3", ANOTHER_PROJECT, STATUS_PROCESSED);
132     insertMeasure(anotherProjectAnalysis, ANOTHER_PROJECT, new MeasureDto().setValue(500.0).setMetricUuid(NCLOC.getUuid()));
133     dbTester.commit();
134
135     underTest.execute(new TestComputationStepContext());
136
137     List<ProjectDump.Measure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES);
138     assertThat(exportedMeasures).hasSize(2);
139     assertThat(exportedMeasures).extracting(ProjectDump.Measure::getAnalysisUuid).containsOnly(firstAnalysis.getUuid(), secondAnalysis.getUuid());
140     assertThat(exportedMeasures).extracting(ProjectDump.Measure::getMetricRef).containsOnly(0);
141     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 measures exported");
142     assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(NCLOC.getUuid());
143   }
144
145   @Test
146   public void do_not_export_measures_on_unprocessed_snapshots() {
147     SnapshotDto firstAnalysis = insertSnapshot("U_1", PROJECT, STATUS_UNPROCESSED);
148     insertMeasure(firstAnalysis, PROJECT, new MeasureDto().setValue(100.0).setMetricUuid(NCLOC.getUuid()));
149     dbTester.commit();
150
151     underTest.execute(new TestComputationStepContext());
152
153     List<ProjectDump.Measure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES);
154     assertThat(exportedMeasures).isEmpty();
155   }
156
157   @Test
158   public void do_not_export_measures_on_disabled_metrics() {
159     SnapshotDto firstAnalysis = insertSnapshot("U_1", PROJECT, STATUS_PROCESSED);
160     insertMeasure(firstAnalysis, PROJECT, new MeasureDto().setValue(100.0).setMetricUuid(DISABLED_METRIC.getUuid()));
161     dbTester.commit();
162
163     underTest.execute(new TestComputationStepContext());
164
165     List<ProjectDump.Measure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES);
166     assertThat(exportedMeasures).isEmpty();
167   }
168
169   @Test
170   public void test_exported_fields() {
171     SnapshotDto analysis = insertSnapshot("U_1", PROJECT, STATUS_PROCESSED);
172     MeasureDto dto = new MeasureDto()
173       .setMetricUuid(NCLOC.getUuid())
174       .setValue(100.0)
175       .setData("data")
176       .setAlertStatus("OK")
177       .setAlertText("alert text")
178       .setVariation(1.0);
179     insertMeasure(analysis, PROJECT, dto);
180     dbTester.commit();
181
182     underTest.execute(new TestComputationStepContext());
183
184     List<ProjectDump.Measure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES);
185     ProjectDump.Measure measure = exportedMeasures.get(0);
186     assertThat(measure.getAlertStatus()).isEqualTo(dto.getAlertStatus());
187     assertThat(measure.getAlertText()).isEqualTo(dto.getAlertText());
188     assertThat(measure.getDoubleValue().getValue()).isEqualTo(dto.getValue());
189     assertThat(measure.getTextValue()).isEqualTo(dto.getData());
190     assertThat(measure.getMetricRef()).isZero();
191     assertThat(measure.getAnalysisUuid()).isEqualTo(analysis.getUuid());
192     assertThat(measure.getVariation1().getValue()).isEqualTo(dto.getVariation());
193   }
194
195   @Test
196   public void test_null_exported_fields() {
197     SnapshotDto analysis = insertSnapshot("U_1", PROJECT, STATUS_PROCESSED);
198     insertMeasure(analysis, PROJECT, new MeasureDto().setMetricUuid(NCLOC.getUuid()));
199     dbTester.commit();
200
201     underTest.execute(new TestComputationStepContext());
202
203     ProjectDump.Measure measure = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES).get(0);
204     assertThat(measure.getAlertStatus()).isEmpty();
205     assertThat(measure.getAlertText()).isEmpty();
206     assertThat(measure.hasDoubleValue()).isFalse();
207     assertThat(measure.getTextValue()).isEmpty();
208     assertThat(measure.hasVariation1()).isFalse();
209   }
210
211   @Test
212   public void test_getDescription() {
213     assertThat(underTest.getDescription()).isEqualTo("Export measures");
214   }
215
216   private SnapshotDto insertSnapshot(String snapshotUuid, ComponentDto project, String status) {
217     SnapshotDto snapshot = new SnapshotDto()
218       .setUuid(snapshotUuid)
219       .setComponentUuid(project.uuid())
220       .setStatus(status)
221       .setLast(true);
222     dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), snapshot);
223     return snapshot;
224   }
225
226   private void insertMeasure(SnapshotDto analysisDto, ComponentDto componentDto, MeasureDto measureDto) {
227     measureDto
228       .setAnalysisUuid(analysisDto.getUuid())
229       .setComponentUuid(componentDto.uuid());
230     dbTester.getDbClient().measureDao().insert(dbTester.getSession(), measureDto);
231   }
232 }