HonahX commented on code in PR #1665:
URL: https://github.com/apache/polaris/pull/1665#discussion_r2105502726


##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisPolicyServiceIntegrationTest.java:
##########
@@ -30,11 +30,7 @@
 import java.lang.reflect.Method;
 import java.net.URI;
 import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.UUID;
+import java.util.*;

Review Comment:
   This is probably due to the IDEA's default codestyle setting, I remember I 
have to manually change some config to avoid it automatically turns things into 
wildcard imports



##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisPolicyServiceIntegrationTest.java:
##########
@@ -333,6 +325,149 @@ public void testCreatePolicyWithInvalidName(String 
policyName) {
     }
   }
 
+  @Test
+  public void testCreatePolicyWithNonExistingNamespace() {
+    CreatePolicyRequest request =
+        CreatePolicyRequest.builder()
+            .setType(PredefinedPolicyTypes.DATA_COMPACTION.getName())
+            .setName(currentCatalogName)
+            .setDescription("test policy")
+            .setContent(EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT)
+            .build();
+    try (Response res =
+        policyApi
+            .request(
+                "polaris/v1/{cat}/namespaces/{ns}/policies",
+                Map.of("cat", currentCatalogName, "ns", "INVALID_NAMESPACE"))
+            .post(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Namespace does not exist: INVALID_NAMESPACE");
+    }
+  }
+
+  @Test
+  public void testAttachPolicyToNonExistingNamespace() {
+    restCatalog.createNamespace(NS1);
+    String ns = RESTUtil.encodeNamespace(NS1);
+    policyApi.createPolicy(
+        currentCatalogName,
+        NS1_P1,
+        PredefinedPolicyTypes.DATA_COMPACTION,
+        EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT,
+        "test policy");
+
+    Namespace invalidNamespace = Namespace.of("INVALID_NAMESPACE");
+    var invalidTarget =
+        new PolicyAttachmentTarget(
+            PolicyAttachmentTarget.TypeEnum.NAMESPACE, 
List.of(invalidNamespace.levels()));
+
+    AttachPolicyRequest request =
+        
AttachPolicyRequest.builder().setTarget(invalidTarget).setParameters(Map.of()).build();
+    try (Response res =
+        policyApi
+            .request(
+                
"polaris/v1/{cat}/namespaces/{ns}/policies/{policy-name}/mappings",
+                Map.of("cat", currentCatalogName, "ns", ns, "policy-name", 
NS1_P1.getName()))
+            .put(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Namespace does not exist: INVALID_NAMESPACE");
+    }
+    policyApi.dropPolicy(currentCatalogName, NS1_P1);
+  }
+
+  @Test
+  public void testAttachPolicyToNonExistingTable() {
+    restCatalog.createNamespace(NS1);
+    String ns = RESTUtil.encodeNamespace(NS1);
+
+    policyApi.createPolicy(
+        currentCatalogName,
+        NS1_P1,
+        PredefinedPolicyTypes.DATA_COMPACTION,
+        EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT,
+        "test policy");
+    TableIdentifier invalidTable = TableIdentifier.of(NS1, "INVALID_TABLE");
+    var invalidTarget =
+        new PolicyAttachmentTarget(
+            PolicyAttachmentTarget.TypeEnum.TABLE_LIKE,
+            List.of(invalidTable.toString().split("\\.")));
+
+    AttachPolicyRequest request =
+        
AttachPolicyRequest.builder().setTarget(invalidTarget).setParameters(Map.of()).build();
+    try (Response res =
+        policyApi
+            .request(
+                
"polaris/v1/{cat}/namespaces/{ns}/policies/{policy-name}/mappings",
+                Map.of("cat", currentCatalogName, "ns", ns, "policy-name", 
NS1_P1.getName()))
+            .put(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Table or view does not exist: NS1.INVALID_TABLE");
+    }
+    policyApi.dropPolicy(currentCatalogName, NS1_P1);
+  }
+
+  @Test
+  public void testDetachPolicyFromNonExistingNamespace() {
+    restCatalog.createNamespace(NS1);
+    String ns = RESTUtil.encodeNamespace(NS1);
+    policyApi.createPolicy(
+        currentCatalogName,
+        NS1_P1,
+        PredefinedPolicyTypes.DATA_COMPACTION,
+        EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT,
+        "test policy");
+    Namespace invalidNamespace = Namespace.of("INVALID_NAMESPACE");
+    var invalidTarget =
+        new PolicyAttachmentTarget(
+            PolicyAttachmentTarget.TypeEnum.NAMESPACE, 
List.of(invalidNamespace.levels()));
+
+    DetachPolicyRequest request = 
DetachPolicyRequest.builder().setTarget(invalidTarget).build();
+    try (Response res =
+        policyApi
+            .request(
+                
"polaris/v1/{cat}/namespaces/{ns}/policies/{policy-name}/mappings",
+                Map.of("cat", currentCatalogName, "ns", ns, "policy-name", 
NS1_P1.getName()))
+            .post(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Namespace does not exist: INVALID_NAMESPACE");
+    }
+    policyApi.dropPolicy(currentCatalogName, NS1_P1);
+  }
+
+  @Test
+  public void testDetachPolicyFromNonExistingTable() {
+    restCatalog.createNamespace(NS1);
+    String ns = RESTUtil.encodeNamespace(NS1);
+    policyApi.createPolicy(
+        currentCatalogName,
+        NS1_P1,
+        PredefinedPolicyTypes.DATA_COMPACTION,
+        EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT,
+        "test policy");
+    TableIdentifier invalidTable = TableIdentifier.of(NS1, "INVALID_TABLE");
+    var invalidTarget =
+        new PolicyAttachmentTarget(
+            PolicyAttachmentTarget.TypeEnum.TABLE_LIKE,
+            List.of(invalidTable.toString().split("\\.")));
+
+    DetachPolicyRequest request = 
DetachPolicyRequest.builder().setTarget(invalidTarget).build();
+    try (Response res =
+        policyApi
+            .request(
+                
"polaris/v1/{cat}/namespaces/{ns}/policies/{policy-name}/mappings",
+                Map.of("cat", currentCatalogName, "ns", ns, "policy-name", 
NS1_P1.getName()))
+            .post(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Table or view does not exist: NS1");

Review Comment:
   Is this the full error message? I think it should also contains the table 
name `INVALID_TABLE`?



##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisPolicyServiceIntegrationTest.java:
##########
@@ -333,6 +325,149 @@ public void testCreatePolicyWithInvalidName(String 
policyName) {
     }
   }
 
+  @Test
+  public void testCreatePolicyWithNonExistingNamespace() {
+    CreatePolicyRequest request =
+        CreatePolicyRequest.builder()
+            .setType(PredefinedPolicyTypes.DATA_COMPACTION.getName())
+            .setName(currentCatalogName)
+            .setDescription("test policy")
+            .setContent(EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT)
+            .build();
+    try (Response res =
+        policyApi
+            .request(
+                "polaris/v1/{cat}/namespaces/{ns}/policies",
+                Map.of("cat", currentCatalogName, "ns", "INVALID_NAMESPACE"))
+            .post(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Namespace does not exist: INVALID_NAMESPACE");
+    }
+  }
+
+  @Test
+  public void testAttachPolicyToNonExistingNamespace() {
+    restCatalog.createNamespace(NS1);
+    String ns = RESTUtil.encodeNamespace(NS1);
+    policyApi.createPolicy(
+        currentCatalogName,
+        NS1_P1,
+        PredefinedPolicyTypes.DATA_COMPACTION,
+        EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT,
+        "test policy");
+
+    Namespace invalidNamespace = Namespace.of("INVALID_NAMESPACE");
+    var invalidTarget =
+        new PolicyAttachmentTarget(
+            PolicyAttachmentTarget.TypeEnum.NAMESPACE, 
List.of(invalidNamespace.levels()));
+
+    AttachPolicyRequest request =
+        
AttachPolicyRequest.builder().setTarget(invalidTarget).setParameters(Map.of()).build();
+    try (Response res =
+        policyApi
+            .request(
+                
"polaris/v1/{cat}/namespaces/{ns}/policies/{policy-name}/mappings",
+                Map.of("cat", currentCatalogName, "ns", ns, "policy-name", 
NS1_P1.getName()))
+            .put(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Namespace does not exist: INVALID_NAMESPACE");
+    }
+    policyApi.dropPolicy(currentCatalogName, NS1_P1);
+  }
+
+  @Test
+  public void testAttachPolicyToNonExistingTable() {
+    restCatalog.createNamespace(NS1);
+    String ns = RESTUtil.encodeNamespace(NS1);
+
+    policyApi.createPolicy(
+        currentCatalogName,
+        NS1_P1,
+        PredefinedPolicyTypes.DATA_COMPACTION,
+        EXAMPLE_TABLE_MAINTENANCE_POLICY_CONTENT,
+        "test policy");
+    TableIdentifier invalidTable = TableIdentifier.of(NS1, "INVALID_TABLE");
+    var invalidTarget =
+        new PolicyAttachmentTarget(
+            PolicyAttachmentTarget.TypeEnum.TABLE_LIKE,
+            List.of(invalidTable.toString().split("\\.")));
+
+    AttachPolicyRequest request =
+        
AttachPolicyRequest.builder().setTarget(invalidTarget).setParameters(Map.of()).build();
+    try (Response res =
+        policyApi
+            .request(
+                
"polaris/v1/{cat}/namespaces/{ns}/policies/{policy-name}/mappings",
+                Map.of("cat", currentCatalogName, "ns", ns, "policy-name", 
NS1_P1.getName()))
+            .put(Entity.json(request))) {
+      
Assertions.assertThat(res.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+      Assertions.assertThat(res.readEntity(String.class))
+          .contains("Table or view does not exist: NS1.INVALID_TABLE");
+    }
+    policyApi.dropPolicy(currentCatalogName, NS1_P1);
+  }
+
+  @Test
+  public void testDetachPolicyFromNonExistingNamespace() {

Review Comment:
   This test is mainly to cover cases where users make invalid inputs in REST 
calls. For instance, when calling detachPolicy, a user might mistakenly try to 
detach a policy from a namespace that no longer exists. In such cases, we want 
to make sure Polaris returns a proper and clear error message.



-- 
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

Reply via email to