From 77af82fd520c9579a083c458ba68d55c5177d96a Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 26 Jan 2016 11:07:20 +0100 Subject: [PATCH] SONAR-7070 WS qualitygates/* leak period enforced when creating/updating a quality gate condition --- .../sonar/server/qualitygate/QualityGates.java | 17 ++++++++++++----- .../sonar/server/qualitygate/ws/AppAction.java | 6 +----- .../sonar/server/qualitygate/ws/QGatesWs.java | 4 ++-- .../server/qualitygate/QualityGatesTest.java | 4 ++-- .../server/qualitygate/ws/AppActionTest.java | 2 +- .../server/qualitygate/ws/QGatesWsTest.java | 3 +-- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java index 91d69f7a494..8f1c65dbc50 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java @@ -179,7 +179,10 @@ public class QualityGates { validateCondition(metric, operator, warningThreshold, errorThreshold, period); QualityGateConditionDto newCondition = new QualityGateConditionDto().setQualityGateId(qGateId) .setMetricId(metric.getId()).setMetricKey(metric.getKey()) - .setOperator(operator).setWarningThreshold(warningThreshold).setErrorThreshold(errorThreshold).setPeriod(period); + .setOperator(operator) + .setWarningThreshold(warningThreshold) + .setErrorThreshold(errorThreshold) + .setPeriod(period); conditionDao.insert(newCondition); return newCondition; } @@ -190,8 +193,13 @@ public class QualityGates { QualityGateConditionDto condition = getNonNullCondition(condId); Metric metric = getNonNullMetric(metricKey); validateCondition(metric, operator, warningThreshold, errorThreshold, period); - condition.setMetricId(metric.getId()).setMetricKey(metric.getKey()) - .setOperator(operator).setWarningThreshold(warningThreshold).setErrorThreshold(errorThreshold).setPeriod(period); + condition + .setMetricId(metric.getId()) + .setMetricKey(metric.getKey()) + .setOperator(operator) + .setWarningThreshold(warningThreshold) + .setErrorThreshold(errorThreshold) + .setPeriod(period); conditionDao.update(condition); return condition; } @@ -269,9 +277,8 @@ public class QualityGates { private void checkPeriod(Metric metric, @Nullable Integer period, Errors errors) { if (period == null) { errors.check(!metric.getKey().startsWith("new_"), "A period must be selected for differential metrics."); - } else { - errors.check(period >= 1 && period <= 5, "Valid periods are integers between 1 and 5 (included)."); + errors.check(period == 1, "The only valid quality gate period is 1, the leak period."); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java index cedc36c18ae..50123dd2e02 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java @@ -20,7 +20,6 @@ package org.sonar.server.qualitygate.ws; import org.apache.commons.lang.BooleanUtils; -import org.sonar.api.i18n.I18n; import org.sonar.api.measures.Metric; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -37,12 +36,9 @@ public class AppAction implements QGateWsAction { private final Periods periods; - private final I18n i18n; - - public AppAction(QualityGates qualityGates, Periods periods, I18n i18n) { + public AppAction(QualityGates qualityGates, Periods periods) { this.qualityGates = qualityGates; this.periods = periods; - this.i18n = i18n; } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesWs.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesWs.java index 61d12a7709f..1e1802bc386 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesWs.java @@ -79,8 +79,8 @@ public class QGatesWs implements WebService { .setPossibleValues(QualityGateConditionDto.ALL_OPERATORS); action.createParam(PARAM_PERIOD) - .setDescription("Condition period") - .setExampleValue("1"); + .setDescription("Condition period. If not set, the absolute value is considered.") + .setPossibleValues("1"); action.createParam(PARAM_WARNING) .setDescription("Condition warning threshold") diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java index 889e9dda4a4..fb610571c13 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java @@ -347,7 +347,7 @@ public class QualityGatesTest { Metric newCoverage = Mockito.spy(CoreMetrics.NEW_COVERAGE); when(newCoverage.getId()).thenReturn(metricId); when(metricFinder.findByKey(metricKey)).thenReturn(newCoverage); - int period = 2; + int period = 1; QualityGateConditionDto newCondition = qGates.createCondition(qGateId, metricKey, operator, null, errorThreshold, period); assertThat(newCondition.getQualityGateId()).isEqualTo(qGateId); @@ -461,7 +461,7 @@ public class QualityGatesTest { Metric newCoverage = Mockito.spy(CoreMetrics.NEW_COVERAGE); when(newCoverage.getId()).thenReturn(metricId); when(metricFinder.findByKey(metricKey)).thenReturn(newCoverage); - int period = 2; + int period = 1; assertThat(qGates.updateCondition(condId, metricKey, operator, null, errorThreshold, period)).isEqualTo(condition); assertThat(condition.getId()).isEqualTo(condId); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java index 584f5efe471..b02c7e783e6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java @@ -67,7 +67,7 @@ public class AppActionTest { new CreateAction(qGates), new CopyAction(qGates), new DestroyAction(qGates), new RenameAction(qGates), new SetAsDefaultAction(qGates), new UnsetDefaultAction(qGates), new CreateConditionAction(qGates), new UpdateConditionAction(qGates), new DeleteConditionAction(qGates), - new SelectAction(qGates), new DeselectAction(qGates), new AppAction(qGates, periods, i18n))); + new SelectAction(qGates), new DeselectAction(qGates), new AppAction(qGates, periods))); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QGatesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QGatesWsTest.java index 03598405b45..448b09e7281 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QGatesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QGatesWsTest.java @@ -27,7 +27,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.api.i18n.I18n; import org.sonar.api.server.ws.WebService.Action; import org.sonar.api.server.ws.WebService.Controller; import org.sonar.db.qualitygate.ProjectQgateAssociation; @@ -71,7 +70,7 @@ public class QGatesWsTest { new CreateAction(qGates), new CopyAction(qGates), new DestroyAction(qGates), new RenameAction(qGates), new SetAsDefaultAction(qGates), new UnsetDefaultAction(qGates), new CreateConditionAction(qGates), new UpdateConditionAction(qGates), new DeleteConditionAction(qGates), - new SelectAction(qGates), new DeselectAction(qGates), new AppAction(qGates, mock(Periods.class), mock(I18n.class)))); + new SelectAction(qGates), new DeselectAction(qGates), new AppAction(qGates, mock(Periods.class)))); } @Test -- 2.39.5