3 * Copyright (C) 2009-2024 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.projectanalysis.measure;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import java.util.Optional;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.sonar.ce.task.projectanalysis.metric.Metric;
28 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
29 import org.sonar.scanner.protocol.output.ScannerReport;
30 import org.sonar.scanner.protocol.output.ScannerReport.Measure.BoolValue;
31 import org.sonar.scanner.protocol.output.ScannerReport.Measure.DoubleValue;
32 import org.sonar.scanner.protocol.output.ScannerReport.Measure.IntValue;
33 import org.sonar.scanner.protocol.output.ScannerReport.Measure.LongValue;
34 import org.sonar.scanner.protocol.output.ScannerReport.Measure.StringValue;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 @RunWith(DataProviderRunner.class)
40 public class BatchMeasureToMeasureTest {
41 private static final Metric SOME_INT_METRIC = new MetricImpl("42", "int", "name", Metric.MetricType.INT);
42 private static final Metric SOME_LONG_METRIC = new MetricImpl("42", "long", "name", Metric.MetricType.WORK_DUR);
43 private static final Metric SOME_DOUBLE_METRIC = new MetricImpl("42", "double", "name", Metric.MetricType.FLOAT);
44 private static final Metric SOME_STRING_METRIC = new MetricImpl("42", "string", "name", Metric.MetricType.STRING);
45 private static final Metric SOME_BOOLEAN_METRIC = new MetricImpl("42", "boolean", "name", Metric.MetricType.BOOL);
46 private static final Metric SOME_LEVEL_METRIC = new MetricImpl("42", "level", "name", Metric.MetricType.LEVEL);
48 private static final String SOME_DATA = "some_data man!";
49 private static final ScannerReport.Measure EMPTY_BATCH_MEASURE = ScannerReport.Measure.newBuilder().build();
51 private BatchMeasureToMeasure underTest = new BatchMeasureToMeasure();
54 public void toMeasure_returns_absent_for_null_argument() {
55 assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent();
59 public void toMeasure_throws_NPE_if_metric_argument_is_null() {
60 assertThatThrownBy(() -> underTest.toMeasure(EMPTY_BATCH_MEASURE, null))
61 .isInstanceOf(NullPointerException.class);
65 public void toMeasure_throws_NPE_if_both_arguments_are_null() {
66 assertThatThrownBy(() -> underTest.toMeasure(null, null))
67 .isInstanceOf(NullPointerException.class);
71 public void toMeasure_returns_no_value_if_dto_has_no_string_value_for_LEVEL_Metric() {
72 Optional<Measure> measure = underTest.toMeasure(EMPTY_BATCH_MEASURE, SOME_LEVEL_METRIC);
74 assertThat(measure).isPresent();
75 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
79 public void toMeasure_returns_no_value_if_dto_has_invalid_string_value_for_LEVEL_Metric() {
80 Optional<Measure> measure = underTest.toMeasure(ScannerReport.Measure.newBuilder().setStringValue(StringValue.newBuilder().setValue("trololo")).build(), SOME_LEVEL_METRIC);
82 assertThat(measure).isPresent();
83 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
87 public void toMeasure_returns_no_value_if_dto_has_value_in_wrong_case_for_LEVEL_Metric() {
88 Optional<Measure> measure = underTest.toMeasure(ScannerReport.Measure.newBuilder().setStringValue(StringValue.newBuilder().setValue("waRn")).build(), SOME_LEVEL_METRIC);
90 assertThat(measure).isPresent();
91 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
95 public void toMeasure_returns_value_for_LEVEL_Metric() {
96 for (Measure.Level alertStatus : Measure.Level.values()) {
97 verify_toMeasure_returns_value_for_LEVEL_Metric(alertStatus);
101 private void verify_toMeasure_returns_value_for_LEVEL_Metric(Measure.Level expectedQualityGateStatus) {
102 Optional<Measure> measure = underTest.toMeasure(ScannerReport.Measure.newBuilder().setStringValue(StringValue.newBuilder().setValue(expectedQualityGateStatus.name())).build(),
104 assertThat(measure).isPresent();
105 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
106 assertThat(measure.get().getLevelValue()).isEqualTo(expectedQualityGateStatus);
110 public void toMeasure_for_LEVEL_Metric_maps_QualityGateStatus() {
111 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
112 .setStringValue(StringValue.newBuilder().setValue(Measure.Level.OK.name()))
115 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_LEVEL_METRIC);
117 assertThat(measure).isPresent();
118 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
119 assertThat(measure.get().getLevelValue()).isEqualTo(Measure.Level.OK);
123 public void toMeasure_for_LEVEL_Metric_parses_level_from_data() {
124 for (Measure.Level level : Measure.Level.values()) {
125 verify_toMeasure_for_LEVEL_Metric_parses_level_from_data(level);
129 private void verify_toMeasure_for_LEVEL_Metric_parses_level_from_data(Measure.Level expectedLevel) {
130 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
131 .setStringValue(StringValue.newBuilder().setValue(expectedLevel.name()))
134 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_LEVEL_METRIC);
136 assertThat(measure).isPresent();
137 assertThat(measure.get().getLevelValue()).isEqualTo(expectedLevel);
141 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Int_Metric() {
142 Optional<Measure> measure = underTest.toMeasure(EMPTY_BATCH_MEASURE, SOME_INT_METRIC);
144 assertThat(measure).isPresent();
145 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
149 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Int_Metric() {
150 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
151 .setIntValue(IntValue.newBuilder().setValue(10).setData(SOME_DATA))
154 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_INT_METRIC);
156 assertThat(measure).isPresent();
157 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT);
158 assertThat(measure.get().getIntValue()).isEqualTo(10);
159 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
163 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Long_Metric() {
164 Optional<Measure> measure = underTest.toMeasure(EMPTY_BATCH_MEASURE, SOME_LONG_METRIC);
166 assertThat(measure).isPresent();
167 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
171 public void toMeasure_returns_long_part_of_value_in_dto_for_Long_Metric() {
172 Optional<Measure> measure = underTest.toMeasure(ScannerReport.Measure.newBuilder().setLongValue(LongValue.newBuilder().setValue(15L)).build(), SOME_LONG_METRIC);
174 assertThat(measure).isPresent();
175 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
176 assertThat(measure.get().getLongValue()).isEqualTo(15);
180 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Long_Metric() {
181 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
182 .setLongValue(LongValue.newBuilder().setValue(10L).setData(SOME_DATA))
185 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_LONG_METRIC);
187 assertThat(measure).isPresent();
188 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
189 assertThat(measure.get().getLongValue()).isEqualTo(10);
190 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
194 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Double_Metric() {
195 Optional<Measure> measure = underTest.toMeasure(EMPTY_BATCH_MEASURE, SOME_DOUBLE_METRIC);
197 assertThat(measure).isPresent();
198 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
202 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Double_Metric() {
203 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
204 .setDoubleValue(DoubleValue.newBuilder().setValue(10.6395d).setData(SOME_DATA))
207 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_DOUBLE_METRIC);
209 assertThat(measure).isPresent();
210 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.DOUBLE);
211 assertThat(measure.get().getDoubleValue()).isEqualTo(10.6395d);
212 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
216 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Boolean_metric() {
217 Optional<Measure> measure = underTest.toMeasure(EMPTY_BATCH_MEASURE, SOME_BOOLEAN_METRIC);
219 assertThat(measure).isPresent();
220 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
224 public void toMeasure_returns_false_value_if_dto_has_invalid_value_for_Boolean_metric() {
225 verify_toMeasure_returns_false_value_if_dto_has_invalid_value_for_Boolean_metric(true);
226 verify_toMeasure_returns_false_value_if_dto_has_invalid_value_for_Boolean_metric(false);
229 private void verify_toMeasure_returns_false_value_if_dto_has_invalid_value_for_Boolean_metric(boolean expected) {
230 Optional<Measure> measure = underTest.toMeasure(ScannerReport.Measure.newBuilder().setBooleanValue(BoolValue.newBuilder().setValue(expected)).build(), SOME_BOOLEAN_METRIC);
232 assertThat(measure).isPresent();
233 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
234 assertThat(measure.get().getBooleanValue()).isEqualTo(expected);
238 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Boolean_metric() {
239 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
240 .setBooleanValue(BoolValue.newBuilder().setValue(true).setData(SOME_DATA)).build();
242 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_BOOLEAN_METRIC);
244 assertThat(measure).isPresent();
245 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
246 assertThat(measure.get().getBooleanValue()).isTrue();
247 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
251 public void toMeasure_returns_no_value_if_dto_has_no_value_for_String_Metric() {
252 Optional<Measure> measure = underTest.toMeasure(EMPTY_BATCH_MEASURE, SOME_STRING_METRIC);
254 assertThat(measure).isPresent();
255 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
259 public void toMeasure_maps_alert_properties_in_dto_for_String_Metric() {
260 ScannerReport.Measure batchMeasure = ScannerReport.Measure.newBuilder()
261 .setStringValue(StringValue.newBuilder().setValue(SOME_DATA))
264 Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_STRING_METRIC);
266 assertThat(measure).isPresent();
267 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.STRING);
268 assertThat(measure.get().getStringValue()).isEqualTo(SOME_DATA);
269 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
273 public static Object[][] all_types_batch_measure_builders() {
274 return new Object[][] {
275 {ScannerReport.Measure.newBuilder().setBooleanValue(BoolValue.newBuilder().setValue(true)), SOME_BOOLEAN_METRIC},
276 {ScannerReport.Measure.newBuilder().setIntValue(IntValue.newBuilder().setValue(1)), SOME_INT_METRIC},
277 {ScannerReport.Measure.newBuilder().setLongValue(LongValue.newBuilder().setValue(1)), SOME_LONG_METRIC},
278 {ScannerReport.Measure.newBuilder().setDoubleValue(DoubleValue.newBuilder().setValue(1)), SOME_DOUBLE_METRIC},
279 {ScannerReport.Measure.newBuilder().setStringValue(StringValue.newBuilder().setValue("1")), SOME_STRING_METRIC},
280 {ScannerReport.Measure.newBuilder().setStringValue(StringValue.newBuilder().setValue(Measure.Level.OK.name())), SOME_LEVEL_METRIC}