]> source.dussan.org Git - sonarqube.git/blob
3532024fab7e2df217be243db916888ef7a60bad
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.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.server.computation.task.projectanalysis.metric.Metric;
33 import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.server.computation.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, 1);
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, 1);
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_sets_onLeakPeriod_flag_when_Condition_has_non_null_Period() {
109     Condition noPeriodCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD, null);
110     ConditionToCondition underTest = new ConditionToCondition(of(
111       SOME_CONDITION, SOME_CONDITION_STATUS,
112       noPeriodCondition, SOME_CONDITION_STATUS));
113
114     assertThat(underTest.apply(SOME_CONDITION).isOnLeakPeriod()).isTrue();
115     assertThat(underTest.apply(noPeriodCondition).isOnLeakPeriod()).isFalse();
116   }
117
118   @Test
119   public void apply_copies_value() {
120     Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD, null);
121     ConditionToCondition underTest = new ConditionToCondition(of(
122         SOME_CONDITION, SOME_CONDITION_STATUS,
123         otherCondition, ConditionStatus.NO_VALUE_STATUS
124         ));
125
126     assertThat(underTest.apply(SOME_CONDITION).getValue()).isEqualTo(SOME_VALUE);
127
128     QualityGate.Condition res = underTest.apply(otherCondition);
129
130     expectedException.expect(IllegalStateException.class);
131     expectedException.expectMessage("There is no value when status is NO_VALUE");
132
133     res.getValue();
134   }
135
136   @DataProvider
137   public static Object[][] allEvaluationStatuses() {
138     Object[][] res = new Object[ConditionStatus.EvaluationStatus.values().length][1];
139     int i = 0;
140     for (ConditionStatus.EvaluationStatus status : ConditionStatus.EvaluationStatus.values()) {
141       res[i][0] = status;
142       i++;
143     }
144     return res;
145   }
146
147   @DataProvider
148   public static Object[][] allOperatorValues() {
149     Object[][] res = new Object[Condition.Operator.values().length][1];
150     int i = 0;
151     for (Condition.Operator operator : Condition.Operator.values()) {
152       res[i][0] = operator;
153       i++;
154     }
155     return res;
156   }
157
158   private static Metric newMetric(String metricKey) {
159     Metric metric = mock(Metric.class);
160     when(metric.getKey()).thenReturn(metricKey);
161     return metric;
162   }
163 }