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

index 30ad7a49f28f5e22c12ecfc52c00ea2bb1e543a8..a88029e17fb819d164443a23677e79bcfd69ccd9 100644 (file)
@@ -71,6 +71,10 @@ public class GroupDao implements Dao {
     mapper(dbSession).deleteById(groupId);
   }
 
+  public void deleteByOrganization(DbSession dbSession, String organizationUuid) {
+    mapper(dbSession).deleteByOrganization(organizationUuid);
+  }
+
   public int countByQuery(DbSession session, String organizationUuid, @Nullable String query) {
     return mapper(session).countByQuery(organizationUuid, groupSearchToSql(query));
   }
index d056931638aab78dd5f71ee592306d0d3fc0303c..cbb3e0dff7c3a49ccf7a7f0305171822c7a50723 100644 (file)
@@ -44,6 +44,8 @@ public interface GroupMapper {
 
   void deleteById(long groupId);
 
+  void deleteByOrganization(@Param("organizationUuid") String organizationUuid);
+
   @CheckForNull
   GroupDto selectByName(@Param("organizationUuid") String organizationUuid, @Param("name") String name);
 
index 45d71a4d7b65e4a7695fef7a799853209b1ad617..bc74e334d7e4a402de7d149bcb72034911120305 100644 (file)
     </where>
   </delete>
 
+  <delete id="deleteByOrganization" parameterType="String">
+    delete from
+      groups
+    where
+      organization_uuid=#{organizationUuid,jdbcType=VARCHAR}
+  </delete>
+
   <select id="selectByUserLogin" parameterType="string" resultType="Group">
     select
     <include refid="groupColumns"/>
index 99ee0be311529d2400b32919593ca7146d9dc7fe..e5482dcfedc2788f9245c6a7d2d73021002f6bea 100644 (file)
@@ -408,6 +408,51 @@ public class GroupDaoTest {
     stream(adminPerGroupPermissionUsers).forEach(user -> db.rootFlag().verify(user, false, DATE_1));
   }
 
+  @Test
+  public void deleteByOrganization_does_not_fail_when_table_is_empty() {
+    underTest.deleteByOrganization(dbSession, "some uuid");
+    dbSession.commit();
+  }
+
+  @Test
+  public void deleteByOrganization_does_not_fail_when_organization_has_no_group() {
+    OrganizationDto organization = db.organizations().insert();
+
+    underTest.deleteByOrganization(dbSession, organization.getUuid());
+    dbSession.commit();
+  }
+
+  @Test
+  public void deleteByOrganization_deletes_all_groups_in_specified_organization() {
+    OrganizationDto organization1 = db.organizations().insert();
+    OrganizationDto organization2 = db.organizations().insert();
+    OrganizationDto organization3 = db.organizations().insert();
+
+    db.users().insertGroup(organization1);
+    db.users().insertGroup(organization2);
+    db.users().insertGroup(organization3);
+    db.users().insertGroup(organization3);
+    db.users().insertGroup(organization2);
+
+    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 groups"))
+      .extracting(row -> (String) row.get("organizationUuid"))
+      .containsOnly(organizationUuids);
+  }
+
   private void call_updateRootFlagFromPermissions(GroupDto groupDto, long now) {
     when(system2.now()).thenReturn(now);
     underTest.updateRootFlagOfUsersInGroupFromPermissions(db.getSession(), groupDto.getId(), db.getDefaultOrganization().getUuid());