]> source.dussan.org Git - sonarqube.git/blob
b1cdec26627705409df9a30e941382e3e107639f
[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.measure;
21
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;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
38
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);
47
48   private static final String SOME_DATA = "some_data man!";
49   private static final ScannerReport.Measure EMPTY_BATCH_MEASURE = ScannerReport.Measure.newBuilder().build();
50
51   private BatchMeasureToMeasure underTest = new BatchMeasureToMeasure();
52
53   @Test
54   public void toMeasure_returns_absent_for_null_argument() {
55     assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent();
56   }
57
58   @Test
59   public void toMeasure_throws_NPE_if_metric_argument_is_null() {
60     assertThatThrownBy(() -> underTest.toMeasure(EMPTY_BATCH_MEASURE, null))
61       .isInstanceOf(NullPointerException.class);
62   }
63
64   @Test
65   public void toMeasure_throws_NPE_if_both_arguments_are_null() {
66     assertThatThrownBy(() -> underTest.toMeasure(null, null))
67       .isInstanceOf(NullPointerException.class);
68   }
69
70   @Test
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);
73
74     assertThat(measure).isPresent();
75     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
76   }
77
78   @Test
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);
81
82     assertThat(measure).isPresent();
83     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
84   }
85
86   @Test
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);
89
90     assertThat(measure).isPresent();
91     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
92   }
93
94   @Test
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);
98     }
99   }
100
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(),
103       SOME_LEVEL_METRIC);
104     assertThat(measure).isPresent();
105     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
106     assertThat(measure.get().getLevelValue()).isEqualTo(expectedQualityGateStatus);
107   }
108
109   @Test
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()))
113       .build();
114
115     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_LEVEL_METRIC);
116
117     assertThat(measure).isPresent();
118     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
119     assertThat(measure.get().getLevelValue()).isEqualTo(Measure.Level.OK);
120   }
121
122   @Test
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);
126     }
127   }
128
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()))
132       .build();
133
134     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_LEVEL_METRIC);
135
136     assertThat(measure).isPresent();
137     assertThat(measure.get().getLevelValue()).isEqualTo(expectedLevel);
138   }
139
140   @Test
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);
143
144     assertThat(measure).isPresent();
145     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
146   }
147
148   @Test
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))
152       .build();
153
154     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_INT_METRIC);
155
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);
160   }
161
162   @Test
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);
165
166     assertThat(measure).isPresent();
167     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
168   }
169
170   @Test
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);
173
174     assertThat(measure).isPresent();
175     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
176     assertThat(measure.get().getLongValue()).isEqualTo(15);
177   }
178
179   @Test
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))
183       .build();
184
185     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_LONG_METRIC);
186
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);
191   }
192
193   @Test
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);
196
197     assertThat(measure).isPresent();
198     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
199   }
200
201   @Test
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))
205       .build();
206
207     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_DOUBLE_METRIC);
208
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);
213   }
214
215   @Test
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);
218
219     assertThat(measure).isPresent();
220     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
221   }
222
223   @Test
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);
227   }
228
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);
231
232     assertThat(measure).isPresent();
233     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
234     assertThat(measure.get().getBooleanValue()).isEqualTo(expected);
235   }
236
237   @Test
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();
241
242     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_BOOLEAN_METRIC);
243
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);
248   }
249
250   @Test
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);
253
254     assertThat(measure).isPresent();
255     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
256   }
257
258   @Test
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))
262       .build();
263
264     Optional<Measure> measure = underTest.toMeasure(batchMeasure, SOME_STRING_METRIC);
265
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);
270   }
271
272   @DataProvider
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}
281     };
282   }
283 }