]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Add organization parameter in api/qualitygates/destroy
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 7 Dec 2017 08:46:16 +0000 (09:46 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java

index 614320715efb952808ad148b7571c915ca72a80a..5e226c64537b85bed9b1104934e9f7363b15d543 100644 (file)
@@ -72,7 +72,8 @@ public class QualityGateDao implements Dao {
   }
 
   public void delete(QualityGateDto qGate, DbSession session) {
-    mapper(session).delete(qGate.getId());
+    mapper(session).delete(qGate.getUuid());
+    mapper(session).deleteOrgQualityGatesByQualityGateUuid(qGate.getUuid());
   }
 
   public void update(QualityGateDto qGate, DbSession session) {
index 533be402260b5a5485b887fc74c038983039ff2e..b804b904f0ed07ace36aeeabc307f2f5dcd2eeaf 100644 (file)
@@ -44,7 +44,9 @@ public interface QualityGateMapper {
 
   QualityGateDto selectBuiltIn();
 
-  void delete(long id);
+  void delete(String uuid);
+
+  void deleteOrgQualityGatesByQualityGateUuid(String uuid);
 
   void update(QualityGateDto qGate);
 
index aeddd3947e4a263bc3ca88cca2609fd1c2e35f5a..89142897865d4991d36b21def446cef5a0e2511d 100644 (file)
     INNER JOIN organizations o ON o.default_quality_gate_uuid = qg.uuid AND o.uuid=#{organizationUuid, jdbcType=VARCHAR}
   </select>
 
-  <update id="delete" parameterType="long">
-    delete from quality_gates where id=#{id}
+  <update id="delete" parameterType="String">
+    delete from quality_gates where uuid=#{uuid}
+  </update>
+
+  <update id="deleteOrgQualityGatesByQualityGateUuid" parameterType="String">
+    delete from org_quality_gates where quality_gate_uuid=#{uuid}
   </update>
 
   <update id="update" parameterType="QualityGate">
index 99e93b43054cab510c589b4c7856856e5cc1736a..037058302560cc65f2f634f901231a362b41e4b7 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.organization.OrganizationDto;
 
+import static java.lang.String.format;
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class QualityGateDaoTest {
@@ -168,7 +169,9 @@ public class QualityGateDaoTest {
     dbSession.commit();
 
     assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid())).isNull();
+    assertThat(db.countSql(dbSession, format("select count(*) from org_quality_gates where quality_gate_uuid='%s'", qualityGate.getUuid()))).isZero();
     assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, otherQualityGate.getUuid())).isNotNull();
+    assertThat(db.countSql(dbSession, format("select count(*) from org_quality_gates where quality_gate_uuid='%s'", otherQualityGate.getUuid()))).isEqualTo(1);
   }
 
   @Test
index 1f5f70a34676301e470dfddd229271daeb720727..959ecb787d8ff6481cbef80533cf6a9c5fd0df18 100644 (file)
@@ -25,6 +25,8 @@ 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.QualityGateDto;
 import org.sonar.server.qualitygate.QualityGateFinder;
 
@@ -55,20 +57,21 @@ public class DestroyAction implements QualityGatesWsAction {
       .setDescription("ID of the quality gate to delete")
       .setRequired(true)
       .setExampleValue("1");
+
+    wsSupport.createOrganizationParam(action);
   }
 
   @Override
   public void handle(Request request, Response response) {
     long qualityGateId = request.mandatoryParamAsLong(QualityGatesWsParameters.PARAM_ID);
     try (DbSession dbSession = dbClient.openSession(false)) {
-
-      QualityGateDto qualityGate = finder.getById(dbSession, qualityGateId);
+      OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
+      QGateWithOrgDto qualityGate = finder.getByOrganizationAndId(dbSession, organization, qualityGateId);
       Optional<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
       checkArgument(!defaultQualityGate.isPresent() || !defaultQualityGate.get().getId().equals(qualityGate.getId()), "The default quality gate cannot be removed");
       wsSupport.checkCanEdit(qualityGate);
 
       dbClient.qualityGateDao().delete(qualityGate, dbSession);
-
       dbSession.commit();
       response.noContent();
     }
index 7579f39d0afbb51e9f922e35aa1a6ef858d952ca..496eabce71037d5feb5a9286b1fde6483981ed76 100644 (file)
@@ -27,6 +27,8 @@ 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.organization.OrganizationDto;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
 import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -39,9 +41,11 @@ import static java.lang.String.format;
 import static java.lang.String.valueOf;
 import static org.apache.commons.lang.StringUtils.EMPTY;
 import static org.assertj.core.api.Assertions.assertThat;
+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 DestroyActionTest {
 
@@ -61,68 +65,71 @@ public class DestroyActionTest {
   private DestroyAction underTest = new DestroyAction(dbClient, wsSupport, qualityGateFinder);
   private WsActionTester ws = new WsActionTester(underTest);
 
-  @Test
-  public void definition() {
-    WebService.Action definition = ws.getDef();
-
-    assertThat(definition.since()).isEqualTo("4.3");
-    assertThat(definition.isPost()).isTrue();
-    assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("id");
-
-    WebService.Param id = definition.param("id");
-    assertThat(id.isRequired()).isTrue();
-  }
-
   @Test
   public void delete_quality_gate() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
-    Long qualityGateId = qualityGate.getId();
-    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull();
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
     ws.newRequest()
-      .setParam(PARAM_ID, valueOf(qualityGateId))
+      .setParam(PARAM_ID, qualityGate.getId().toString())
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
 
-    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNull();
+    assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull();
+    assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isZero();
   }
 
   @Test
   public void delete_quality_gate_if_non_default_when_a_default_exist() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
-    Long toDeleteQualityGateId = qualityGate.getId();
-    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNotNull();
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
-    QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Default"));
+    QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate();
     db.qualityGates().setDefaultQualityGate(defaultQualityGate);
 
     ws.newRequest()
-      .setParam(PARAM_ID, valueOf(toDeleteQualityGateId))
+      .setParam(PARAM_ID, valueOf(qualityGate.getId()))
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
 
-    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNull();
+    assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull();
   }
 
   @Test
   public void does_not_delete_built_in_quality_gate() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
-    Long qualityGateId = qualityGate.getId();
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
 
     ws.newRequest()
-      .setParam(PARAM_ID, valueOf(qualityGateId))
+      .setParam(PARAM_ID, valueOf(qualityGate.getId()))
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
+  }
+
+  @Test
+  public void default_organization_is_used_when_no_organization_parameter() {
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+    OrganizationDto otherOrganization = db.organizations().insert();
+    QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
 
-    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull();
+    ws.newRequest()
+      .setParam(PARAM_ID, valueOf(qualityGate.getId()))
+      .execute();
+
+    assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, db.getDefaultOrganization(), qualityGate.getId())).isNull();
   }
 
   @Test
   public void fail_when_invalid_id() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
 
     String invalidId = "invalid-id";
 
@@ -131,56 +138,78 @@ public class DestroyActionTest {
 
     ws.newRequest()
       .setParam(PARAM_ID, valueOf(invalidId))
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
   }
 
   @Test
   public void fail_when_missing_id() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
 
     expectedException.expect(IllegalArgumentException.class);
 
     ws.newRequest()
       .setParam(PARAM_ID, valueOf(EMPTY))
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
   }
 
   @Test
   public void fail_to_delete_default_quality_gate() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
     db.qualityGates().setDefaultQualityGate(qualityGate);
-    Long qualityGateId = qualityGate.getId();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("The default quality gate cannot be removed");
 
     ws.newRequest()
-      .setParam(PARAM_ID, valueOf(qualityGateId))
+      .setParam(PARAM_ID, valueOf(qualityGate.getId()))
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
   }
 
   @Test
   public void fail_on_unknown_quality_gate() {
-    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    OrganizationDto organization = db.organizations().insert();
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
 
     expectedException.expect(NotFoundException.class);
 
     ws.newRequest()
       .setParam(PARAM_ID, "123")
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
   }
 
   @Test
   public void fail_when_not_quality_gates_administer() {
-    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, db.getDefaultOrganization());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
+    OrganizationDto organization = db.organizations().insert();
+    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
     expectedException.expect(ForbiddenException.class);
 
     ws.newRequest()
       .setParam(PARAM_ID, qualityGate.getId().toString())
+      .setParam(PARAM_ORGANIZATION, organization.getKey())
       .execute();
   }
 
+  @Test
+  public void definition() {
+    WebService.Action action = ws.getDef();
+
+    assertThat(action.since()).isEqualTo("4.3");
+    assertThat(action.isPost()).isTrue();
+
+    assertThat(action.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(
+        tuple("id", true),
+        tuple("organization", false));
+  }
+
 }