Skip to content
Snippets Groups Projects
Commit fc458932 authored by Lukas Malte Monnerjahn's avatar Lukas Malte Monnerjahn
Browse files

OZG-6897 clean up

parent 6f25d3fc
Branches
Tags
No related merge requests found
......@@ -26,12 +26,12 @@ package de.ozgcloud.user;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.inject.Inject;
......@@ -79,14 +79,13 @@ public abstract class UserResourceMapper {
Set<String> mapOrganisationsEinheitIds(UserResource userRes) {
var groups = userRes.groups();
var organisationsEinheitIds = Stream.concat(
return Stream.concat(
getOrganisationsEinheitIdsFromGroups(groups),
getOrganisationsEinheitIdsFromUser(userRes)
)
.filter(Objects::nonNull)
.filter(oeId -> !oeId.isBlank())
.toList();
return new HashSet<>(organisationsEinheitIds);
.collect(Collectors.toSet());
}
private Stream<String> getOrganisationsEinheitIdsFromGroups(List<GroupRepresentation> groups) {
......@@ -99,8 +98,8 @@ public abstract class UserResourceMapper {
}
private Stream<String> getOrganisationsEinheitIdsFromUser(UserResource userRes) {
return Optional.ofNullable(userRes.toRepresentation().getAttributes().get(properties.organisationsEinheitIdKey()))
.orElse(List.of())
return Optional.ofNullable(getUserAttributes(userRes).get(properties.organisationsEinheitIdKey()))
.orElse(Collections.emptyList())
.stream();
}
......@@ -159,4 +158,8 @@ public abstract class UserResourceMapper {
return String.join(" ", Stream.of(userRes.toRepresentation().getLastName(), userRes.toRepresentation().getFirstName())
.filter(Objects::nonNull).toArray(String[]::new));
}
private Map<String, List<String>> getUserAttributes(UserResource userResource) {
return Optional.ofNullable(userResource.toRepresentation().getAttributes()).orElse(Map.of());
}
}
......@@ -35,16 +35,12 @@ class GroupRepresentationTestFactory {
return group;
}
public static GroupRepresentation createByPathAndOrganisationEinheitId(String groupPath, String organisationEinheitId) {
return createByPathAndOrganisationEinheitIds(groupPath, List.of(organisationEinheitId));
}
public static GroupRepresentation createByPathAndOrganisationEinheitIds(String groupPath, List<String> organisationEinheitIds) {
public static GroupRepresentation createByPathAndOrganisationEinheitIds(String groupPath, String... organisationEinheitIds) {
var groupRepresentation = new GroupRepresentation();
groupRepresentation.setName(groupPath);
groupRepresentation.setPath(groupPath);
groupRepresentation.setAttributes(Map.of(UserResourceMapperTest.ORGANISATIONS_EINHEIT_ID_KEY,
organisationEinheitIds));
List.of(organisationEinheitIds)));
return groupRepresentation;
}
}
......@@ -36,7 +36,6 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.RoleMappingResource;
import org.keycloak.admin.client.resource.RoleScopeResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientMappingsRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
......@@ -79,7 +78,7 @@ class UserResourceMapperTest {
when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(properties.client()).thenReturn("alfa");
when(realm.getGroupByPath(GROUP_1_PATH))
.thenReturn(GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(GROUP_1_PATH, ORGANISATIONS_EINHEIT_ID_1));
.thenReturn(GroupRepresentationTestFactory.createByPathAndOrganisationEinheitIds(GROUP_1_PATH, ORGANISATIONS_EINHEIT_ID_1));
when(properties.ldapIdKey()).thenReturn("LDAP_ID");
when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId");
when(properties.client()).thenReturn("alfa");
......@@ -148,23 +147,6 @@ class UserResourceMapperTest {
verify(mapper).mapOrganisationsEinheitIds(userResource);
}
@Test
void shouldMapMultipleOrganisationsEinheitIds() {
var groupRepresentation = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(GROUP_2_PATH,
ORGANISATIONS_EINHEIT_ID_2);
when(realm.getGroupByPath(GROUP_2_PATH)).thenReturn(groupRepresentation);
var user = toKopUser(buildUserResourceWithGroups());
assertThat(user.getOrganisationsEinheitIds()).isNotEmpty().hasSize(2).contains(ORGANISATIONS_EINHEIT_ID_2);
}
private UserResource buildUserResourceWithGroups() {
return UserResourceTestFactory.createWithGroups(List.of(
GroupRepresentationTestFactory.createGroup(UserResourceMapperTest.GROUP_1_PATH),
GroupRepresentationTestFactory.createGroup(UserResourceMapperTest.GROUP_2_PATH)));
}
@Test
void shouldMapRoles() {
var user = toKopUser();
......@@ -209,11 +191,9 @@ class UserResourceMapperTest {
static final String ORGANISATIONS_EINHEIT_ID_3 = "6287";
private final GroupRepresentation group1 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitIds(
GROUP_1_PATH, List.of(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_3));
private final GroupRepresentation group2 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitId(
GROUP_1_PATH, ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_3);
private final GroupRepresentation group2 = GroupRepresentationTestFactory.createByPathAndOrganisationEinheitIds(
GROUP_2_PATH, ORGANISATIONS_EINHEIT_ID_2);
private final Map<String, List<String>> attributes = Map.of(
ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2));
@BeforeEach
void beforeEach() {
......@@ -238,11 +218,16 @@ class UserResourceMapperTest {
var result = mapper.mapOrganisationsEinheitIds(userResource);
assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2, ORGANISATIONS_EINHEIT_ID_3);
assertThat(result).containsExactlyInAnyOrder(
ORGANISATIONS_EINHEIT_ID_1,
ORGANISATIONS_EINHEIT_ID_2,
ORGANISATIONS_EINHEIT_ID_3
);
}
@Test
void shouldMapOrganisationsEinheitIdsFromUser() {
var attributes = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2));
userResource = UserResourceTestFactory.createWithAttributes(attributes);
var result = mapper.mapOrganisationsEinheitIds(userResource);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment