diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java index 091e38fc2..4b2ba57a0 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java @@ -122,6 +122,11 @@ public static HugeClientBuilder builder(String url, String graphSpace, String gr return new HugeClientBuilder(url, graphSpace, graph); } + public static HugeClientBuilder builder(String url, String graphSpace, String graph, + boolean skipRequiredChecks) { + return new HugeClientBuilder(url, graphSpace, graph, skipRequiredChecks); + } + public static HugeClientBuilder builder(String url, String graph) { return new HugeClientBuilder(url, HugeClientBuilder.DEFAULT_GRAPHSPACE, graph); } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java index 9b9c03b9e..8438e8aeb 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java @@ -50,13 +50,26 @@ public class HugeClientBuilder { /** Set them null by default to keep compatibility with 'timeout' */ private Integer connectTimeout; private Integer readTimeout; + private final boolean skipRequiredChecks; public HugeClientBuilder(String url, String graphSpace, String graph) { - E.checkArgument(url != null && !url.isEmpty(), - "Expect a string value as the url parameter argument, but got: %s", url); - E.checkArgument(graph != null && !graph.isEmpty(), - "Expect a string value as the graph name parameter argument, but got: %s", - graph); + this(url, graphSpace, graph, false); + } + + public HugeClientBuilder(String url, String graphSpace, String graph, + boolean skipRequiredChecks) { + this.skipRequiredChecks = skipRequiredChecks; + + if (!skipRequiredChecks) { + E.checkArgument(url != null && !url.isEmpty(), + "Expect a string value as the url parameter " + + "argument, but got: %s", url); + E.checkArgument(graph != null && !graph.isEmpty(), + "Expect a string value as the graph name " + + "parameter argument, but got: %s", + graph); + } + this.url = url; this.graphSpace = graphSpace; this.graph = graph; @@ -76,8 +89,10 @@ public HugeClientBuilder(String url, String graphSpace, String graph) { } public HugeClient build() { - E.checkArgument(this.url != null, "The url parameter can't be null"); - E.checkArgument(this.graph != null, "The graph parameter can't be null"); + if (!this.skipRequiredChecks) { + E.checkArgument(this.url != null, "The url parameter can't be null"); + E.checkArgument(this.graph != null, "The graph parameter can't be null"); + } return new HugeClient(this); } diff --git a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/auth/User.java b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/auth/User.java index 3f7ba4c6e..2ee579c9c 100644 --- a/hugegraph-client/src/main/java/org/apache/hugegraph/structure/auth/User.java +++ b/hugegraph-client/src/main/java/org/apache/hugegraph/structure/auth/User.java @@ -155,9 +155,11 @@ public static class UserRole { // Mapping of: graphSpace -> graph -> permission -> resourceType -> resources @JsonProperty("roles") - private Map>>>> roles; + private Map>>>> roles; - public Map>>>> roles() { + public Map>>>> roles() { return Collections.unmodifiableMap(this.roles); } diff --git a/hugegraph-client/src/test/java/org/apache/hugegraph/unit/HugeClientBuilderTest.java b/hugegraph-client/src/test/java/org/apache/hugegraph/unit/HugeClientBuilderTest.java new file mode 100644 index 000000000..3d9c2df6a --- /dev/null +++ b/hugegraph-client/src/test/java/org/apache/hugegraph/unit/HugeClientBuilderTest.java @@ -0,0 +1,51 @@ +package org.apache.hugegraph.unit; + +import org.apache.hugegraph.driver.HugeClient; +import org.apache.hugegraph.driver.HugeClientBuilder; +import org.apache.hugegraph.rest.ClientException; +import org.junit.Assert; +import org.junit.Test; + +public class HugeClientBuilderTest { + + @Test + public void testBuilderWithSkipRequiredChecks() { + // Should not throw IllegalArgumentException when skipRequiredChecks is true and graph is null + HugeClientBuilder builder = new HugeClientBuilder("http://127.0.0.1:8080", "DEFAULT", null, true); + try { + builder.build(); + } catch (IllegalArgumentException e) { + Assert.fail("Should not throw IllegalArgumentException when skipRequiredChecks is true, but got: " + e.getMessage()); + } catch (Exception e) { + // Expected since there is probably no server running at localhost:8080 + // The fact we reach here means the bypass of graph/url check was successful. + } + } + + @Test(expected = IllegalArgumentException.class) + public void testBuilderWithoutSkipRequiredChecks() { + // Should throw exception when skipRequiredChecks is false and graph is null + HugeClientBuilder builder = new HugeClientBuilder("http://127.0.0.1:8080", "DEFAULT", null, false); + builder.build(); + } + + @Test + public void testHugeClientBuilderMethod() { + // Should not throw IllegalArgumentException + HugeClientBuilder builder = HugeClient.builder("http://127.0.0.1:8080", "DEFAULT", null, true); + try { + builder.build(); + } catch (IllegalArgumentException e) { + Assert.fail("Should not throw IllegalArgumentException when skipRequiredChecks is true, but got: " + e.getMessage()); + } catch (Exception e) { + // Expected + } + } + + @Test(expected = IllegalArgumentException.class) + public void testHugeClientBuilderMethodWithoutSkip() { + // Should throw exception + HugeClientBuilder builder = HugeClient.builder("http://127.0.0.1:8080", "DEFAULT", null); + builder.build(); + } +}