Skip to content

Commit

Permalink
review: customizable client
Browse files Browse the repository at this point in the history
  • Loading branch information
dimas-b committed Jan 10, 2025
1 parent 61dbd62 commit 7c44ea3
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,47 @@
*/
package org.apache.polaris.service.it.env;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.apache.polaris.service.it.ext.PolarisServerManagerLoader.polarisServerManager;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import java.util.Random;
import org.apache.iceberg.rest.RESTSerializers;
import org.apache.polaris.core.admin.model.PrincipalWithCredentials;

public final class PolarisClient implements AutoCloseable {
private final PolarisApiEndpoints endpoints;
private final Client client;
// Use an alphanumeric ID for widest compatibility in HTTP and SQL.
// Use MAX_RADIX for shorter output.
private final String clientId =
Long.toString(Math.abs(new Random().nextLong()), Character.MAX_RADIX);

private PolarisClient(PolarisApiEndpoints endpoints) {
this.endpoints = endpoints;

this.client = polarisServerManager().createClient();
}

public static PolarisClient polarisClient(PolarisApiEndpoints endpoints) {
return new PolarisClient(endpoints);
}

public static ObjectMapper buildObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.KebabCaseStrategy());
RESTSerializers.registerAll(mapper);

this.client =
ClientBuilder.newBuilder()
.readTimeout(5, MINUTES)
.connectTimeout(1, MINUTES)
.register(new JacksonJsonProvider(mapper))
.build();
return mapper;
}

public static PolarisClient polarisClient(PolarisApiEndpoints endpoints) {
return new PolarisClient(endpoints);
public String newEntityName(String hint) {
return polarisServerManager().transformEntityName(hint + "_" + clientId);
}

public ManagementApi managementApi(String authToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
package org.apache.polaris.service.it.ext;

import java.util.ServiceLoader;
import static org.apache.polaris.service.it.ext.PolarisServerManagerLoader.polarisServerManager;

import org.apache.polaris.service.it.env.ClientCredentials;
import org.apache.polaris.service.it.env.PolarisApiEndpoints;
import org.apache.polaris.service.it.env.Server;
Expand All @@ -34,11 +35,6 @@ public class PolarisIntegrationTestExtension implements ParameterResolver {
private static final Namespace NAMESPACE =
Namespace.create(PolarisIntegrationTestExtension.class);

private static final PolarisServerManager manager =
ServiceLoader.load(PolarisServerManager.class)
.findFirst()
.orElseThrow(() -> new IllegalStateException("PolarisServerManager not found"));

@Override
public boolean supportsParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
Expand Down Expand Up @@ -66,7 +62,7 @@ private Env env(ExtensionContext context) {
ExtensionContext classCtx = classContext(context);
ExtensionContext.Store store = classCtx.getStore(NAMESPACE);
return store.getOrComputeIfAbsent(
Env.class, (key) -> new Env(manager.serverForContext(classCtx)), Env.class);
Env.class, (key) -> new Env(polarisServerManager().serverForContext(classCtx)), Env.class);
}

private ExtensionContext classContext(ExtensionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
package org.apache.polaris.service.it.ext;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.apache.polaris.service.it.env.PolarisClient.buildObjectMapper;

import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import org.apache.polaris.service.it.env.Server;
import org.junit.jupiter.api.extension.ExtensionContext;

Expand All @@ -31,4 +37,26 @@ public interface PolarisServerManager {
* the argument to this call is closed.
*/
Server serverForContext(ExtensionContext context);

/** Create a new HTTP client for accessing the server targeted by tests. */
default Client createClient() {
return ClientBuilder.newBuilder()
.readTimeout(5, MINUTES)
.connectTimeout(1, MINUTES)
.register(new JacksonJsonProvider(buildObjectMapper()))
.build();
}

/**
* Transforms the name of an entity that tests need to create. Implementations may prepend of
* append text to the original name, but they should not alter the original name text or add any
* characters that have special meaning in Spark SQL, HTTP or Iceberg REST specifications.
*
* <p>This method will be called for all top-level entities (catalogs, principal, principal
* roles), but may not be called for secondary entities (such as catalog roles, namespaces,
* tables, etc.).
*/
default String transformEntityName(String name) {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.it.ext;

import java.util.ServiceLoader;

public final class PolarisServerManagerLoader {

private static final PolarisServerManager manager =
ServiceLoader.load(PolarisServerManager.class)
.findFirst()
.orElseThrow(() -> new IllegalStateException("PolarisServerManager not found"));

private PolarisServerManagerLoader() {}

public static PolarisServerManager polarisServerManager() {
return manager;
}
}
Loading

0 comments on commit 7c44ea3

Please sign in to comment.