void testDropEntities()

in polaris-core/src/testFixtures/java/org/apache/polaris/core/persistence/PolarisTestMetaStoreManager.java [2226:2405]


  void testDropEntities() {
    // create test catalog
    PolarisBaseEntity catalog = this.createTestCatalog("test");
    Assertions.assertThat(catalog).isNotNull();

    // find namespace N1/N2
    PolarisBaseEntity N1 =
        this.ensureExistsByName(List.of(catalog), PolarisEntityType.NAMESPACE, "N1");
    PolarisBaseEntity N1_N2 =
        this.ensureExistsByName(List.of(catalog, N1), PolarisEntityType.NAMESPACE, "N2");

    // attempt to drop the N1/N2 namespace. Will fail because not empty
    this.dropEntity(List.of(catalog, N1), N1_N2);

    // attempt to drop the N1/N4 namespace. Will succeed because empty
    PolarisBaseEntity N1_N4 =
        this.ensureExistsByName(List.of(catalog, N1), PolarisEntityType.NAMESPACE, "N4");
    this.dropEntity(List.of(catalog, N1), N1_N4);

    // find table N5/N6/T6
    PolarisBaseEntity N5 =
        this.ensureExistsByName(List.of(catalog), PolarisEntityType.NAMESPACE, "N5");
    PolarisBaseEntity N5_N6 =
        this.ensureExistsByName(List.of(catalog, N5), PolarisEntityType.NAMESPACE, "N6");
    PolarisBaseEntity T6 =
        this.ensureExistsByName(
            List.of(catalog, N5, N5_N6),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_TABLE,
            "T6");
    Assertions.assertThat(T6).isNotNull();

    // drop table N5/N6/T6
    this.dropEntity(List.of(catalog, N5, N5_N6), T6);

    // drop the catalog role R2
    PolarisBaseEntity R2 =
        this.ensureExistsByName(List.of(catalog), PolarisEntityType.CATALOG_ROLE, "R2");
    this.dropEntity(List.of(catalog), R2);

    // attempt to drop the entire catalog, should not work since not empty
    this.dropEntity(null, catalog);

    // now drop everything
    PolarisBaseEntity T1 =
        this.ensureExistsByName(
            List.of(catalog, N1, N1_N2),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_TABLE,
            "T1");
    this.dropEntity(List.of(catalog, N1, N1_N2), T1);
    PolarisBaseEntity T2 =
        this.ensureExistsByName(
            List.of(catalog, N1, N1_N2),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_TABLE,
            "T2");
    this.dropEntity(List.of(catalog, N1, N1_N2), T2);
    PolarisBaseEntity V1 =
        this.ensureExistsByName(
            List.of(catalog, N1, N1_N2),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_VIEW,
            "V1");
    this.dropEntity(List.of(catalog, N1, N1_N2), V1);
    this.dropEntity(List.of(catalog, N1), N1_N2);

    PolarisBaseEntity N1_N3 =
        this.ensureExistsByName(List.of(catalog, N1), PolarisEntityType.NAMESPACE, "N3");
    PolarisBaseEntity T3 =
        this.ensureExistsByName(
            List.of(catalog, N1, N1_N3),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_TABLE,
            "T3");
    this.dropEntity(List.of(catalog, N1, N1_N3), T3);
    PolarisBaseEntity V2 =
        this.ensureExistsByName(
            List.of(catalog, N1, N1_N3),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_VIEW,
            "V2");
    this.dropEntity(List.of(catalog, N1, N1_N3), V2);
    this.dropEntity(List.of(catalog, N1), N1_N3);

    PolarisBaseEntity T4 =
        this.ensureExistsByName(
            List.of(catalog, N1),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_TABLE,
            "T4");
    this.dropEntity(List.of(catalog, N1), T4);
    this.dropEntity(List.of(catalog), N1);

    PolarisBaseEntity T5 =
        this.ensureExistsByName(
            List.of(catalog, N5, N5_N6),
            PolarisEntityType.TABLE_LIKE,
            PolarisEntitySubType.ICEBERG_TABLE,
            "T5");
    this.dropEntity(List.of(catalog, N5, N5_N6), T5);
    this.dropEntity(List.of(catalog, N5), N5_N6);
    this.dropEntity(List.of(catalog), N5);

    PolarisBaseEntity N7 =
        this.ensureExistsByName(List.of(catalog), PolarisEntityType.NAMESPACE, "N7");
    PolarisBaseEntity N7_N8 =
        this.ensureExistsByName(
            List.of(catalog, N7),
            PolarisEntityType.NAMESPACE,
            PolarisEntitySubType.ANY_SUBTYPE,
            "N8");
    PolarisBaseEntity POL1 =
        this.ensureExistsByName(List.of(catalog, N7, N7_N8), PolarisEntityType.POLICY, "POL1");
    PolarisBaseEntity POL2 =
        this.ensureExistsByName(List.of(catalog, N7, N7_N8), PolarisEntityType.POLICY, "POL2");
    PolarisBaseEntity POL3 =
        this.ensureExistsByName(List.of(catalog, N7), PolarisEntityType.POLICY, "POL3");
    this.dropEntity(List.of(catalog, N7, N7_N8), POL1);
    this.dropEntity(List.of(catalog, N7, N7_N8), POL2);
    this.dropEntity(List.of(catalog, N7), POL3);
    this.dropEntity(List.of(catalog, N7), N7_N8);
    this.dropEntity(List.of(catalog), N7);

    // attempt to drop the catalog again, should fail because of role R1
    this.dropEntity(null, catalog);

    // catalog exists
    EntityResult catalogFound =
        polarisMetaStoreManager.readEntityByName(
            this.polarisCallContext,
            null,
            PolarisEntityType.CATALOG,
            PolarisEntitySubType.NULL_SUBTYPE,
            "test");
    // success and found
    Assertions.assertThat(catalogFound.isSuccess()).isTrue();
    Assertions.assertThat(catalogFound.getEntity()).isNotNull();

    // drop the last role
    PolarisBaseEntity R1 =
        this.ensureExistsByName(List.of(catalog), PolarisEntityType.CATALOG_ROLE, "R1");
    this.dropEntity(List.of(catalog), R1);

    // the catalog admin role cannot be dropped
    PolarisBaseEntity CATALOG_ADMIN =
        this.ensureExistsByName(
            List.of(catalog),
            PolarisEntityType.CATALOG_ROLE,
            PolarisEntityConstants.getNameOfCatalogAdminRole());
    this.dropEntity(List.of(catalog), CATALOG_ADMIN);
    // should be found since it is undroppable
    this.ensureExistsByName(
        List.of(catalog),
        PolarisEntityType.CATALOG_ROLE,
        PolarisEntityConstants.getNameOfCatalogAdminRole());

    // drop the catalog, should work now. The CATALOG_ADMIN role will be dropped too
    this.dropEntity(null, catalog);

    // catalog exists?
    catalogFound =
        polarisMetaStoreManager.readEntityByName(
            this.polarisCallContext,
            null,
            PolarisEntityType.CATALOG,
            PolarisEntitySubType.NULL_SUBTYPE,
            "test");
    // success and not found
    Assertions.assertThat(catalogFound.getReturnStatus())
        .isEqualTo(BaseResult.ReturnStatus.ENTITY_NOT_FOUND);

    // drop the principal role PR1
    PolarisBaseEntity PR1 = this.ensureExistsByName(null, PolarisEntityType.PRINCIPAL_ROLE, "PR1");
    this.dropEntity(null, PR1);

    // drop the principal role P1
    PolarisBaseEntity P1 = this.ensureExistsByName(null, PolarisEntityType.PRINCIPAL, "P1");
    this.dropEntity(null, P1);
  }