private Resolver resolveDriver()

in polaris-core/src/testFixtures/java/org/apache/polaris/core/persistence/BaseResolverTest.java [717:913]


  private Resolver resolveDriver(
      InMemoryEntityCache cache,
      Set<String> principalRolesScope,
      String principalName,
      boolean isPrincipalNameOptional,
      String principalRoleName,
      String catalogName,
      String catalogRoleName,
      ResolverPath path,
      List<ResolverPath> paths,
      ResolverStatus.StatusEnum expectedStatus,
      Set<String> expectedActivatedCatalogRoles) {

    // if null we expect success
    if (expectedStatus == null) {
      expectedStatus = ResolverStatus.StatusEnum.SUCCESS;
    }

    // allocate resolver
    Resolver resolver = allocateResolver(cache, principalRolesScope, catalogName);

    // principal name?
    if (principalName != null) {
      if (isPrincipalNameOptional) {
        resolver.addOptionalEntityByName(PolarisEntityType.PRINCIPAL, principalName);
      } else {
        resolver.addEntityByName(PolarisEntityType.PRINCIPAL, principalName);
      }
    }

    // add principal role if one passed-in
    if (principalRoleName != null) {
      resolver.addEntityByName(PolarisEntityType.PRINCIPAL_ROLE, principalRoleName);
    }

    // add catalog role if one passed-in
    if (catalogRoleName != null) {
      resolver.addEntityByName(PolarisEntityType.CATALOG_ROLE, catalogRoleName);
    }

    // add all paths
    if (path != null) {
      resolver.addPath(path);
    } else if (paths != null) {
      paths.forEach(resolver::addPath);
    }

    // done, run resolve
    ResolverStatus status = resolver.resolveAll();

    // we expect success unless a status
    Assertions.assertThat(status).isNotNull();
    Assertions.assertThat(status.getStatus()).isEqualTo(expectedStatus);

    // validate if status is success
    if (status.getStatus() == ResolverStatus.StatusEnum.SUCCESS) {

      // the principal does not exist, check that this is the case
      if (principalName != null) {
        // see if the principal exists
        EntityResult result =
            metaStoreManager()
                .readEntityByName(
                    callCtx(),
                    null,
                    PolarisEntityType.PRINCIPAL,
                    PolarisEntitySubType.NULL_SUBTYPE,
                    principalName);
        // if found, ensure properly resolved
        if (result.getEntity() != null) {
          // the principal exist, check that this is the case
          this.ensureResolved(
              resolver.getResolvedEntity(PolarisEntityType.PRINCIPAL, principalName),
              PolarisEntityType.PRINCIPAL,
              principalName);
        } else {
          // principal was optional
          Assertions.assertThat(isPrincipalNameOptional).isTrue();
          // not found
          Assertions.assertThat(
                  resolver.getResolvedEntity(PolarisEntityType.PRINCIPAL, principalName))
              .isNull();
        }
      }

      // validate that the correct set if principal roles have been activated
      List<ResolvedPolarisEntity> principalRolesResolved =
          resolver.getResolvedCallerPrincipalRoles();
      principalRolesResolved.sort(Comparator.comparing(p -> p.getEntity().getName()));

      // expect two principal roles if not scoped
      int expectedSize;
      if (principalRolesScope != null) {
        expectedSize = 0;
        for (String pr : principalRolesScope) {
          if (pr.equals("PR1") || pr.equals("PR2")) {
            expectedSize++;
          }
        }
      } else {
        // both PR1 and PR2
        expectedSize = 2;
      }

      // ensure the right set of principal roles were activated
      Assertions.assertThat(principalRolesResolved).hasSize(expectedSize);

      // expect either PR1 and PR2
      for (ResolvedPolarisEntity principalRoleResolved : principalRolesResolved) {
        Assertions.assertThat(principalRoleResolved).isNotNull();
        Assertions.assertThat(principalRoleResolved.getEntity()).isNotNull();
        String roleName = principalRoleResolved.getEntity().getName();

        // should be either PR1 or PR2
        Assertions.assertThat(roleName.equals("PR1") || roleName.equals("PR2")).isTrue();

        // ensure they are PR1 and PR2
        this.ensureResolved(principalRoleResolved, PolarisEntityType.PRINCIPAL_ROLE, roleName);
      }

      // if a principal role was passed-in, ensure it exists
      if (principalRoleName != null) {
        this.ensureResolved(
            resolver.getResolvedEntity(PolarisEntityType.PRINCIPAL_ROLE, principalRoleName),
            PolarisEntityType.PRINCIPAL_ROLE,
            principalRoleName);
      }

      // if a catalog was passed-in, ensure it exists
      if (catalogName != null) {
        ResolvedPolarisEntity catalogEntry =
            resolver.getResolvedEntity(PolarisEntityType.CATALOG, catalogName);
        Assertions.assertThat(catalogEntry).isNotNull();
        this.ensureResolved(catalogEntry, PolarisEntityType.CATALOG, catalogName);

        // if a catalog role was passed-in, ensure that it was properly resolved
        if (catalogRoleName != null) {
          ResolvedPolarisEntity catalogRoleEntry =
              resolver.getResolvedEntity(PolarisEntityType.CATALOG_ROLE, catalogRoleName);
          this.ensureResolved(
              catalogRoleEntry,
              List.of(catalogEntry.getEntity()),
              PolarisEntityType.CATALOG_ROLE,
              catalogRoleName);
        }

        // validate activated catalog roles
        Map<Long, ResolvedPolarisEntity> activatedCatalogs = resolver.getResolvedCatalogRoles();

        // if there is an expected set, ensure we have the same set
        if (expectedActivatedCatalogRoles != null) {
          Assertions.assertThat(activatedCatalogs).hasSameSizeAs(expectedActivatedCatalogRoles);
        }

        // process each of those
        for (ResolvedPolarisEntity resolvedActivatedCatalogEntry : activatedCatalogs.values()) {
          // must be in the expected list
          Assertions.assertThat(resolvedActivatedCatalogEntry).isNotNull();
          PolarisBaseEntity activatedCatalogRole = resolvedActivatedCatalogEntry.getEntity();
          Assertions.assertThat(activatedCatalogRole).isNotNull();
          // ensure well resolved
          this.ensureResolved(
              resolvedActivatedCatalogEntry,
              List.of(catalogEntry.getEntity()),
              PolarisEntityType.CATALOG_ROLE,
              activatedCatalogRole.getName());

          // in the set of expected catalog roles
          Assertions.assertThat(
                  expectedActivatedCatalogRoles == null
                      || expectedActivatedCatalogRoles.contains(activatedCatalogRole.getName()))
              .isTrue();
        }

        // resolve each path
        if (path != null || paths != null) {
          // path to validate
          List<ResolverPath> allPathsToCheck = (paths == null) ? List.of(path) : paths;

          // all resolved path
          List<List<ResolvedPolarisEntity>> allResolvedPaths = resolver.getResolvedPaths();

          // same size
          Assertions.assertThat(allResolvedPaths).hasSameSizeAs(allPathsToCheck);

          // check that each path was properly resolved
          int pathCount = 0;
          Iterator<ResolverPath> allPathsToCheckIt = allPathsToCheck.iterator();
          for (List<ResolvedPolarisEntity> resolvedPath : allResolvedPaths) {
            this.ensurePathResolved(
                pathCount++, catalogEntry.getEntity(), allPathsToCheckIt.next(), resolvedPath);
          }
        }
      }
    }
    return resolver;
  }