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 52f53b25185c18d5c163bdb4ff695a0adc353266..51b2a9f56ee1b05bf590fad51da0f02b0f242dc7 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 fcc0dd786ad938ab75905d6b23e6efbde16cb725..091dd033210f9fa498e2b303adc8de3b465548e4 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 d50c9ee0fca676dfa08335cc4d7fc16ff3f8c519..1c2218a1ce66485fe8b554edb88bf38f3214e961 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 {