]> source.dussan.org Git - sonarqube.git/blob
bd0739dc0db29d737327d7c0f36f6bdfb416582a
[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.DataProviderRunner;
23 import java.util.Optional;
24 import org.assertj.core.data.Offset;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.sonar.ce.task.projectanalysis.measure.Measure.Level;
28 import org.sonar.ce.task.projectanalysis.metric.Metric;
29 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
30 import org.sonar.db.measure.LiveMeasureDto;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.assertThatThrownBy;
34
35 @RunWith(DataProviderRunner.class)
36 public class LiveMeasureDtoToMeasureTest {
37   private static final Metric SOME_INT_METRIC = new MetricImpl("42", "int", "name", Metric.MetricType.INT);
38   private static final Metric SOME_LONG_METRIC = new MetricImpl("42", "long", "name", Metric.MetricType.WORK_DUR);
39   private static final Metric SOME_DOUBLE_METRIC = new MetricImpl("42", "double", "name", Metric.MetricType.FLOAT);
40   private static final Metric SOME_STRING_METRIC = new MetricImpl("42", "string", "name", Metric.MetricType.STRING);
41   private static final Metric SOME_BOOLEAN_METRIC = new MetricImpl("42", "boolean", "name", Metric.MetricType.BOOL);
42   private static final Metric SOME_LEVEL_METRIC = new MetricImpl("42", "level", "name", Metric.MetricType.LEVEL);
43   private static final LiveMeasureDto EMPTY_MEASURE_DTO = new LiveMeasureDto();
44
45   private LiveMeasureDtoToMeasure underTest = new LiveMeasureDtoToMeasure();
46
47   @Test
48   public void toMeasure_returns_absent_for_null_argument() {
49     assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent();
50   }
51
52   @Test
53   public void toMeasure_throws_NPE_if_metric_argument_is_null() {
54     assertThatThrownBy(() -> underTest.toMeasure(EMPTY_MEASURE_DTO, null))
55       .isInstanceOf(NullPointerException.class);
56   }
57
58   @Test
59   public void toMeasure_throws_NPE_if_both_arguments_are_null() {
60     assertThatThrownBy(() -> underTest.toMeasure(null, null))
61       .isInstanceOf(NullPointerException.class);
62   }
63
64   @Test
65   public void toMeasure_returns_no_value_if_dto_has_no_data_for_Level_Metric() {
66     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_LEVEL_METRIC);
67     assertThat(measure).isPresent();
68     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
69   }
70
71   @Test
72   public void toMeasure_returns_no_value_if_dto_has_invalid_data_for_Level_Metric() {
73     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("trololo"), SOME_LEVEL_METRIC);
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_dta_has_data_in_wrong_case_for_Level_Metric() {
80     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("waRn"), SOME_LEVEL_METRIC);
81     assertThat(measure).isPresent();
82     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
83   }
84
85   @Test
86   public void toMeasure_returns_no_QualityGateStatus_if_dto_has_no_alertStatus_for_Level_Metric() {
87     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_STRING_METRIC);
88     assertThat(measure).isPresent();
89     assertThat(measure.get().hasQualityGateStatus()).isFalse();
90   }
91
92   @Test
93   public void toMeasure_returns_no_QualityGateStatus_if_alertStatus_has_invalid_data_for_Level_Metric() {
94     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("trololo"), SOME_STRING_METRIC);
95     assertThat(measure).isPresent();
96     assertThat(measure.get().hasQualityGateStatus()).isFalse();
97   }
98
99   @Test
100   public void toMeasure_returns_no_QualityGateStatus_if_alertStatus_has_data_in_wrong_case_for_Level_Metric() {
101     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("waRn"), SOME_STRING_METRIC);
102     assertThat(measure).isPresent();
103     assertThat(measure.get().hasQualityGateStatus()).isFalse();
104   }
105
106   @Test
107   public void toMeasure_returns_value_for_LEVEL_Metric() {
108     for (Level level : Level.values()) {
109       verify_toMeasure_returns_value_for_LEVEL_Metric(level);
110     }
111   }
112
113   private void verify_toMeasure_returns_value_for_LEVEL_Metric(Level expectedLevel) {
114     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData(expectedLevel.name()), SOME_LEVEL_METRIC);
115     assertThat(measure).isPresent();
116     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL);
117     assertThat(measure.get().getLevelValue()).isEqualTo(expectedLevel);
118   }
119
120   @Test
121   public void toMeasure_returns_no_value_if_dto_has_no_value_for_Int_Metric() {
122     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_INT_METRIC);
123
124     assertThat(measure).isPresent();
125     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
126   }
127
128   @Test
129   public void toMeasure_returns_int_part_of_value_in_dto_for_Int_Metric() {
130     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.5d), SOME_INT_METRIC);
131
132     assertThat(measure).isPresent();
133     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT);
134     assertThat(measure.get().getIntValue()).isOne();
135   }
136
137   @Test
138   public void toMeasure_returns_no_value_if_dto_has_no_value_for_Long_Metric() {
139     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_LONG_METRIC);
140
141     assertThat(measure).isPresent();
142     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
143   }
144
145   @Test
146   public void toMeasure_returns_long_part_of_value_in_dto_for_Long_Metric() {
147     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.5d), SOME_LONG_METRIC);
148
149     assertThat(measure).isPresent();
150     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
151     assertThat(measure.get().getLongValue()).isOne();
152   }
153
154   @Test
155   public void toMeasure_returns_no_value_if_dto_has_no_value_for_Double_Metric() {
156     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_DOUBLE_METRIC);
157
158     assertThat(measure).isPresent();
159     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
160   }
161
162   @Test
163   public void toMeasure_returns_no_value_if_dto_has_no_value_for_Boolean_metric() {
164     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_BOOLEAN_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_false_value_if_dto_has_invalid_value_for_Boolean_metric() {
172     Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.987d), SOME_BOOLEAN_METRIC);
173
174     assertThat(measure).isPresent();
175     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
176     assertThat(measure.get().getBooleanValue()).isFalse();
177   }
178
179   @Test
180   public void toMeasure_returns_no_value_if_dto_has_no_value_for_String_Metric() {
181     Optional<Measure> measure = underTest.toMeasure(EMPTY_MEASURE_DTO, SOME_STRING_METRIC);
182
183     assertThat(measure).isPresent();
184     assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
185   }
186
187   @Test
188   public void toMeasure_should_not_loose_decimals_of_float_values() {
189     MetricImpl metric = new MetricImpl("42", "double", "name", Metric.MetricType.FLOAT, 5, null, false, false);
190     LiveMeasureDto LiveMeasureDto = new LiveMeasureDto()
191       .setValue(0.12345);
192
193     Optional<Measure> measure = underTest.toMeasure(LiveMeasureDto, metric);
194
195     assertThat(measure.get().getDoubleValue()).isEqualTo(0.12345, Offset.offset(0.000001));
196   }
197 }