]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19085 Add step to delete removed groups.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Mon, 8 May 2023 07:59:15 +0000 (09:59 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:14 +0000 (20:03 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/ExternalGroupMapper.xml
server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/GroupService.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/GroupServiceTest.java

index 8b790881b36bac06b08881b712d5f7ba4fe5642b..baa685e0ab9331cbacccb4653c049a69eb32a13f 100644 (file)
@@ -59,6 +59,16 @@ public class ExternalGroupDaoIT {
     assertThat(savedGroup).containsExactlyInAnyOrderElementsOf(expectedGroups);
   }
 
+  @Test
+  public void deleteByGroupUuid_deletesTheGroup() {
+    List<ExternalGroupDto> insertedGroups = createAndInsertExternalGroupDtos("provider1", 3);
+
+    ExternalGroupDto toRemove = insertedGroups.remove(0);
+    underTest.deleteByGroupUuid(dbSession, toRemove.groupUuid());
+    List<ExternalGroupDto> remainingGroups = underTest.selectByIdentityProvider(dbSession, "provider1");
+    assertThat(remainingGroups).containsExactlyInAnyOrderElementsOf(insertedGroups);
+  }
+
   private List<ExternalGroupDto> createAndInsertExternalGroupDtos(String provider, int numberOfGroups) {
     List<ExternalGroupDto> expectedExternalGroupDtos = new ArrayList<>();
     for (int i = 1; i <= numberOfGroups; i++) {
index e208eba651d6eb4beeb5c9191873179b549fdb3c..c6c95692efa73aa227f46b7d72aa2106b2f9b578 100644 (file)
@@ -33,6 +33,10 @@ public class ExternalGroupDao implements Dao {
     return mapper(dbSession).selectByIdentityProvider(identityProvider);
   }
 
+  public void deleteByGroupUuid(DbSession dbSession, String groupUuid) {
+    mapper(dbSession).deleteByGroupUuid(groupUuid);
+  }
+
   private static ExternalGroupMapper mapper(DbSession session) {
     return session.getMapper(ExternalGroupMapper.class);
   }
index 58f632c0ebad34979a88502c274bbcc4ab98a59f..b456be15a66d84a5ea41ef4e5006b6af229b7167 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.db.user;
 
 import java.util.List;
+import org.apache.ibatis.annotations.Param;
 
 public interface ExternalGroupMapper {
 
@@ -27,4 +28,7 @@ public interface ExternalGroupMapper {
 
   List<ExternalGroupDto> selectByIdentityProvider(String identityProvider);
 
+  void deleteByGroupUuid(@Param("groupUuid") String groupUuid);
+
+
 }
index 561a24093a63e75d756b1e4a2ac2ae41f20dc5df..be2db7475b49c40026f41ea2c1d18811ee13a06a 100644 (file)
@@ -27,4 +27,8 @@
     WHERE eg.external_identity_provider=#{identityProvider,jdbcType=VARCHAR}
   </select>
 
+  <delete id="deleteByGroupUuid" parameterType="String">
+    delete from external_groups where group_uuid = #{groupUuid, jdbcType=VARCHAR}
+  </delete>
+
 </mapper>
index bf57fe9fc07a0da3fc55ed50500661bfa78c5533..37fef76d14fe3ab3774aee8e3b4658011715cc82 100644 (file)
@@ -59,6 +59,7 @@ public class GroupService {
     removeGroupFromQualityProfileEdit(dbSession, group);
     removeGroupFromQualityGateEdit(dbSession, group);
     removeGroupScimLink(dbSession, group);
+    removeExternalGroupMapping(dbSession, group);
     removeGroup(dbSession, group);
   }
 
@@ -158,6 +159,10 @@ public class GroupService {
     dbClient.scimGroupDao().deleteByGroupUuid(dbSession, group.getUuid());
   }
 
+  private void removeExternalGroupMapping(DbSession dbSession, GroupDto group) {
+    dbClient.externalGroupDao().deleteByGroupUuid(dbSession, group.getUuid());
+  }
+
   private void removeGroup(DbSession dbSession, GroupDto group) {
     dbClient.groupDao().deleteByUuid(dbSession, group.getUuid(), group.getName());
   }
index ce1c489f895fac979020297d96ae85f3bc1c1cdb..b9513fe3dbf3b6b040c292077085232bc081541b 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.db.permission.template.PermissionTemplateDao;
 import org.sonar.db.qualitygate.QualityGateGroupPermissionsDao;
 import org.sonar.db.qualityprofile.QProfileEditGroupsDao;
 import org.sonar.db.scim.ScimGroupDao;
+import org.sonar.db.user.ExternalGroupDao;
 import org.sonar.db.user.GroupDao;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.RoleDao;
@@ -92,6 +93,7 @@ public class GroupServiceTest {
     when(dbClient.qProfileEditGroupsDao()).thenReturn(mock(QProfileEditGroupsDao.class));
     when(dbClient.qualityGateGroupPermissionsDao()).thenReturn(mock(QualityGateGroupPermissionsDao.class));
     when(dbClient.scimGroupDao()).thenReturn(mock(ScimGroupDao.class));
+    when(dbClient.externalGroupDao()).thenReturn(mock(ExternalGroupDao.class));
     when(dbClient.groupDao()).thenReturn(mock(GroupDao.class));
   }
 
@@ -327,6 +329,7 @@ public class GroupServiceTest {
     verify(dbClient.qProfileEditGroupsDao()).deleteByGroup(dbSession, groupDto);
     verify(dbClient.qualityGateGroupPermissionsDao()).deleteByGroup(dbSession, groupDto);
     verify(dbClient.scimGroupDao()).deleteByGroupUuid(dbSession, groupDto.getUuid());
+    verify(dbClient.externalGroupDao()).deleteByGroupUuid(dbSession, groupDto.getUuid());
     verify(dbClient.groupDao()).deleteByUuid(dbSession, groupDto.getUuid(), groupDto.getName());
   }
 }