]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16316 Improvements on quality gate findings CSV file
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>
Wed, 1 Jun 2022 08:45:30 +0000 (10:45 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 1 Jun 2022 20:03:02 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateFindingDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java

index 20829ac2df49aaa5fb5f7ca498b4131f37737699..2272cd8cc8c41bd1ea0cb08f1630a0278f628d06 100644 (file)
  */
 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;
   private String operator = null;
-  private String kee = null;
-  private Boolean isEnabled = null;
   private String valueType = null;
-  private Double bestValue = null;
-  private Double worstValue = null;
-  private Boolean optimizedBestValue = null;
   private String errorThreshold = null;
-  private Integer decimalScale = null;
 
   public String getDescription() {
     return description;
@@ -49,53 +40,34 @@ public class QualityGateFindingDto {
     return PercentageType.valueOf(getOperator()).getDescription();
   }
 
-  public Boolean isNewCodeMetric() {
-    return kee.startsWith(NEW_CODE_METRIC_PREFIX);
-  }
-
-  public boolean isEnabled() {
-    return isEnabled;
-  }
-
-  public String getValueType() {
-    return valueType;
-  }
-
-  @CheckForNull
-  public Double getBestValue() {
-    return bestValue;
-  }
-
-  @CheckForNull
-  public Double getWorstValue() {
-    return worstValue;
-  }
-
   public String getErrorThreshold() {
     if (isRating(getValueType())) {
       return RatingValue.valueOf(Integer.parseInt(errorThreshold));
     }
 
-    return errorThreshold;
-  }
-
-  public boolean isOptimizedBestValue() {
-    return optimizedBestValue;
-  }
+    if (isPercentage(getValueType())) {
+      return errorThreshold + "%";
+    }
 
-  @CheckForNull
-  public Integer getDecimalScale() {
-    return decimalScale;
+    return errorThreshold;
   }
 
   private String getOperator() {
     return operator;
   }
 
+  private String getValueType() {
+    return valueType;
+  }
+
   private static boolean isRating(String metricType) {
     return RATING_VALUE_TYPE.equals(metricType);
   }
 
+  private static boolean isPercentage(String metricType) {
+    return PERCENT_VALUE_TYPE.equals(metricType);
+  }
+
   public enum RatingType {
     LT("Is Better Than"),
     GT("Is Worse Than"),
index 2488f9adcfbaff72839947fcaa977d08db398c17..0771e2eaddf238325c3dedb85a28e66493cf0dc5 100644 (file)
@@ -7,16 +7,10 @@
   </sql>
 
   <sql id="qualityGateFindingColumns">
-    m.description as description,
+    m.short_name as description,
     qgc.operator as operator,
-    m.name as kee,
-    m.enabled as isEnabled,
     m.val_type as valueType,
-    m.best_value as bestValue,
-    m.worst_value as worstValue,
-    m.optimized_best_value as optimizedBestValue,
-    qgc.value_error as errorThreshold,
-    m.decimal_scale as decimalScale
+    qgc.value_error as errorThreshold
   </sql>
 
   <insert id="insertQualityGate" parameterType="QualityGate" useGeneratedKeys="false">
index a5d9a085c1c30b453ac4d3937ce87ca9e8d5072d..24e8c1530aedfcaca43936d85d6344a9ff2605a7 100644 (file)
@@ -38,8 +38,7 @@ import org.sonar.db.project.ProjectDto;
 
 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.PERCENT_VALUE_TYPE;
 import static org.sonar.db.qualitygate.QualityGateFindingDto.RATING_VALUE_TYPE;
 
 public class QualityGateDaoTest {
@@ -133,13 +132,13 @@ public class QualityGateDaoTest {
     QualityGateDto gate = db.qualityGates().insertQualityGate();
     db.qualityGates().setDefaultQualityGate(gate);
 
-    MetricDto metric1 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.PERCENT.name()).setDescription("metric 1 description").setDecimalScale(0));
+    MetricDto metric1 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.PERCENT.name()).setShortName("metric 1"));
     QualityGateConditionDto condition1 = db.qualityGates().addCondition(gate, metric1, c -> c.setErrorThreshold("13"));
 
-    MetricDto metric2 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.RATING.name()).setDescription("metric 2 description").setDecimalScale(1));
+    MetricDto metric2 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.RATING.name()).setShortName("metric 2"));
     QualityGateConditionDto condition2 = db.qualityGates().addCondition(gate, metric2, c -> c.setErrorThreshold("1"));
 
-    MetricDto metric3 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.INT.name()).setDescription("metric 3 description").setDecimalScale(0));
+    MetricDto metric3 = db.measures().insertMetric(m -> m.setValueType(Metric.ValueType.INT.name()).setShortName("metric 3"));
     QualityGateConditionDto condition3 = db.qualityGates().addCondition(gate, metric3, c -> c.setErrorThreshold("0"));
 
     db.qualityGates().associateProjectToQualityGate(project, gate);
@@ -150,17 +149,16 @@ public class QualityGateDaoTest {
 
     // check fields
     assertThat(findings).hasSize(3);
-    assertThat(findings.stream().map(f -> f.getDescription()).collect(Collectors.toSet())).containsExactlyInAnyOrder(metric1.getDescription(), metric2.getDescription(), metric3.getDescription());
+    assertThat(findings.stream().map(QualityGateFindingDto::getDescription).collect(Collectors.toSet())).containsExactlyInAnyOrder(metric1.getShortName(), metric2.getShortName(), metric3.getShortName());
 
-    QualityGateFindingDto finding1 = findings.stream().filter(f -> f.getDescription().equals(metric1.getDescription())).findFirst().get();
+    QualityGateFindingDto finding1 = findings.stream().filter(f -> f.getDescription().equals(metric1.getShortName())).findFirst().get();
     validateQualityGateFindingFields(finding1, metric1, condition1);
 
-    QualityGateFindingDto finding2 = findings.stream().filter(f -> f.getDescription().equals(metric2.getDescription())).findFirst().get();
+    QualityGateFindingDto finding2 = findings.stream().filter(f -> f.getDescription().equals(metric2.getShortName())).findFirst().get();
     validateQualityGateFindingFields(finding2, metric2, condition2);
 
-    QualityGateFindingDto finding3 = findings.stream().filter(f -> f.getDescription().equals(metric3.getDescription())).findFirst().get();
+    QualityGateFindingDto finding3 = findings.stream().filter(f -> f.getDescription().equals(metric3.getShortName())).findFirst().get();
     validateQualityGateFindingFields(finding3, metric3, condition3);
-
   }
 
   @Test
@@ -255,19 +253,16 @@ public class QualityGateDaoTest {
       return QualityGateFindingDto.RatingValue.valueOf(Integer.parseInt(errorThreshold));
     }
 
+    if (PERCENT_VALUE_TYPE.equals(valueType)) {
+      return 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.getDescription()).isEqualTo(metric.getShortName());
+    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());
   }
 }