Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove warning when role id exist #9754

Merged
merged 1 commit into from
Oct 30, 2024
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 @@ -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;

Expand Down Expand Up @@ -101,11 +102,16 @@ private void validateMemberRole(Input input, Set<MemberCRD> sanitized, ArrayList

private Optional<Role> 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<MemberCRD> sanitized, ArrayList<Error> errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -70,15 +71,15 @@ void setUp() {
.name("USER")
.referenceType(Role.ReferenceType.ORGANIZATION)
.referenceId(ORG_ID)
.id("user_role_id")
.id(ROLE_ID)
.scope(Role.Scope.APPLICATION)
.build(),
Role
.builder()
.name("USER")
.referenceType(Role.ReferenceType.ORGANIZATION)
.referenceId(ORG_ID)
.id("user_role_id")
.id(ROLE_ID)
.scope(Role.Scope.API)
.build(),
Role
Expand Down Expand Up @@ -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) {
Expand Down