]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18525 Support of DELETE verb for /api/scim/v2/Groups
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Thu, 23 Feb 2023 17:22:16 +0000 (18:22 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:06 +0000 (20:04 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimGroupMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimGroupMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java

index fad37c6c455f6732fc632e2ce7ca5b649b0a767e..36e336cff48cef5a77891d4ffa2fe5e2072f16f9 100644 (file)
@@ -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<ScimGroupDto> findScimGroups(DbSession dbSession, int offset, int limit) {
+    return mapper(dbSession).findScimGroups(new RowBounds(offset, limit));
+  }
+
   public Optional<ScimGroupDto> 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);
index 538181eeae23e03329b889a039ece8128379f76e..715d68b40da4ba3f6e50053fd4d75cabc780ce7b 100644 (file)
@@ -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<ScimGroupDto> findScimGroups(RowBounds rowBounds);
+
+  int countScimGroups();
+
   void insert(@Param("scimGroupDto") ScimGroupDto scimGroupDto);
 
   void deleteByGroupUuid(@Param("groupUuid") String groupUuid);
index cb58c126cea482dc01cfbe6f223c302815d6fe25..86dea4d8772e4e13c60173e13908a64eb5e4b5f2 100644 (file)
       group_uuid = #{groupUuid,jdbcType=VARCHAR}
   </select>
 
+  <select id="findScimGroups" resultType="org.sonar.db.scim.ScimGroupDto">
+    select
+    <include refid="scimGroupsColumns"/>
+    from scim_groups s
+    order by s.scim_uuid asc
+  </select>
+
+  <select id="countScimGroups" resultType="int">
+    select count(1)
+    from scim_groups
+  </select>
+
   <insert id="insert" parameterType="map" useGeneratedKeys="false">
     insert into scim_groups (
       scim_uuid,
index 75d52cb5203a459f30556b58ae15a88ea89d701b..47ccb73f1bb33b68a0ff1e3ea6f34ced759c3833 100644 (file)
  */
 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<String> expectedScimGroupUuids) {
+    generateScimGroups(totalScimGroups);
+
+    List<ScimGroupDto> scimUserDtos = scimGroupDao.findScimGroups(db.getSession(), offset, pageSize);
+
+    List<String> scimGroupsUuids = toScimGroupsUuids(scimUserDtos);
+    assertThat(scimGroupsUuids).containsExactlyElementsOf(expectedScimGroupUuids);
+  }
+
+  private void generateScimGroups(int totalScimGroups) {
+    List<ScimGroupDto> 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<String, Object> data = Map.of("scim_uuid", scimGroupDto.getScimGroupUuid(), "group_uuid", scimGroupDto.getGroupUuid());
+    db.executeInsert("scim_groups", data);
+
+    return scimGroupDto;
+  }
+
+  private static List<String> toScimGroupsUuids(Collection<ScimGroupDto> scimGroupDtos) {
+    return scimGroupDtos.stream()
+      .map(ScimGroupDto::getScimGroupUuid)
+      .collect(Collectors.toList());
+  }
+
   @Test
   public void findByScimUuid_whenScimUuidNotFound_shouldReturnEmptyOptional() {
     assertThat(scimGroupDao.findByScimUuid(db.getSession(), "unknownId")).isEmpty();