From: Julien Lancelot Date: Fri, 24 Nov 2017 15:16:23 +0000 (+0100) Subject: SONAR-10088 Improve isolation of CreateConditionActionTest X-Git-Tag: 7.0-RC1~217 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0bcf71b6c53b7bb260360458f52d549a3b2cb374;p=sonarqube.git SONAR-10088 Improve isolation of CreateConditionActionTest --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java index f37e8c72f60..0d94f7bbda3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java @@ -20,21 +20,17 @@ package org.sonar.server.qualitygate.ws; import java.util.ArrayList; -import java.util.List; import javax.annotation.Nullable; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.metric.MetricDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualitygate.QualityGateConditionDto; -import org.sonar.db.qualitygate.QualityGateDbTester; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.organization.TestDefaultOrganizationProvider; @@ -45,9 +41,9 @@ import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Qualitygates.CreateConditionResponse; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.db.metric.MetricTesting.newMetricDto; +import static org.assertj.core.api.Assertions.tuple; +import static org.sonar.api.measures.Metric.ValueType.INT; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; -import static org.sonar.server.computation.task.projectanalysis.metric.Metric.MetricType.PERCENT; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC; @@ -64,64 +60,75 @@ public class CreateConditionActionTest { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public DbTester db = DbTester.create(System2.INSTANCE); + public DbTester db = DbTester.create(); private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private DbClient dbClient = db.getDbClient(); private DbSession dbSession = db.getSession(); - private QualityGateDbTester qualityGateDbTester = new QualityGateDbTester(db); private CreateConditionAction underTest = new CreateConditionAction(userSession, dbClient, new QualityGateConditionsUpdater(dbClient), defaultOrganizationProvider); - private QualityGateDto qualityGateDto; - private MetricDto coverageMetricDto = newMetricDto() - .setKey("coverage") - .setShortName("Coverage") - .setValueType(PERCENT.name()) - .setHidden(false); private WsActionTester ws = new WsActionTester(underTest); - @Before - public void setUp() throws Exception { - qualityGateDto = qualityGateDbTester.insertQualityGate(); - dbClient.metricDao().insert(dbSession, coverageMetricDto); - dbSession.commit(); - } - @Test public void create_warning_condition() throws Exception { logInAsQualityGateAdmin(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + MetricDto metric = insertMetric(); - CreateConditionResponse response = executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", "90", null, null); + executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", null, null); - assertCondition(response, "LT", "90", null, null); + assertCondition(qualityGate, metric, "LT", "90", null, null); } @Test public void create_error_condition() throws Exception { logInAsQualityGateAdmin(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + MetricDto metric = insertMetric(); - CreateConditionResponse response = executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", null, "90", null); + executeRequest(qualityGate.getId(), metric.getKey(), "LT", null, "90", null); - assertCondition(response, "LT", null, "90", null); + assertCondition(qualityGate, metric, "LT", null, "90", null); } @Test public void create_condition_over_leak_period() throws Exception { logInAsQualityGateAdmin(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + MetricDto metric = insertMetric(); - CreateConditionResponse response = executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", null, "90", 1); + executeRequest(qualityGate.getId(), metric.getKey(), "LT", null, "90", 1); - assertCondition(response, "LT", null, "90", 1); + assertCondition(qualityGate, metric, "LT", null, "90", 1); + } + + @Test + public void test_response() throws Exception { + logInAsQualityGateAdmin(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + MetricDto metric = insertMetric(); + + CreateConditionResponse response = executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", "45", 1); + + QualityGateConditionDto condition = new ArrayList<>(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGate.getId())).get(0); + assertThat(response.getId()).isEqualTo(condition.getId()); + assertThat(response.getMetric()).isEqualTo(metric.getKey()); + assertThat(response.getOp()).isEqualTo("LT"); + assertThat(response.getWarning()).isEqualTo("90"); + assertThat(response.getError()).isEqualTo("45"); + assertThat(response.getPeriod()).isEqualTo(1); } @Test public void throw_ForbiddenException_if_not_gate_administrator() throws Exception { userSession.logIn(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + MetricDto metric = insertMetric(); expectedException.expect(ForbiddenException.class); expectedException.expectMessage("Insufficient privileges"); - executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", "90", null, null); + executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", null, null); } @Test @@ -130,11 +137,13 @@ public class CreateConditionActionTest { // is defined on the default organization OrganizationDto org = db.organizations().insert(); userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, org); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + MetricDto metric = insertMetric(); expectedException.expect(ForbiddenException.class); expectedException.expectMessage("Insufficient privileges"); - executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", "90", null, null); + executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", null, null); } @Test @@ -147,35 +156,11 @@ public class CreateConditionActionTest { assertThat(action.params()).hasSize(6); } - private void assertCondition(CreateConditionResponse response, String operator, @Nullable String warning, @Nullable String error, @Nullable Integer period) { - List conditionDtoList = new ArrayList<>(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateDto.getId())); - assertThat(conditionDtoList).hasSize(1); - QualityGateConditionDto qualityGateConditionDto = conditionDtoList.get(0); - assertThat(qualityGateConditionDto.getQualityGateId()).isEqualTo(qualityGateDto.getId()); - assertThat(qualityGateConditionDto.getMetricId()).isEqualTo(coverageMetricDto.getId().longValue()); - assertThat(qualityGateConditionDto.getOperator()).isEqualTo(operator); - assertThat(qualityGateConditionDto.getWarningThreshold()).isEqualTo(warning); - assertThat(qualityGateConditionDto.getErrorThreshold()).isEqualTo(error); - assertThat(qualityGateConditionDto.getPeriod()).isEqualTo(period); - - assertThat(response.getId()).isEqualTo(qualityGateConditionDto.getId()); - assertThat(response.getMetric()).isEqualTo(coverageMetricDto.getKey()); - assertThat(response.getOp()).isEqualTo(operator); - if (warning != null) { - assertThat(response.getWarning()).isEqualTo(warning); - } else { - assertThat(response.hasWarning()).isFalse(); - } - if (error != null) { - assertThat(response.getError()).isEqualTo(error); - } else { - assertThat(response.hasError()).isFalse(); - } - if (period != null) { - assertThat(response.getPeriod()).isEqualTo(period); - } else { - assertThat(response.hasPeriod()).isFalse(); - } + private void assertCondition(QualityGateDto qualityGate, MetricDto metric, String operator, @Nullable String warning, @Nullable String error, @Nullable Integer period) { + assertThat(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGate.getId())) + .extracting(QualityGateConditionDto::getQualityGateId, QualityGateConditionDto::getMetricId, QualityGateConditionDto::getOperator, + QualityGateConditionDto::getWarningThreshold, QualityGateConditionDto::getErrorThreshold, QualityGateConditionDto::getPeriod) + .containsExactlyInAnyOrder(tuple(qualityGate.getId(), metric.getId().longValue(), operator, warning, error, period)); } private CreateConditionResponse executeRequest(long qualityProfileId, String metricKey, String operator, @Nullable String warning, @Nullable String error, @@ -199,4 +184,8 @@ public class CreateConditionActionTest { private void logInAsQualityGateAdmin() { userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); } + + private MetricDto insertMetric() { + return db.measures().insertMetric(m -> m.setValueType(INT.name()).setHidden(false)); + } }