aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-10-18 18:06:44 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-10-20 17:17:53 +0200
commitf06a13c3a2fd9b34680637d1f5ea1535a1c60b03 (patch)
treefc9f9018b55cfcbef0ed1755d8d50c77a4ba87a0 /sonar-db
parent7126f9a0e374165f67fc42d35500eb90ed59541e (diff)
downloadsonarqube-f06a13c3a2fd9b34680637d1f5ea1535a1c60b03.tar.gz
sonarqube-f06a13c3a2fd9b34680637d1f5ea1535a1c60b03.zip
SONAR-8100 add GroupPermissionDao#deleteByOrganization
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java3
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml7
-rw-r--r--sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java55
4 files changed, 69 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
index eb98102cdac..77ee9cd6907 100644
--- a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
@@ -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);
}
diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
index 7391bcd3eaa..29273b484d3 100644
--- a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
@@ -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);
+
}
diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
index 564ab95dd08..bfdf258f4be 100644
--- a/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
@@ -189,4 +189,11 @@
</choose>
</delete>
+ <delete id="deleteByOrganization" parameterType="String">
+ delete from
+ group_roles
+ where
+ organization_uuid=#{organizationUuid,jdbcType=VARCHAR}
+ </delete>
+
</mapper>
diff --git a/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java
index 35bc6077431..7de85e0808f 100644
--- a/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java
@@ -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);
}