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 java.util.stream.IntStream;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.core.util.Uuids;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.metric.MetricDto;
30 import org.sonar.db.qualitygate.QualityGateConditionDto;
32 import static org.junit.Assert.assertEquals;
33 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES;
34 import static org.sonar.api.measures.CoreMetrics.LINE_COVERAGE;
35 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
36 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
37 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
38 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
39 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
40 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING;
41 import static org.sonar.server.qualitygate.QualityGateCaycChecker.CAYC_METRICS;
42 import static org.sonar.server.qualitygate.QualityGateCaycStatus.COMPLIANT;
43 import static org.sonar.server.qualitygate.QualityGateCaycStatus.NON_COMPLIANT;
44 import static org.sonar.server.qualitygate.QualityGateCaycStatus.OVER_COMPLIANT;
46 public class QualityGateCaycCheckerIT {
49 public DbTester db = DbTester.create();
50 QualityGateCaycChecker underTest = new QualityGateCaycChecker(db.getDbClient());
53 public void checkCaycCompliant() {
54 String qualityGateUuid = "abcd";
55 CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
56 assertEquals(COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
60 public void check_Cayc_non_compliant_with_extra_conditions() {
61 String qualityGateUuid = "abcd";
62 CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
64 // extra conditions outside of CAYC requirements
65 List.of(LINE_COVERAGE, DUPLICATED_LINES).forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
67 assertEquals(OVER_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
71 public void check_Cayc_NonCompliant_with_lesser_threshold_value() {
72 var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList();
74 IntStream.range(0, 4).forEach(idx -> {
75 String qualityGateUuid = "abcd" + idx;
76 for (int i = 0; i < metrics.size(); i++) {
77 var metric = metrics.get(i);
78 insertCondition(metric, qualityGateUuid, idx == i ? metric.getWorstValue() : metric.getBestValue());
80 assertEquals(NON_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
85 public void check_Cayc_NonCompliant_with_missing_metric() {
86 String qualityGateUuid = "abcd";
87 List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_DUPLICATED_LINES_DENSITY)
88 .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
89 assertEquals(NON_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
93 public void existency_requirements_check_only_existency() {
94 String qualityGateUuid = "abcd";
95 List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING)
96 .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
97 List.of(NEW_COVERAGE, NEW_DUPLICATED_LINES_DENSITY)
98 .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getWorstValue()));
99 assertEquals(COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
102 private void insertCondition(MetricDto metricDto, String qualityGateUuid, Double threshold) {
103 QualityGateConditionDto newCondition = new QualityGateConditionDto().setQualityGateUuid(qualityGateUuid)
104 .setUuid(Uuids.create())
105 .setMetricUuid(metricDto.getUuid())
107 .setErrorThreshold(threshold.toString());
108 db.getDbClient().gateConditionDao().insert(newCondition, db.getSession());
112 private MetricDto insertMetric(Metric metric) {
113 return db.measures().insertMetric(m -> m
114 .setKey(metric.key())
115 .setValueType(metric.getType().name())
117 .setBestValue(metric.getBestValue())
118 .setBestValue(metric.getWorstValue())
119 .setDirection(metric.getDirection()));