3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.task.projectanalysis.api.posttask;
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;
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;
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;
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);
52 public ExpectedException expectedException = ExpectedException.none();
55 public void apply_throws_NPE_if_Condition_argument_is_null() {
56 ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS);
58 expectedException.expect(NullPointerException.class);
60 underTest.apply(null);
64 public void apply_throws_ISE_if_there_is_no_ConditionStatus_for_Condition_argument() {
65 ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS);
67 expectedException.expect(IllegalStateException.class);
68 expectedException.expectMessage("Missing ConditionStatus for condition on metric key " + METRIC_KEY);
70 underTest.apply(SOME_CONDITION);
74 @UseDataProvider("allEvaluationStatuses")
75 public void apply_converts_all_values_of_status(ConditionStatus.EvaluationStatus status) {
76 ConditionToCondition underTest = new ConditionToCondition(of(
78 status == ConditionStatus.EvaluationStatus.NO_VALUE ? ConditionStatus.NO_VALUE_STATUS : ConditionStatus.create(status, SOME_VALUE)));
80 assertThat(underTest.apply(SOME_CONDITION).getStatus().name()).isEqualTo(status.name());
84 public void apply_converts_key_from_metric() {
85 ConditionToCondition underTest = new ConditionToCondition(of(SOME_CONDITION, SOME_CONDITION_STATUS));
87 assertThat(underTest.apply(SOME_CONDITION).getMetricKey()).isEqualTo(METRIC_KEY);
91 public void apply_copies_thresholds() {
92 ConditionToCondition underTest = new ConditionToCondition(of(SOME_CONDITION, SOME_CONDITION_STATUS));
94 assertThat(underTest.apply(SOME_CONDITION).getErrorThreshold()).isEqualTo(ERROR_THRESHOLD);
95 assertThat(underTest.apply(SOME_CONDITION).getWarningThreshold()).isEqualTo(WARN_THRESHOLD);
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));
104 assertThat(underTest.apply(condition).getOperator().name()).isEqualTo(operator.name());
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));
114 assertThat(underTest.apply(SOME_CONDITION).isOnLeakPeriod()).isTrue();
115 assertThat(underTest.apply(noPeriodCondition).isOnLeakPeriod()).isFalse();
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
126 assertThat(underTest.apply(SOME_CONDITION).getValue()).isEqualTo(SOME_VALUE);
128 QualityGate.Condition res = underTest.apply(otherCondition);
130 expectedException.expect(IllegalStateException.class);
131 expectedException.expectMessage("There is no value when status is NO_VALUE");
137 public static Object[][] allEvaluationStatuses() {
138 Object[][] res = new Object[ConditionStatus.EvaluationStatus.values().length][1];
140 for (ConditionStatus.EvaluationStatus status : ConditionStatus.EvaluationStatus.values()) {
148 public static Object[][] allOperatorValues() {
149 Object[][] res = new Object[Condition.Operator.values().length][1];
151 for (Condition.Operator operator : Condition.Operator.values()) {
152 res[i][0] = operator;
158 private static Metric newMetric(String metricKey) {
159 Metric metric = mock(Metric.class);
160 when(metric.getKey()).thenReturn(metricKey);