]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17815 isCaycCompliant flag should be false with extra non-CAYC conditions
authorZipeng WU <zipeng.wu@sonarsource.com>
Tue, 10 Jan 2023 14:17:47 +0000 (15:17 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 10 Jan 2023 20:03:01 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateCaycChecker.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateCaycCheckerTest.java

index 1bac942bda219a2490f668b5ff834171a3c56810..c1ae05f9364d72109d085037e82c87dd2711011c 100644 (file)
@@ -24,7 +24,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.stream.Collectors;
 import org.sonar.api.measures.Metric;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -72,13 +71,20 @@ public class QualityGateCaycChecker {
     var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid)
       .stream()
       .collect(uniqueIndex(QualityGateConditionDto::getMetricUuid));
+
+    if (conditionsByMetricId.size() != CAYC_METRICS.size()) {
+      return false;
+    }
+
     var metrics = dbClient.metricDao().selectByUuids(dbSession, conditionsByMetricId.keySet())
       .stream()
       .filter(MetricDto::isEnabled)
-      .collect(Collectors.toSet());
+      .toList();
+
     long count = metrics.stream()
       .filter(metric -> checkMetricCaycCompliant(conditionsByMetricId.get(metric.getUuid()), metric))
       .count();
+
     return count == CAYC_METRICS.size();
   }
 
index c86475c5e386fffc263ea55677c5a946a5aeca63..51471717a459dcb63935e8e1533465e0e4994fbf 100644 (file)
@@ -31,6 +31,8 @@ import org.sonar.db.metric.MetricDto;
 import org.sonar.db.qualitygate.QualityGateConditionDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES;
+import static org.sonar.api.measures.CoreMetrics.LINE_COVERAGE;
 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
@@ -52,6 +54,17 @@ public class QualityGateCaycCheckerTest {
     assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isTrue();
   }
 
+  @Test
+  public void check_Cayc_non_compliant_with_extra_conditions() {
+    String qualityGateUuid = "abcd";
+    CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
+
+    // extra conditions outside of CAYC requirements
+    List.of(LINE_COVERAGE, DUPLICATED_LINES).forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
+
+    assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isFalse();
+  }
+
   @Test
   public void check_Cayc_NonCompliant_with_lesser_threshold_value() {
     var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList();