diff options
author | BenoƮt Gianinetti <benoit.gianinetti@sonarsource.com> | 2018-12-14 14:53:58 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-01-08 20:21:07 +0100 |
commit | 0c8a948cbd786a35f87ed962e8fff86a316c4153 (patch) | |
tree | c05347a0567148e738487ce4d97c056f37627e36 /server/sonar-ce-task-projectanalysis | |
parent | b87a76fa468a726a61c1c635b4334387398aaa06 (diff) | |
download | sonarqube-0c8a948cbd786a35f87ed962e8fff86a316c4153.tar.gz sonarqube-0c8a948cbd786a35f87ed962e8fff86a316c4153.zip |
SONAR-11572 Limit list of operators for QG conditions
Diffstat (limited to 'server/sonar-ce-task-projectanalysis')
13 files changed, 80 insertions, 213 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToCondition.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToCondition.java index 2226440dbcc..769089445b1 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToCondition.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToCondition.java @@ -73,10 +73,6 @@ class ConditionToCondition implements Function<Condition, QualityGate.Condition> private static QualityGate.Operator convert(Condition.Operator operator) { switch (operator) { - case EQUALS: - return QualityGate.Operator.EQUALS; - case NOT_EQUALS: - return QualityGate.Operator.NOT_EQUALS; case GREATER_THAN: return QualityGate.Operator.GREATER_THAN; case LESS_THAN: diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/Condition.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/Condition.java index 9aa090afc39..707450bfd62 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/Condition.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/Condition.java @@ -31,7 +31,7 @@ import static java.util.Objects.requireNonNull; public class Condition { public enum Operator { - EQUALS("EQ"), NOT_EQUALS("NE"), GREATER_THAN("GT"), LESS_THAN("LT"); + GREATER_THAN("GT"), LESS_THAN("LT"); private final String dbValue; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluator.java index 2f5b44d83b8..c713e0abd06 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluator.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluator.java @@ -19,6 +19,7 @@ */ package org.sonar.ce.task.projectanalysis.qualitygate; +import java.util.EnumSet; import java.util.Optional; import javax.annotation.CheckForNull; import org.sonar.ce.task.projectanalysis.measure.Measure; @@ -26,14 +27,23 @@ import org.sonar.ce.task.projectanalysis.metric.Metric; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Optional.of; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.FLOAT; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.INT; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.LEVEL; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.MILLISEC; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.PERCENT; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.RATING; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.WORK_DUR; public final class ConditionEvaluator { + private static final EnumSet<Metric.MetricType> SUPPORTED_METRIC_TYPE = EnumSet.of(INT, MILLISEC, RATING, WORK_DUR, FLOAT, PERCENT, LEVEL); + /** * Evaluates the condition for the specified measure */ public EvaluationResult evaluate(Condition condition, Measure measure) { - checkArgument(condition.getMetric().getType() != Metric.MetricType.DATA, "Conditions on MetricType DATA are not supported"); + checkArgument(SUPPORTED_METRIC_TYPE.contains(condition.getMetric().getType()), "Conditions on MetricType %s are not supported", condition.getMetric().getType()); Comparable measureComparable = parseMeasure(condition, measure); if (measureComparable == null) { @@ -61,10 +71,6 @@ public final class ConditionEvaluator { private static boolean doesReachThresholds(Comparable measureValue, Comparable criteriaValue, Condition condition) { int comparison = measureValue.compareTo(criteriaValue); switch (condition.getOperator()) { - case EQUALS: - return comparison == 0; - case NOT_EQUALS: - return comparison != 0; case GREATER_THAN: return comparison > 0; case LESS_THAN: @@ -76,15 +82,12 @@ public final class ConditionEvaluator { private static Comparable parseConditionValue(Metric metric, String value) { switch (metric.getType().getValueType()) { - case BOOLEAN: - return Integer.parseInt(value) == 1; case INT: return parseInteger(value); case LONG: return Long.parseLong(value); case DOUBLE: return Double.parseDouble(value); - case STRING: case LEVEL: return value; default: @@ -102,16 +105,12 @@ public final class ConditionEvaluator { return parseMeasureFromVariation(condition, measure); } switch (measure.getValueType()) { - case BOOLEAN: - return measure.getBooleanValue(); case INT: return measure.getIntValue(); case LONG: return measure.getLongValue(); case DOUBLE: return measure.getDoubleValue(); - case STRING: - return measure.getStringValue(); case LEVEL: return measure.getLevelValue().name(); case NO_VALUE: @@ -131,8 +130,6 @@ public final class ConditionEvaluator { Double variation = measure.getVariation(); Metric.MetricType metricType = condition.getMetric().getType(); switch (metricType.getValueType()) { - case BOOLEAN: - return variation.intValue() == 1; case INT: return variation.intValue(); case LONG: @@ -140,7 +137,6 @@ public final class ConditionEvaluator { case DOUBLE: return variation; case NO_VALUE: - case STRING: case LEVEL: default: throw new IllegalArgumentException("Unsupported metric type " + metricType); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterImpl.java index 4bb8911286a..32d4cfbab5b 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterImpl.java @@ -33,8 +33,6 @@ import static java.util.Objects.requireNonNull; public final class EvaluationResultTextConverterImpl implements EvaluationResultTextConverter { private static final Map<Condition.Operator, String> OPERATOR_LABELS = ImmutableMap.of( - Condition.Operator.EQUALS, "=", - Condition.Operator.NOT_EQUALS, "!=", Condition.Operator.GREATER_THAN, ">", Condition.Operator.LESS_THAN, "<"); @@ -59,14 +57,9 @@ public final class EvaluationResultTextConverterImpl implements EvaluationResult private String getAlertLabel(Condition condition) { String metric = i18n.message(Locale.ENGLISH, "metric." + condition.getMetric().getKey() + ".name", condition.getMetric().getName()); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(metric); - - stringBuilder - .append(" ").append(OPERATOR_LABELS.get(condition.getOperator())).append(" ") - .append(alertValue(condition)); - - return stringBuilder.toString(); + return metric + + " " + OPERATOR_LABELS.get(condition.getOperator()) + " " + + alertValue(condition); } private String alertValue(Condition condition) { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToConditionTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToConditionTest.java index 3fd169ee0ca..e721ae850f2 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToConditionTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionToConditionTest.java @@ -45,7 +45,7 @@ public class ConditionToConditionTest { private static final Map<Condition, ConditionStatus> NO_STATUS_PER_CONDITIONS = Collections.emptyMap(); private static final String SOME_VALUE = "some value"; private static final ConditionStatus SOME_CONDITION_STATUS = ConditionStatus.create(ConditionStatus.EvaluationStatus.OK, SOME_VALUE); - private static final Condition SOME_CONDITION = new Condition(newMetric(METRIC_KEY), Condition.Operator.EQUALS.getDbValue(), ERROR_THRESHOLD); + private static final Condition SOME_CONDITION = new Condition(newMetric(METRIC_KEY), Condition.Operator.LESS_THAN.getDbValue(), ERROR_THRESHOLD); @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -103,7 +103,7 @@ public class ConditionToConditionTest { @Test public void apply_copies_value() { - Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD); + Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.LESS_THAN.getDbValue(), ERROR_THRESHOLD); ConditionToCondition underTest = new ConditionToCondition(of( SOME_CONDITION, SOME_CONDITION_STATUS, otherCondition, ConditionStatus.NO_VALUE_STATUS)); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java index 6a460d59425..2f578d0701b 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java @@ -391,7 +391,7 @@ public class PostProjectAnalysisTasksExecutorTest { private static Condition createCondition(String metricKey) { Metric metric = mock(Metric.class); when(metric.getKey()).thenReturn(metricKey); - return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), "error threshold"); + return new Condition(metric, Condition.Operator.LESS_THAN.getDbValue(), "error threshold"); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/EvaluatedConditionTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/EvaluatedConditionTest.java index 8e85b43bb05..9b333c33515 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/EvaluatedConditionTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/EvaluatedConditionTest.java @@ -34,7 +34,7 @@ public class EvaluatedConditionTest { static { when(SOME_METRIC.getKey()).thenReturn("dummy key"); } - private static final Condition SOME_CONDITION = new Condition(SOME_METRIC, Condition.Operator.EQUALS.getDbValue(), "1"); + private static final Condition SOME_CONDITION = new Condition(SOME_METRIC, Condition.Operator.LESS_THAN.getDbValue(), "1"); private static final Measure.Level SOME_LEVEL = Measure.Level.OK; private static final String SOME_VALUE = "some value"; diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluatorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluatorTest.java index d8d2f9de383..476ed506cb0 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluatorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionEvaluatorTest.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.EnumSet; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -42,18 +43,18 @@ import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.OK; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.BOOL; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.DATA; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.DISTRIB; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.FLOAT; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.INT; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.LEVEL; +import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.MILLISEC; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.PERCENT; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.RATING; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.STRING; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.WORK_DUR; import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.values; -import static org.sonar.ce.task.projectanalysis.qualitygate.Condition.Operator.EQUALS; import static org.sonar.ce.task.projectanalysis.qualitygate.Condition.Operator.GREATER_THAN; import static org.sonar.ce.task.projectanalysis.qualitygate.Condition.Operator.LESS_THAN; -import static org.sonar.ce.task.projectanalysis.qualitygate.Condition.Operator.NOT_EQUALS; import static org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResultAssert.assertThat; @RunWith(DataProviderRunner.class) @@ -68,7 +69,7 @@ public class ConditionEvaluatorTest { try { Metric metric = createMetric(FLOAT); Measure measure = newMeasureBuilder().create(10.2d, 1, null); - underTest.evaluate(createErrorCondition(metric, LESS_THAN, "20"), measure); + underTest.evaluate(createCondition(metric, LESS_THAN, "20"), measure); } catch (NumberFormatException ex) { fail(); } @@ -76,7 +77,7 @@ public class ConditionEvaluatorTest { try { Metric metric = createMetric(INT); Measure measure = newMeasureBuilder().create(5, null); - underTest.evaluate(createErrorCondition(metric, LESS_THAN, "20.1"), measure); + underTest.evaluate(createCondition(metric, LESS_THAN, "20.1"), measure); } catch (NumberFormatException ex) { fail(); } @@ -84,56 +85,20 @@ public class ConditionEvaluatorTest { try { Metric metric = createMetric(PERCENT); Measure measure = newMeasureBuilder().create(10.2d, 1, null); - underTest.evaluate(createErrorCondition(metric, LESS_THAN, "20.1"), measure); + underTest.evaluate(createCondition(metric, LESS_THAN, "20.1"), measure); } catch (NumberFormatException ex) { fail(); } } @Test - public void testEquals_for_double() { - Metric metric = createMetric(FLOAT); - Measure measure = newMeasureBuilder().create(10.2d, 1, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.2"), measure)).hasLevel(ERROR).hasValue(10.2d); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.1"), measure)).hasLevel(OK).hasValue(10.2d); - } - - @Test - public void testEquals_for_String() { - Metric metric = createMetric(STRING); - Measure measure = newMeasureBuilder().create("TEST"); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "TEST"), measure)).hasLevel(ERROR).hasValue("TEST"); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "TEST2"), measure)).hasLevel(OK).hasValue("TEST"); - } - - @Test - public void testNotEquals_for_double() { - Metric metric = createMetric(FLOAT); - Measure measure = newMeasureBuilder().create(10.2d, 1, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, "10.2"), measure)).hasLevel(OK).hasValue(10.2d); - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, "10.1"), measure)).hasLevel(ERROR).hasValue(10.2d); - } - - @Test - public void testNotEquals() { - Metric metric = createMetric(STRING); - Measure measure = newMeasureBuilder().create("TEST"); - - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, "TEST"), measure)).hasLevel(OK).hasValue("TEST"); - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, "TEST2"), measure)).hasLevel(ERROR).hasValue("TEST"); - } - - @Test public void testGreater() { Metric metric = createMetric(FLOAT); Measure measure = newMeasureBuilder().create(10.2d, 1, null); - assertThat(underTest.evaluate(createErrorCondition(metric, GREATER_THAN, "10.1"), measure)).hasLevel(ERROR).hasValue(10.2d); - assertThat(underTest.evaluate(createErrorCondition(metric, GREATER_THAN, "10.2"), measure)).hasLevel(OK).hasValue(10.2d); - assertThat(underTest.evaluate(createErrorCondition(metric, GREATER_THAN, "10.3"), measure)).hasLevel(OK).hasValue(10.2d); + assertThat(underTest.evaluate(createCondition(metric, GREATER_THAN, "10.1"), measure)).hasLevel(ERROR).hasValue(10.2d); + assertThat(underTest.evaluate(createCondition(metric, GREATER_THAN, "10.2"), measure)).hasLevel(OK).hasValue(10.2d); + assertThat(underTest.evaluate(createCondition(metric, GREATER_THAN, "10.3"), measure)).hasLevel(OK).hasValue(10.2d); } @Test @@ -141,89 +106,9 @@ public class ConditionEvaluatorTest { Metric metric = createMetric(FLOAT); Measure measure = newMeasureBuilder().create(10.2d, 1, null); - assertThat(underTest.evaluate(createErrorCondition(metric, LESS_THAN, "10.1"), measure)).hasLevel(OK).hasValue(10.2d); - assertThat(underTest.evaluate(createErrorCondition(metric, LESS_THAN, "10.2"), measure)).hasLevel(OK).hasValue(10.2d); - assertThat(underTest.evaluate(createErrorCondition(metric, LESS_THAN, "10.3"), measure)).hasLevel(ERROR).hasValue(10.2d); - } - - @Test - public void testEquals_Percent() { - Metric metric = createMetric(PERCENT); - Measure measure = newMeasureBuilder().create(10.2d, 1, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.2"), measure)).hasLevel(ERROR).hasValue(10.2d); - } - - @Test - public void testEquals_Float() { - Metric metric = createMetric(PERCENT); - Measure measure = newMeasureBuilder().create(10.2d, 1, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.2"), measure)).hasLevel(ERROR).hasValue(10.2d); - } - - @Test - public void testEquals_Int() { - Metric metric = createMetric(INT); - Measure measure = newMeasureBuilder().create(10, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10"), measure)).hasLevel(ERROR).hasValue(10); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.2"), measure)).hasLevel(ERROR).hasValue(10); - } - - @Test - public void testEquals_Level() { - Metric metric = createMetric(LEVEL); - Measure measure = newMeasureBuilder().create(ERROR); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, ERROR.name()), measure)).hasLevel(ERROR).hasValue(ERROR.name()); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, OK.name()), measure)).hasLevel(OK).hasValue(ERROR.name()); - } - - @Test - public void testNotEquals_Level() { - Metric metric = createMetric(LEVEL); - Measure measure = newMeasureBuilder().create(ERROR); - - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, OK.name()), measure)).hasLevel(ERROR).hasValue(ERROR.name()); - } - - @Test - public void testEquals_BOOL() { - Metric metric = createMetric(BOOL); - Measure measure = newMeasureBuilder().create(false, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "1"), measure)).hasLevel(OK).hasValue(false); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "0"), measure)).hasLevel(ERROR).hasValue(false); - } - - @Test - public void testNotEquals_BOOL() { - Metric metric = createMetric(BOOL); - Measure measure = newMeasureBuilder().create(false, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, "1"), measure)).hasLevel(ERROR).hasValue(false); - assertThat(underTest.evaluate(createErrorCondition(metric, NOT_EQUALS, "0"), measure)).hasLevel(OK).hasValue(false); - } - - @Test - public void getLevel_throws_IEA_if_error_threshold_is_not_parsable_boolean() { - Metric metric = createMetric(BOOL); - Measure measure = newMeasureBuilder().create(false, null); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Quality Gate: Unable to parse value 'polop' to compare against name"); - - underTest.evaluate(createErrorCondition(metric, EQUALS, "polop"), measure); - } - - @Test - public void testEquals_work_duration() { - Metric metric = createMetric(WORK_DUR); - Measure measure = newMeasureBuilder().create(60l, null); - - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "60"), measure)).hasLevel(ERROR); + assertThat(underTest.evaluate(createCondition(metric, LESS_THAN, "10.1"), measure)).hasLevel(OK).hasValue(10.2d); + assertThat(underTest.evaluate(createCondition(metric, LESS_THAN, "10.2"), measure)).hasLevel(OK).hasValue(10.2d); + assertThat(underTest.evaluate(createCondition(metric, LESS_THAN, "10.3"), measure)).hasLevel(ERROR).hasValue(10.2d); } @Test @@ -234,7 +119,7 @@ public class ConditionEvaluatorTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Quality Gate: Unable to parse value 'polop' to compare against name"); - underTest.evaluate(createErrorCondition(metric, EQUALS, "polop"), measure); + underTest.evaluate(createCondition(metric, LESS_THAN, "polop"), measure); } @Test @@ -242,32 +127,42 @@ public class ConditionEvaluatorTest { Metric metric = createMetric(FLOAT); Measure measure = newMeasureBuilder().create(10.2d, 1, null); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.2"), measure)).hasLevel(ERROR); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.1"), measure)).hasLevel(OK); + assertThat(underTest.evaluate(createCondition(metric, LESS_THAN, "10.3"), measure)).hasLevel(ERROR); + assertThat(underTest.evaluate(createCondition(metric, LESS_THAN, "10.1"), measure)).hasLevel(OK); - assertThat(underTest.evaluate(new Condition(metric, EQUALS.getDbValue(), "10.3"), measure)).hasLevel(Measure.Level.OK); assertThat(underTest.evaluate(new Condition(metric, LESS_THAN.getDbValue(), "10.3"), measure)).hasLevel(Measure.Level.ERROR); } @Test public void condition_is_always_ok_when_measure_is_noValue() { - for (MetricType metricType : from(asList(values())).filter(not(in(ImmutableSet.of(DATA, LEVEL))))) { + for (MetricType metricType : from(asList(values())).filter(not(in(ImmutableSet.of(BOOL, DATA, DISTRIB, STRING))))) { Metric metric = createMetric(metricType); Measure measure = newMeasureBuilder().createNoValue(); - assertThat(underTest.evaluate(createErrorCondition(metric, EQUALS, "10.2"), measure)).hasLevel(OK); + assertThat(underTest.evaluate(createCondition(metric, LESS_THAN, "10.2"), measure)).hasLevel(OK); } } @Test - public void testUnsupportedType() { - Metric metric = createMetric(DATA); + @UseDataProvider("unsupportedMetricTypes") + public void fail_when_metric_is_not_supported(MetricType metricType) { + Metric metric = createMetric(metricType); Measure measure = newMeasureBuilder().create("3.14159265358"); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Conditions on MetricType DATA are not supported"); + expectedException.expectMessage(String.format("Conditions on MetricType %s are not supported", metricType)); - underTest.evaluate(createErrorCondition(metric, EQUALS, "1.60217657"), measure); + underTest.evaluate(createCondition(metric, LESS_THAN, "1.60217657"), measure); + } + + @DataProvider + public static Object[][] unsupportedMetricTypes() { + return new Object[][] { + {BOOL}, + {STRING}, + {DATA}, + {DISTRIB} + }; } @Test @@ -299,23 +194,14 @@ public class ConditionEvaluatorTest { } @Test - @UseDataProvider("unsupportedNewMetricTypes") - public void condition_on_new_metric_with_unsupported_type(MetricType metricType) { - Metric metric = createNewMetric(metricType); + public void fail_when_condition_on_leak_period_is_using_unsupported_metric() { + Metric metric = createNewMetric(LEVEL); Measure measure = newMeasureBuilder().setVariation(0d).createNoValue(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Unsupported metric type " + metricType); - - underTest.evaluate(new Condition(metric, EQUALS.getDbValue(), "3"), measure); - } + expectedException.expectMessage("Unsupported metric type LEVEL"); - @DataProvider - public static Object[][] unsupportedNewMetricTypes() { - return new Object[][] { - {STRING}, - {LEVEL}, - }; + underTest.evaluate(new Condition(metric, LESS_THAN.getDbValue(), "3"), measure); } @Test @@ -327,7 +213,7 @@ public class ConditionEvaluatorTest { assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2"), measure)).hasLevel(ERROR).hasValue(4); } - private static Condition createErrorCondition(Metric metric, Condition.Operator operator, String errorThreshold) { + private static Condition createCondition(Metric metric, Condition.Operator operator, String errorThreshold) { return new Condition(metric, operator.getDbValue(), errorThreshold); } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionTest.java index e6acb186e2e..3e763512b14 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/ConditionTest.java @@ -35,7 +35,7 @@ public class ConditionTest { public ExpectedException expectedException = ExpectedException.none(); private static final Metric SOME_METRIC = mock(Metric.class); - private static final String SOME_OPERATOR = "EQ"; + private static final String SOME_OPERATOR = "LT"; @Before public void setUp() { @@ -67,7 +67,7 @@ public class ConditionTest { Condition condition = new Condition(SOME_METRIC, SOME_OPERATOR, error); assertThat(condition.getMetric()).isSameAs(SOME_METRIC); - assertThat(condition.getOperator()).isSameAs(Condition.Operator.EQUALS); + assertThat(condition.getOperator()).isSameAs(Condition.Operator.LESS_THAN); assertThat(condition.getErrorThreshold()).isEqualTo(error); } @@ -76,7 +76,8 @@ public class ConditionTest { when(SOME_METRIC.toString()).thenReturn("metric1"); assertThat(new Condition(SOME_METRIC, SOME_OPERATOR, "error_l").toString()) - .isEqualTo("Condition{metric=metric1, operator=EQUALS, errorThreshold=error_l}"); + .isEqualTo("Condition{metric=metric1, operator=LESS_THAN, errorThreshold=error_l}"); } + } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterTest.java index 5a9213c5255..fe5e3819f9c 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/EvaluationResultTextConverterTest.java @@ -42,7 +42,7 @@ import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR; public class EvaluationResultTextConverterTest { private static final Metric INT_METRIC = new MetricImpl(1, "key", "int_metric_name", Metric.MetricType.INT); private static final Metric SOME_VARIATION_METRIC = new MetricImpl(2, "new_variation_of_trololo", "variation_of_trololo_name", Metric.MetricType.INT); - private static final Condition EQ_10_CONDITION = new Condition(INT_METRIC, Condition.Operator.EQUALS.getDbValue(), "10"); + private static final Condition LT_10_CONDITION = new Condition(INT_METRIC, Condition.Operator.LESS_THAN.getDbValue(), "10"); private static final EvaluationResult OK_EVALUATION_RESULT = new EvaluationResult(Measure.Level.OK, null); private static final String ERROR_THRESHOLD = "error_threshold"; @@ -57,12 +57,12 @@ public class EvaluationResultTextConverterTest { @Test(expected = NullPointerException.class) public void evaluate_throws_NPE_if_EvaluationResult_arg_is_null() { - underTest.asText(EQ_10_CONDITION, null); + underTest.asText(LT_10_CONDITION, null); } @Test public void evaluate_returns_null_if_EvaluationResult_has_level_OK() { - assertThat(underTest.asText(EQ_10_CONDITION, OK_EVALUATION_RESULT)).isNull(); + assertThat(underTest.asText(LT_10_CONDITION, OK_EVALUATION_RESULT)).isNull(); } @DataProvider @@ -104,10 +104,6 @@ public class EvaluationResultTextConverterTest { private static String toSign(Condition.Operator operator) { switch (operator) { - case EQUALS: - return "="; - case NOT_EQUALS: - return "!="; case GREATER_THAN: return ">"; case LESS_THAN: diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java index efcf5054b30..934bf7f99bc 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java @@ -53,9 +53,9 @@ public class QualityGateServiceImplTest { private static final long METRIC_ID_2 = 753; private static final Metric METRIC_1 = mock(Metric.class); private static final Metric METRIC_2 = mock(Metric.class); - private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("EQ") + private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("LT") .setErrorThreshold("error_th"); - private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("NE").setErrorThreshold("error_th"); + private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("GT").setErrorThreshold("error_th"); private QualityGateDao qualityGateDao = mock(QualityGateDao.class); private QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStepTest.java index eddec0e7051..ee87d157d35 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStepTest.java @@ -23,7 +23,6 @@ import java.util.Optional; import java.util.Collections; import java.util.Map; import java.util.Objects; -import java.util.Optional; import javax.annotation.Nullable; import org.assertj.core.api.AbstractAssert; import org.junit.Before; @@ -150,7 +149,7 @@ public class QualityGateMeasuresStepTest { @Test public void new_measures_are_created_even_if_there_is_no_rawMeasure_for_metric_of_condition() { - Condition equals2Condition = createEqualsCondition(INT_METRIC_1, "2"); + Condition equals2Condition = createLessThanCondition(INT_METRIC_1, "2"); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(equals2Condition))); underTest.execute(new TestComputationStepContext()); @@ -172,8 +171,8 @@ public class QualityGateMeasuresStepTest { @Test public void rawMeasure_is_updated_if_present_and_new_measures_are_created_if_project_has_measure_for_metric_of_condition() { - int rawValue = 1; - Condition equals2Condition = createEqualsCondition(INT_METRIC_1, "2"); + int rawValue = 3; + Condition equals2Condition = createLessThanCondition(INT_METRIC_1, "2"); Measure rawMeasure = newMeasureBuilder().create(rawValue, null); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(equals2Condition))); @@ -200,9 +199,9 @@ public class QualityGateMeasuresStepTest { @Test public void new_measures_have_ERROR_level_if_at_least_one_updated_measure_has_ERROR_level() { - int rawValue = 1; - Condition equalsOneErrorCondition = createEqualsCondition(INT_METRIC_1, "1"); - Condition equalsOneOkCondition = createEqualsCondition(INT_METRIC_2, "2"); + int rawValue = 3; + Condition equalsOneErrorCondition = createLessThanCondition(INT_METRIC_1, "4"); + Condition equalsOneOkCondition = createLessThanCondition(INT_METRIC_2, "2"); Measure rawMeasure = newMeasureBuilder().create(rawValue, null); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(equalsOneErrorCondition, equalsOneOkCondition))); @@ -238,9 +237,9 @@ public class QualityGateMeasuresStepTest { @Test public void new_measure_has_ERROR_level_of_all_conditions_for_a_specific_metric_if_its_the_worst() { - int rawValue = 1; - Condition fixedCondition = createEqualsCondition(INT_METRIC_1, "1"); - Condition periodCondition = createEqualsCondition(INT_METRIC_1, "2"); + int rawValue = 3; + Condition fixedCondition = createLessThanCondition(INT_METRIC_1, "4"); + Condition periodCondition = createLessThanCondition(INT_METRIC_1, "2"); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(fixedCondition, periodCondition))); Measure measure = newMeasureBuilder().create(rawValue, null); @@ -256,9 +255,9 @@ public class QualityGateMeasuresStepTest { @Test public void new_measure_has_condition_on_leak_period_when_all_conditions_on_specific_metric_has_same_QG_level() { - int rawValue = 1; - Condition fixedCondition = createEqualsCondition(INT_METRIC_1, "1"); - Condition periodCondition = createEqualsCondition(INT_METRIC_1, "1"); + int rawValue = 0; + Condition fixedCondition = createLessThanCondition(INT_METRIC_1, "1"); + Condition periodCondition = createLessThanCondition(INT_METRIC_1, "1"); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(fixedCondition, periodCondition))); Measure measure = newMeasureBuilder() @@ -282,8 +281,8 @@ public class QualityGateMeasuresStepTest { return measureRepository.getAddedRawMeasure(PROJECT_REF, CoreMetrics.QUALITY_GATE_DETAILS_KEY); } - private static Condition createEqualsCondition(Metric metric, String errorThreshold) { - return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), errorThreshold); + private static Condition createLessThanCondition(Metric metric, String errorThreshold) { + return new Condition(metric, Condition.Operator.LESS_THAN.getDbValue(), errorThreshold); } private static MetricImpl createIntMetric(int index) { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTaskTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTaskTest.java index e967a56a6f2..e47d7a97533 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTaskTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTaskTest.java @@ -85,7 +85,7 @@ public class WebhookPostTaskTest { public void call_webhooks_with_analysis_and_qualitygate() { QualityGate.Condition condition = newConditionBuilder() .setMetricKey(randomAlphanumeric(96)) - .setOperator(QualityGate.Operator.values()[random.nextInt(QualityGate.Operator.values().length)]) + .setOperator(QualityGate.Operator.LESS_THAN) .setErrorThreshold(randomAlphanumeric(22)) .setOnLeakPeriod(random.nextBoolean()) .build(QualityGate.EvaluationStatus.OK, randomAlphanumeric(33)); |