3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.task.projectanalysis.measure;
22 import com.google.common.base.Optional;
23 import com.tngtech.java.junit.dataprovider.DataProvider;
24 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
25 import com.tngtech.java.junit.dataprovider.UseDataProvider;
26 import org.assertj.core.data.Offset;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.sonar.db.measure.MeasureDto;
32 import org.sonar.server.computation.task.projectanalysis.measure.Measure.Level;
33 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
34 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.guava.api.Assertions.assertThat;
39 @RunWith(DataProviderRunner.class)
40 public class MeasureDtoToMeasureTest {
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 String SOME_ALERT_TEXT = "some alert text_be_careFul!";
50 private static final MeasureDto EMPTY_MEASURE_DTO = new MeasureDto();
53 public final ExpectedException expectedException = ExpectedException.none();
55 private MeasureDtoToMeasure underTest = new MeasureDtoToMeasure();
58 public void toMeasure_returns_absent_for_null_argument() {
59 assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isAbsent();
62 @Test(expected = NullPointerException.class)
63 public void toMeasure_throws_NPE_if_metric_argument_is_null() {
64 underTest.toMeasure(EMPTY_MEASURE_DTO, null);
67 @Test(expected = NullPointerException.class)
68 public void toMeasure_throws_NPE_if_both_arguments_are_null() {
69 underTest.toMeasure(null, null);
73 public void toMeasure_returns_no_value_if_dto_has_no_data_for_Level_Metric() {
74 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_LEVEL_METRIC);
75 assertThat(measure).isPresent();
76 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
80 public void toMeasure_returns_no_value_if_dto_has_invalid_data_for_Level_Metric() {
81 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setData("trololo"), 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_dta_has_data_in_wrong_case_for_Level_Metric() {
88 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setData("waRn"), SOME_LEVEL_METRIC);
89 assertThat(measure).isPresent();
90 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
94 public void toMeasure_returns_no_QualityGateStatus_if_dto_has_no_alertStatus_for_Level_Metric() {
95 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_STRING_METRIC);
96 assertThat(measure).isPresent();
97 assertThat(measure.get().hasQualityGateStatus()).isFalse();
101 public void toMeasure_returns_no_QualityGateStatus_if_alertStatus_has_invalid_data_for_Level_Metric() {
102 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setData("trololo"), SOME_STRING_METRIC);
103 assertThat(measure).isPresent();
104 assertThat(measure.get().hasQualityGateStatus()).isFalse();
108 public void toMeasure_returns_no_QualityGateStatus_if_alertStatus_has_data_in_wrong_case_for_Level_Metric() {
109 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setData("waRn"), SOME_STRING_METRIC);
110 assertThat(measure).isPresent();
111 assertThat(measure.get().hasQualityGateStatus()).isFalse();
115 public void toMeasure_returns_value_for_LEVEL_Metric() {
116 for (Level level : Level.values()) {
117 verify_toMeasure_returns_value_for_LEVEL_Metric(level);
121 private void verify_toMeasure_returns_value_for_LEVEL_Metric(Level expectedLevel) {
122 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setData(expectedLevel.name()), SOME_LEVEL_METRIC);
123 assertThat(measure).isPresent();
124 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
125 assertThat(measure.get().getLevelValue()).isEqualTo(expectedLevel);
129 public void toMeasure_for_LEVEL_Metric_can_have_an_qualityGateStatus() {
130 MeasureDto measureDto = new MeasureDto().setData(Level.OK.name()).setAlertStatus(Level.ERROR.name()).setAlertText(SOME_ALERT_TEXT);
132 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LEVEL_METRIC);
134 assertThat(measure).isPresent();
135 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
136 assertThat(measure.get().getLevelValue()).isEqualTo(Level.OK);
137 assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.ERROR);
138 assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT);
141 @Test(expected = IllegalStateException.class)
142 public void toMeasure_for_LEVEL_Metric_ignores_data() {
143 MeasureDto measureDto = new MeasureDto().setAlertStatus(Level.ERROR.name()).setData(SOME_DATA);
145 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LEVEL_METRIC);
147 assertThat(measure).isPresent();
148 measure.get().getStringValue();
152 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Int_Metric() {
153 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_INT_METRIC);
155 assertThat(measure.isPresent()).isTrue();
156 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
160 public void toMeasure_returns_int_part_of_value_in_dto_for_Int_Metric() {
161 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setValue(1.5d), SOME_INT_METRIC);
163 assertThat(measure.isPresent()).isTrue();
164 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT);
165 assertThat(measure.get().getIntValue()).isEqualTo(1);
169 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Int_Metric() {
170 MeasureDto measureDto = new MeasureDto().setValue(10d).setData(SOME_DATA).setAlertStatus(Level.OK.name()).setAlertText(SOME_ALERT_TEXT);
172 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_INT_METRIC);
174 assertThat(measure.isPresent()).isTrue();
175 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT);
176 assertThat(measure.get().getIntValue()).isEqualTo(10);
177 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
178 assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.OK);
179 assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT);
183 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Long_Metric() {
184 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_LONG_METRIC);
186 assertThat(measure.isPresent()).isTrue();
187 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
191 public void toMeasure_returns_long_part_of_value_in_dto_for_Long_Metric() {
192 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setValue(1.5d), SOME_LONG_METRIC);
194 assertThat(measure.isPresent()).isTrue();
195 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
196 assertThat(measure.get().getLongValue()).isEqualTo(1);
200 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Long_Metric() {
201 MeasureDto measureDto = new MeasureDto().setValue(10d).setData(SOME_DATA).setAlertStatus(Level.OK.name()).setAlertText(SOME_ALERT_TEXT);
203 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LONG_METRIC);
205 assertThat(measure.isPresent()).isTrue();
206 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
207 assertThat(measure.get().getLongValue()).isEqualTo(10);
208 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
209 assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.OK);
210 assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT);
214 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Double_Metric() {
215 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_DOUBLE_METRIC);
217 assertThat(measure.isPresent()).isTrue();
218 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
222 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Double_Metric() {
223 MeasureDto measureDto = new MeasureDto().setValue(10.6395d).setData(SOME_DATA).setAlertStatus(Level.OK.name()).setAlertText(SOME_ALERT_TEXT);
225 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_DOUBLE_METRIC);
227 assertThat(measure.isPresent()).isTrue();
228 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.DOUBLE);
229 assertThat(measure.get().getDoubleValue()).isEqualTo(10.6395d);
230 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
231 assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.OK);
232 assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT);
236 public void toMeasure_returns_no_value_if_dto_has_no_value_for_Boolean_metric() {
237 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_BOOLEAN_METRIC);
239 assertThat(measure.isPresent()).isTrue();
240 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
244 public void toMeasure_returns_false_value_if_dto_has_invalid_value_for_Boolean_metric() {
245 Optional<Measure> measure = underTest.toMeasure(new MeasureDto().setValue(1.987d), SOME_BOOLEAN_METRIC);
247 assertThat(measure.isPresent()).isTrue();
248 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
249 assertThat(measure.get().getBooleanValue()).isFalse();
253 public void toMeasure_maps_data_and_alert_properties_in_dto_for_Boolean_metric() {
254 MeasureDto measureDto = new MeasureDto().setValue(1d).setData(SOME_DATA).setAlertStatus(Level.OK.name()).setAlertText(SOME_ALERT_TEXT);
256 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_BOOLEAN_METRIC);
258 assertThat(measure.isPresent()).isTrue();
259 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
260 assertThat(measure.get().getBooleanValue()).isTrue();
261 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
262 assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.OK);
263 assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT);
267 public void toMeasure_returns_no_value_if_dto_has_no_value_for_String_Metric() {
268 Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_STRING_METRIC);
270 assertThat(measure.isPresent()).isTrue();
271 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
275 public void toMeasure_maps_alert_properties_in_dto_for_String_Metric() {
276 MeasureDto measureDto = new MeasureDto().setData(SOME_DATA).setAlertStatus(Level.OK.name()).setAlertText(SOME_ALERT_TEXT);
278 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_STRING_METRIC);
280 assertThat(measure.isPresent()).isTrue();
281 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.STRING);
282 assertThat(measure.get().getStringValue()).isEqualTo(SOME_DATA);
283 assertThat(measure.get().getData()).isEqualTo(SOME_DATA);
284 assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.OK);
285 assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT);
289 public static Object[][] all_types_MeasureDtos() {
290 return new Object[][] {
291 {new MeasureDto().setValue(1d), SOME_BOOLEAN_METRIC},
292 {new MeasureDto().setValue(1d), SOME_INT_METRIC},
293 {new MeasureDto().setValue(1d), SOME_LONG_METRIC},
294 {new MeasureDto().setValue(1d), SOME_DOUBLE_METRIC},
295 {new MeasureDto().setData("1"), SOME_STRING_METRIC},
296 {new MeasureDto().setData(Measure.Level.OK.name()), SOME_LEVEL_METRIC}
301 @UseDataProvider("all_types_MeasureDtos")
302 public void toMeasure_creates_no_MeasureVariation_if_dto_has_none_whichever_the_ValueType(MeasureDto measureDto, Metric metric) {
303 assertThat(underTest.toMeasure(measureDto, metric).get().hasVariations()).isFalse();
307 @UseDataProvider("all_types_MeasureDtos")
308 public void toMeasure_creates_MeasureVariation_and_maps_the_right_one(MeasureDto builder, Metric metric) {
309 assertThat(underTest.toMeasure(builder.setVariation(1d), metric).get().getVariations().getVariation1()).isEqualTo(1);
313 public void toMeasure_creates_MeasureVariation_and_maps_the_right_one() {
314 MeasureDto measureDto = new MeasureDto()
318 Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_STRING_METRIC);
320 assertThat(measure.get().getVariations().getVariation1()).isEqualTo(2);
324 public void toMeasure_should_not_loose_decimals_of_float_values() {
325 MetricImpl metric = new MetricImpl(42, "double", "name", Metric.MetricType.FLOAT, 5, null, false);
326 MeasureDto measureDto = new MeasureDto()
329 Optional<Measure> measure = underTest.toMeasure(measureDto, metric);
331 assertThat(measure.get().getDoubleValue()).isEqualTo(0.12345, Offset.offset(0.000001));