]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8100 add GroupPermissionDao#deleteByOrganization
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 18 Oct 2016 16:06:44 +0000 (18:06 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Oct 2016 15:17:53 +0000 (17:17 +0200)
sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java

index eb98102cdacb825e56c21bcaf8ffc1c330b7d9c7..77ee9cd690716834abb7852e72fb939e246fee78 100644 (file)
@@ -130,6 +130,10 @@ public class GroupPermissionDao implements Dao {
     mapper(dbSession).delete(permission, organizationUuid, groupId, rootComponentId);
   }
 
+  public void deleteByOrganization(DbSession dbSession, String organizationUuid) {
+    mapper(dbSession).deleteByOrganization(organizationUuid);
+  }
+
   private static GroupPermissionMapper mapper(DbSession session) {
     return session.getMapper(GroupPermissionMapper.class);
   }
index 7391bcd3eaae2c2dcd134acac8f7b2762236e5cf..29273b484d33e8722ed671cc7b16dd2a6a5599e2 100644 (file)
@@ -48,4 +48,7 @@ public interface GroupPermissionMapper {
   List<String> selectGlobalPermissionsOfGroup(@Param("organizationUuid") String organizationUuid, @Nullable @Param("groupId") Long groupId);
 
   List<String> selectProjectPermissionsOfGroup(@Param("organizationUuid") String organizationUuid, @Nullable @Param("groupId") Long groupId, @Param("projectId") long projectId);
+
+  void deleteByOrganization(@Param("organizationUuid") String organizationUuid);
+
 }
index 564ab95dd08eb410625446ca348ffbe2cf74b596..bfdf258f4be4a3fb87e1fbcaa4b62f6e5bd7a172 100644 (file)
     </choose>
   </delete>
 
+  <delete id="deleteByOrganization" parameterType="String">
+    delete from
+      group_roles
+    where
+      organization_uuid=#{organizationUuid,jdbcType=VARCHAR}
+  </delete>
+
 </mapper>
index 35bc6077431e7b2d79c8df4ed0c1544abf7a9ada..7de85e0808f6137dfa5bec2cc7be00598337ea6a 100644 (file)
@@ -433,6 +433,61 @@ public class GroupPermissionDaoTest {
     assertThat(db.countRowsOfTable("group_roles")).isEqualTo(3);
   }
 
+  @Test
+  public void deleteByOrganization_does_not_fail_on_empty_db() {
+    underTest.deleteByOrganization(dbSession, "some uuid");
+    dbSession.commit();
+  }
+
+  @Test
+  public void deleteByOrganization_does_not_fail_if_organization_has_no_group() {
+    OrganizationDto organization = db.organizations().insert();
+
+    underTest.deleteByOrganization(dbSession, organization.getUuid());
+    dbSession.commit();
+  }
+
+  @Test
+  public void deleteByOrganization_deletes_all_groups_of_organization() {
+    OrganizationDto organization1 = db.organizations().insert();
+    OrganizationDto organization2 = db.organizations().insert();
+    OrganizationDto organization3 = db.organizations().insert();
+    insertGroupWithPermissions(organization1);
+    insertGroupWithPermissions(organization2);
+    insertGroupWithPermissions(organization3);
+    insertGroupWithPermissions(organization3);
+    insertGroupWithPermissions(organization2);
+    db.users().insertPermissionOnAnyone(organization1, "pop");
+    db.users().insertPermissionOnAnyone(organization2, "pop");
+    db.users().insertPermissionOnAnyone(organization3, "pop");
+
+    underTest.deleteByOrganization(dbSession, organization2.getUuid());
+    dbSession.commit();
+    verifyOrganizationUuidsInTable(organization1.getUuid(), organization3.getUuid());
+
+    underTest.deleteByOrganization(dbSession, organization1.getUuid());
+    dbSession.commit();
+    verifyOrganizationUuidsInTable(organization3.getUuid());
+
+    underTest.deleteByOrganization(dbSession, organization3.getUuid());
+    dbSession.commit();
+    verifyOrganizationUuidsInTable();
+  }
+
+  private void verifyOrganizationUuidsInTable(String... organizationUuids) {
+    assertThat(db.select("select distinct organization_uuid as \"organizationUuid\" from group_roles"))
+        .extracting((row) -> (String) row.get("organizationUuid"))
+        .containsOnly(organizationUuids);
+  }
+
+  private Long insertGroupWithPermissions(OrganizationDto organization1) {
+    GroupDto group = db.users().insertGroup(organization1);
+    db.users().insertPermissionOnGroup(group, "foo");
+    db.users().insertPermissionOnGroup(group, "bar");
+    db.users().insertPermissionOnGroup(group, "doh");
+    return group.getId();
+  }
+
   private void assertThatNoPermission(String permission) {
     assertThat(db.countSql("select count(id) from group_roles where role='" + permission + "'")).isEqualTo(0);
   }