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 f9f2486ea48654c5493876af69f760f9b1367daf..6dfc125640d0f379e2153d96f9c07ed511e7278b 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 @@ -93,6 +93,7 @@ public abstract class UserResourceMapper { .map(this::mapGroup) .filter(Objects::nonNull) .map(attributeMap -> attributeMap.get(properties.organisationsEinheitIdKey())) + .filter(Objects::nonNull) .flatMap(Collection::stream); } 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 ef6f392a0ca35993bd50422df19a42ac3a69367b..8be817f43c5f957db4e55ba5c1959b42ff75a07b 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 @@ -36,11 +36,15 @@ class GroupRepresentationTestFactory { } public static GroupRepresentation createByPathAndOrganisationEinheitIds(String groupPath, String... organisationEinheitIds) { + return createWithPathAndAttributes(groupPath, Map.of(UserResourceMapperTest.ORGANISATIONS_EINHEIT_ID_KEY, + List.of(organisationEinheitIds))); + } + + public static GroupRepresentation createWithPathAndAttributes(String groupPath, Map<String, List<String>> attributes) { var groupRepresentation = new GroupRepresentation(); groupRepresentation.setName(groupPath); groupRepresentation.setPath(groupPath); - groupRepresentation.setAttributes(Map.of(UserResourceMapperTest.ORGANISATIONS_EINHEIT_ID_KEY, - List.of(organisationEinheitIds))); + groupRepresentation.setAttributes(attributes); 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 effcd75641189d1acd52922498cabd394175d55f..beb45ba1b47c07cd6933a005b8bb07f446d0c624 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 @@ -56,6 +56,7 @@ class UserResourceMapperTest { static final String ORGANISATIONS_EINHEIT_ID_2 = "4711"; static final String GROUP_1_PATH = "/group1"; static final String GROUP_2_PATH = "/group2"; + static final String GROUP_3_PATH = "/group3"; @Spy @InjectMocks @@ -186,12 +187,25 @@ class UserResourceMapperTest { 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 GroupRepresentation group3 = GroupRepresentationTestFactory.createWithPathAndAttributes( + GROUP_3_PATH, Map.of()); @BeforeEach void beforeEach() { when(properties.organisationsEinheitIdKey()).thenReturn(ORGANISATIONS_EINHEIT_ID_KEY); } + @DisplayName("should map no organisations einheit id if group has none") + @Test + void shouldMapNoOrganisationsEinheitIdIfGroupHasNone() { + when(realm.getGroupByPath(GROUP_3_PATH)).thenReturn(group3); + var userResource = UserResourceTestFactory.createWithGroups(group3); + + var result = mapper.mapOrganisationsEinheitIds(userResource); + + assertThat(result).isEmpty(); + } + @Test void shouldMapOrganisationsEinheitIdsFromSingleGroup() { var userResource = UserResourceTestFactory.createWithGroups(group1); @@ -204,9 +218,10 @@ class UserResourceMapperTest { @Test void shouldMapOrganisationsEinheitIdsFromGroups() { - var userResource = UserResourceTestFactory.createWithGroups(group1, group2); + var userResource = UserResourceTestFactory.createWithGroups(group1, group2, group3); when(realm.getGroupByPath(GROUP_1_PATH)).thenReturn(group1); when(realm.getGroupByPath(GROUP_2_PATH)).thenReturn(group2); + when(realm.getGroupByPath(GROUP_3_PATH)).thenReturn(group3); var result = mapper.mapOrganisationsEinheitIds(userResource);