]> source.dussan.org Git - sonarqube.git/blob
881379b75070e8030bd935d28ae7b374270cf398
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.measure;
21
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;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.guava.api.Assertions.assertThat;
38
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);
47
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();
51
52   @Rule
53   public final ExpectedException expectedException = ExpectedException.none();
54
55   private MeasureDtoToMeasure underTest = new MeasureDtoToMeasure();
56
57   @Test
58   public void toMeasure_returns_absent_for_null_argument() {
59     assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isAbsent();
60   }
61
62   @Test(expected = NullPointerException.class)
63   public void toMeasure_throws_NPE_if_metric_argument_is_null() {
64     underTest.toMeasure(EMPTY_MEASURE_DTO, null);
65   }
66
67   @Test(expected = NullPointerException.class)
68   public void toMeasure_throws_NPE_if_both_arguments_are_null() {
69     underTest.toMeasure(null, null);
70   }
71
72   @Test
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);
77   }
78
79   @Test
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);
84   }
85
86   @Test
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);
91   }
92
93   @Test
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();
98   }
99
100   @Test
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();
105   }
106
107   @Test
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();
112   }
113
114   @Test
115   public void toMeasure_returns_value_for_LEVEL_Metric() {
116     for (Level level : Level.values()) {
117       verify_toMeasure_returns_value_for_LEVEL_Metric(level);
118     }
119   }
120
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);
126   }
127
128   @Test
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);
131
132     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LEVEL_METRIC);
133
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);
139   }
140
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);
144
145     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LEVEL_METRIC);
146
147     assertThat(measure).isPresent();
148     measure.get().getStringValue();
149   }
150
151   @Test
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);
154
155     assertThat(measure.isPresent()).isTrue();
156     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
157   }
158
159   @Test
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);
162
163     assertThat(measure.isPresent()).isTrue();
164     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT);
165     assertThat(measure.get().getIntValue()).isEqualTo(1);
166   }
167
168   @Test
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);
171
172     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_INT_METRIC);
173
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);
180   }
181
182   @Test
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);
185
186     assertThat(measure.isPresent()).isTrue();
187     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
188   }
189
190   @Test
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);
193
194     assertThat(measure.isPresent()).isTrue();
195     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
196     assertThat(measure.get().getLongValue()).isEqualTo(1);
197   }
198
199   @Test
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);
202
203     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LONG_METRIC);
204
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);
211   }
212
213   @Test
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);
216
217     assertThat(measure.isPresent()).isTrue();
218     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
219   }
220
221   @Test
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);
224
225     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_DOUBLE_METRIC);
226
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);
233   }
234
235   @Test
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);
238
239     assertThat(measure.isPresent()).isTrue();
240     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
241   }
242
243   @Test
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);
246
247     assertThat(measure.isPresent()).isTrue();
248     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
249     assertThat(measure.get().getBooleanValue()).isFalse();
250   }
251
252   @Test
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);
255
256     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_BOOLEAN_METRIC);
257
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);
264   }
265
266   @Test
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);
269
270     assertThat(measure.isPresent()).isTrue();
271     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
272   }
273
274   @Test
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);
277
278     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_STRING_METRIC);
279
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);
286   }
287
288   @DataProvider
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}
297     };
298   }
299
300   @Test
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();
304   }
305
306   @Test
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);
310   }
311
312   @Test
313   public void toMeasure_creates_MeasureVariation_and_maps_the_right_one() {
314     MeasureDto measureDto = new MeasureDto()
315       .setData("1")
316       .setVariation(2d);
317
318     Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_STRING_METRIC);
319
320     assertThat(measure.get().getVariations().getVariation1()).isEqualTo(2);
321   }
322
323   @Test
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()
327       .setValue(0.12345);
328
329     Optional<Measure> measure = underTest.toMeasure(measureDto, metric);
330
331     assertThat(measure.get().getDoubleValue()).isEqualTo(0.12345, Offset.offset(0.000001));
332   }
333 }