From 0706ae1bfaffe149329878f7fecf790c57e4c5c9 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 5 Dec 2017 19:30:22 +0100 Subject: [PATCH] SONAR-10134 Add organization parameter in api/qualitygates/delete_condition --- .../qualitygate/ws/DeleteConditionAction.java | 9 +- .../ws/DeleteConditionActionTest.java | 107 +++++++++++++----- 2 files changed, 85 insertions(+), 31 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeleteConditionAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeleteConditionAction.java index 3353e082d85..f8af20364f1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeleteConditionAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeleteConditionAction.java @@ -24,8 +24,9 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateConditionDto; -import org.sonar.db.qualitygate.QualityGateDto; import static com.google.common.base.Preconditions.checkState; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; @@ -54,15 +55,17 @@ public class DeleteConditionAction implements QualityGatesWsAction { .setRequired(true) .setDescription("Condition ID") .setExampleValue("2"); + + wsSupport.createOrganizationParam(createCondition); } @Override public void handle(Request request, Response response) { long conditionId = request.mandatoryParamAsLong(PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { - + OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QualityGateConditionDto condition = wsSupport.getCondition(dbSession, conditionId); - QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, condition.getQualityGateId()); + QGateWithOrgDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndId(dbSession, organization, condition.getQualityGateId()); checkState(qualityGateDto != null, "Condition '%s' is linked to an unknown quality gate '%s'", conditionId, condition.getQualityGateId()); wsSupport.checkCanEdit(qualityGateDto); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java index 16b98a21ca4..46c498ea9d1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeleteConditionActionTest.java @@ -25,9 +25,10 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WebService.Param; import org.sonar.db.DbTester; import org.sonar.db.metric.MetricDto; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; @@ -41,9 +42,11 @@ import static java.lang.String.format; import static java.lang.String.valueOf; import static java.net.HttpURLConnection.HTTP_NO_CONTENT; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; +import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; +import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; +import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION; public class DeleteConditionActionTest { @@ -61,45 +64,68 @@ public class DeleteConditionActionTest { @Test public void definition() { - WebService.Action definition = ws.getDef(); + WebService.Action action = ws.getDef(); + assertThat(action.since()).isEqualTo("4.3"); + assertThat(action.isPost()).isTrue(); + assertThat(action.isInternal()).isFalse(); + assertThat(action.params()) + .extracting(WebService.Param::key, WebService.Param::isRequired) + .containsExactlyInAnyOrder( + tuple("id", true), + tuple("organization", false)); + } - assertThat(definition.since()).isEqualTo("4.3"); - assertThat(definition.isPost()).isTrue(); - assertThat(definition.isInternal()).isFalse(); - assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("id"); + @Test + public void delete_condition() { + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + MetricDto metric = db.measures().insertMetric(); + QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric); + + ws.newRequest() + .setParam(PARAM_ID, valueOf(qualityGateCondition.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); - Param id = definition.param("id"); - assertThat(id.isRequired()).isTrue(); + assertThat(searchConditionsOf(qualityGate)).isEmpty(); } @Test - public void delete_condition() { + public void default_organization_is_used_when_no_organization_parameter() { userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); MetricDto metric = db.measures().insertMetric(); QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric); - call(qualityGateCondition.getId()); + ws.newRequest() + .setParam(PARAM_ID, valueOf(qualityGateCondition.getId())) + .execute(); assertThat(searchConditionsOf(qualityGate)).isEmpty(); } @Test public void no_content() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); MetricDto metric = db.measures().insertMetric(); QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric); - TestResponse result = call(qualityGateCondition.getId()); + TestResponse result = ws.newRequest() + .setParam(PARAM_ID, valueOf(qualityGateCondition.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT); } @Test public void fail_if_built_in_quality_gate() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true)); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true)); MetricDto metric = db.measures().insertMetric(); QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric); @@ -108,25 +134,31 @@ public class DeleteConditionActionTest { ws.newRequest() .setParam(PARAM_ID, valueOf(qualityGateCondition.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } @Test public void fail_if_not_quality_gate_administrator() { - userSession.addPermission(ADMINISTER, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_PROFILES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); MetricDto metric = db.measures().insertMetric(); QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric); expectedException.expect(ForbiddenException.class); - call(qualityGateCondition.getId()); + ws.newRequest() + .setParam(PARAM_ID, valueOf(qualityGateCondition.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); } @Test public void fail_if_condition_id_is_not_found() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_PROFILES, organization); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); MetricDto metric = db.measures().insertMetric(); QualityGateConditionDto qualityGateCondition = db.qualityGates().addCondition(qualityGate, metric); long unknownConditionId = qualityGateCondition.getId() + 42L; @@ -134,12 +166,16 @@ public class DeleteConditionActionTest { expectedException.expect(NotFoundException.class); expectedException.expectMessage("No quality gate condition with id '" + unknownConditionId + "'"); - call(unknownConditionId); + ws.newRequest() + .setParam(PARAM_ID, valueOf(unknownConditionId)) + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); } @Test public void fail_when_condition_match_unknown_quality_gate() { - userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_PROFILES, organization); QualityGateConditionDto condition = new QualityGateConditionDto().setQualityGateId(123L); db.getDbClient().gateConditionDao().insert(condition, db.getSession()); db.commit(); @@ -147,12 +183,27 @@ public class DeleteConditionActionTest { expectedException.expect(IllegalStateException.class); expectedException.expectMessage(format("Condition '%s' is linked to an unknown quality gate '%s'", condition.getId(), 123L)); - call(condition.getId()); + ws.newRequest() + .setParam(PARAM_ID, valueOf(condition.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); } - private TestResponse call(long qualityGateConditionId) { - return ws.newRequest() - .setParam(PARAM_ID, valueOf(qualityGateConditionId)) + @Test + public void fail_when_condition_match_quality_gate_on_other_organization() { + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(ADMINISTER_QUALITY_PROFILES, organization); + OrganizationDto otherOrganization = db.organizations().insert(); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(otherOrganization); + MetricDto metric = db.measures().insertMetric(); + QualityGateConditionDto condition = db.qualityGates().addCondition(qualityGate, metric); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage(format("Condition '%s' is linked to an unknown quality gate '%s'", condition.getId(), qualityGate.getId())); + + ws.newRequest() + .setParam(PARAM_ID, valueOf(condition.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); } -- 2.39.5