From: Antoine Vigneau Date: Thu, 23 Feb 2023 17:22:16 +0000 (+0100) Subject: SONAR-18525 Support of DELETE verb for /api/scim/v2/Groups X-Git-Tag: 10.0.0.68432~113 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f13f329eb0e3f2b7993c7fab15346c55a6e6b468;p=sonarqube.git SONAR-18525 Support of DELETE verb for /api/scim/v2/Groups --- diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java index fad37c6c455..36e336cff48 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java @@ -21,6 +21,7 @@ package org.sonar.db.scim; import java.util.List; import java.util.Optional; +import org.apache.ibatis.session.RowBounds; import org.sonar.core.util.UuidFactory; import org.sonar.db.Dao; import org.sonar.db.DbSession; @@ -36,6 +37,10 @@ public class ScimGroupDao implements Dao { return mapper(dbSession).findAll(); } + public List findScimGroups(DbSession dbSession, int offset, int limit) { + return mapper(dbSession).findScimGroups(new RowBounds(offset, limit)); + } + public Optional findByScimUuid(DbSession dbSession, String scimGroupUuid) { return Optional.ofNullable(mapper(dbSession).findByScimUuid(scimGroupUuid)); } @@ -44,6 +49,10 @@ public class ScimGroupDao implements Dao { return Optional.ofNullable(mapper(dbSession).findByGroupUuid(groupUuid)); } + public int countScimGroups(DbSession dbSession) { + return mapper(dbSession).countScimGroups(); + } + public ScimGroupDto enableScimForGroup(DbSession dbSession, String groupUuid) { ScimGroupDto scimGroupDto = new ScimGroupDto(uuidFactory.create(), groupUuid); mapper(dbSession).insert(scimGroupDto); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java index 538181eeae2..715d68b40da 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java @@ -22,6 +22,7 @@ package org.sonar.db.scim; import java.util.List; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.session.RowBounds; public interface ScimGroupMapper { @@ -33,6 +34,10 @@ public interface ScimGroupMapper { @CheckForNull ScimGroupDto findByGroupUuid(@Param("groupUuid") String groupUuid); + List findScimGroups(RowBounds rowBounds); + + int countScimGroups(); + void insert(@Param("scimGroupDto") ScimGroupDto scimGroupDto); void deleteByGroupUuid(@Param("groupUuid") String groupUuid); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml index cb58c126cea..86dea4d8772 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml @@ -30,6 +30,18 @@ group_uuid = #{groupUuid,jdbcType=VARCHAR} + + + + insert into scim_groups ( scim_uuid, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java index 75d52cb5203..47ccb73f1bb 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java @@ -19,8 +19,14 @@ */ package org.sonar.db.scim; +import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -58,6 +64,68 @@ public class ScimGroupDaoTest { ); } + @Test + public void countScimGroups_shouldReturnTheTotalNumberOfScimGroups() { + int totalScimGroups = 15; + generateScimGroups(totalScimGroups); + + assertThat(scimGroupDao.countScimGroups(db.getSession())).isEqualTo(totalScimGroups); + } + + @Test + public void countScimGroups_shouldReturnZero_whenNoScimGroups() { + assertThat(scimGroupDao.countScimGroups(db.getSession())).isZero(); + } + + @DataProvider + public static Object[][] paginationData() { + return new Object[][] { + {5, 0, 20, List.of("1", "2", "3", "4", "5")}, + {9, 0, 5, List.of("1", "2", "3", "4", "5")}, + {9, 3, 3, List.of("4", "5", "6")}, + {9, 7, 3, List.of("8", "9")}, + {5, 5, 20, List.of()}, + {5, 0, 0, List.of()} + }; + } + + @Test + @UseDataProvider("paginationData") + public void findScimGroups_whenPaginationAndStartIndex_shouldReturnTheCorrectNumberOfScimGroups(int totalScimGroups, int offset, int pageSize, List expectedScimGroupUuids) { + generateScimGroups(totalScimGroups); + + List scimUserDtos = scimGroupDao.findScimGroups(db.getSession(), offset, pageSize); + + List scimGroupsUuids = toScimGroupsUuids(scimUserDtos); + assertThat(scimGroupsUuids).containsExactlyElementsOf(expectedScimGroupUuids); + } + + private void generateScimGroups(int totalScimGroups) { + List allScimGroups = Stream.iterate(1, i -> i + 1) + .map(i -> insertScimGroup(i.toString())) + .limit(totalScimGroups) + .collect(Collectors.toList()); + assertThat(allScimGroups).hasSize(totalScimGroups); + } + + private ScimGroupDto insertScimGroup(String scimGroupUuid) { + return insertScimGroup(scimGroupUuid, randomAlphanumeric(40)); + } + + private ScimGroupDto insertScimGroup(String scimGroupUuid, String groupUuid) { + ScimGroupDto scimGroupDto = new ScimGroupDto(scimGroupUuid, groupUuid); + Map data = Map.of("scim_uuid", scimGroupDto.getScimGroupUuid(), "group_uuid", scimGroupDto.getGroupUuid()); + db.executeInsert("scim_groups", data); + + return scimGroupDto; + } + + private static List toScimGroupsUuids(Collection scimGroupDtos) { + return scimGroupDtos.stream() + .map(ScimGroupDto::getScimGroupUuid) + .collect(Collectors.toList()); + } + @Test public void findByScimUuid_whenScimUuidNotFound_shouldReturnEmptyOptional() { assertThat(scimGroupDao.findByScimUuid(db.getSession(), "unknownId")).isEmpty();