]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16316 Update wording for value type & error threshold of quality gate findings
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>
Tue, 24 May 2022 15:24:51 +0000 (17:24 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 25 May 2022 20:03:16 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateFindingDto.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java

index 171215f93a09d149463fd159f40e3d4581f47315..20829ac2df49aaa5fb5f7ca498b4131f37737699 100644 (file)
@@ -22,6 +22,8 @@ package org.sonar.db.qualitygate;
 import javax.annotation.CheckForNull;
 
 public class QualityGateFindingDto {
+  public static final String RATING_VALUE_TYPE = "RATING";
+  public static final String PERCENT_VALUE_TYPE = "PERCENT";
   public static final String NEW_CODE_METRIC_PREFIX = "new_";
 
   private String description = null;
@@ -35,16 +37,16 @@ public class QualityGateFindingDto {
   private String errorThreshold = null;
   private Integer decimalScale = null;
 
-  private String getOperator() {
-    return operator;
-  }
-
   public String getDescription() {
     return description;
   }
 
   public String getOperatorDescription() {
-    return OperatorDescription.valueOf(getOperator()).getDescription();
+    if (isRating(getValueType())) {
+      return RatingType.valueOf(getOperator()).getDescription();
+    }
+
+    return PercentageType.valueOf(getOperator()).getDescription();
   }
 
   public Boolean isNewCodeMetric() {
@@ -70,6 +72,10 @@ public class QualityGateFindingDto {
   }
 
   public String getErrorThreshold() {
+    if (isRating(getValueType())) {
+      return RatingValue.valueOf(Integer.parseInt(errorThreshold));
+    }
+
     return errorThreshold;
   }
 
@@ -82,7 +88,32 @@ public class QualityGateFindingDto {
     return decimalScale;
   }
 
-  public enum OperatorDescription {
+  private String getOperator() {
+    return operator;
+  }
+
+  private static boolean isRating(String metricType) {
+    return RATING_VALUE_TYPE.equals(metricType);
+  }
+
+  public enum RatingType {
+    LT("Is Better Than"),
+    GT("Is Worse Than"),
+    EQ("Is"),
+    NE("Is Not");
+
+    private final String desc;
+
+    RatingType(String desc) {
+      this.desc = desc;
+    }
+
+    public String getDescription() {
+      return desc;
+    }
+  }
+
+  public enum PercentageType {
     LT("Is Less Than"),
     GT("Is Greater Than"),
     EQ("Is Equal To"),
@@ -90,7 +121,7 @@ public class QualityGateFindingDto {
 
     private final String desc;
 
-    OperatorDescription(String desc) {
+    PercentageType(String desc) {
       this.desc = desc;
     }
 
@@ -98,4 +129,12 @@ public class QualityGateFindingDto {
       return desc;
     }
   }
+
+  public enum RatingValue {
+    A, B, C, D, E;
+
+    public static String valueOf(int index) {
+      return values()[index - 1].name();
+    }
+  }
 }
index 4225abb47783f0c9f8ab554898963cab9caa0368..a5d9a085c1c30b453ac4d3937ce87ca9e8d5072d 100644 (file)
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.stream.Collectors;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonar.api.measures.Metric;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbSession;
@@ -39,6 +40,7 @@ import static java.util.Arrays.asList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.AssertionsForClassTypes.within;
 import static org.sonar.db.qualitygate.QualityGateFindingDto.NEW_CODE_METRIC_PREFIX;
+import static org.sonar.db.qualitygate.QualityGateFindingDto.RATING_VALUE_TYPE;
 
 public class QualityGateDaoTest {
 
@@ -131,14 +133,14 @@ public class QualityGateDaoTest {
     QualityGateDto gate = db.qualityGates().insertQualityGate();
     db.qualityGates().setDefaultQualityGate(gate);
 
-    MetricDto metric1 = db.measures().insertMetric(m -> m.setDescription("metric 1 description").setDecimalScale(0));
-    QualityGateConditionDto condition1 = db.qualityGates().addCondition(gate, metric1);
+    MetricDto metric1 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.PERCENT.name()).setDescription("metric 1 description").setDecimalScale(0));
+    QualityGateConditionDto condition1 = db.qualityGates().addCondition(gate, metric1, c -> c.setErrorThreshold("13"));
 
-    MetricDto metric2 = db.measures().insertMetric(m -> m.setDescription("metric 2 description").setDecimalScale(1));
-    QualityGateConditionDto condition2 = db.qualityGates().addCondition(gate, metric2);
+    MetricDto metric2 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.RATING.name()).setDescription("metric 2 description").setDecimalScale(1));
+    QualityGateConditionDto condition2 = db.qualityGates().addCondition(gate, metric2, c -> c.setErrorThreshold("1"));
 
-    MetricDto metric3 = db.measures().insertMetric(m -> m.setDescription("metric 3 description").setDecimalScale(0));
-    QualityGateConditionDto condition3 = db.qualityGates().addCondition(gate, metric3);
+    MetricDto metric3 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.INT.name()).setDescription("metric 3 description").setDecimalScale(0));
+    QualityGateConditionDto condition3 = db.qualityGates().addCondition(gate, metric3, c -> c.setErrorThreshold("0"));
 
     db.qualityGates().associateProjectToQualityGate(project, gate);
     db.commit();
@@ -146,21 +148,19 @@ public class QualityGateDaoTest {
     List<QualityGateFindingDto> findings = new ArrayList<>();
     underTest.selectQualityGateFindings(db.getSession(), gate.getUuid(), result -> findings.add(result.getResultObject()));
 
-    QualityGateFindingDto finding = findings.stream().filter(f -> f.getDescription().equals("metric 1 description")).findFirst().get();
-
     // check fields
     assertThat(findings).hasSize(3);
-    assertThat(findings.stream().map(f -> f.getDescription()).collect(Collectors.toSet())).containsExactlyInAnyOrder("metric 1 description", "metric 2 description", "metric 3 description");
-    assertThat(finding.getDescription()).isEqualTo(metric1.getDescription());
-    assertThat(finding.getOperatorDescription()).isEqualTo(QualityGateFindingDto.OperatorDescription.valueOf(condition1.getOperator()).getDescription());
-    assertThat(finding.getErrorThreshold()).isEqualTo(condition1.getErrorThreshold());
-    assertThat(finding.getValueType()).isEqualTo(metric1.getValueType());
-    assertThat(finding.isNewCodeMetric()).isEqualTo(metric1.getKey().startsWith(NEW_CODE_METRIC_PREFIX));
-    assertThat(finding.isEnabled()).isEqualTo(metric1.isEnabled());
-    assertThat(finding.getBestValue()).isEqualTo(metric1.getBestValue(), within(0.00001));
-    assertThat(finding.getWorstValue()).isEqualTo(metric1.getWorstValue(), within(0.00001));
-    assertThat(finding.isOptimizedBestValue()).isEqualTo(metric1.isOptimizedBestValue());
-    assertThat(finding.getDecimalScale()).isEqualTo(metric1.getDecimalScale());
+    assertThat(findings.stream().map(f -> f.getDescription()).collect(Collectors.toSet())).containsExactlyInAnyOrder(metric1.getDescription(), metric2.getDescription(), metric3.getDescription());
+
+    QualityGateFindingDto finding1 = findings.stream().filter(f -> f.getDescription().equals(metric1.getDescription())).findFirst().get();
+    validateQualityGateFindingFields(finding1, metric1, condition1);
+
+    QualityGateFindingDto finding2 = findings.stream().filter(f -> f.getDescription().equals(metric2.getDescription())).findFirst().get();
+    validateQualityGateFindingFields(finding2, metric2, condition2);
+
+    QualityGateFindingDto finding3 = findings.stream().filter(f -> f.getDescription().equals(metric3.getDescription())).findFirst().get();
+    validateQualityGateFindingFields(finding3, metric3, condition3);
+
   }
 
   @Test
@@ -241,4 +241,33 @@ public class QualityGateDaoTest {
     qualityGateDbTester.insertQualityGate(g -> g.setName("Balanced").setBuiltIn(false));
     qualityGateDbTester.insertQualityGate(g -> g.setName("Lenient").setBuiltIn(false));
   }
+
+  private String getOperatorDescription(String operator, String valueType) {
+    if (RATING_VALUE_TYPE.equals(valueType)) {
+      return QualityGateFindingDto.RatingType.valueOf(operator).getDescription();
+    }
+
+    return QualityGateFindingDto.PercentageType.valueOf(operator).getDescription();
+  }
+
+  private String getErrorThreshold(String errorThreshold, String valueType) {
+    if (RATING_VALUE_TYPE.equals(valueType)) {
+      return QualityGateFindingDto.RatingValue.valueOf(Integer.parseInt(errorThreshold));
+    }
+
+    return errorThreshold;
+  }
+
+  private void validateQualityGateFindingFields(QualityGateFindingDto finding, MetricDto metric, QualityGateConditionDto condition) {
+    assertThat(finding.getDescription()).isEqualTo(metric.getDescription());
+    assertThat(finding.getOperatorDescription()).isEqualTo( getOperatorDescription(condition.getOperator(), metric.getValueType()));
+    assertThat(finding.getErrorThreshold()).isEqualTo(getErrorThreshold(condition.getErrorThreshold(), metric.getValueType()));
+    assertThat(finding.getValueType()).isEqualTo(metric.getValueType());
+    assertThat(finding.isNewCodeMetric()).isEqualTo(metric.getKey().startsWith(NEW_CODE_METRIC_PREFIX));
+    assertThat(finding.isEnabled()).isEqualTo(metric.isEnabled());
+    assertThat(finding.getBestValue()).isEqualTo(metric.getBestValue(), within(0.00001));
+    assertThat(finding.getWorstValue()).isEqualTo(metric.getWorstValue(), within(0.00001));
+    assertThat(finding.isOptimizedBestValue()).isEqualTo(metric.isOptimizedBestValue());
+    assertThat(finding.getDecimalScale()).isEqualTo(metric.getDecimalScale());
+  }
 }