3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.ce.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.ce.task.projectanalysis.metric.Metric;
33 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.ce.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);
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);
102 ConditionToCondition underTest = new ConditionToCondition(of(condition, SOME_CONDITION_STATUS));
104 assertThat(underTest.apply(condition).getOperator().name()).isEqualTo(operator.name());
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));
114 assertThat(underTest.apply(SOME_CONDITION).getValue()).isEqualTo(SOME_VALUE);
116 QualityGate.Condition res = underTest.apply(otherCondition);
118 expectedException.expect(IllegalStateException.class);
119 expectedException.expectMessage("There is no value when status is NO_VALUE");
125 public static Object[][] allEvaluationStatuses() {
126 Object[][] res = new Object[ConditionStatus.EvaluationStatus.values().length][1];
128 for (ConditionStatus.EvaluationStatus status : ConditionStatus.EvaluationStatus.values()) {
136 public static Object[][] allOperatorValues() {
137 Object[][] res = new Object[Condition.Operator.values().length][1];
139 for (Condition.Operator operator : Condition.Operator.values()) {
140 res[i][0] = operator;
146 private static Metric newMetric(String metricKey) {
147 Metric metric = mock(Metric.class);
148 when(metric.getKey()).thenReturn(metricKey);