]> source.dussan.org Git - sonarqube.git/blob
b78da1eb6689d58b90f72e62bb47767c7733a8c6
[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 java.util.Collections;
26 import java.util.Map;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.sonar.api.ce.posttask.QualityGate;
32 import org.sonar.ce.task.projectanalysis.metric.Metric;
33 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus;
35
36 import static com.google.common.collect.ImmutableMap.of;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41 @RunWith(DataProviderRunner.class)
42 public class ConditionToConditionTest {
43   private static final String METRIC_KEY = "metricKey";
44   private static final String ERROR_THRESHOLD = "error threshold";
45   private static final String WARN_THRESHOLD = "warn threshold";
46   private static final Map<Condition, ConditionStatus> NO_STATUS_PER_CONDITIONS = Collections.emptyMap();
47   private static final String SOME_VALUE = "some value";
48   private static final ConditionStatus SOME_CONDITION_STATUS = ConditionStatus.create(ConditionStatus.EvaluationStatus.OK, SOME_VALUE);
49   private static final Condition SOME_CONDITION = new Condition(newMetric(METRIC_KEY), Condition.Operator.EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD);
50
51   @Rule
52   public ExpectedException expectedException = ExpectedException.none();
53
54   @Test
55   public void apply_throws_NPE_if_Condition_argument_is_null() {
56     ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS);
57
58     expectedException.expect(NullPointerException.class);
59
60     underTest.apply(null);
61   }
62
63   @Test
64   public void apply_throws_ISE_if_there_is_no_ConditionStatus_for_Condition_argument() {
65     ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS);
66
67     expectedException.expect(IllegalStateException.class);
68     expectedException.expectMessage("Missing ConditionStatus for condition on metric key " + METRIC_KEY);
69
70     underTest.apply(SOME_CONDITION);
71   }
72
73   @Test
74   @UseDataProvider("allEvaluationStatuses")
75   public void apply_converts_all_values_of_status(ConditionStatus.EvaluationStatus status) {
76     ConditionToCondition underTest = new ConditionToCondition(of(
77       SOME_CONDITION,
78       status == ConditionStatus.EvaluationStatus.NO_VALUE ? ConditionStatus.NO_VALUE_STATUS : ConditionStatus.create(status, SOME_VALUE)));
79
80     assertThat(underTest.apply(SOME_CONDITION).getStatus().name()).isEqualTo(status.name());
81   }
82
83   @Test
84   public void apply_converts_key_from_metric() {
85     ConditionToCondition underTest = new ConditionToCondition(of(SOME_CONDITION, SOME_CONDITION_STATUS));
86
87     assertThat(underTest.apply(SOME_CONDITION).getMetricKey()).isEqualTo(METRIC_KEY);
88   }
89
90   @Test
91   public void apply_copies_thresholds() {
92     ConditionToCondition underTest = new ConditionToCondition(of(SOME_CONDITION, SOME_CONDITION_STATUS));
93
94     assertThat(underTest.apply(SOME_CONDITION).getErrorThreshold()).isEqualTo(ERROR_THRESHOLD);
95     assertThat(underTest.apply(SOME_CONDITION).getWarningThreshold()).isEqualTo(WARN_THRESHOLD);
96   }
97
98   @Test
99   @UseDataProvider("allOperatorValues")
100   public void apply_converts_all_values_of_operator(Condition.Operator operator) {
101     Condition condition = new Condition(newMetric(METRIC_KEY), operator.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD);
102     ConditionToCondition underTest = new ConditionToCondition(of(condition, SOME_CONDITION_STATUS));
103
104     assertThat(underTest.apply(condition).getOperator().name()).isEqualTo(operator.name());
105   }
106
107   @Test
108   public void apply_copies_value() {
109     Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD);
110     ConditionToCondition underTest = new ConditionToCondition(of(
111       SOME_CONDITION, SOME_CONDITION_STATUS,
112       otherCondition, ConditionStatus.NO_VALUE_STATUS));
113
114     assertThat(underTest.apply(SOME_CONDITION).getValue()).isEqualTo(SOME_VALUE);
115
116     QualityGate.Condition res = underTest.apply(otherCondition);
117
118     expectedException.expect(IllegalStateException.class);
119     expectedException.expectMessage("There is no value when status is NO_VALUE");
120
121     res.getValue();
122   }
123
124   @DataProvider
125   public static Object[][] allEvaluationStatuses() {
126     Object[][] res = new Object[ConditionStatus.EvaluationStatus.values().length][1];
127     int i = 0;
128     for (ConditionStatus.EvaluationStatus status : ConditionStatus.EvaluationStatus.values()) {
129       res[i][0] = status;
130       i++;
131     }
132     return res;
133   }
134
135   @DataProvider
136   public static Object[][] allOperatorValues() {
137     Object[][] res = new Object[Condition.Operator.values().length][1];
138     int i = 0;
139     for (Condition.Operator operator : Condition.Operator.values()) {
140       res[i][0] = operator;
141       i++;
142     }
143     return res;
144   }
145
146   private static Metric newMetric(String metricKey) {
147     Metric metric = mock(Metric.class);
148     when(metric.getKey()).thenReturn(metricKey);
149     return metric;
150   }
151 }