]> source.dussan.org Git - sonarqube.git/blob
990000b6f889b7ced206320c2e2913baaeae6199
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.api.ce.posttask;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
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";
35
36   @Rule
37   public ExpectedException expectedException = ExpectedException.none();
38
39   private PostProjectAnalysisTaskTester.ConditionBuilder underTest = PostProjectAnalysisTaskTester.newConditionBuilder();
40
41   @Test
42   public void setMetricKey_throws_NPE_if_operator_is_null() {
43     expectedException.expect(NullPointerException.class);
44     expectedException.expectMessage("metricKey cannot be null");
45
46     underTest.setMetricKey(null);
47   }
48
49   @Test
50   public void setOperator_throws_NPE_if_operator_is_null() {
51     expectedException.expect(NullPointerException.class);
52     expectedException.expectMessage("operator cannot be null");
53
54     underTest.setOperator(null);
55   }
56
57   @Test
58   public void buildNoValue_throws_NPE_if_metricKey_is_null() {
59     underTest.setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
60
61     expectedException.expect(NullPointerException.class);
62     expectedException.expectMessage("metricKey cannot be null");
63
64     underTest.buildNoValue();
65   }
66
67   @Test
68   public void buildNoValue_throws_NPE_if_operator_is_null() {
69     underTest.setMetricKey(SOME_METRIC_KEY).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
70
71     expectedException.expect(NullPointerException.class);
72     expectedException.expectMessage("operator cannot be null");
73
74     underTest.buildNoValue();
75   }
76
77   @Test
78   public void buildNoValue_throws_ISE_if_both_warningThreshold_and_errorThreshold_are_null() {
79     underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR);
80
81     expectedException.expect(IllegalStateException.class);
82     expectedException.expectMessage("At least one of errorThreshold and warningThreshold must be non null");
83
84     underTest.buildNoValue();
85   }
86
87   @Test
88   public void buildNoValue_returns_Condition_which_getStatus_method_returns_NO_VALUE() {
89     initValidBuilder();
90
91     assertThat(underTest.buildNoValue().getStatus()).isEqualTo(QualityGate.EvaluationStatus.NO_VALUE);
92   }
93
94   @Test
95   public void buildNoValue_returns_Condition_which_getValue_method_throws_ISE() {
96     initValidBuilder();
97     QualityGate.Condition condition = underTest.buildNoValue();
98
99     expectedException.expect(IllegalStateException.class);
100     expectedException.expectMessage("There is no value when status is NO_VALUE");
101
102     condition.getValue();
103   }
104
105   @Test
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);
108
109     underTest.buildNoValue();
110   }
111
112   @Test
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);
115
116     underTest.buildNoValue();
117   }
118
119   @Test
120   public void buildNoValue_returns_new_instance_at_each_call() {
121     initValidBuilder();
122
123     assertThat(underTest.buildNoValue()).isNotSameAs(underTest.buildNoValue());
124   }
125
126   @Test
127   public void buildNoValue_has_no_value_in_toString_and_status_is_NO_VALUE() {
128     initValidBuilder();
129
130     assertThat(underTest.buildNoValue().toString())
131       .isEqualTo(
132         "Condition{status=NO_VALUE, metricKey='some metric key', operator=GREATER_THAN, " +
133           "errorThreshold='some error threshold', warningThreshold='some warning threshold'}");
134   }
135
136   @Test
137   public void verify_getters_of_object_returned_by_buildNoValue() {
138     initValidBuilder().setOnLeakPeriod(true);
139
140     QualityGate.Condition condition = underTest.buildNoValue();
141
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);
146   }
147
148   @Test
149   public void build_throws_NPE_if_status_is_null() {
150     initValidBuilder();
151
152     expectedException.expect(NullPointerException.class);
153     expectedException.expectMessage("status cannot be null");
154
155     underTest.build(null, SOME_VALUE);
156   }
157
158   @Test
159   public void build_throws_IAE_if_status_is_NO_VALUE() {
160     initValidBuilder();
161
162     expectedException.expect(IllegalArgumentException.class);
163     expectedException.expectMessage("status cannot be NO_VALUE, use method buildNoValue() instead");
164
165     underTest.build(QualityGate.EvaluationStatus.NO_VALUE, SOME_VALUE);
166   }
167
168   @Test
169   public void build_throws_NPE_if_value_is_null() {
170     initValidBuilder();
171
172     expectedException.expect(NullPointerException.class);
173     expectedException.expectMessage("value cannot be null, use method buildNoValue() instead");
174
175     underTest.build(SOME_STATUS_BUT_NO_VALUE, null);
176   }
177
178   @Test
179   public void build_throws_NPE_if_metricKey_is_null() {
180     underTest.setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
181
182     expectedException.expect(NullPointerException.class);
183     expectedException.expectMessage("metricKey cannot be null");
184
185     underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
186   }
187
188   @Test
189   public void build_throws_NPE_if_operator_is_null() {
190     underTest.setMetricKey(SOME_METRIC_KEY).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
191
192     expectedException.expect(NullPointerException.class);
193     expectedException.expectMessage("operator cannot be null");
194
195     underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
196   }
197
198   @Test
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);
201
202     underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
203   }
204
205   @Test
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);
208
209     underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
210   }
211
212   @Test
213   public void build_throws_ISE_if_both_warningThreshold_and_errorThreshold_are_null() {
214     underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR);
215
216     expectedException.expect(IllegalStateException.class);
217     expectedException.expectMessage("At least one of errorThreshold and warningThreshold must be non null");
218
219     underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
220   }
221
222   @Test
223   public void build_returns_new_instance_at_each_call() {
224     initValidBuilder();
225
226     assertThat(underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE)).isNotSameAs(underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE));
227   }
228
229   @Test
230   public void build_has_value_in_toString_and_specified_status() {
231     initValidBuilder();
232
233     assertThat(underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE).toString())
234       .isEqualTo(
235         "Condition{status=OK, metricKey='some metric key', operator=GREATER_THAN, "
236           + "errorThreshold='some error threshold', warningThreshold='some warning threshold', value='some value'}");
237   }
238
239   @Test
240   public void build_returns_Condition_which_getStatus_method_returns_NO_VALUE() {
241     initValidBuilder();
242
243     assertThat(underTest.buildNoValue().getStatus()).isEqualTo(QualityGate.EvaluationStatus.NO_VALUE);
244   }
245
246   @Test
247   public void build_returns_Condition_which_getValue_method_throws_ISE() {
248     initValidBuilder();
249     QualityGate.Condition condition = underTest.buildNoValue();
250
251     expectedException.expect(IllegalStateException.class);
252     expectedException.expectMessage("There is no value when status is NO_VALUE");
253
254     condition.getValue();
255   }
256
257   @Test
258   public void verify_getters_of_object_returned_by_build() {
259     initValidBuilder().setOnLeakPeriod(true);
260
261     QualityGate.Condition condition = underTest.build(SOME_STATUS_BUT_NO_VALUE, SOME_VALUE);
262
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);
269   }
270
271   private PostProjectAnalysisTaskTester.ConditionBuilder initValidBuilder() {
272     underTest.setMetricKey(SOME_METRIC_KEY).setOperator(SOME_OPERATOR).setErrorThreshold(SOME_ERROR_THRESHOLD).setWarningThreshold(SOME_WARNING_THRESHOLD);
273     return underTest;
274   }
275 }