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 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;
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;
.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) {
}
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;
+ }
}
}
*/
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;
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 {
@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);
@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())