Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
Expand All @@ -37,9 +38,11 @@ public List<String> resolveWhitelistedRolesForCatalogItemId(String catalogItemId

Optional<RolesWhitelisted> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading