3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectexport.steps;
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;
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;
49 public class ExportMeasuresStepTest {
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)
58 private static final ComponentDto FILE = new ComponentDto()
61 .setRootUuid("project_uuid")
62 .setProjectUuid("project_uuid")
63 .setUuidPath(UUID_PATH_OF_ROOT + PROJECT.uuid() + UUID_PATH_SEPARATOR)
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)
73 private static final MetricDto NCLOC = new MetricDto()
76 .setShortName("Lines of code")
79 private static final MetricDto DISABLED_METRIC = new MetricDto()
82 .setShortName("Coverage")
85 private static final List<BranchDto> BRANCHES = newArrayList(
87 .setBranchType(BranchType.BRANCH)
89 .setUuid(PROJECT.uuid())
90 .setProjectUuid(PROJECT.uuid()));
94 public LogTester logTester = new LogTester();
97 public DbTester dbTester = DbTester.create(System2.INSTANCE);
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);
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);
112 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
113 when(projectHolder.branches()).thenReturn(BRANCHES);
117 public void export_zero_measures() {
118 underTest.execute(new TestComputationStepContext());
120 assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES)).isEmpty();
121 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 measures exported");
122 assertThat(metricRepository.getRefByUuid()).isEmpty();
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()));
135 underTest.execute(new TestComputationStepContext());
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());
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()));
151 underTest.execute(new TestComputationStepContext());
153 List<ProjectDump.Measure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES);
154 assertThat(exportedMeasures).isEmpty();
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()));
163 underTest.execute(new TestComputationStepContext());
165 List<ProjectDump.Measure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.MEASURES);
166 assertThat(exportedMeasures).isEmpty();
170 public void test_exported_fields() {
171 SnapshotDto analysis = insertSnapshot("U_1", PROJECT, STATUS_PROCESSED);
172 MeasureDto dto = new MeasureDto()
173 .setMetricUuid(NCLOC.getUuid())
176 .setAlertStatus("OK")
177 .setAlertText("alert text")
179 insertMeasure(analysis, PROJECT, dto);
182 underTest.execute(new TestComputationStepContext());
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());
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()));
201 underTest.execute(new TestComputationStepContext());
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();
212 public void test_getDescription() {
213 assertThat(underTest.getDescription()).isEqualTo("Export measures");
216 private SnapshotDto insertSnapshot(String snapshotUuid, ComponentDto project, String status) {
217 SnapshotDto snapshot = new SnapshotDto()
218 .setUuid(snapshotUuid)
219 .setComponentUuid(project.uuid())
222 dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), snapshot);
226 private void insertMeasure(SnapshotDto analysisDto, ComponentDto componentDto, MeasureDto measureDto) {
228 .setAnalysisUuid(analysisDto.getUuid())
229 .setComponentUuid(componentDto.uuid());
230 dbTester.getDbClient().measureDao().insert(dbTester.getSession(), measureDto);