diff --git a/user-manager-server/src/main/java/de/ozgcloud/user/UserResourceMapper.java b/user-manager-server/src/main/java/de/ozgcloud/user/UserResourceMapper.java index ba7ca20c6921171be74ff1a758202e4cb35b5e8a..65826fba33a2f1ba591163c88bcffd77431bea42 100644 --- a/user-manager-server/src/main/java/de/ozgcloud/user/UserResourceMapper.java +++ b/user-manager-server/src/main/java/de/ozgcloud/user/UserResourceMapper.java @@ -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( - getOrganisationsEinheitIdsFromGroups(groups), - getOrganisationsEinheitIdsFromUser(userRes) - ) + 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()); + } } diff --git a/user-manager-server/src/test/java/de/ozgcloud/user/GroupRepresentationTestFactory.java b/user-manager-server/src/test/java/de/ozgcloud/user/GroupRepresentationTestFactory.java index 617f124641d8366a8785c3951a3a6ea5fed7c201..ef6f392a0ca35993bd50422df19a42ac3a69367b 100644 --- a/user-manager-server/src/test/java/de/ozgcloud/user/GroupRepresentationTestFactory.java +++ b/user-manager-server/src/test/java/de/ozgcloud/user/GroupRepresentationTestFactory.java @@ -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; } } diff --git a/user-manager-server/src/test/java/de/ozgcloud/user/UserResourceMapperTest.java b/user-manager-server/src/test/java/de/ozgcloud/user/UserResourceMapperTest.java index ef790b9a137fd722f8a67552fa915e2ed28a8055..40a505bec362d80c4ef168875cec373729a453de 100644 --- a/user-manager-server/src/test/java/de/ozgcloud/user/UserResourceMapperTest.java +++ b/user-manager-server/src/test/java/de/ozgcloud/user/UserResourceMapperTest.java @@ -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);