]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17815 Adapt isCaycCompliant computation to its new definition
authorZipeng WU <zipeng.wu@sonarsource.com>
Mon, 9 Jan 2023 13:45:11 +0000 (14:45 +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 49446e85d2dc170f881fb7dd56e08225595b80c0..1bac942bda219a2490f668b5ff834171a3c56810 100644 (file)
@@ -23,6 +23,7 @@ import java.io.Serializable;
 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;
@@ -32,7 +33,9 @@ import org.sonar.db.qualitygate.QualityGateConditionDto;
 
 import static java.util.stream.Collectors.toUnmodifiableMap;
 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE;
+import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY;
+import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
@@ -50,7 +53,13 @@ public class QualityGateCaycChecker {
     NEW_COVERAGE
   );
 
-  private static final Map<String, Double> CAYC_REQUIREMENTS = CAYC_METRICS.stream()
+  private static final Set<String> EXISTENCY_REQUIREMENTS = Set.of(
+    NEW_DUPLICATED_LINES_DENSITY_KEY,
+    NEW_COVERAGE_KEY
+  );
+
+  private static final Map<String, Double> BEST_VALUE_REQUIREMENTS = CAYC_METRICS.stream()
+    .filter(metric -> !EXISTENCY_REQUIREMENTS.contains(metric.getKey()))
     .collect(toUnmodifiableMap(Metric::getKey, Metric::getBestValue));
 
   private final DbClient dbClient;
@@ -68,10 +77,9 @@ public class QualityGateCaycChecker {
       .filter(MetricDto::isEnabled)
       .collect(Collectors.toSet());
     long count = metrics.stream()
-      .filter(metric -> CAYC_REQUIREMENTS.containsKey(metric.getKey()))
       .filter(metric -> checkMetricCaycCompliant(conditionsByMetricId.get(metric.getUuid()), metric))
       .count();
-    return count == CAYC_REQUIREMENTS.size();
+    return count == CAYC_METRICS.size();
   }
 
   public boolean checkCaycCompliantFromProject(DbSession dbSession, String projectUuid) {
@@ -82,9 +90,15 @@ public class QualityGateCaycChecker {
   }
 
   private static boolean checkMetricCaycCompliant(QualityGateConditionDto condition, MetricDto metric) {
-    Double errorThreshold = Double.valueOf(condition.getErrorThreshold());
-    Double caycRequiredThreshold = CAYC_REQUIREMENTS.get(metric.getKey());
-    return caycRequiredThreshold.compareTo(errorThreshold) == 0;
+    if (EXISTENCY_REQUIREMENTS.contains(metric.getKey())) {
+      return true;
+    } else if (BEST_VALUE_REQUIREMENTS.containsKey(metric.getKey())) {
+      Double errorThreshold = Double.valueOf(condition.getErrorThreshold());
+      Double caycRequiredThreshold = BEST_VALUE_REQUIREMENTS.get(metric.getKey());
+      return caycRequiredThreshold.compareTo(errorThreshold) == 0;
+    } else {
+      return false;
+    }
   }
 
 }
index d1cf22fd214641b5567381565a37c0785d8898ec..c86475c5e386fffc263ea55677c5a946a5aeca63 100644 (file)
@@ -19,9 +19,7 @@
  */
 package org.sonar.server.qualitygate;
 
-import java.io.Serializable;
 import java.util.List;
-import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import org.junit.Rule;
 import org.junit.Test;
@@ -39,6 +37,7 @@ import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING;
 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING;
 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING;
+import static org.sonar.server.qualitygate.QualityGateCaycChecker.CAYC_METRICS;
 
 public class QualityGateCaycCheckerTest {
 
@@ -49,20 +48,15 @@ public class QualityGateCaycCheckerTest {
   @Test
   public void checkCaycCompliant() {
     String qualityGateUuid = "abcd";
-    List<Metric<? extends Serializable>> CAYC_REQUIREMENT_METRICS = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING,
-      NEW_DUPLICATED_LINES_DENSITY, NEW_COVERAGE);
-    CAYC_REQUIREMENT_METRICS
-      .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
+    CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
     assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isTrue();
   }
 
   @Test
   public void check_Cayc_NonCompliant_with_lesser_threshold_value() {
-    var metrics = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING).stream()
-      .map(this::insertMetric)
-      .collect(Collectors.toList());
+    var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList();
 
-    IntStream.range(0, metrics.size()).forEach(idx -> {
+    IntStream.range(0, 4).forEach(idx -> {
       String qualityGateUuid = "abcd" + idx;
       for (int i = 0; i < metrics.size(); i++) {
         var metric = metrics.get(i);
@@ -75,11 +69,21 @@ public class QualityGateCaycCheckerTest {
   @Test
   public void check_Cayc_NonCompliant_with_missing_metric() {
     String qualityGateUuid = "abcd";
-    List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED)
+    List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_DUPLICATED_LINES_DENSITY)
       .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
     assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isFalse();
   }
 
+  @Test
+  public void existency_requirements_check_only_existency() {
+    String qualityGateUuid = "abcd";
+    List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING)
+      .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue()));
+    List.of(NEW_COVERAGE, NEW_DUPLICATED_LINES_DENSITY)
+      .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getWorstValue()));
+    assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isTrue();
+  }
+
   private void insertCondition(MetricDto metricDto, String qualityGateUuid, Double threshold) {
     QualityGateConditionDto newCondition = new QualityGateConditionDto().setQualityGateUuid(qualityGateUuid)
       .setUuid(Uuids.create())