import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
+import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
assertThat(scimUsersUuids).containsExactlyElementsOf(expectedScimUserUuids);
}
+ @Test
+ public void findScimUsers_whenFilteringByGroupUuid_shouldReturnTheExpectedScimUsers() {
+ List<ScimUserTestData> scimUsersTestData = insertScimUsersWithUsers(List.of("userAInGroupA", "userBInGroupA", "userAInGroupB", "userNotInGroup"));
+ Map<String, ScimUserTestData> users = scimUsersTestData.stream()
+ .collect(Collectors.toMap(testData -> testData.getUserDto().getExternalId(), Function.identity()));
+
+ GroupDto group1dto = createGroupWithUsers(users.get("userAInGroupA"), users.get("userBInGroupA"));
+ createGroupWithUsers(users.get("userAInGroupB"));
+
+ ScimUserQuery query = ScimUserQuery.builder().groupUuid(group1dto.getUuid()).build();
+
+ List<ScimUserDto> scimUsers = scimUserDao.findScimUsers(dbSession, query, 0, 100);
+
+ List<String> scimUsersUuids = toScimUsersUuids(scimUsers);
+ assertThat(scimUsersUuids).containsExactlyInAnyOrder(
+ users.get("userAInGroupA").getScimUserUuid(),
+ users.get("userBInGroupA").getScimUserUuid()
+ );
+ }
+
+ private GroupDto createGroupWithUsers(ScimUserTestData... testUsers) {
+ GroupDto group = db.users().insertGroup();
+
+ UserDto[] userDtos = Arrays.stream(testUsers)
+ .map(ScimUserTestData::getUserDto)
+ .toArray(UserDto[]::new);
+ db.users().insertMembers(group, userDtos);
+ return group;
+ }
+
@Test
public void findScimUsers_whenFilteringByScimUuidsWithLongRange_shouldReturnTheExpectedScimUsers() {
generateScimUsers(3000);
private ScimUserTestData insertScimUserWithUser(String userLogin, String scimUuid) {
UserDto userDto = db.users().insertUser(u -> u.setExternalId(userLogin));
- return insertScimUser(scimUuid, userDto.getUuid());
+ ScimUserTestData scimUserTestData = insertScimUser(scimUuid, userDto.getUuid());
+ scimUserTestData.setUserDto(userDto);
+ return scimUserTestData;
}
private ScimUserTestData insertScimUser(String scimUserUuid) {
private final String scimUserUuid;
private final String userUuid;
+ private UserDto userDto;
private ScimUserTestData(String scimUserUuid, String userUuid) {
this.scimUserUuid = scimUserUuid;
return userUuid;
}
+ private UserDto getUserDto() {
+ return userDto;
+ }
+
+ private void setUserDto(UserDto userDto) {
+ this.userDto = userDto;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o)
private final String userName;
private final Set<String> scimUserUuids;
private final Set<String> userUuids;
+ private final String groupUuid;
- private ScimUserQuery(@Nullable String userName, @Nullable Set<String> scimUserUuids, @Nullable Set<String> userUuids) {
+ private ScimUserQuery(@Nullable String userName, @Nullable Set<String> scimUserUuids,
+ @Nullable Set<String> userUuids, @Nullable String groupUuid) {
this.userName = userName;
this.scimUserUuids = scimUserUuids;
this.userUuids = userUuids;
+ this.groupUuid = groupUuid;
}
@CheckForNull
return userUuids;
}
+ @CheckForNull
+ public String getGroupUuid() {
+ return groupUuid;
+ }
+
public static ScimUserQuery empty() {
return builder().build();
}
private String userName;
private Set<String> scimUserUuids;
private Set<String> userUuids;
+ private String groupUuid;
private ScimUserQueryBuilder() {
}
return this;
}
+ public ScimUserQueryBuilder groupUuid(String groupUuid) {
+ this.groupUuid = groupUuid;
+ return this;
+ }
+
public ScimUserQuery build() {
- return new ScimUserQuery(userName, scimUserUuids, userUuids);
+ return new ScimUserQuery(userName, scimUserUuids, userUuids, groupUuid);
}
}
}
<mapper namespace="org.sonar.db.scim.ScimUserMapper">
<sql id="scimUsersColumns">
- scim_uuid as scimUserUuid,
- user_uuid as userUuid
+ s.scim_uuid as scimUserUuid,
+ s.user_uuid as userUuid
</sql>
<select id="findAll" resultType="org.sonar.db.scim.ScimUserDto">
select
<include refid="scimUsersColumns"/>
- from scim_users
+ from scim_users s
</select>
<select id="findByScimUuid" parameterType="String" resultType="org.sonar.db.scim.ScimUserDto">
select
<include refid="scimUsersColumns"/>
- from scim_users
+ from scim_users s
where
scim_uuid = #{scimUserUuid,jdbcType=VARCHAR}
</select>
<select id="selectByLogins" parameterType="string" resultType="User">
select
<include refid="scimUsersColumns"/>
- from scim_users
+ from scim_users s
where
scim_uuid in
<foreach collection="list" open="(" close=")" item="login" separator=",">
<select id="findByUserUuid" parameterType="String" resultType="org.sonar.db.scim.ScimUserDto">
select
<include refid="scimUsersColumns"/>
- from scim_users
+ from scim_users s
where
user_uuid = #{userUuid,jdbcType=VARCHAR}
</select>
<sql id="sqlSelectByQuery">
from scim_users s
inner join users u on u.uuid=s.user_uuid
+ <if test="query.groupUuid != null">
+ left join groups_users gu on gu.user_uuid = u.uuid
+ </if>
where 1=1
<if test="query.userName != null">
and lower(u.external_id) like lower(#{query.userName,jdbcType=VARCHAR}) escape '/'
#{userUuid, jdbcType=VARCHAR}
</foreach>
</if>
+ <if test="query.groupUuid != null">
+ and gu.group_uuid = #{query.groupUuid, jdbcType=VARCHAR}
+ </if>
</sql>
<delete id="deleteByUserUuid" parameterType="String">