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;
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();
}
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;
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();