dimas-b commented on code in PR #2170: URL: https://github.com/apache/polaris/pull/2170#discussion_r2227053889
########## service/common/src/test/java/org/apache/polaris/service/admin/PolarisAdminServiceTest.java: ########## @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.polaris.service.admin; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import jakarta.ws.rs.core.SecurityContext; +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisDiagnostics; +import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal; +import org.apache.polaris.core.auth.PolarisAuthorizer; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntityType; +import org.apache.polaris.core.entity.PolarisPrivilege; +import org.apache.polaris.core.persistence.PolarisEntityManager; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.dao.entity.PrivilegeResult; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifest; +import org.apache.polaris.core.persistence.resolver.ResolverStatus; +import org.apache.polaris.core.secrets.UserSecretsManager; +import org.apache.polaris.service.config.ReservedProperties; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class PolarisAdminServiceTest { + + @Mock private CallContext callContext; + @Mock private PolarisCallContext polarisCallContext; + @Mock private PolarisDiagnostics polarisDiagnostics; + @Mock private PolarisEntityManager entityManager; + @Mock private PolarisMetaStoreManager metaStoreManager; + @Mock private UserSecretsManager userSecretsManager; + @Mock private SecurityContext securityContext; + @Mock private PolarisAuthorizer authorizer; + @Mock private ReservedProperties reservedProperties; + @Mock private AuthenticatedPolarisPrincipal authenticatedPrincipal; + @Mock private PolarisResolutionManifest resolutionManifest; + @Mock private PolarisResolvedPathWrapper resolvedPathWrapper; + + private PolarisAdminService adminService; + + @BeforeEach + void setUp() throws Exception { + MockitoAnnotations.openMocks(this); + when(securityContext.getUserPrincipal()).thenReturn(authenticatedPrincipal); + when(callContext.getPolarisCallContext()).thenReturn(polarisCallContext); + when(polarisCallContext.getDiagServices()).thenReturn(polarisDiagnostics); + + adminService = + new PolarisAdminService( + callContext, + entityManager, + metaStoreManager, + userSecretsManager, + securityContext, + authorizer, + reservedProperties); + } + + @Test + void testGrantPrivilegeOnNamespaceToRole() throws Exception { + String catalogName = "test-catalog"; + String catalogRoleName = "test-role"; + Namespace namespace = Namespace.of("existing-ns"); + PolarisPrivilege privilege = PolarisPrivilege.NAMESPACE_FULL_METADATA; + + setupSuccessfulNamespaceResolution(catalogName, catalogRoleName, namespace); + + PrivilegeResult successResult = mock(PrivilegeResult.class); + when(successResult.isSuccess()).thenReturn(true); + when(metaStoreManager.grantPrivilegeOnSecurableToRole(any(), any(), any(), any(), any())) + .thenReturn(successResult); + + boolean result = + adminService.grantPrivilegeOnNamespaceToRole( + catalogName, catalogRoleName, namespace, privilege); + + assertThat(result).isTrue(); + } + + @Test + void testGrantPrivilegeOnNamespaceToRole_ThrowsNamespaceNotFoundException() { + String catalogName = "test-catalog"; + String catalogRoleName = "test-role"; + Namespace namespace = Namespace.of("non-existent-ns"); + PolarisPrivilege privilege = PolarisPrivilege.NAMESPACE_FULL_METADATA; + + when(entityManager.prepareResolutionManifest(any(), any(), eq(catalogName))) + .thenReturn(resolutionManifest); + when(resolutionManifest.resolveAll()).thenReturn(createSuccessfulResolverStatus()); + + PolarisResolvedPathWrapper catalogRoleWrapper = mock(PolarisResolvedPathWrapper.class); + PolarisEntity catalogRoleEntity = createEntity(catalogRoleName, PolarisEntityType.CATALOG_ROLE); + when(catalogRoleWrapper.getRawLeafEntity()).thenReturn(catalogRoleEntity); + when(resolutionManifest.getResolvedPath(eq(catalogRoleName))).thenReturn(catalogRoleWrapper); + + when(resolutionManifest.getResolvedPath(eq(namespace))).thenReturn(null); + + assertThatThrownBy( + () -> + adminService.grantPrivilegeOnNamespaceToRole( + catalogName, catalogRoleName, namespace, privilege)) + .isInstanceOf(NotFoundException.class) + .hasMessageContaining("Namespace " + namespace + " not found"); + } + + @Test + void testGrantPrivilegeOnNamespaceToRole_IncompleteNamespaceThrowsNamespaceNotFoundException() + throws Exception { + String catalogName = "test-catalog"; + String catalogRoleName = "test-role"; + Namespace namespace = Namespace.of("incomplete-ns"); Review Comment: This is a single-level namespace... How is "incomplete" different from "non existent"? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org