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.api.ce.posttask;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
26 import static org.assertj.core.api.Assertions.assertThat;
28 public class ConditionBuilder_PostProjectAnalysisTaskTesterTest {
29 private static final String SOME_METRIC_KEY = "some metric key";
30 private static final QualityGate.Operator SOME_OPERATOR = QualityGate.Operator.GREATER_THAN;
31 private static final String SOME_ERROR_THRESHOLD = "some error threshold";
32 private static final String SOME_WARNING_THRESHOLD = "some warning threshold";
33 private static final QualityGate.EvaluationStatus SOME_STATUS_BUT_NO_VALUE = QualityGate.EvaluationStatus.OK;
34 private static final String SOME_VALUE = "some value";
37 public ExpectedException expectedException = ExpectedException.none();
39 private PostProjectAnalysisTaskTester.ConditionBuilder underTest = PostProjectAnalysisTaskTester.newConditionBuilder();
42 public void setMetricKey_throws_NPE_if_operator_is_null() {
43 expectedException.expect(NullPointerException.class);
44 expectedException.expectMessage("metricKey cannot be null");
46 underTest.setMetricKey(null);
50 public void setOperator_throws_NPE_if_operator_is_null() {
51 expectedException.expect(NullPointerException.class);
52 expectedException.expectMessage("operator cannot be null");
54 underTest.setOperator(null);
58 public void buildNoValue_throws_NPE_if_metricKey_is_null() {
59 underTest.setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
61 expectedException.expect(NullPointerException.class);
62 expectedException.expectMessage("metricKey cannot be null");
64 underTest.buildNoValue();
68 public void buildNoValue_throws_NPE_if_operator_is_null() {
69 underTest.setMetricKey(SOME_METRIC_KEY).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
71 expectedException.expect(NullPointerException.class);
72 expectedException.expectMessage("operator cannot be null");
74 underTest.buildNoValue();
78 public void buildNoValue_throws_ISE_if_both_warningThreshold_and_errorThreshold_are_null() {
79 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR);
81 expectedException.expect(IllegalStateException.class);
82 expectedException.expectMessage("At least one of errorThreshold and warningThreshold must be non null");
84 underTest.buildNoValue();
88 public void buildNoValue_returns_Condition_which_getStatus_method_returns_NO_VALUE() {
91 assertThat(underTest.buildNoValue().getStatus()).isEqualTo(QualityGate.EvaluationStatus.NO_VALUE);
95 public void buildNoValue_returns_Condition_which_getValue_method_throws_ISE() {
97 QualityGate.Condition condition = underTest.buildNoValue();
99 expectedException.expect(IllegalStateException.class);
100 expectedException.expectMessage("There is no value when status is NO_VALUE");
102 condition.getValue();
106 public void buildNoValue_does_not_fail_when_only_errorThreshold_is_set() {
107 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD);
109 underTest.buildNoValue();
113 public void buildNoValue_does_not_fail_when_only_wardThreshold_is_set() {
114 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR).setWarningThreshold(SOME_WARNING_THRESHOLD);
116 underTest.buildNoValue();
120 public void buildNoValue_returns_new_instance_at_each_call() {
123 assertThat(underTest.buildNoValue()).isNotSameAs(underTest.buildNoValue());
127 public void buildNoValue_has_no_value_in_toString_and_status_is_NO_VALUE() {
130 assertThat(underTest.buildNoValue().toString())
132 "Condition{status=NO_VALUE, metricKey='some metric key', operator=GREATER_THAN, " +
133 "errorThreshold='some error threshold', warningThreshold='some warning threshold'}");
137 public void verify_getters_of_object_returned_by_buildNoValue() {
138 initValidBuilder().setOnLeakPeriod(true);
140 QualityGate.Condition condition = underTest.buildNoValue();
142 assertThat(condition.getMetricKey()).isEqualTo(SOME_METRIC_KEY);
143 assertThat(condition.getOperator()).isEqualTo(SOME_OPERATOR);
144 assertThat(condition.getErrorThreshold()).isEqualTo(SOME_ERROR_THRESHOLD);
145 assertThat(condition.getWarningThreshold()).isEqualTo(SOME_WARNING_THRESHOLD);
149 public void build_throws_NPE_if_status_is_null() {
152 expectedException.expect(NullPointerException.class);
153 expectedException.expectMessage("status cannot be null");
155 underTest.build(null, SOME_VALUE);
159 public void build_throws_IAE_if_status_is_NO_VALUE() {
162 expectedException.expect(IllegalArgumentException.class);
163 expectedException.expectMessage("status cannot be NO_VALUE, use method buildNoValue() instead");
165 underTest.build(QualityGate.EvaluationStatus.NO_VALUE, SOME_VALUE);
169 public void build_throws_NPE_if_value_is_null() {
172 expectedException.expect(NullPointerException.class);
173 expectedException.expectMessage("value cannot be null, use method buildNoValue() instead");
175 underTest.build(SOME_STATUS_BUT_NO_VALUE, null);
179 public void build_throws_NPE_if_metricKey_is_null() {
180 underTest.setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
182 expectedException.expect(NullPointerException.class);
183 expectedException.expectMessage("metricKey cannot be null");
185 underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
189 public void build_throws_NPE_if_operator_is_null() {
190 underTest.setMetricKey(SOME_METRIC_KEY).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
192 expectedException.expect(NullPointerException.class);
193 expectedException.expectMessage("operator cannot be null");
195 underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
199 public void build_does_not_fail_when_only_errorThreshold_is_set() {
200 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD);
202 underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
206 public void build_does_not_fail_when_only_wardThreshold_is_set() {
207 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR).setWarningThreshold(SOME_WARNING_THRESHOLD);
209 underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
213 public void build_throws_ISE_if_both_warningThreshold_and_errorThreshold_are_null() {
214 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR);
216 expectedException.expect(IllegalStateException.class);
217 expectedException.expectMessage("At least one of errorThreshold and warningThreshold must be non null");
219 underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
223 public void build_returns_new_instance_at_each_call() {
226 assertThat(underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE)).isNotSameAs(underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE));
230 public void build_has_value_in_toString_and_specified_status() {
233 assertThat(underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE).toString())
235 "Condition{status=OK, metricKey='some metric key', operator=GREATER_THAN, "
236 + "errorThreshold='some error threshold', warningThreshold='some warning threshold', value='some value'}");
240 public void build_returns_Condition_which_getStatus_method_returns_NO_VALUE() {
243 assertThat(underTest.buildNoValue().getStatus()).isEqualTo(QualityGate.EvaluationStatus.NO_VALUE);
247 public void build_returns_Condition_which_getValue_method_throws_ISE() {
249 QualityGate.Condition condition = underTest.buildNoValue();
251 expectedException.expect(IllegalStateException.class);
252 expectedException.expectMessage("There is no value when status is NO_VALUE");
254 condition.getValue();
258 public void verify_getters_of_object_returned_by_build() {
259 initValidBuilder().setOnLeakPeriod(true);
261 QualityGate.Condition condition = underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
263 assertThat(condition.getStatus()).isEqualTo(SOME_STATUS_BUT_NO_VALUE);
264 assertThat(condition.getMetricKey()).isEqualTo(SOME_METRIC_KEY);
265 assertThat(condition.getOperator()).isEqualTo(SOME_OPERATOR);
266 assertThat(condition.getErrorThreshold()).isEqualTo(SOME_ERROR_THRESHOLD);
267 assertThat(condition.getWarningThreshold()).isEqualTo(SOME_WARNING_THRESHOLD);
268 assertThat(condition.getValue()).isEqualTo(SOME_VALUE);
271 private PostProjectAnalysisTaskTester.ConditionBuilder initValidBuilder() {
272 underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);