From 0d4e6b043a1d9f595e891a5e46e68a25e86b6cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Risue=C3=B1o?= Date: Fri, 4 Sep 2026 14:35:22 +0200 Subject: [PATCH] Add fix and tests --- .../services/RolesWhitelistedService.java | 9 ++++--- .../services/RolesWhitelistedServiceTest.java | 24 ++++++++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedService.java b/src/main/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedService.java index 9b17127..77304c9 100644 --- a/src/main/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedService.java +++ b/src/main/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedService.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; @Service @@ -37,9 +38,11 @@ public List resolveWhitelistedRolesForCatalogItemId(String catalogItemId Optional roles = catalogServiceAdapter.getYamlEntity(pathAt, RolesWhitelisted.class); - return roles.orElseThrow(() -> new InvalidEntityException("Invalid RolesWhitelisted.yaml file, path: %s".formatted(pathAt.getPathAt()))) - .getRoles().entrySet().stream() - .filter(role -> role.getValue().contains(itemSlug.toString())) + return roles.stream() + .map(RolesWhitelisted::getRoles) + .filter(Objects::nonNull) + .flatMap(map -> map.entrySet().stream()) + .filter(entry -> entry.getValue().contains(itemSlug.toString())) .map(Map.Entry::getKey) .toList(); } diff --git a/src/test/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedServiceTest.java b/src/test/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedServiceTest.java index 5b2fa83..a7d0594 100644 --- a/src/test/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedServiceTest.java +++ b/src/test/java/org/opendevstack/component_catalog/server/services/RolesWhitelistedServiceTest.java @@ -125,24 +125,32 @@ void givenInvalidCatalogItemId_whenResolveWhitelistedRoles_thenThrowsInvalidEnti verifyNoInteractions(bitbucketService, provisionerActionsConfiguration); } - @Test - void givenMissingRolesWhitelistedFile_whenResolveWhitelistedRoles_thenThrowsInvalidEntityException() + @Test + void givenRolesWhitelistedWithNullRoles_whenResolveWhitelistedRoles_thenReturnsEmptyList() throws InvalidIdException { // given var catalogItemId = "catalog-item-id"; var catalogItemPathAt = mock(BitbucketPathAt.class); + when(catalogItemPathAt.getProjectKey()).thenReturn("MYPROJECT"); when(catalogItemPathAt.getRepoSlug()).thenReturn("my-repo"); when(catalogServiceAdapter.bitbucketPathAtFromId(catalogItemId)).thenReturn(catalogItemPathAt); + configureRolesWhitelistedPath(); + + var rolesWhitelisted = RolesWhitelisted.builder() + .roles(null) + .build(); + when(catalogServiceAdapter.getYamlEntity(any(BitbucketPathAt.class), eq(RolesWhitelisted.class))) - .thenReturn(Optional.empty()); + .thenReturn(Optional.of(rolesWhitelisted)); - // when // then - assertThatThrownBy(() -> rolesWhitelistedService.resolveWhitelistedRolesForCatalogItemId(catalogItemId)) - .isInstanceOf(InvalidEntityException.class) - .hasMessageContaining("Invalid RolesWhitelisted.yaml file, path: projects/PROVISIONER/repos/"); - } + // when + var result = rolesWhitelistedService.resolveWhitelistedRolesForCatalogItemId(catalogItemId); + + // then + assertThat(result).isEmpty(); + } private void configureRolesWhitelistedPath() { when(provisionerActionsConfiguration.getProjectKey()).thenReturn("PROVISIONER");