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.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;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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();
45 private LiveMeasureDtoToMeasure underTest = new LiveMeasureDtoToMeasure();
48 public void toMeasure_returns_absent_for_null_argument() {
49 assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent();
53 public void toMeasure_throws_NPE_if_metric_argument_is_null() {
54 assertThatThrownBy(() -> underTest.toMeasure(EMPTY_MEASURE_DTO, null))
55 .isInstanceOf(NullPointerException.class);
59 public void toMeasure_throws_NPE_if_both_arguments_are_null() {
60 assertThatThrownBy(() -> underTest.toMeasure(null, null))
61 .isInstanceOf(NullPointerException.class);
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);
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);
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);
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();
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();
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();
107 public void toMeasure_returns_value_for_LEVEL_Metric() {
108 for (Level level : Level.values()) {
109 verify_toMeasure_returns_value_for_LEVEL_Metric(level);
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);
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);
124 assertThat(measure).isPresent();
125 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
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);
132 assertThat(measure).isPresent();
133 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT);
134 assertThat(measure.get().getIntValue()).isOne();
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);
141 assertThat(measure).isPresent();
142 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
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);
149 assertThat(measure).isPresent();
150 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG);
151 assertThat(measure.get().getLongValue()).isOne();
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);
158 assertThat(measure).isPresent();
159 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
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);
166 assertThat(measure).isPresent();
167 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
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);
174 assertThat(measure).isPresent();
175 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN);
176 assertThat(measure.get().getBooleanValue()).isFalse();
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);
183 assertThat(measure).isPresent();
184 assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE);
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()
193 Optional<Measure> measure = underTest.toMeasure(LiveMeasureDto, metric);
195 assertThat(measure.get().getDoubleValue()).isEqualTo(0.12345, Offset.offset(0.000001));