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.ce.task.projectanalysis.api.posttask;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.ce.posttask.QualityGate;
31 import static org.assertj.core.api.Assertions.assertThat;
33 @RunWith(DataProviderRunner.class)
34 public class ConditionImplTest {
35 private static final String METRIC_KEY = "metricKey";
36 private static final String ERROR_THRESHOLD = "error threshold";
37 private static final String WARN_THRESHOLD = "warn threshold";
38 private static final String VALUE = "value";
41 public ExpectedException expectedException = ExpectedException.none();
43 private ConditionImpl.Builder builder = ConditionImpl.newBuilder()
44 .setStatus(QualityGate.EvaluationStatus.OK)
45 .setMetricKey(METRIC_KEY)
46 .setOperator(QualityGate.Operator.GREATER_THAN)
47 .setErrorThreshold(ERROR_THRESHOLD)
48 .setWarningThreshold(WARN_THRESHOLD)
52 public void build_throws_NPE_if_status_is_null() {
53 builder.setStatus(null);
55 expectedException.expect(NullPointerException.class);
56 expectedException.expectMessage("status can not be null");
62 public void build_throws_NPE_if_metricKey_is_null() {
63 builder.setMetricKey(null);
65 expectedException.expect(NullPointerException.class);
66 expectedException.expectMessage("metricKey can not be null");
72 public void build_throws_NPE_if_operator_is_null() {
73 builder.setOperator(null);
75 expectedException.expect(NullPointerException.class);
76 expectedException.expectMessage("operator can not be null");
82 public void build_throws_IAE_if_both_thresholds_are_null() {
83 builder.setWarningThreshold(null)
84 .setErrorThreshold(null);
86 expectedException.expect(IllegalArgumentException.class);
87 expectedException.expectMessage("At least one of errorThreshold and warningThreshold must be non null");
93 public void getValue_throws_ISE_when_condition_type_is_NO_VALUE() {
94 builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE).setValue(null);
95 ConditionImpl condition = builder.build();
97 expectedException.expect(IllegalStateException.class);
98 expectedException.expectMessage("There is no value when status is NO_VALUE");
100 condition.getValue();
104 public static Object[][] allStatusesButNO_VALUE() {
105 Object[][] res = new Object[QualityGate.EvaluationStatus.values().length - 1][1];
107 for (QualityGate.EvaluationStatus status : QualityGate.EvaluationStatus.values()) {
108 if (status != QualityGate.EvaluationStatus.NO_VALUE) {
117 @UseDataProvider("allStatusesButNO_VALUE")
118 public void build_throws_IAE_if_value_is_null_but_status_is_not_NO_VALUE(QualityGate.EvaluationStatus status) {
119 builder.setStatus(status)
122 expectedException.expect(IllegalArgumentException.class);
123 expectedException.expectMessage("value can not be null when status is not NO_VALUE");
129 public void build_throws_IAE_if_value_is_not_null_but_status_is_NO_VALUE() {
130 builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE);
132 expectedException.expect(IllegalArgumentException.class);
133 expectedException.expectMessage("value must be null when status is NO_VALUE");
139 public void toString_ConditionImpl_of_type_different_from_NO_VALUE() {
140 assertThat(builder.build().toString())
142 "ConditionImpl{status=OK, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', value='value'}");
146 public void toString_ConditionImpl_of_type_NO_VALUE() {
147 builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE)
150 assertThat(builder.build().toString())
152 "ConditionImpl{status=NO_VALUE, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', value='null'}");
156 public void verify_getters() {
157 ConditionImpl underTest = builder.build();
159 assertThat(underTest.getStatus()).isEqualTo(QualityGate.EvaluationStatus.OK);
160 assertThat(underTest.getMetricKey()).isEqualTo(METRIC_KEY);
161 assertThat(underTest.getOperator()).isEqualTo(QualityGate.Operator.GREATER_THAN);
162 assertThat(underTest.getErrorThreshold()).isEqualTo(ERROR_THRESHOLD);
163 assertThat(underTest.getWarningThreshold()).isEqualTo(WARN_THRESHOLD);
164 assertThat(underTest.getValue()).isEqualTo(VALUE);