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.qualitygate;
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.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.sonar.api.i18n.I18n;
31 import org.sonar.api.utils.Durations;
32 import org.sonar.ce.task.projectanalysis.measure.Measure;
33 import org.sonar.ce.task.projectanalysis.metric.Metric;
34 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR;
41 @RunWith(DataProviderRunner.class)
42 public class EvaluationResultTextConverterTest {
43 private static final Metric INT_METRIC = new MetricImpl(1, "key", "int_metric_name", Metric.MetricType.INT);
44 private static final Metric SOME_VARIATION_METRIC = new MetricImpl(2, "new_variation_of_trololo", "variation_of_trololo_name", Metric.MetricType.INT);
45 private static final Condition EQ_10_CONDITION = new Condition(INT_METRIC, Condition.Operator.EQUALS.getDbValue(), "10");
46 private static final EvaluationResult OK_EVALUATION_RESULT = new EvaluationResult(Measure.Level.OK, null);
47 private static final String ERROR_THRESHOLD = "error_threshold";
49 private I18n i18n = mock(I18n.class);
50 private Durations durations = mock(Durations.class);
51 private EvaluationResultTextConverter underTest = new EvaluationResultTextConverterImpl(i18n, durations);
53 @Test(expected = NullPointerException.class)
54 public void evaluate_throws_NPE_if_Condition_arg_is_null() {
55 underTest.asText(null, OK_EVALUATION_RESULT);
58 @Test(expected = NullPointerException.class)
59 public void evaluate_throws_NPE_if_EvaluationResult_arg_is_null() {
60 underTest.asText(EQ_10_CONDITION, null);
64 public void evaluate_returns_null_if_EvaluationResult_has_level_OK() {
65 assertThat(underTest.asText(EQ_10_CONDITION, OK_EVALUATION_RESULT)).isNull();
69 public static Object[][] all_operators_for_error_levels() {
70 List<Object[]> res = new ArrayList<>();
71 for (Condition.Operator operator : Condition.Operator.values()) {
72 res.add(new Object[] {operator, ERROR});
74 return res.toArray(new Object[res.size()][2]);
78 @UseDataProvider("all_operators_for_error_levels")
79 public void evaluate_returns_msg_of_metric_plus_operator_plus_threshold_for_level_argument(Condition.Operator operator, Measure.Level level) {
80 String metricMsg = "int_metric_msg";
82 when(i18n.message(Locale.ENGLISH, "metric." + INT_METRIC.getKey() + ".name", INT_METRIC.getName()))
83 .thenReturn(metricMsg);
85 Condition condition = new Condition(INT_METRIC, operator.getDbValue(), ERROR_THRESHOLD);
87 assertThat(underTest.asText(condition, new EvaluationResult(level, null)))
88 .isEqualTo(metricMsg + " " + toSign(operator) + " " + ERROR_THRESHOLD);
92 @UseDataProvider("all_operators_for_error_levels")
93 public void evaluate_does_not_add_variation_if_metric_starts_with_variation_prefix_but_period_is_null(Condition.Operator operator, Measure.Level level) {
94 String metricMsg = "trololo_metric_msg";
96 when(i18n.message(Locale.ENGLISH, "metric." + SOME_VARIATION_METRIC.getKey() + ".name", SOME_VARIATION_METRIC.getName()))
97 .thenReturn(metricMsg);
99 Condition condition = new Condition(SOME_VARIATION_METRIC, operator.getDbValue(), ERROR_THRESHOLD);
101 assertThat(underTest.asText(condition, new EvaluationResult(level, null)))
102 .isEqualTo(metricMsg + " " + toSign(operator) + " " + ERROR_THRESHOLD);
105 private static String toSign(Condition.Operator operator) {
116 throw new IllegalArgumentException("Unsupported operator");