*/
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;
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"),
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 {
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);
// 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
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());
}
}