]> source.dussan.org Git - sonarqube.git/blob
e9e9e712f3e9f45827819dbc255cc5e3c0f119c7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualitygate;
21
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;
31
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;
45
46 public class QualityGateCaycCheckerIT {
47
48   @Rule
49   public DbTester db = DbTester.create();
50   QualityGateCaycChecker underTest = new QualityGateCaycChecker(db.getDbClient());
51
52   @Test
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));
57   }
58
59   @Test
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()));
63
64     // extra conditions outside of CAYC requirements
65     List.of(LINE_COVERAGE, DUPLICATED_LINES).forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
66
67     assertEquals(OVER_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
68   }
69
70   @Test
71   public void check_Cayc_NonCompliant_with_lesser_threshold_value() {
72     var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList();
73
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());
79       }
80       assertEquals(NON_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
81     });
82   }
83
84   @Test
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));
90   }
91
92   @Test
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));
100   }
101
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())
106       .setOperator("LT")
107       .setErrorThreshold(threshold.toString());
108     db.getDbClient().gateConditionDao().insert(newCondition, db.getSession());
109     db.commit();
110   }
111
112   private MetricDto insertMetric(Metric metric) {
113     return db.measures().insertMetric(m -> m
114       .setKey(metric.key())
115       .setValueType(metric.getType().name())
116       .setHidden(false)
117       .setBestValue(metric.getBestValue())
118       .setBestValue(metric.getWorstValue())
119       .setDirection(metric.getDirection()));
120   }
121 }