]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23012 Add userName in scim users endpoint response
authorNolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com>
Wed, 18 Sep 2024 14:55:37 +0000 (16:55 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 18 Sep 2024 20:02:59 +0000 (20:02 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserWithUsernameDto.java [new file with mode: 0644]
server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml

index bba0587f4e8a84272da78c5755fc6c0b944ee2a0..71e0d3a0b6a70115385d297d25d27fcd8328878d 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.db.user.UserDto;
 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.AssertionsForClassTypes.tuple;
 import static org.assertj.core.api.Fail.fail;
 
 class ScimUserDaoIT {
@@ -118,16 +119,16 @@ class ScimUserDaoIT {
     List<String> expectedScimUserUuids) {
     generateScimUsers(totalScimUsers);
 
-    List<ScimUserDto> scimUserDtos = scimUserDao.findScimUsers(dbSession, ScimUserQuery.empty(), OffsetBasedPagination.forOffset(offset,
+    List<ScimUserWithUsernameDto> scimUserDtos = scimUserDao.findScimUsers(dbSession, ScimUserQuery.empty(), OffsetBasedPagination.forOffset(offset,
       pageSize));
 
     List<String> scimUsersUuids = toScimUsersUuids(scimUserDtos);
     assertThat(scimUsersUuids).containsExactlyElementsOf(expectedScimUserUuids);
   }
 
-  private List<String> toScimUsersUuids(Collection<ScimUserDto> scimUserDtos) {
+  private List<String> toScimUsersUuids(Collection<ScimUserWithUsernameDto> scimUserDtos) {
     return scimUserDtos.stream()
-      .map(ScimUserDto::getScimUserUuid)
+      .map(ScimUserWithUsernameDto::getScimUserUuid)
       .toList();
   }
 
@@ -194,7 +195,7 @@ class ScimUserDaoIT {
     insertScimUsersWithUsers(userLogins);
     ScimUserQuery query = ScimUserQuery.builder().userName(search).build();
 
-    List<ScimUserDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+    List<ScimUserWithUsernameDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
 
     List<String> scimUsersUuids = toScimUsersUuids(scimUsersByQuery);
     assertThat(scimUsersUuids).containsExactlyElementsOf(expectedScimUserUuids);
@@ -212,7 +213,7 @@ class ScimUserDaoIT {
 
     ScimUserQuery query = ScimUserQuery.builder().groupUuid(group1dto.getUuid()).build();
 
-    List<ScimUserDto> scimUsers = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+    List<ScimUserWithUsernameDto> scimUsers = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
 
     List<String> scimUsersUuids = toScimUsersUuids(scimUsers);
     assertThat(scimUsersUuids).containsExactlyInAnyOrder(
@@ -221,6 +222,23 @@ class ScimUserDaoIT {
     );
   }
 
+  @Test
+  void findScimUsers_shouldReturnTheExpectedScimUsersWithUsername() {
+    insertScimUsersWithUsers(List.of("userA", "userB"));
+
+    ScimUserQuery query = ScimUserQuery.builder().userName("userA").build();
+
+    List<ScimUserWithUsernameDto> scimUsers = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+
+    List<String> scimUsersUuids = toScimUsersUuids(scimUsers);
+
+    assertThat(scimUsers)
+      .extracting(ScimUserWithUsernameDto::getScimUserUuid, ScimUserWithUsernameDto::getUserName)
+      .contains(
+        tuple(scimUsersUuids.get(0), "userA")
+      );
+  }
+
   private GroupDto createGroupWithUsers(ScimUserTestData... testUsers) {
     GroupDto group = db.users().insertGroup();
 
@@ -238,7 +256,7 @@ class ScimUserDaoIT {
 
     ScimUserQuery query = ScimUserQuery.builder().scimUserUuids(expectedScimUserUuids).build();
 
-    List<ScimUserDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+    List<ScimUserWithUsernameDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
 
     List<String> scimUsersUuids = toScimUsersUuids(scimUsersByQuery);
     assertThat(scimUsersByQuery)
@@ -255,10 +273,10 @@ class ScimUserDaoIT {
 
     ScimUserQuery query = ScimUserQuery.builder().scimUserUuids(scimUserUuids).userName("username_5").build();
 
-    List<ScimUserDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+    List<ScimUserWithUsernameDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
 
     assertThat(scimUsersByQuery).hasSize(1)
-      .extracting(ScimUserDto::getScimUserUuid)
+      .extracting(ScimUserWithUsernameDto::getScimUserUuid)
       .contains("6");
   }
 
@@ -271,11 +289,11 @@ class ScimUserDaoIT {
 
     ScimUserQuery query = ScimUserQuery.builder().userUuids(allUsersUuid).build();
 
-    List<ScimUserDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+    List<ScimUserWithUsernameDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
 
     assertThat(scimUsersByQuery)
       .hasSameSizeAs(allUsersUuid)
-      .extracting(ScimUserDto::getUserUuid)
+      .extracting(ScimUserWithUsernameDto::getUserUuid)
       .containsAll(allUsersUuid);
   }
 
@@ -288,10 +306,10 @@ class ScimUserDaoIT {
 
     ScimUserQuery query = ScimUserQuery.builder().userUuids(allUsersUuid).userName("username_5").build();
 
-    List<ScimUserDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
+    List<ScimUserWithUsernameDto> scimUsersByQuery = scimUserDao.findScimUsers(dbSession, query, Pagination.all());
 
     assertThat(scimUsersByQuery).hasSize(1)
-      .extracting(ScimUserDto::getScimUserUuid)
+      .extracting(ScimUserWithUsernameDto::getScimUserUuid)
       .contains("6");
   }
 
index c041dca82a4430277f7a0b02a2288111f05a0daa..ddc056227972a7c601dde6256269059ed4980db9 100644 (file)
@@ -56,7 +56,7 @@ public class ScimUserDao implements Dao {
     return scimUserDto;
   }
 
-  public List<ScimUserDto> findScimUsers(DbSession dbSession, ScimUserQuery scimUserQuery, Pagineable pagination) {
+  public List<ScimUserWithUsernameDto> findScimUsers(DbSession dbSession, ScimUserQuery scimUserQuery, Pagineable pagination) {
     checkState(scimUserQuery.getUserUuids() == null || scimUserQuery.getScimUserUuids() == null,
       "Only one of userUuids & scimUserUuids request parameter is supported.");
     if (scimUserQuery.getScimUserUuids() != null) {
@@ -79,7 +79,7 @@ public class ScimUserDao implements Dao {
     return mapper(dbSession).findScimUsers(scimUserQuery, pagination);
   }
 
-  private static List<ScimUserDto> createPartialQuery(ScimUserQuery completeQuery, List<String> strings,
+  private static List<ScimUserWithUsernameDto> createPartialQuery(ScimUserQuery completeQuery, List<String> strings,
     BiFunction<ScimUserQuery.ScimUserQueryBuilder, List<String>, ScimUserQuery.ScimUserQueryBuilder> queryModifier,
     DbSession dbSession, Pagineable pagination) {
 
index 68f20dea012aa1168a5a6cffb55320645b2efe82..3d2f809916f65cbc33567de02e0c200e3fb9bcc7 100644 (file)
@@ -36,7 +36,7 @@ public interface ScimUserMapper {
 
   void insert(@Param("scimUserDto") ScimUserDto scimUserDto);
 
-  List<ScimUserDto> findScimUsers(@Param("query") ScimUserQuery scimUserQuery, @Param("pagination") Pagineable pagination);
+  List<ScimUserWithUsernameDto> findScimUsers(@Param("query") ScimUserQuery scimUserQuery, @Param("pagination") Pagineable pagination);
 
   int countScimUsers(@Param("query") ScimUserQuery scimUserQuery);
 
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserWithUsernameDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserWithUsernameDto.java
new file mode 100644 (file)
index 0000000..acf81ac
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.db.scim;
+
+public final class ScimUserWithUsernameDto extends ScimUserDto {
+
+  private String userName;
+
+  public ScimUserWithUsernameDto(String scimUserUuid, String userUuid, String userName) {
+    super(scimUserUuid, userUuid);
+    this.userName = userName;
+  }
+
+  public String getUserName() {
+    return userName;
+  }
+}
index 10f5857faa3ab6029ab46bb5324dc8d6ca5963ff..7cc7d6b447b51c828e380efbbbf0e06282691d61 100644 (file)
@@ -8,6 +8,11 @@
       s.user_uuid as userUuid
   </sql>
 
+  <sql id="scimUsersWithUsernameColumns">
+      <include refid="scimUsersColumns"/>,
+      u.external_id as userName
+  </sql>
+
   <select id="findAll" resultType="org.sonar.db.scim.ScimUserDto">
     select
     <include refid="scimUsersColumns"/>
@@ -51,9 +56,9 @@
     )
   </insert>
 
-  <select id="findScimUsers" resultType="org.sonar.db.scim.ScimUserDto">
+  <select id="findScimUsers" resultType="org.sonar.db.scim.ScimUserWithUsernameDto">
     select
-    <include refid="scimUsersColumns"/>
+    <include refid="scimUsersWithUsernameColumns"/>
     <include refid="sqlSelectByQuery"/>
     order by s.scim_uuid asc
     <include refid="org.sonar.db.common.Common.pagination"/>