]> source.dussan.org Git - sonarqube.git/blob
190a16e3215bd3893142f823d313d2ab04057baa
[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.ce.task.projectanalysis.api.posttask;
21
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;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32
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";
39
40   @Rule
41   public ExpectedException expectedException = ExpectedException.none();
42
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)
49     .setValue(VALUE);
50
51   @Test
52   public void build_throws_NPE_if_status_is_null() {
53     builder.setStatus(null);
54
55     expectedException.expect(NullPointerException.class);
56     expectedException.expectMessage("status can not be null");
57
58     builder.build();
59   }
60
61   @Test
62   public void build_throws_NPE_if_metricKey_is_null() {
63     builder.setMetricKey(null);
64
65     expectedException.expect(NullPointerException.class);
66     expectedException.expectMessage("metricKey can not be null");
67
68     builder.build();
69   }
70
71   @Test
72   public void build_throws_NPE_if_operator_is_null() {
73     builder.setOperator(null);
74
75     expectedException.expect(NullPointerException.class);
76     expectedException.expectMessage("operator can not be null");
77
78     builder.build();
79   }
80
81   @Test
82   public void build_throws_IAE_if_both_thresholds_are_null() {
83     builder.setWarningThreshold(null)
84       .setErrorThreshold(null);
85
86     expectedException.expect(IllegalArgumentException.class);
87     expectedException.expectMessage("At least one of errorThreshold and warningThreshold must be non null");
88
89     builder.build();
90   }
91
92   @Test
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();
96
97     expectedException.expect(IllegalStateException.class);
98     expectedException.expectMessage("There is no value when status is NO_VALUE");
99
100     condition.getValue();
101   }
102
103   @DataProvider
104   public static Object[][] allStatusesButNO_VALUE() {
105     Object[][] res = new Object[QualityGate.EvaluationStatus.values().length - 1][1];
106     int i = 0;
107     for (QualityGate.EvaluationStatus status : QualityGate.EvaluationStatus.values()) {
108       if (status != QualityGate.EvaluationStatus.NO_VALUE) {
109         res[i][0] = status;
110         i++;
111       }
112     }
113     return res;
114   }
115
116   @Test
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)
120       .setValue(null);
121
122     expectedException.expect(IllegalArgumentException.class);
123     expectedException.expectMessage("value can not be null when status is not NO_VALUE");
124
125     builder.build();
126   }
127
128   @Test
129   public void build_throws_IAE_if_value_is_not_null_but_status_is_NO_VALUE() {
130     builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE);
131
132     expectedException.expect(IllegalArgumentException.class);
133     expectedException.expectMessage("value must be null when status is NO_VALUE");
134
135     builder.build();
136   }
137
138   @Test
139   public void toString_ConditionImpl_of_type_different_from_NO_VALUE() {
140     assertThat(builder.build().toString())
141       .isEqualTo(
142         "ConditionImpl{status=OK, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', value='value'}");
143   }
144
145   @Test
146   public void toString_ConditionImpl_of_type_NO_VALUE() {
147     builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE)
148       .setValue(null);
149
150     assertThat(builder.build().toString())
151       .isEqualTo(
152         "ConditionImpl{status=NO_VALUE, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', value='null'}");
153   }
154
155   @Test
156   public void verify_getters() {
157     ConditionImpl underTest = builder.build();
158
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);
165   }
166 }