Skip to content

Commit 0d61180

Browse files
committed
review: customizable client
1 parent 369ea1d commit 0d61180

File tree

8 files changed

+223
-102
lines changed

8 files changed

+223
-102
lines changed

integration-tests/src/main/java/org/apache/polaris/service/it/env/PolarisClient.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,47 @@
1818
*/
1919
package org.apache.polaris.service.it.env;
2020

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

2323
import com.fasterxml.jackson.annotation.JsonAutoDetect;
2424
import com.fasterxml.jackson.annotation.PropertyAccessor;
2525
import com.fasterxml.jackson.databind.DeserializationFeature;
2626
import com.fasterxml.jackson.databind.ObjectMapper;
2727
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
28-
import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
2928
import jakarta.ws.rs.client.Client;
30-
import jakarta.ws.rs.client.ClientBuilder;
29+
import java.util.Random;
3130
import org.apache.iceberg.rest.RESTSerializers;
3231
import org.apache.polaris.core.admin.model.PrincipalWithCredentials;
3332

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

3841
private PolarisClient(PolarisApiEndpoints endpoints) {
3942
this.endpoints = endpoints;
4043

44+
this.client = polarisServerManager().createClient();
45+
}
46+
47+
public static PolarisClient polarisClient(PolarisApiEndpoints endpoints) {
48+
return new PolarisClient(endpoints);
49+
}
50+
51+
public static ObjectMapper buildObjectMapper() {
4152
ObjectMapper mapper = new ObjectMapper();
4253
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
4354
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
4455
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.KebabCaseStrategy());
4556
RESTSerializers.registerAll(mapper);
46-
47-
this.client =
48-
ClientBuilder.newBuilder()
49-
.readTimeout(5, MINUTES)
50-
.connectTimeout(1, MINUTES)
51-
.register(new JacksonJsonProvider(mapper))
52-
.build();
57+
return mapper;
5358
}
5459

55-
public static PolarisClient polarisClient(PolarisApiEndpoints endpoints) {
56-
return new PolarisClient(endpoints);
60+
public String newEntityName(String hint) {
61+
return polarisServerManager().transformEntityName(hint + "_" + clientId);
5762
}
5863

5964
public ManagementApi managementApi(String authToken) {

integration-tests/src/main/java/org/apache/polaris/service/it/ext/PolarisIntegrationTestExtension.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*/
1919
package org.apache.polaris.service.it.ext;
2020

21-
import java.util.ServiceLoader;
21+
import static org.apache.polaris.service.it.ext.PolarisServerManagerLoader.polarisServerManager;
22+
2223
import org.apache.polaris.service.it.env.ClientCredentials;
2324
import org.apache.polaris.service.it.env.PolarisApiEndpoints;
2425
import org.apache.polaris.service.it.env.Server;
@@ -34,11 +35,6 @@ public class PolarisIntegrationTestExtension implements ParameterResolver {
3435
private static final Namespace NAMESPACE =
3536
Namespace.create(PolarisIntegrationTestExtension.class);
3637

37-
private static final PolarisServerManager manager =
38-
ServiceLoader.load(PolarisServerManager.class)
39-
.findFirst()
40-
.orElseThrow(() -> new IllegalStateException("PolarisServerManager not found"));
41-
4238
@Override
4339
public boolean supportsParameter(
4440
ParameterContext parameterContext, ExtensionContext extensionContext)
@@ -66,7 +62,7 @@ private Env env(ExtensionContext context) {
6662
ExtensionContext classCtx = classContext(context);
6763
ExtensionContext.Store store = classCtx.getStore(NAMESPACE);
6864
return store.getOrComputeIfAbsent(
69-
Env.class, (key) -> new Env(manager.serverForContext(classCtx)), Env.class);
65+
Env.class, (key) -> new Env(polarisServerManager().serverForContext(classCtx)), Env.class);
7066
}
7167

7268
private ExtensionContext classContext(ExtensionContext context) {

integration-tests/src/main/java/org/apache/polaris/service/it/ext/PolarisServerManager.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
*/
1919
package org.apache.polaris.service.it.ext;
2020

21+
import static java.util.concurrent.TimeUnit.MINUTES;
22+
import static org.apache.polaris.service.it.env.PolarisClient.buildObjectMapper;
23+
24+
import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
25+
import jakarta.ws.rs.client.Client;
26+
import jakarta.ws.rs.client.ClientBuilder;
2127
import org.apache.polaris.service.it.env.Server;
2228
import org.junit.jupiter.api.extension.ExtensionContext;
2329

@@ -31,4 +37,26 @@ public interface PolarisServerManager {
3137
* the argument to this call is closed.
3238
*/
3339
Server serverForContext(ExtensionContext context);
40+
41+
/** Create a new HTTP client for accessing the server targeted by tests. */
42+
default Client createClient() {
43+
return ClientBuilder.newBuilder()
44+
.readTimeout(5, MINUTES)
45+
.connectTimeout(1, MINUTES)
46+
.register(new JacksonJsonProvider(buildObjectMapper()))
47+
.build();
48+
}
49+
50+
/**
51+
* Transforms the name of an entity that tests need to create. Implementations may prepend of
52+
* append text to the original name, but they should not alter the original name text or add any
53+
* characters that have special meaning in Spark SQL, HTTP or Iceberg REST specifications.
54+
*
55+
* <p>This method will be called for all top-level entities (catalogs, principal, principal
56+
* roles), but may not be called for secondary entities (such as catalog roles, namespaces,
57+
* tables, etc.).
58+
*/
59+
default String transformEntityName(String name) {
60+
return name;
61+
}
3462
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.service.it.ext;
20+
21+
import java.util.ServiceLoader;
22+
23+
public final class PolarisServerManagerLoader {
24+
25+
private static final PolarisServerManager manager =
26+
ServiceLoader.load(PolarisServerManager.class)
27+
.findFirst()
28+
.orElseThrow(() -> new IllegalStateException("PolarisServerManager not found"));
29+
30+
private PolarisServerManagerLoader() {}
31+
32+
public static PolarisServerManager polarisServerManager() {
33+
return manager;
34+
}
35+
}

0 commit comments

Comments
 (0)