3 * Copyright (C) 2009-2021 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.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
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.metric.MetricDto;
37 import org.sonar.db.project.ProjectDto;
39 import static com.google.common.collect.Lists.newArrayList;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.tuple;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.api.measures.Metric.ValueType.INT;
46 public class ExportLiveMeasuresStepTest {
48 public ExpectedException expectedException = ExpectedException.none();
50 public LogTester logTester = new LogTester();
52 public DbTester dbTester = DbTester.create(System2.INSTANCE);
54 private ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl();
55 private MutableMetricRepository metricRepository = new MutableMetricRepositoryImpl();
56 private ProjectHolder projectHolder = mock(ProjectHolder.class);
57 private FakeDumpWriter dumpWriter = new FakeDumpWriter();
58 private ExportLiveMeasuresStep underTest = new ExportLiveMeasuresStep(dbTester.getDbClient(), projectHolder, componentRepository, metricRepository, dumpWriter);
61 public void export_zero_measures() {
62 when(projectHolder.branches()).thenReturn(newArrayList());
63 when(projectHolder.projectDto()).thenReturn(new ProjectDto().setUuid("does not exist"));
65 underTest.execute(new TestComputationStepContext());
67 assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES)).isEmpty();
68 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 live measures exported");
69 assertThat(metricRepository.getRefByUuid()).isEmpty();
73 public void export_measures() {
74 ComponentDto project = createProject(true);
75 componentRepository.register(1, project.uuid(), false);
76 MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
77 dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d).setVariation(null));
78 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
79 when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
80 .setProjectUuid(project.uuid())
81 .setUuid(project.uuid())
83 .setBranchType(BranchType.BRANCH)));
84 underTest.execute(new TestComputationStepContext());
86 List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
87 assertThat(exportedMeasures).hasSize(1);
88 assertThat(exportedMeasures)
89 .extracting(ProjectDump.LiveMeasure::getMetricRef, m -> m.getDoubleValue().getValue(), ProjectDump.LiveMeasure::hasVariation)
90 .containsOnly(tuple(0, 4711.0d, false));
91 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported");
92 assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid());
96 public void do_not_export_measures_on_disabled_projects() {
97 ComponentDto project = createProject(false);
98 componentRepository.register(1, project.uuid(), false);
99 MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
100 dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d));
101 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
102 when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
103 .setProjectUuid(project.uuid())
104 .setUuid(project.uuid())
106 .setBranchType(BranchType.BRANCH)));
108 underTest.execute(new TestComputationStepContext());
110 List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
111 assertThat(exportedMeasures).isEmpty();
115 public void do_not_export_measures_on_disabled_metrics() {
116 ComponentDto project = createProject(true);
117 componentRepository.register(1, project.uuid(), false);
118 MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setEnabled(false));
119 dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d));
120 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
121 when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
122 .setProjectUuid(project.uuid())
123 .setUuid(project.uuid())
125 .setBranchType(BranchType.BRANCH)));
127 underTest.execute(new TestComputationStepContext());
129 List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
130 assertThat(exportedMeasures).isEmpty();
134 public void test_exported_fields() {
135 ComponentDto project = createProject(true);
136 componentRepository.register(1, project.uuid(), false);
137 MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
138 dbTester.measures().insertLiveMeasure(project, metric, m -> m.setProjectUuid(project.uuid()).setValue(4711.0d).setData("test").setVariation(7.0d));
139 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
140 when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
141 .setProjectUuid(project.uuid())
142 .setUuid(project.uuid())
144 .setBranchType(BranchType.BRANCH)));
146 underTest.execute(new TestComputationStepContext());
148 List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
149 assertThat(exportedMeasures).hasSize(1);
150 assertThat(exportedMeasures)
152 ProjectDump.LiveMeasure::getComponentRef,
153 ProjectDump.LiveMeasure::getMetricRef,
154 m -> m.getDoubleValue().getValue(),
155 ProjectDump.LiveMeasure::getTextValue,
156 m -> m.getVariation().getValue())
163 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported");
164 assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid());
168 public void test_null_exported_fields() {
169 ComponentDto project = createProject(true);
170 componentRepository.register(1, project.uuid(), false);
171 MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
172 dbTester.measures().insertLiveMeasure(project, metric, m -> m.setProjectUuid(project.uuid()).setValue(null).setData((String) null).setVariation(null));
173 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
174 when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
175 .setProjectUuid(project.uuid())
176 .setUuid(project.uuid())
178 .setBranchType(BranchType.BRANCH)));
180 underTest.execute(new TestComputationStepContext());
182 List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
183 assertThat(exportedMeasures).hasSize(1);
184 assertThat(exportedMeasures)
186 ProjectDump.LiveMeasure::hasDoubleValue,
187 ProjectDump.LiveMeasure::getTextValue,
188 ProjectDump.LiveMeasure::hasVariation)
193 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported");
194 assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid());
198 public void test_getDescription() {
199 assertThat(underTest.getDescription()).isEqualTo("Export live measures");
202 private ComponentDto createProject(boolean enabled) {
203 return dbTester.components().insertPrivateProject(p -> p.setEnabled(enabled));