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