From 24a7dbfc56df30e1a2e862b846b081f7058de9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=AE=87?= <940643974@qq.com> Date: Mon, 30 Oct 2023 23:27:56 +0800 Subject: [PATCH] fix: code issue --- .../hugegraph/rest/AbstractRestClient.java | 22 +++++--------- .../hugegraph/unit/rest/RestClientTest.java | 29 +++++++++---------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java index 4aa009b1..b2ea96e7 100644 --- a/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java +++ b/hugegraph-common/src/main/java/org/apache/hugegraph/rest/AbstractRestClient.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Map; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import javax.net.ssl.HostnameVerifier; @@ -316,8 +315,7 @@ public RestResult post(String path, Object object, RestHeaders headers, Request.Builder requestBuilder = getRequestBuilder(path, null, headers, params); requestBuilder.post(buildRequestBody(object, headers)); - try (Response response = this.request( - () -> client.newCall(requestBuilder.build()).execute())) { + try (Response response = request(requestBuilder)) { checkStatus(response, 200, 201, 202); return new RestResult(response); } @@ -346,8 +344,7 @@ public RestResult put(String path, String id, Object object, Request.Builder requestBuilder = getRequestBuilder(path, id, headers, params); requestBuilder.put(buildRequestBody(object, headers)); - try (Response response = this.request( - () -> client.newCall(requestBuilder.build()).execute())) { + try (Response response = request(requestBuilder)) { checkStatus(response, 200, 202); return new RestResult(response); } @@ -372,8 +369,7 @@ public RestResult get(String path, String id) { private RestResult get(String path, String id, Map params) { Request.Builder requestBuilder = getRequestBuilder(path, id, null, params); - try (Response response = this.request( - () -> client.newCall(requestBuilder.build()).execute())) { + try (Response response = request(requestBuilder)) { checkStatus(response, 200); return new RestResult(response); } @@ -395,8 +391,7 @@ private RestResult delete(String path, String id, Request.Builder requestBuilder = getRequestBuilder(path, id, null, params); requestBuilder.delete(); - try (Response response = this.request( - () -> client.newCall(requestBuilder.build()).execute())) { + try (Response response = request(requestBuilder)) { checkStatus(response, 204, 202); return new RestResult(response); } @@ -404,12 +399,9 @@ private RestResult delete(String path, String id, protected abstract void checkStatus(Response response, int... statuses); - protected Response request(Callable method) { - try { - return method.call(); - } catch (Exception e) { - throw new ClientException("Failed to do request", e); - } + @SneakyThrows + protected Response request(Request.Builder requestBuilder) { + return client.newCall(requestBuilder.build()).execute(); } @SneakyThrows diff --git a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java index 8426c664..0fa654a6 100644 --- a/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java +++ b/hugegraph-common/src/test/java/org/apache/hugegraph/unit/rest/RestClientTest.java @@ -21,7 +21,6 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import java.util.concurrent.Callable; import java.util.function.BiFunction; import javax.net.ssl.SSLContext; @@ -342,33 +341,33 @@ public void testRequest() { client.setAuthContext("token1"); result = client.delete("test", ImmutableMap.of()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(RequestHeaders.AUTHORIZATION,"token1"); + Mockito.verify(requestBuilder).addHeader(RequestHeaders.AUTHORIZATION, "token1"); client.resetAuthContext(); client.setAuthContext("token2"); result = client.delete("test", "id"); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token2"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token2"); client.resetAuthContext(); // Test get client.setAuthContext("token3"); result = client.get("test"); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token3"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token3"); client.resetAuthContext(); client.setAuthContext("token4"); result = client.get("test", ImmutableMap.of()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token4"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token4"); client.resetAuthContext(); client.setAuthContext("token5"); result = client.get("test", "id"); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token5"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token5"); client.resetAuthContext(); // Test put @@ -376,52 +375,52 @@ public void testRequest() { // result = client.post("test", new Object()); //why use new Object() as args here? result = client.post("test", null); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token6"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token6"); client.resetAuthContext(); client.setAuthContext("token7"); result = client.post("test", null, new RestHeaders()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token7"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token7"); client.resetAuthContext(); client.setAuthContext("token8"); result = client.post("test", null, ImmutableMap.of()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token8"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token8"); client.resetAuthContext(); client.setAuthContext("token9"); result = client.post("test", null, new RestHeaders(), ImmutableMap.of()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token9"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token9"); client.resetAuthContext(); // Test post client.setAuthContext("token10"); result = client.post("test", null); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token10"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token10"); client.resetAuthContext(); client.setAuthContext("token11"); result = client.post("test", null, new RestHeaders()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token11"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token11"); client.resetAuthContext(); client.setAuthContext("token12"); result = client.post("test", null, ImmutableMap.of()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token12"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token12"); client.resetAuthContext(); client.setAuthContext("token13"); result = client.post("test", null, new RestHeaders(), ImmutableMap.of()); Assert.assertEquals(200, result.status()); - Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token13"); + Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token13"); client.resetAuthContext(); } @@ -517,7 +516,7 @@ public RestClientImpl(String url, int timeout, int status, @SneakyThrows @Override - protected Response request(Callable method) { + protected Response request(Request.Builder requestBuilder) { Response response = Mockito.mock(Response.class, Mockito.RETURNS_DEEP_STUBS); Mockito.when(response.code()).thenReturn(this.status); Mockito.when(response.headers()).thenReturn(this.headers.toOkHttpHeader());