Skip to content

Commit

Permalink
Extract test client for HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Oct 23, 2024
1 parent 02468e0 commit 337d532
Show file tree
Hide file tree
Showing 8 changed files with 598 additions and 928 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.apache.polaris.core.entity.CatalogEntity;
import org.apache.polaris.core.entity.PolarisEntityConstants;
import org.apache.polaris.service.auth.BasePolarisAuthenticator;
import org.apache.polaris.service.catalog.PolarisTestClient;
import org.apache.polaris.service.config.PolarisApplicationConfig;
import org.apache.polaris.service.test.PolarisConnectionExtension;
import org.apache.polaris.service.test.PolarisRealm;
Expand Down Expand Up @@ -121,6 +122,7 @@ public class PolarisApplicationIntegrationTest {
private static SnowmanCredentialsExtension.SnowmanCredentials snowmanCredentials;
private static Path testDir;
private static String realm;
private static PolarisTestClient userClient;

@BeforeAll
public static void setup(
Expand All @@ -136,47 +138,22 @@ public static void setup(
PolarisApplicationIntegrationTest.userToken = userToken.token();
PolarisApplicationIntegrationTest.snowmanCredentials = snowmanCredentials;

userClient = new PolarisTestClient(EXT.client(), EXT.getLocalPort(), userToken.token(), realm);
PrincipalRole principalRole = new PrincipalRole(PRINCIPAL_ROLE_NAME);
try (Response createPrResponse =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/management/v1/principal-roles", EXT.getLocalPort()))
.request("application/json")
.header("Authorization", "Bearer " + userToken.token())
.header(REALM_PROPERTY_KEY, realm)
.post(Entity.json(principalRole))) {
try (Response createPrResponse = userClient.createPrincipalRole(principalRole)) {
assertThat(createPrResponse)
.returns(Response.Status.CREATED.getStatusCode(), Response::getStatus);
}

try (Response assignPrResponse =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/management/v1/principals/snowman/principal-roles",
EXT.getLocalPort()))
.request("application/json")
.header("Authorization", "Bearer " + PolarisApplicationIntegrationTest.userToken)
.header(REALM_PROPERTY_KEY, realm)
.put(Entity.json(principalRole))) {
try (Response assignPrResponse = userClient.assignPrincipalRole("snowman", principalRole)) {
assertThat(assignPrResponse)
.returns(Response.Status.CREATED.getStatusCode(), Response::getStatus);
}
}

@AfterAll
public static void deletePrincipalRole() {
EXT.client()
.target(
String.format(
"http://localhost:%d/api/management/v1/principal-roles/%s",
EXT.getLocalPort(), PRINCIPAL_ROLE_NAME))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.delete()
.close();
userClient.deletePrincipalRole(PRINCIPAL_ROLE_NAME).close();
}

/**
Expand Down Expand Up @@ -238,41 +215,17 @@ private static void createCatalog(
.setProperties(props)
.setStorageConfigInfo(storageConfig)
.build();
try (Response response =
EXT.client()
.target(
String.format("http://localhost:%d/api/management/v1/catalogs", EXT.getLocalPort()))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.post(Entity.json(catalog))) {
try (Response response = userClient.createCatalog(catalog)) {
assertThat(response).returns(Response.Status.CREATED.getStatusCode(), Response::getStatus);
}
try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/management/v1/catalogs/%s/catalog-roles/%s",
EXT.getLocalPort(),
catalogName,
PolarisEntityConstants.getNameOfCatalogAdminRole()))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.get()) {
userClient.getCatalogRole(
catalogName, PolarisEntityConstants.getNameOfCatalogAdminRole())) {
assertThat(response).returns(Response.Status.OK.getStatusCode(), Response::getStatus);
CatalogRole catalogRole = response.readEntity(CatalogRole.class);

try (Response assignResponse =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/management/v1/principal-roles/%s/catalog-roles/%s",
EXT.getLocalPort(), principalRoleName, catalogName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.put(Entity.json(catalogRole))) {
userClient.grantCatalogRoleToPrincipalRole(principalRoleName, catalogName, catalogRole)) {
assertThat(assignResponse)
.returns(Response.Status.CREATED.getStatusCode(), Response::getStatus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
*/
package org.apache.polaris.service.admin;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -36,9 +33,9 @@
import org.apache.polaris.core.admin.model.AwsStorageConfigInfo;
import org.apache.polaris.core.admin.model.Catalog;
import org.apache.polaris.core.admin.model.CatalogProperties;
import org.apache.polaris.core.admin.model.CreateCatalogRequest;
import org.apache.polaris.core.admin.model.StorageConfigInfo;
import org.apache.polaris.service.PolarisApplication;
import org.apache.polaris.service.catalog.PolarisTestClient;
import org.apache.polaris.service.config.PolarisApplicationConfig;
import org.apache.polaris.service.test.PolarisConnectionExtension;
import org.apache.polaris.service.test.PolarisRealm;
Expand All @@ -58,32 +55,23 @@ public class PolarisOverlappingCatalogTest {
ConfigOverride.config("server.adminConnectors[0].port", "0"),
// Block overlapping catalog paths:
ConfigOverride.config("featureConfiguration.ALLOW_OVERLAPPING_CATALOG_URLS", "false"));
private static String userToken;
private static String realm;
private static PolarisTestClient userClient;

@BeforeAll
public static void setup(
PolarisConnectionExtension.PolarisToken adminToken, @PolarisRealm String polarisRealm)
throws IOException {
userToken = adminToken.token();
realm = polarisRealm;
String userToken = adminToken.token();
userClient = new PolarisTestClient(EXT.client(), EXT.getLocalPort(), userToken, polarisRealm);

// Set up the database location
PolarisConnectionExtension.createTestDir(realm);
PolarisConnectionExtension.createTestDir(polarisRealm);
}

private Response createCatalog(String prefix, String defaultBaseLocation, boolean isExternal) {
return createCatalog(prefix, defaultBaseLocation, isExternal, new ArrayList<String>());
}

private static Invocation.Builder request() {
return EXT.client()
.target(String.format("http://localhost:%d/api/management/v1/catalogs", EXT.getLocalPort()))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm);
}

private Response createCatalog(
String prefix,
String defaultBaseLocation,
Expand Down Expand Up @@ -113,7 +101,7 @@ private Response createCatalog(
System.currentTimeMillis(),
1,
config);
try (Response response = request().post(Entity.json(new CreateCatalogRequest(catalog)))) {
try (Response response = userClient.createCatalog(catalog)) {
return response;
}
}
Expand Down
Loading

0 comments on commit 337d532

Please sign in to comment.