]> source.dussan.org Git - sonarqube.git/blob
d5d151227bc2e31eed087e102ae9abd2a1b6b617
[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 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;
30
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;
47
48 public class QualityGateCaycCheckerIT {
49
50   @Rule
51   public DbTester db = DbTester.create();
52   QualityGateCaycChecker underTest = new QualityGateCaycChecker(db.getDbClient());
53
54   @Test
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));
59   }
60
61   @Test
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()));
65
66     // extra conditions outside of CAYC requirements
67     List.of(LINE_COVERAGE, DUPLICATED_LINES).forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid,
68       metric.getBestValue()));
69
70     assertEquals(OVER_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
71   }
72
73   @Test
74   public void checkCaycCompliant_when_conditions_have_lesser_threshold_value_should_return_non_compliant() {
75     var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList();
76
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);
81       } else {
82         insertCondition(metric, qualityGateUuid, metric.getBestValue());
83       }
84     }
85     assertEquals(NON_COMPLIANT, underTest.checkCaycCompliant(db.getSession(), qualityGateUuid));
86   }
87
88   @Test
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)));
92   }
93
94   @Test
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)));
99   }
100
101
102   @Test
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));
108   }
109
110   @Test
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));
118   }
119
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())
124       .setOperator("LT")
125       .setErrorThreshold(threshold.toString());
126     db.getDbClient().gateConditionDao().insert(newCondition, db.getSession());
127     db.commit();
128   }
129
130   private MetricDto insertMetric(Metric metric) {
131     return db.measures().insertMetric(m -> m
132       .setKey(metric.key())
133       .setValueType(metric.getType().name())
134       .setHidden(false)
135       .setBestValue(metric.getBestValue())
136       .setWorstValue(metric.getWorstValue())
137       .setDirection(metric.getDirection()));
138   }
139
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());
147   }
148 }