3 * Copyright (C) 2009-2019 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.server.measure.custom.ws;
22 import org.assertj.core.data.Offset;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.db.measure.custom.CustomMeasureDto;
27 import org.sonar.server.exceptions.BadRequestException;
28 import org.sonar.db.measure.custom.CustomMeasureTesting;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.sonar.api.measures.Metric.Level.WARN;
32 import static org.sonar.api.measures.Metric.ValueType.BOOL;
33 import static org.sonar.api.measures.Metric.ValueType.FLOAT;
34 import static org.sonar.api.measures.Metric.ValueType.INT;
35 import static org.sonar.api.measures.Metric.ValueType.LEVEL;
36 import static org.sonar.api.measures.Metric.ValueType.STRING;
37 import static org.sonar.api.measures.Metric.ValueType.WORK_DUR;
38 import static org.sonar.db.metric.MetricTesting.newMetricDto;
39 import static org.sonar.server.util.TypeValidationsTesting.newFullTypeValidations;
41 public class CustomMeasureValidatorTest {
44 public ExpectedException expectedException = ExpectedException.none();
45 CustomMeasureValidator underTest = new CustomMeasureValidator(newFullTypeValidations());
46 CustomMeasureDto customMeasure = CustomMeasureTesting.newCustomMeasureDto();
49 public void set_boolean_true_value() {
50 underTest.setMeasureValue(customMeasure, "true", newMetricDto().setValueType(BOOL.name()));
52 assertThat(customMeasure.getValue()).isCloseTo(1.0d, defaultOffset());
56 public void set_boolean_false_value() {
57 underTest.setMeasureValue(customMeasure, "false", newMetricDto().setValueType(BOOL.name()));
59 assertThat(customMeasure.getValue()).isCloseTo(0.0d, defaultOffset());
63 public void set_integer_value() {
64 underTest.setMeasureValue(customMeasure, "1984", newMetricDto().setValueType(INT.name()));
66 assertThat(customMeasure.getValue()).isCloseTo(1984d, defaultOffset());
70 public void set_float_value() {
71 underTest.setMeasureValue(customMeasure, "3.14", newMetricDto().setValueType(FLOAT.name()));
73 assertThat(customMeasure.getValue()).isCloseTo(3.14d, defaultOffset());
77 public void set_long_value() {
78 underTest.setMeasureValue(customMeasure, "123456789", newMetricDto().setValueType(WORK_DUR.name()));
80 assertThat(customMeasure.getValue()).isCloseTo(123456789d, defaultOffset());
84 public void set_level_value() {
85 underTest.setMeasureValue(customMeasure, WARN.name(), newMetricDto().setValueType(LEVEL.name()));
87 assertThat(customMeasure.getTextValue()).isEqualTo(WARN.name());
91 public void set_string_value() {
92 underTest.setMeasureValue(customMeasure, "free-text-string", newMetricDto().setValueType(STRING.name()));
94 assertThat(customMeasure.getTextValue()).isEqualTo("free-text-string");
98 public void fail_when_non_compliant_value() {
99 expectedException.expect(BadRequestException.class);
101 underTest.setMeasureValue(customMeasure, "non-compliant-boolean-value", newMetricDto().setValueType(BOOL.name()));
105 public void fail_when_non_compliant_level_value() {
106 expectedException.expect(BadRequestException.class);
108 underTest.setMeasureValue(customMeasure, "non-compliant-level-value", newMetricDto().setValueType(LEVEL.name()));
111 private Offset<Double> defaultOffset() {
112 return Offset.offset(0.01d);