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.google.common.collect.ImmutableList;
23 import com.tngtech.java.junit.dataprovider.DataProvider;
24 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
25 import com.tngtech.java.junit.dataprovider.UseDataProvider;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Locale;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.sonar.api.i18n.I18n;
32 import org.sonar.api.utils.Durations;
33 import org.sonar.ce.task.projectanalysis.measure.Measure;
34 import org.sonar.ce.task.projectanalysis.metric.Metric;
35 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR;
41 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.WARN;
43 @RunWith(DataProviderRunner.class)
44 public class EvaluationResultTextConverterTest {
45 private static final Metric INT_METRIC = new MetricImpl(1, "key", "int_metric_name", Metric.MetricType.INT);
46 private static final Metric SOME_VARIATION_METRIC = new MetricImpl(2, "new_variation_of_trololo", "variation_of_trololo_name", Metric.MetricType.INT);
47 private static final Condition EQ_10_CONDITION = new Condition(INT_METRIC, Condition.Operator.EQUALS.getDbValue(), "10", null);
48 private static final EvaluationResult OK_EVALUATION_RESULT = new EvaluationResult(Measure.Level.OK, null);
49 private static final String ERROR_THRESHOLD = "error_threshold";
50 private static final String WARNING_THRESHOLD = "warning_threshold";
51 private static final String SOME_MODE = "mode";
52 private static final String SOME_ANALYSIS_UUID = "u1";
54 private I18n i18n = mock(I18n.class);
55 private Durations durations = mock(Durations.class);
56 private EvaluationResultTextConverter underTest = new EvaluationResultTextConverterImpl(i18n, durations);
58 @Test(expected = NullPointerException.class)
59 public void evaluate_throws_NPE_if_Condition_arg_is_null() {
60 underTest.asText(null, OK_EVALUATION_RESULT);
63 @Test(expected = NullPointerException.class)
64 public void evaluate_throws_NPE_if_EvaluationResult_arg_is_null() {
65 underTest.asText(EQ_10_CONDITION, null);
69 public void evaluate_returns_null_if_EvaluationResult_has_level_OK() {
70 assertThat(underTest.asText(EQ_10_CONDITION, OK_EVALUATION_RESULT)).isNull();
74 public static Object[][] all_operators_for_error_warning_levels() {
75 List<Object[]> res = new ArrayList<>();
76 for (Condition.Operator operator : Condition.Operator.values()) {
77 for (Measure.Level level : ImmutableList.of(ERROR, WARN)) {
78 res.add(new Object[] {operator, level});
81 return res.toArray(new Object[res.size()][2]);
85 @UseDataProvider("all_operators_for_error_warning_levels")
86 public void evaluate_returns_msg_of_metric_plus_operator_plus_threshold_for_level_argument(Condition.Operator operator, Measure.Level level) {
87 String metricMsg = "int_metric_msg";
89 when(i18n.message(Locale.ENGLISH, "metric." + INT_METRIC.getKey() + ".name", INT_METRIC.getName()))
90 .thenReturn(metricMsg);
92 Condition condition = new Condition(INT_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD);
94 assertThat(underTest.asText(condition, new EvaluationResult(level, null)))
95 .isEqualTo(metricMsg + " " + toSign(operator) + " " + getThreshold(level));
98 private String getThreshold(Measure.Level level) {
99 return level == ERROR ? ERROR_THRESHOLD : WARNING_THRESHOLD;
103 @UseDataProvider("all_operators_for_error_warning_levels")
104 public void evaluate_does_not_add_variation_if_metric_starts_with_variation_prefix_but_period_is_null(Condition.Operator operator, Measure.Level level) {
105 String metricMsg = "trololo_metric_msg";
107 when(i18n.message(Locale.ENGLISH, "metric." + SOME_VARIATION_METRIC.getKey() + ".name", SOME_VARIATION_METRIC.getName()))
108 .thenReturn(metricMsg);
110 Condition condition = new Condition(SOME_VARIATION_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD);
112 assertThat(underTest.asText(condition, new EvaluationResult(level, null)))
113 .isEqualTo(metricMsg + " " + toSign(operator) + " " + getThreshold(level));
116 private static String toSign(Condition.Operator operator) {
127 throw new IllegalArgumentException("Unsupported operator");