From 0d1a6be87f276abd0a2f70dee351dd775a94e140 Mon Sep 17 00:00:00 2001 From: Lukas Malte Monnerjahn <lukasmalte.monnerjahn@dataport.de> Date: Mon, 14 Oct 2024 10:40:06 +0200 Subject: [PATCH] OZG-6897 map organisations einheit ids tests --- .../de/ozgcloud/user/UserResourceMapper.java | 4 + .../user/GroupRepresentationTestFactory.java | 9 +++ .../ozgcloud/user/UserResourceMapperTest.java | 74 +++++++++++++++++-- 3 files changed, 81 insertions(+), 6 deletions(-) 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 52f53b25..51b2a9f5 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 @@ -92,6 +92,10 @@ public abstract class UserResourceMapper { .toList(); } + private List<String> getOrganisationsEinheitIdsFromUser(UserResource userRes) { + return List.of(); + } + private Map<String, List<String>> mapGroup(GroupRepresentation group) { var groupFromRealm = realm.getGroupByPath(group.getPath()); 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 fcc0dd78..091dd033 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 @@ -43,4 +43,13 @@ class GroupRepresentationTestFactory { List.of(organisationEinheitId))); return groupRepresentation; } + + public static GroupRepresentation createByPathAndOrganisationEinheitIds(String groupPath, List<String> organisationEinheitIds) { + var groupRepresentation = new GroupRepresentation(); + groupRepresentation.setName(groupPath); + groupRepresentation.setPath(groupPath); + groupRepresentation.setAttributes(Map.of(UserResourceMapperTest.ORGANISATIONS_EINHEIT_ID_KEY, + 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 d50c9ee0..1c2218a1 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 @@ -39,11 +39,13 @@ 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; import org.keycloak.representations.idm.MappingsRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.mapstruct.factory.Mappers; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import de.ozgcloud.user.keycloak.KeycloakApiProperties; @@ -55,9 +57,7 @@ class UserResourceMapperTest { static final String GROUP_1_PATH = "/group1"; static final String GROUP_2_PATH = "/group2"; - static final Map<String, List<String>> ATTRIBUTES_1 = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_1)); - static final Map<String, List<String>> ATTRIBUTES_2 = Map.of(ORGANISATIONS_EINHEIT_ID_KEY, List.of(ORGANISATIONS_EINHEIT_ID_2)); - + @Spy @InjectMocks UserResourceMapper mapper = Mappers.getMapper(UserResourceMapper.class); @@ -67,6 +67,8 @@ class UserResourceMapperTest { @Mock RealmResource realm; + private UserResource userResource; + @DisplayName("To kop user") @Nested class TestToKopUser { @@ -81,6 +83,8 @@ class UserResourceMapperTest { when(properties.ldapIdKey()).thenReturn("LDAP_ID"); when(properties.organisationsEinheitIdKey()).thenReturn("organisationseinheitId"); when(properties.client()).thenReturn("alfa"); + + userResource = UserResourceTestFactory.create(); } @Test @@ -139,9 +143,9 @@ class UserResourceMapperTest { @Test void shouldMapOrganisationsEinheitIds() { - var user = toKopUser(); + toKopUser(); - assertThat(user.getOrganisationsEinheitIds()).isNotEmpty().contains(ORGANISATIONS_EINHEIT_ID_1); + verify(mapper).mapOrganisationsEinheitIds(userResource); } @Test @@ -190,7 +194,7 @@ class UserResourceMapperTest { } private User toKopUser() { - return toKopUser(UserResourceTestFactory.create()); + return toKopUser(userResource); } private User toKopUser(UserResource userResource) { @@ -198,6 +202,64 @@ class UserResourceMapperTest { } } + @DisplayName("Map organisations einheit ids") + @Nested + class TestMapOrganisationsEinheitIds { + private UserResource userResource; + + 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_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() { + when(properties.organisationsEinheitIdKey()).thenReturn(ORGANISATIONS_EINHEIT_ID_KEY); + } + + @Test + void shouldMapOrganisationsEinheitIdsFromSingleGroup() { + userResource = UserResourceTestFactory.createWithGroups(List.of(group1)); + when(realm.getGroupByPath(GROUP_1_PATH)).thenReturn(group1); + + var result = mapper.mapOrganisationsEinheitIds(userResource); + + assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_3); + } + + @Test + void shouldMapOrganisationsEinheitIdsFromGroups() { + userResource = UserResourceTestFactory.createWithGroups(List.of(group1, group2)); + when(realm.getGroupByPath(GROUP_1_PATH)).thenReturn(group1); + when(realm.getGroupByPath(GROUP_2_PATH)).thenReturn(group2); + + var result = mapper.mapOrganisationsEinheitIds(userResource); + + assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2, ORGANISATIONS_EINHEIT_ID_3); + } + + @Test + void shouldMapOrganisationsEinheitIdsFromUser() { + userResource = UserResourceTestFactory.createWithAttributes(attributes); + + var result = mapper.mapOrganisationsEinheitIds(userResource); + + assertThat(result).containsExactlyInAnyOrder(ORGANISATIONS_EINHEIT_ID_1, ORGANISATIONS_EINHEIT_ID_2); + } + + @Test + void shouldReturnEmptyIfNoOrganisationsEinheitIds() { + userResource = UserResourceTestFactory.create(); + + var result = mapper.mapOrganisationsEinheitIds(userResource); + + assertThat(result).isEmpty(); + } + } + @DisplayName("Get client roles") @Nested class TestGetClientRoles { -- GitLab