From 56f75a7534c5bc6b7b1f578db465cd8f5ffa71de Mon Sep 17 00:00:00 2001 From: Kamiel Ahmadpour Date: Tue, 29 Oct 2024 22:23:32 +0000 Subject: [PATCH] fix: remove warning when role id exist see: https://gravitee.atlassian.net/browse/GKO-744 --- .../ValidateCRDMembersDomainService.java | 16 ++++++++++----- .../ValidateCRDMembersDomainServiceTest.java | 20 ++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/apim/core/member/domain_service/ValidateCRDMembersDomainService.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/apim/core/member/domain_service/ValidateCRDMembersDomainService.java index 14e964f7892..bb9343f54cd 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/apim/core/member/domain_service/ValidateCRDMembersDomainService.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/apim/core/member/domain_service/ValidateCRDMembersDomainService.java @@ -32,6 +32,7 @@ import java.util.HashSet; import java.util.Optional; import java.util.Set; +import java.util.UUID; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -101,11 +102,16 @@ private void validateMemberRole(Input input, Set sanitized, ArrayList private Optional findRole(String organizationId, MembershipReferenceType scope, String role) { var context = new ReferenceContext(ORGANIZATION, organizationId); - return switch (scope) { - case API -> roleQueryService.findApiRole(role, context); - case APPLICATION -> roleQueryService.findApplicationRole(role, context); - default -> throw new TechnicalDomainException(String.format("Role scope [%s] is not supported", scope)); - }; + try { + UUID roleId = UUID.fromString(role); + return roleQueryService.findByIds(Set.of(roleId.toString())).stream().findFirst(); + } catch (IllegalArgumentException e) { + return switch (scope) { + case API -> roleQueryService.findApiRole(role, context); + case APPLICATION -> roleQueryService.findApplicationRole(role, context); + default -> throw new TechnicalDomainException(String.format("Role scope [%s] is not supported", scope)); + }; + } } private void validatePrimaryOwner(Input input, Set sanitized, ArrayList errors) { diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/apim/core/api/domain_service/ValidateCRDMembersDomainServiceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/apim/core/api/domain_service/ValidateCRDMembersDomainServiceTest.java index 5637fa3edb3..68a0d589f79 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/apim/core/api/domain_service/ValidateCRDMembersDomainServiceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/apim/core/api/domain_service/ValidateCRDMembersDomainServiceTest.java @@ -15,7 +15,6 @@ */ package io.gravitee.apim.core.api.domain_service; -import static io.gravitee.apim.core.member.model.SystemRole.PRIMARY_OWNER; import static org.assertj.core.api.Assertions.assertThat; import inmemory.RoleQueryServiceInMemory; @@ -30,6 +29,7 @@ import io.gravitee.apim.core.validation.Validator; import java.util.List; import java.util.Set; +import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -42,6 +42,7 @@ class ValidateCRDMembersDomainServiceTest { static final String USER_SOURCE = "MEMORY"; static final String USER_ID = "USER"; static final String ACTOR_USER_ID = "ACTOR"; + static final String ROLE_ID = UUID.randomUUID().toString(); static final AuditInfo AUDIT_INFO = AuditInfo .builder() @@ -70,7 +71,7 @@ void setUp() { .name("USER") .referenceType(Role.ReferenceType.ORGANIZATION) .referenceId(ORG_ID) - .id("user_role_id") + .id(ROLE_ID) .scope(Role.Scope.APPLICATION) .build(), Role @@ -78,7 +79,7 @@ void setUp() { .name("USER") .referenceType(Role.ReferenceType.ORGANIZATION) .referenceId(ORG_ID) - .id("user_role_id") + .id(ROLE_ID) .scope(Role.Scope.API) .build(), Role @@ -149,6 +150,19 @@ void should_return_warning_with_unknown_member_role(MembershipReferenceType refe ); } + @ParameterizedTest + @EnumSource(value = MembershipReferenceType.class, names = { "APPLICATION", "API" }) + void should_not_return_warning_with_known_member_role(MembershipReferenceType referenceType) { + var members = Set.of(MemberCRD.builder().source(USER_SOURCE).sourceId(USER_ID).role(ROLE_ID).build()); + var expectedMembers = Set.copyOf(members); + var result = cut.validateAndSanitize(new ValidateCRDMembersDomainService.Input(AUDIT_INFO, "", referenceType, members)); + + result.peek( + sanitized -> assertThat(Set.copyOf(sanitized.members())).isEqualTo(expectedMembers), + errors -> assertThat(errors).isEmpty() + ); + } + @ParameterizedTest @EnumSource(value = MembershipReferenceType.class, names = { "APPLICATION", "API" }) void should_return_error_with_primary_owner_role(MembershipReferenceType referenceType) {