3 * Copyright (C) 2009-2023 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 java.util.List;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.measures.Metric;
26 import org.sonar.core.util.Uuids;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.metric.MetricDto;
29 import org.sonar.db.qualitygate.QualityGateConditionDto;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertTrue;
34 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS;
35 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES;
36 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY;
37 import static org.sonar.api.measures.CoreMetrics.LINE_COVERAGE;
38 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
39 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
40 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
41 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS;
42 import static org.sonar.server.qualitygate.QualityGateCaycChecker.BEST_VALUE_REQUIREMENTS;
43 import static org.sonar.server.qualitygate.QualityGateCaycChecker.CAYC_METRICS;
44 import static org.sonar.server.qualitygate.QualityGateCaycStatus.COMPLIANT;
45 import static org.sonar.server.qualitygate.QualityGateCaycStatus.NON_COMPLIANT;
46 import static org.sonar.server.qualitygate.QualityGateCaycStatus.OVER_COMPLIANT;
48 public class QualityGateCaycCheckerIT {
51 public DbTester db = DbTester.create();
52 QualityGateCaycChecker underTest = new QualityGateCaycChecker(db.getDbClient());
55 public void checkCaycCompliant_when_contains_all_and_only_complicant_conditions_should_return_compliant() {
56 String qualityGateUuid = "abcd";
57 CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
58 assertEquals(COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
62 public void checkCaycCompliant_when_extra_conditions_should_return_over_compliant() {
63 String qualityGateUuid = "abcd";
64 CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
66 // extra conditions outside of CAYC requirements
67 List.of(LINE_COVERAGE, DUPLICATED_LINES).forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid,
68 metric.getBestValue()));
70 assertEquals(OVER_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
74 public void checkCaycCompliant_when_conditions_have_lesser_threshold_value_should_return_non_compliant() {
75 var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList();
77 String qualityGateUuid = "abcd";
78 for (var metric : metrics) {
79 if (BEST_VALUE_REQUIREMENTS.keySet().contains(metric.getKey())) {
80 insertCondition(metric, qualityGateUuid, metric.getBestValue() - 1);
82 insertCondition(metric, qualityGateUuid, metric.getBestValue());
85 assertEquals(NON_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
89 public void isCaycCondition_when_check_compliant_condition_should_return_true() {
90 CAYC_METRICS.stream().map(this::toMetricDto)
91 .forEach(metricDto -> assertTrue(underTest.isCaycCondition(metricDto)));
95 public void isCaycCondition_when_check_non_compliant_condition_should_return_false() {
96 List.of(BLOCKER_VIOLATIONS, FUNCTION_COMPLEXITY)
97 .stream().map(this::toMetricDto)
98 .forEach(metricDto -> assertFalse(underTest.isCaycCondition(metricDto)));
103 public void checkCaycCompliant_when_missing_compliant_condition_should_return_non_compliant() {
104 String qualityGateUuid = "abcd";
105 List.of(NEW_VIOLATIONS, NEW_SECURITY_HOTSPOTS_REVIEWED)
106 .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
107 assertEquals(NON_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
111 public void existency_requirements_check_only_existency() {
112 String qualityGateUuid = "abcd";
113 List.of(NEW_VIOLATIONS, NEW_SECURITY_HOTSPOTS_REVIEWED)
114 .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
115 List.of(NEW_COVERAGE, NEW_DUPLICATED_LINES_DENSITY)
116 .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getWorstValue()));
117 assertEquals(COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
120 private void insertCondition(MetricDto metricDto, String qualityGateUuid, Double threshold) {
121 QualityGateConditionDto newCondition = new QualityGateConditionDto().setQualityGateUuid(qualityGateUuid)
122 .setUuid(Uuids.create())
123 .setMetricUuid(metricDto.getUuid())
125 .setErrorThreshold(threshold.toString());
126 db.getDbClient().gateConditionDao().insert(newCondition, db.getSession());
130 private MetricDto insertMetric(Metric metric) {
131 return db.measures().insertMetric(m -> m
132 .setKey(metric.key())
133 .setValueType(metric.getType().name())
135 .setBestValue(metric.getBestValue())
136 .setWorstValue(metric.getWorstValue())
137 .setDirection(metric.getDirection()));
140 private MetricDto toMetricDto(Metric metric) {
141 return new MetricDto()
142 .setKey(metric.key())
143 .setValueType(metric.getType().name())
144 .setBestValue(metric.getBestValue())
145 .setWorstValue(metric.getWorstValue())
146 .setDirection(metric.getDirection());