diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2018-12-11 13:29:15 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-01-08 20:21:06 +0100 |
commit | 1547d5cc2b446b6d0816bb5607500db9332bd746 (patch) | |
tree | 01e47ded240e5f04fda60a8e8d571c8c6503e92b /server/sonar-ce-task-projectanalysis | |
parent | 0ffc6374e98d787c7a7d72d6947e06c07419af5d (diff) | |
download | sonarqube-1547d5cc2b446b6d0816bb5607500db9332bd746.tar.gz sonarqube-1547d5cc2b446b6d0816bb5607500db9332bd746.zip |
SONAR-11570 Drop period from web services
Diffstat (limited to 'server/sonar-ce-task-projectanalysis')
21 files changed, 139 insertions, 234 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImpl.java index cc4149638d4..3871e10e93c 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImpl.java @@ -53,7 +53,7 @@ class ConditionImpl implements QualityGate.Condition { this.operator = builder.operator; this.errorThreshold = builder.errorThreshold; this.warningThreshold = builder.warningThreshold; - this.onLeakPeriod = builder.onLeakPeriod; + this.onLeakPeriod = builder.metricKey.startsWith("new_"); this.value = builder.value; } @@ -82,7 +82,6 @@ class ConditionImpl implements QualityGate.Condition { private String errorThreshold; @CheckForNull private String warningThreshold; - private boolean onLeakPeriod; @CheckForNull private String value; private QualityGate.EvaluationStatus status; @@ -111,8 +110,11 @@ class ConditionImpl implements QualityGate.Condition { return this; } + /** + * @deprecated in 7.6. This method has no longer any effect. + */ + @Deprecated public Builder setOnLeakPeriod(boolean onLeakPeriod) { - this.onLeakPeriod = onLeakPeriod; return this; } @@ -156,6 +158,10 @@ class ConditionImpl implements QualityGate.Condition { return warningThreshold; } + /** + * @deprecated in 7.6. Conditions "on leak period" were removed. Use "New X" conditions instead. + */ + @Deprecated @Override public boolean isOnLeakPeriod() { return onLeakPeriod; @@ -176,7 +182,6 @@ class ConditionImpl implements QualityGate.Condition { ", operator=" + operator + ", errorThreshold='" + errorThreshold + '\'' + ", warningThreshold='" + warningThreshold + '\'' + - ", onLeakPeriod=" + onLeakPeriod + ", value='" + value + '\'' + '}'; } 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 2c1e0f868fa..acc0297c63f 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 @@ -25,8 +25,6 @@ import javax.annotation.Nonnull; import org.sonar.api.ce.posttask.QualityGate; import org.sonar.ce.task.projectanalysis.qualitygate.Condition; import org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus; -import org.sonar.ce.task.projectanalysis.qualitygate.Condition; -import org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus; import static com.google.common.base.Preconditions.checkState; import static java.lang.String.format; @@ -55,7 +53,6 @@ class ConditionToCondition implements Function<Condition, QualityGate.Condition> .setOperator(convert(input.getOperator())) .setErrorThreshold(input.getErrorThreshold()) .setWarningThreshold(input.getWarningThreshold()) - .setOnLeakPeriod(input.hasPeriod()) .setValue(conditionStatus.getValue()) .build(); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsData.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsData.java index 6ed81d9da65..8ed3865cff4 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsData.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsData.java @@ -25,8 +25,6 @@ import java.util.List; import javax.annotation.concurrent.Immutable; import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.qualitygate.Condition; -import org.sonar.ce.task.projectanalysis.measure.Measure; -import org.sonar.ce.task.projectanalysis.qualitygate.Condition; import static com.google.common.collect.FluentIterable.from; import static java.util.Objects.requireNonNull; @@ -64,7 +62,9 @@ public class QualityGateDetailsData { JsonObject result = new JsonObject(); result.addProperty("metric", condition.getMetric().getKey()); result.addProperty("op", condition.getOperator().getDbValue()); - if (condition.hasPeriod()) { + if (condition.useVariation()) { + // without this for new_ metrics, the UI will show "-" instead of + // the actual value in the QG failure reason result.addProperty("period", 1); } if (condition.getWarningThreshold() != null) { 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 0c3abaaef9a..8ee946eb63b 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 @@ -24,7 +24,6 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.sonar.ce.task.projectanalysis.metric.Metric; -import org.sonar.ce.task.projectanalysis.metric.Metric; import static java.util.Objects.hash; import static java.util.Objects.requireNonNull; @@ -52,14 +51,13 @@ public class Condition { private final String warningThreshold; @CheckForNull private final String errorThreshold; - private final boolean hasPeriod; + private final boolean useVariation; public Condition(Metric metric, String operator, - @Nullable String errorThreshold, @Nullable String warningThreshold, - boolean hasPeriod) { + @Nullable String errorThreshold, @Nullable String warningThreshold) { this.metric = requireNonNull(metric); this.operator = parseFromDbValue(requireNonNull(operator)); - this.hasPeriod = hasPeriod; + this.useVariation = metric.getKey().startsWith("new_"); this.errorThreshold = errorThreshold; this.warningThreshold = warningThreshold; } @@ -77,8 +75,8 @@ public class Condition { return metric; } - public boolean hasPeriod() { - return hasPeriod; + public boolean useVariation() { + return useVariation; } public Operator getOperator() { @@ -104,20 +102,18 @@ public class Condition { return false; } Condition that = (Condition) o; - return java.util.Objects.equals(metric, that.metric) - && java.util.Objects.equals(hasPeriod, that.hasPeriod); + return java.util.Objects.equals(metric, that.metric); } @Override public int hashCode() { - return hash(metric, hasPeriod); + return hash(metric); } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("metric", metric) - .add("hasPeriod", hasPeriod) .add("operator", operator) .add("warningThreshold", warningThreshold) .add("errorThreshold", errorThreshold) 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 ffe567563bb..99633200d9f 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 @@ -24,7 +24,6 @@ import javax.annotation.CheckForNull; import org.apache.commons.lang.StringUtils; import org.sonar.ce.task.projectanalysis.metric.Metric; import org.sonar.ce.task.projectanalysis.measure.Measure; -import org.sonar.ce.task.projectanalysis.metric.Metric; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Optional.of; @@ -116,7 +115,7 @@ public final class ConditionEvaluator { @CheckForNull private static Comparable parseMeasure(Condition condition, Measure measure) { - if (condition.hasPeriod()) { + if (condition.useVariation()) { return parseMeasureFromVariation(condition, measure); } switch (measure.getValueType()) { @@ -142,29 +141,26 @@ public final class ConditionEvaluator { @CheckForNull private static Comparable parseMeasureFromVariation(Condition condition, Measure measure) { - Optional<Double> periodValue = getPeriodValue(measure); - if (periodValue.isPresent()) { - switch (condition.getMetric().getType().getValueType()) { - case BOOLEAN: - return periodValue.get().intValue() == 1; - case INT: - return periodValue.get().intValue(); - case LONG: - return periodValue.get().longValue(); - case DOUBLE: - return periodValue.get(); - case NO_VALUE: - case STRING: - case LEVEL: - default: - throw new IllegalArgumentException("Period conditions are not supported for metric type " + condition.getMetric().getType()); - } + if (!measure.hasVariation()) { + return null; } - return null; - } - private static Optional<Double> getPeriodValue(Measure measure) { - return measure.hasVariation() ? Optional.of(measure.getVariation()) : Optional.empty(); + 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: + return variation.longValue(); + 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 9e10cf2de5e..35adc5529a4 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 @@ -24,23 +24,15 @@ import java.util.Locale; import java.util.Map; import javax.annotation.CheckForNull; import org.sonar.api.i18n.I18n; -import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Duration; import org.sonar.api.utils.Durations; -import org.sonar.ce.task.projectanalysis.metric.Metric; -import org.sonar.ce.task.projectanalysis.period.Period; -import org.sonar.core.timemachine.Periods; import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.metric.Metric; -import org.sonar.ce.task.projectanalysis.period.Period; -import org.sonar.ce.task.projectanalysis.period.PeriodHolder; import static java.util.Objects.requireNonNull; import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR; public final class EvaluationResultTextConverterImpl implements EvaluationResultTextConverter { - private static final String VARIATION_METRIC_PREFIX = "new_"; - private static final String VARIATION = "variation"; private static final Map<Condition.Operator, String> OPERATOR_LABELS = ImmutableMap.of( Condition.Operator.EQUALS, "=", Condition.Operator.NOT_EQUALS, "!=", @@ -49,14 +41,10 @@ public final class EvaluationResultTextConverterImpl implements EvaluationResult private final I18n i18n; private final Durations durations; - private final Periods periods; - private final PeriodHolder periodHolder; - public EvaluationResultTextConverterImpl(I18n i18n, Durations durations, Periods periods, PeriodHolder periodHolder) { + public EvaluationResultTextConverterImpl(I18n i18n, Durations durations) { this.i18n = i18n; this.durations = durations; - this.periods = periods; - this.periodHolder = periodHolder; } @Override @@ -70,26 +58,15 @@ public final class EvaluationResultTextConverterImpl implements EvaluationResult } private String getAlertLabel(Condition condition, Measure.Level level) { - boolean hasPeriod = condition.hasPeriod(); String metric = i18n.message(Locale.ENGLISH, "metric." + condition.getMetric().getKey() + ".name", condition.getMetric().getName()); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(metric); - if (hasPeriod && !condition.getMetric().getKey().startsWith(VARIATION_METRIC_PREFIX)) { - String variation = i18n.message(Locale.ENGLISH, VARIATION, VARIATION).toLowerCase(Locale.ENGLISH); - stringBuilder.append(" ").append(variation); - } - stringBuilder .append(" ").append(OPERATOR_LABELS.get(condition.getOperator())).append(" ") .append(alertValue(condition, level)); - if (hasPeriod) { - Period period = periodHolder.getPeriod(); - stringBuilder.append(" ").append(periods.label(period.getMode(), period.getModeParameter(), DateUtils.longToDate(period.getSnapshotDate()))); - } - return stringBuilder.toString(); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java index 2667c4b5165..a2f75161f4d 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java @@ -28,7 +28,6 @@ import org.sonar.db.DbSession; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.ce.task.projectanalysis.analysis.Organization; -import org.sonar.ce.task.projectanalysis.metric.MetricRepository; import org.sonar.server.qualitygate.ShortLivingBranchQualityGate; import static org.sonar.core.util.stream.MoreCollectors.toList; @@ -73,7 +72,7 @@ public class QualityGateServiceImpl implements QualityGateService { Iterable<Condition> conditions = dtos.stream() .map(input -> metricRepository.getOptionalById(input.getMetricId()) - .map(metric -> new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold(), input.getPeriod() != null)) + .map(metric -> new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold())) .orElse(null)) .filter(Objects::nonNull) .collect(toList(dtos.size())); @@ -86,7 +85,7 @@ public class QualityGateServiceImpl implements QualityGateService { ShortLivingBranchQualityGate.ID, ShortLivingBranchQualityGate.NAME, ShortLivingBranchQualityGate.CONDITIONS.stream() - .map(c -> new Condition(metricRepository.getByKey(c.getMetricKey()), c.getOperator(), c.getErrorThreshold(), c.getWarnThreshold(), c.isOnLeak())) + .map(c -> new Condition(metricRepository.getByKey(c.getMetricKey()), c.getOperator(), c.getErrorThreshold(), c.getWarnThreshold())) .collect(toList(ShortLivingBranchQualityGate.CONDITIONS.size()))); } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java index f1f917fdc19..2c7925176a9 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/QualityGateMeasuresStep.java @@ -22,7 +22,6 @@ package org.sonar.ce.task.projectanalysis.step; import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Multimap; -import com.google.common.collect.Ordering; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -74,9 +73,6 @@ import static org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus.crea * It must be executed after the computation of differential measures {@link ComputeMeasureVariationsStep} */ public class QualityGateMeasuresStep implements ComputationStep { - // Condition on period should come first - private static final Ordering<Condition> PERIOD_ORDERING = Ordering.natural().reverse().onResultOf(Condition::hasPeriod); - private final TreeRootHolder treeRootHolder; private final QualityGateHolder qualityGateHolder; private final MutableQualityGateStatusHolder qualityGateStatusHolder; @@ -206,7 +202,7 @@ public class QualityGateMeasuresStep implements ComputationStep { private static MetricEvaluationResult evaluateQualityGate(Measure measure, Collection<Condition> conditions) { ConditionEvaluator conditionEvaluator = new ConditionEvaluator(); MetricEvaluationResult metricEvaluationResult = null; - for (Condition newCondition : PERIOD_ORDERING.immutableSortedCopy(conditions)) { + for (Condition newCondition : conditions) { EvaluationResult newEvaluationResult = conditionEvaluator.evaluate(newCondition, measure); if (metricEvaluationResult == null || newEvaluationResult.getLevel().ordinal() > metricEvaluationResult.evaluationResult.getLevel().ordinal()) { metricEvaluationResult = new MetricEvaluationResult(newEvaluationResult, newCondition); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTask.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTask.java index 3585abaf782..91f426782ed 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTask.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/webhook/WebhookPostTask.java @@ -65,7 +65,7 @@ public class WebhookPostTask implements PostProjectAnalysisTask { Set<Condition> conditions = qg.getConditions().stream() .map(q -> { Condition condition = new Condition(q.getMetricKey(), Condition.Operator.valueOf(q.getOperator().name()), - q.getErrorThreshold(), q.getWarningThreshold(), q.isOnLeakPeriod()); + q.getErrorThreshold(), q.getWarningThreshold()); builder.addCondition(condition, EvaluatedCondition.EvaluationStatus.valueOf(q.getStatus().name()), q.getStatus() == org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE ? null : q.getValue()); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImplTest.java index b686e06dc9c..190a16e3215 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/api/posttask/ConditionImplTest.java @@ -46,7 +46,6 @@ public class ConditionImplTest { .setOperator(QualityGate.Operator.GREATER_THAN) .setErrorThreshold(ERROR_THRESHOLD) .setWarningThreshold(WARN_THRESHOLD) - .setOnLeakPeriod(true) .setValue(VALUE); @Test @@ -140,7 +139,7 @@ public class ConditionImplTest { public void toString_ConditionImpl_of_type_different_from_NO_VALUE() { assertThat(builder.build().toString()) .isEqualTo( - "ConditionImpl{status=OK, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', onLeakPeriod=true, value='value'}"); + "ConditionImpl{status=OK, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', value='value'}"); } @Test @@ -150,7 +149,7 @@ public class ConditionImplTest { assertThat(builder.build().toString()) .isEqualTo( - "ConditionImpl{status=NO_VALUE, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', onLeakPeriod=true, value='null'}"); + "ConditionImpl{status=NO_VALUE, metricKey='metricKey', operator=GREATER_THAN, errorThreshold='error threshold', warningThreshold='warn threshold', value='null'}"); } @Test @@ -162,7 +161,6 @@ public class ConditionImplTest { assertThat(underTest.getOperator()).isEqualTo(QualityGate.Operator.GREATER_THAN); assertThat(underTest.getErrorThreshold()).isEqualTo(ERROR_THRESHOLD); assertThat(underTest.getWarningThreshold()).isEqualTo(WARN_THRESHOLD); - assertThat(underTest.isOnLeakPeriod()).isEqualTo(true); assertThat(underTest.getValue()).isEqualTo(VALUE); } } 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 26284de239c..b78da1eb668 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 @@ -46,7 +46,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, WARN_THRESHOLD, true); + private static final Condition SOME_CONDITION = new Condition(newMetric(METRIC_KEY), Condition.Operator.EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD); @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -98,26 +98,15 @@ public class ConditionToConditionTest { @Test @UseDataProvider("allOperatorValues") public void apply_converts_all_values_of_operator(Condition.Operator operator) { - Condition condition = new Condition(newMetric(METRIC_KEY), operator.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD, true); + Condition condition = new Condition(newMetric(METRIC_KEY), operator.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD); ConditionToCondition underTest = new ConditionToCondition(of(condition, SOME_CONDITION_STATUS)); assertThat(underTest.apply(condition).getOperator().name()).isEqualTo(operator.name()); } @Test - public void apply_sets_onLeakPeriod_flag_when_Condition_has_non_null_Period() { - Condition noPeriodCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD, false); - ConditionToCondition underTest = new ConditionToCondition(of( - SOME_CONDITION, SOME_CONDITION_STATUS, - noPeriodCondition, SOME_CONDITION_STATUS)); - - assertThat(underTest.apply(SOME_CONDITION).isOnLeakPeriod()).isTrue(); - assertThat(underTest.apply(noPeriodCondition).isOnLeakPeriod()).isFalse(); - } - - @Test public void apply_copies_value() { - Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_THRESHOLD, false); + Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.NOT_EQUALS.getDbValue(), ERROR_THRESHOLD, WARN_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 5ee2b694d31..737791816d2 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", "warn threshold", false); + return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), "error threshold", "warn 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 7644bb47a3f..2532c59cff2 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 @@ -26,10 +26,15 @@ import org.sonar.ce.task.projectanalysis.qualitygate.Condition; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class EvaluatedConditionTest { - private static final Condition SOME_CONDITION = new Condition(mock(Metric.class), Condition.Operator.EQUALS.getDbValue(), "1", null, false); + private static final Metric SOME_METRIC = mock(Metric.class); + static { + when(SOME_METRIC.getKey()).thenReturn("dummy key"); + } + private static final Condition SOME_CONDITION = new Condition(SOME_METRIC, Condition.Operator.EQUALS.getDbValue(), "1", null); 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/measure/qualitygatedetails/QualityGateDetailsDataTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsDataTest.java index ee5fc4c7f1a..86c431bdace 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsDataTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/qualitygatedetails/QualityGateDetailsDataTest.java @@ -52,7 +52,7 @@ public class QualityGateDetailsDataTest { @Test public void verify_json_for_each_type_of_condition() { String value = "actualValue"; - Condition condition = new Condition(new MetricImpl(1, "key1", "name1", Metric.MetricType.STRING), Condition.Operator.GREATER_THAN.getDbValue(), "errorTh", "warnTh", true); + Condition condition = new Condition(new MetricImpl(1, "key1", "name1", Metric.MetricType.STRING), Condition.Operator.GREATER_THAN.getDbValue(), "errorTh", "warnTh"); ImmutableList<EvaluatedCondition> evaluatedConditions = ImmutableList.of( new EvaluatedCondition(condition, Measure.Level.OK, value), new EvaluatedCondition(condition, Measure.Level.WARN, value), @@ -65,7 +65,6 @@ public class QualityGateDetailsDataTest { " {" + " \"metric\":\"key1\"," + " \"op\":\"GT\"," + - " \"period\":1," + " \"warning\":\"warnTh\"," + " \"error\":\"errorTh\"," + " \"actual\":\"actualValue\"," + @@ -74,7 +73,6 @@ public class QualityGateDetailsDataTest { " {" + " \"metric\":\"key1\"," + " \"op\":\"GT\"," + - " \"period\":1," + " \"warning\":\"warnTh\"," + " \"error\":\"errorTh\"," + " \"actual\":\"actualValue\"," + @@ -83,7 +81,6 @@ public class QualityGateDetailsDataTest { " {" + " \"metric\":\"key1\"," + " \"op\":\"GT\"," + - " \"period\":1," + " \"warning\":\"warnTh\"," + " \"error\":\"errorTh\"," + " \"actual\":\"actualValue\"," + 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 87b9c93007e..b08cc2f14ed 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 @@ -19,11 +19,14 @@ */ package org.sonar.ce.task.projectanalysis.qualitygate; -import com.google.common.collect.ImmutableList; 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 org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.metric.Metric; import org.sonar.ce.task.projectanalysis.metric.MetricImpl; @@ -53,6 +56,7 @@ import static org.sonar.ce.task.projectanalysis.qualitygate.Condition.Operator.L 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) public class ConditionEvaluatorTest { @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -241,8 +245,8 @@ public class ConditionEvaluatorTest { 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(new Condition(metric, EQUALS.getDbValue(), "10.3", "10.2", false), measure)).hasLevel(Measure.Level.WARN); - assertThat(underTest.evaluate(new Condition(metric, LESS_THAN.getDbValue(), "10.3", "10.2", false), measure)).hasLevel(Measure.Level.ERROR); + assertThat(underTest.evaluate(new Condition(metric, EQUALS.getDbValue(), "10.3", "10.2"), measure)).hasLevel(Measure.Level.WARN); + assertThat(underTest.evaluate(new Condition(metric, LESS_THAN.getDbValue(), "10.3", "10.2"), measure)).hasLevel(Measure.Level.ERROR); } @Test @@ -267,54 +271,71 @@ public class ConditionEvaluatorTest { } @Test - public void test_condition_on_period() { - for (MetricType metricType : ImmutableList.of(FLOAT, INT, WORK_DUR)) { - Metric metric = createMetric(metricType); - Measure measure = newMeasureBuilder().setVariation(3d).createNoValue(); + @UseDataProvider("numericNewMetricTypes") + public void test_condition_on_numeric_new_metric(MetricType metricType) { + Metric metric = createNewMetric(metricType); + Measure measure = newMeasureBuilder().setVariation(3d).createNoValue(); - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "3", null, true), measure)).hasLevel(OK); - } + assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "3", null), measure)).hasLevel(OK); + assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2", null), measure)).hasLevel(ERROR); } @Test - public void condition_on_period_without_value_is_OK() { - Metric metric = createMetric(FLOAT); + @UseDataProvider("numericNewMetricTypes") + public void condition_on_new_metric_without_value_is_OK(MetricType metricType) { + Metric metric = createNewMetric(metricType); Measure measure = newMeasureBuilder().createNoValue(); - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "3", null, true), measure)).hasLevel(OK).hasValue(null); + assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "3", null), measure)).hasLevel(OK).hasValue(null); } - @Test - public void test_condition_on_rating() { - Metric metric = createMetric(RATING); - Measure measure = newMeasureBuilder().create(4, "D"); - - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "4", null, false), measure)).hasLevel(OK).hasValue(4); - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2", null, false), measure)).hasLevel(ERROR).hasValue(4); + @DataProvider + public static Object[][] numericNewMetricTypes() { + return new Object[][] { + {FLOAT}, + {INT}, + {WORK_DUR}, + }; } @Test - public void test_condition_on_rating_on_leak_period() { - Metric metric = createMetric(RATING); - Measure measure = newMeasureBuilder().setVariation(4d).createNoValue(); + @UseDataProvider("unsupportedNewMetricTypes") + public void condition_on_new_metric_with_unsupported_type(MetricType metricType) { + Metric metric = createNewMetric(metricType); + Measure measure = newMeasureBuilder().setVariation(0d).createNoValue(); - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "5", null, true), measure)).hasLevel(OK).hasValue(4); - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2", null, true), measure)).hasLevel(ERROR).hasValue(4); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Unsupported metric type " + metricType); + + underTest.evaluate(new Condition(metric, EQUALS.getDbValue(), "3", null), measure); + } + + @DataProvider + public static Object[][] unsupportedNewMetricTypes() { + return new Object[][] { + {STRING}, + {LEVEL}, + }; } @Test - public void test_condition_on_rating_on_leak_period_when_variation_is_zero() { + public void test_condition_on_rating() { Metric metric = createMetric(RATING); - Measure measure = newMeasureBuilder().setVariation(0d).createNoValue(); + Measure measure = newMeasureBuilder().create(4, "D"); - assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "4", null, true), measure)).hasLevel(OK).hasValue(0); + assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "4", null), measure)).hasLevel(OK).hasValue(4); + assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2", null), measure)).hasLevel(ERROR).hasValue(4); } private static Condition createErrorCondition(Metric metric, Condition.Operator operator, String errorThreshold) { - return new Condition(metric, operator.getDbValue(), errorThreshold, null, false); + return new Condition(metric, operator.getDbValue(), errorThreshold, null); } private static MetricImpl createMetric(MetricType metricType) { return new MetricImpl(1, "key", "name", metricType); } + + private static MetricImpl createNewMetric(MetricType metricType) { + return new MetricImpl(1, "new_key", "name", metricType); + } } 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 dde2f6ae3c4..e10977223cb 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 @@ -19,6 +19,7 @@ */ package org.sonar.ce.task.projectanalysis.qualitygate; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -36,14 +37,19 @@ public class ConditionTest { private static final Metric SOME_METRIC = mock(Metric.class); private static final String SOME_OPERATOR = "EQ"; + @Before + public void setUp() { + when(SOME_METRIC.getKey()).thenReturn("dummy key"); + } + @Test(expected = NullPointerException.class) public void constructor_throws_NPE_for_null_metric_argument() { - new Condition(null, SOME_OPERATOR, null, null, false); + new Condition(null, SOME_OPERATOR, null, null); } @Test(expected = NullPointerException.class) public void constructor_throws_NPE_for_null_operator_argument() { - new Condition(SOME_METRIC, null, null, null, false); + new Condition(SOME_METRIC, null, null, null); } @Test @@ -51,7 +57,7 @@ public class ConditionTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Unsupported operator value: 'troloto'"); - new Condition(SOME_METRIC, "troloto", null, null, false); + new Condition(SOME_METRIC, "troloto", null, null); } @Test @@ -59,11 +65,10 @@ public class ConditionTest { String error = "error threshold"; String warning = "warning threshold"; - Condition condition = new Condition(SOME_METRIC, SOME_OPERATOR, error, warning, true); + Condition condition = new Condition(SOME_METRIC, SOME_OPERATOR, error, warning); assertThat(condition.getMetric()).isSameAs(SOME_METRIC); assertThat(condition.getOperator()).isSameAs(Condition.Operator.EQUALS); - assertThat(condition.hasPeriod()).isTrue(); assertThat(condition.getErrorThreshold()).isEqualTo(error); assertThat(condition.getWarningThreshold()).isEqualTo(warning); } @@ -72,9 +77,7 @@ public class ConditionTest { public void all_fields_are_displayed_in_toString() { when(SOME_METRIC.toString()).thenReturn("metric1"); - assertThat(new Condition(SOME_METRIC, SOME_OPERATOR, "error_l", "warn", true).toString()) - .isEqualTo("Condition{metric=metric1, hasPeriod=true, operator=EQUALS, warningThreshold=warn, errorThreshold=error_l}"); - + assertThat(new Condition(SOME_METRIC, SOME_OPERATOR, "error_l", "warn").toString()) + .isEqualTo("Condition{metric=metric1, operator=EQUALS, warningThreshold=warn, 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 f144f76689f..a4f62144b9b 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 @@ -24,10 +24,8 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Locale; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.sonar.api.i18n.I18n; @@ -35,9 +33,6 @@ import org.sonar.api.utils.Durations; import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.metric.Metric; import org.sonar.ce.task.projectanalysis.metric.MetricImpl; -import org.sonar.ce.task.projectanalysis.period.Period; -import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule; -import org.sonar.core.timemachine.Periods; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -49,20 +44,16 @@ import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.WARN; 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", null, false); + private static final Condition EQ_10_CONDITION = new Condition(INT_METRIC, Condition.Operator.EQUALS.getDbValue(), "10", null); private static final EvaluationResult OK_EVALUATION_RESULT = new EvaluationResult(Measure.Level.OK, null); private static final String ERROR_THRESHOLD = "error_threshold"; private static final String WARNING_THRESHOLD = "warning_threshold"; private static final String SOME_MODE = "mode"; private static final String SOME_ANALYSIS_UUID = "u1"; - @Rule - public PeriodHolderRule periodsHolder = new PeriodHolderRule(); - private I18n i18n = mock(I18n.class); private Durations durations = mock(Durations.class); - private Periods periods = mock(Periods.class); - private EvaluationResultTextConverter underTest = new EvaluationResultTextConverterImpl(i18n, durations, periods, periodsHolder); + private EvaluationResultTextConverter underTest = new EvaluationResultTextConverterImpl(i18n, durations); @Test(expected = NullPointerException.class) public void evaluate_throws_NPE_if_Condition_arg_is_null() { @@ -98,7 +89,7 @@ public class EvaluationResultTextConverterTest { when(i18n.message(Locale.ENGLISH, "metric." + INT_METRIC.getKey() + ".name", INT_METRIC.getName())) .thenReturn(metricMsg); - Condition condition = new Condition(INT_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD, false); + Condition condition = new Condition(INT_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD); assertThat(underTest.asText(condition, new EvaluationResult(level, null))) .isEqualTo(metricMsg + " " + toSign(operator) + " " + getThreshold(level)); @@ -116,56 +107,12 @@ public class EvaluationResultTextConverterTest { when(i18n.message(Locale.ENGLISH, "metric." + SOME_VARIATION_METRIC.getKey() + ".name", SOME_VARIATION_METRIC.getName())) .thenReturn(metricMsg); - Condition condition = new Condition(SOME_VARIATION_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD, false); + Condition condition = new Condition(SOME_VARIATION_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD); assertThat(underTest.asText(condition, new EvaluationResult(level, null))) .isEqualTo(metricMsg + " " + toSign(operator) + " " + getThreshold(level)); } - @Test - @UseDataProvider("all_operators_for_error_warning_levels") - public void evaluate_adds_only_period_if_metric_starts_with_new_prefix(Condition.Operator operator, Measure.Level level) { - String metricMsg = "trololo_metric_msg"; - int periodIndex = 1; - String periodLabel = "periodLabel"; - - when(i18n.message(Locale.ENGLISH, "metric." + SOME_VARIATION_METRIC.getKey() + ".name", SOME_VARIATION_METRIC.getName())) - .thenReturn(metricMsg); - - Date date = new Date(); - Period period = new Period(SOME_MODE, null, date.getTime(), SOME_ANALYSIS_UUID); - periodsHolder.setPeriod(period); - when(periods.label(period.getMode(), period.getModeParameter(), date)).thenReturn(periodLabel); - - Condition condition = new Condition(SOME_VARIATION_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD, true); - - assertThat(underTest.asText(condition, new EvaluationResult(level, null))) - .isEqualTo(metricMsg + " " + toSign(operator) + " " + (getThreshold(level)) + " " + periodLabel); - } - - @Test - @UseDataProvider("all_operators_for_error_warning_levels") - public void evaluate_adds_variation_and_period_if_metric_does_not_starts_with_variation_prefix(Condition.Operator operator, Measure.Level level) { - String metricMsg = "trololo_metric_msg"; - String variationMsg = "_variation_"; - int periodIndex = 1; - String periodLabel = "periodLabel"; - - when(i18n.message(Locale.ENGLISH, "metric." + INT_METRIC.getKey() + ".name", INT_METRIC.getName())) - .thenReturn(metricMsg); - when(i18n.message(Locale.ENGLISH, "variation", "variation")).thenReturn(variationMsg); - - Date date = new Date(); - Period period = new Period(SOME_MODE, null, date.getTime(), SOME_ANALYSIS_UUID); - periodsHolder.setPeriod(period); - when(periods.label(period.getMode(), period.getModeParameter(), date)).thenReturn(periodLabel); - - Condition condition = new Condition(INT_METRIC, operator.getDbValue(), ERROR_THRESHOLD, WARNING_THRESHOLD, true); - - assertThat(underTest.asText(condition, new EvaluationResult(level, null))) - .isEqualTo(metricMsg + " " + variationMsg + " " + toSign(operator) + " " + (getThreshold(level)) + " " + periodLabel); - } - private static String toSign(Condition.Operator operator) { switch (operator) { case EQUALS: 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 56cdc603305..efe75abe831 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,7 +53,7 @@ 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").setPeriod(1) + private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("EQ") .setWarningThreshold("warnin_th").setErrorThreshold("error_th"); private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("NE"); @@ -67,6 +67,9 @@ public class QualityGateServiceImplTest { public void setUp() throws Exception { when(dbClient.qualityGateDao()).thenReturn(qualityGateDao); when(dbClient.gateConditionDao()).thenReturn(qualityGateConditionDao); + + when(METRIC_1.getKey()).thenReturn("metric"); + when(METRIC_2.getKey()).thenReturn("new_metric"); } @Test @@ -101,8 +104,8 @@ public class QualityGateServiceImplTest { assertThat(res.get().getId()).isEqualTo(SOME_ID); assertThat(res.get().getName()).isEqualTo(SOME_NAME); assertThat(res.get().getConditions()).containsOnly( - new Condition(METRIC_1, CONDITION_1.getOperator(), CONDITION_1.getErrorThreshold(), CONDITION_1.getWarningThreshold(), true), - new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold(), false)); + new Condition(METRIC_1, CONDITION_1.getOperator(), CONDITION_1.getErrorThreshold(), CONDITION_1.getWarningThreshold()), + new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold())); } @Test @@ -119,11 +122,11 @@ public class QualityGateServiceImplTest { assertThat(res.get().getId()).isEqualTo(SOME_ID); assertThat(res.get().getName()).isEqualTo(SOME_NAME); assertThat(res.get().getConditions()).containsOnly( - new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold(), false)); + new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold())); } @Test - public void findById_of_hardcoded_short_living_branch_returns_hardcoded_qp() { + public void findById_of_hardcoded_short_living_branch_returns_hardcoded_qg() { MetricImpl bugsMetric = mockMetricInRepository(CoreMetrics.BUGS_KEY); MetricImpl vulnerabilitiesMetric = mockMetricInRepository(CoreMetrics.VULNERABILITIES_KEY); MetricImpl codeSmellsMetric = mockMetricInRepository(CoreMetrics.CODE_SMELLS_KEY); @@ -137,10 +140,10 @@ public class QualityGateServiceImplTest { assertThat(qualityGate.getId()).isEqualTo(ShortLivingBranchQualityGate.ID); assertThat(qualityGate.getName()).isEqualTo("Hardcoded short living branch quality gate"); assertThat(qualityGate.getConditions()) - .extracting(Condition::getMetric, Condition::getOperator, Condition::getErrorThreshold, Condition::getWarningThreshold, Condition::hasPeriod) + .extracting(Condition::getMetric, Condition::getOperator, Condition::getErrorThreshold, Condition::getWarningThreshold) .containsOnly( - tuple(openedIssueMetric, GREATER_THAN, "0", null, false), - tuple(reOpenedIssueMetric, GREATER_THAN, "0", null, false)); + tuple(openedIssueMetric, GREATER_THAN, "0", null), + tuple(reOpenedIssueMetric, GREATER_THAN, "0", null)); } private MetricImpl mockMetricInRepository(String metricKey) { 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 f658fb58dd9..8b39421392e 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 @@ -278,7 +278,7 @@ public class QualityGateMeasuresStepTest { 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", null); - Condition periodCondition = createEqualsCondition(INT_METRIC_1, null, "2", true); + Condition periodCondition = createEqualsCondition(INT_METRIC_1, null, "2"); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(fixedCondition, periodCondition))); Measure measure = newMeasureBuilder().create(rawValue, null); @@ -293,30 +293,10 @@ public class QualityGateMeasuresStepTest { } @Test - public void new_measure_has_WARN_level_of_all_conditions_for_a_specific_metric_if_its_the_worst() { - int rawValue = 2; - Condition fixedCondition = createEqualsCondition(INT_METRIC_1, "1", null); - Condition periodCondition = createEqualsCondition(INT_METRIC_1, null, "2", true); - - qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(fixedCondition, periodCondition))); - Measure measure = newMeasureBuilder() - .setVariation(rawValue) - .create(rawValue, null); - measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, measure); - - underTest.execute(new TestComputationStepContext()); - - Optional<Measure> rawMeasure1 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_1_KEY); - assertThat(rawMeasure1.get()) - .hasQualityGateLevel(WARN) - .hasQualityGateText(dumbResultTextAnswer(periodCondition, WARN, rawValue)); - } - - @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", null); - Condition periodCondition = createEqualsCondition(INT_METRIC_1, "1", null, true); + Condition periodCondition = createEqualsCondition(INT_METRIC_1, "1", null); qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(fixedCondition, periodCondition))); Measure measure = newMeasureBuilder() @@ -341,11 +321,7 @@ public class QualityGateMeasuresStepTest { } private static Condition createEqualsCondition(Metric metric, @Nullable String errorThreshold, @Nullable String warningThreshold) { - return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), errorThreshold, warningThreshold, false); - } - - private static Condition createEqualsCondition(Metric metric, @Nullable String errorThreshold, @Nullable String warningThreshold, boolean hasPeriod) { - return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), errorThreshold, warningThreshold, hasPeriod); + return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), errorThreshold, warningThreshold); } private static MetricImpl createIntMetric(int index) { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/SmallChangesetQualityGateSpecialCaseTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/SmallChangesetQualityGateSpecialCaseTest.java index 855bf62122e..9a491a421dc 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/SmallChangesetQualityGateSpecialCaseTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/SmallChangesetQualityGateSpecialCaseTest.java @@ -149,7 +149,7 @@ public class SmallChangesetQualityGateSpecialCaseTest { private QualityGateMeasuresStep.MetricEvaluationResult generateEvaluationResult(String metric, Measure.Level level) { Metric newCoverageMetric = metricRepository.getByKey(metric); - Condition condition = new Condition(newCoverageMetric, "LT", "80", "90", true); + Condition condition = new Condition(newCoverageMetric, "LT", "80", "90"); EvaluationResult evaluationResult = new EvaluationResult(level, mock(Comparable.class)); return new QualityGateMeasuresStep.MetricEvaluationResult(evaluationResult, condition); } 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 3209afc0fe3..6c0d726222d 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 @@ -148,8 +148,8 @@ public class WebhookPostTaskTest { condition.getMetricKey(), Condition.Operator.valueOf(condition.getOperator().name()), condition.getErrorThreshold(), - condition.getWarningThreshold(), - condition.isOnLeakPeriod()); + condition.getWarningThreshold() + ); webQualityGate = EvaluatedQualityGate.newBuilder() .setQualityGate(new org.sonar.server.qualitygate.QualityGate(qualityGate.getId(), qualityGate.getName(), Collections.singleton(qgCondition))) .setStatus(Metric.Level.valueOf(qualityGate.getStatus().name())) |