3 * Copyright (C) 2009-2021 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.server.qualitygate;
22 import com.google.common.collect.ImmutableSet;
24 import java.util.stream.Collectors;
25 import org.junit.Test;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Metric;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32 import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY;
34 public class QualityGateEvaluatorImplTest {
36 private final QualityGateEvaluator underTest = new QualityGateEvaluatorImpl();
39 public void getMetricKeys_includes_by_default_new_lines() {
40 QualityGate gate = mock(QualityGate.class);
41 assertThat(underTest.getMetricKeys(gate)).containsExactly(NEW_LINES_KEY);
45 public void getMetricKeys_includes_metrics_from_qgate() {
46 Set<String> metricKeys = ImmutableSet.of("foo", "bar", "baz");
47 Set<Condition> conditions = metricKeys.stream().map(key -> {
48 Condition condition = mock(Condition.class);
49 when(condition.getMetricKey()).thenReturn(key);
51 }).collect(Collectors.toSet());
53 QualityGate gate = mock(QualityGate.class);
54 when(gate.getConditions()).thenReturn(conditions);
55 assertThat(underTest.getMetricKeys(gate)).containsAll(metricKeys);
59 public void evaluated_conditions_are_sorted() {
60 Set<String> metricKeys = ImmutableSet.of("foo", "bar", CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY);
61 Set<Condition> conditions = metricKeys.stream().map(key -> {
62 Condition condition = mock(Condition.class);
63 when(condition.getMetricKey()).thenReturn(key);
65 }).collect(Collectors.toSet());
67 QualityGate gate = mock(QualityGate.class);
68 when(gate.getConditions()).thenReturn(conditions);
69 QualityGateEvaluator.Measures measures = mock(QualityGateEvaluator.Measures.class);
71 assertThat(underTest.evaluate(gate, measures).getEvaluatedConditions()).extracting(x -> x.getCondition().getMetricKey())
72 .containsExactly(CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY, "bar", "foo");
76 public void evaluate_is_OK_for_empty_qgate() {
77 QualityGate gate = mock(QualityGate.class);
78 QualityGateEvaluator.Measures measures = mock(QualityGateEvaluator.Measures.class);
79 EvaluatedQualityGate evaluatedQualityGate = underTest.evaluate(gate, measures);
80 assertThat(evaluatedQualityGate.getStatus()).isEqualTo(Metric.Level.OK);