Skip to content

Commit 0554611

Browse files
committed
porting low level client unit tests
1 parent 69f013b commit 0554611

35 files changed

+5399
-46
lines changed

java-client/build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ dependencies {
273273
// Apache-2.0
274274
// https://github.com/awaitility/awaitility
275275
testImplementation("org.awaitility", "awaitility", "4.2.0")
276+
277+
// MIT
278+
// https://github.com/mockito/mockito
279+
testImplementation("org.mockito","mockito-core","5.12.0")
280+
281+
// Apache-2.0
282+
// https://github.com/randomizedtesting/randomizedtesting
283+
testImplementation("com.carrotsearch.randomizedtesting","randomizedtesting-runner","2.8.1")
284+
285+
// Apache-2.0
286+
// https://github.com/elastic/mocksocket
287+
testImplementation("org.elasticsearch","mocksocket","1.2")
288+
289+
276290
}
277291

278292

java-client/src/main/java/co/elastic/clients/transport/rest5_client/Rest5ClientHttpClient.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import co.elastic.clients.transport.http.HeaderMap;
2424
import co.elastic.clients.transport.http.TransportHttpClient;
2525
import co.elastic.clients.transport.rest5_client.low_level.Cancellable;
26-
import co.elastic.clients.transport.rest5_client.low_level.ESRequest;
27-
import co.elastic.clients.transport.rest5_client.low_level.ESResponse;
2826
import co.elastic.clients.transport.rest5_client.low_level.ResponseListener;
2927
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
3028
import co.elastic.clients.util.BinaryData;
@@ -91,8 +89,8 @@ public Rest5ClientOptions createOptions(@Nullable TransportOptions options) {
9189
public Response performRequest(String endpointId, @Nullable Node node, Request request,
9290
TransportOptions options) throws IOException {
9391
Rest5ClientOptions rcOptions = Rest5ClientOptions.of(options);
94-
ESRequest restRequest = createRestRequest(request, rcOptions);
95-
ESResponse restResponse = restClient.performRequest(restRequest);
92+
co.elastic.clients.transport.rest5_client.low_level.Request restRequest = createRestRequest(request, rcOptions);
93+
co.elastic.clients.transport.rest5_client.low_level.Response restResponse = restClient.performRequest(restRequest);
9694
return new RestResponse(restResponse);
9795
}
9896

@@ -102,7 +100,7 @@ public CompletableFuture<Response> performRequestAsync(
102100
) {
103101

104102
RequestFuture<Response> future = new RequestFuture<>();
105-
ESRequest restRequest;
103+
co.elastic.clients.transport.rest5_client.low_level.Request restRequest;
106104

107105
try {
108106
Rest5ClientOptions rcOptions = Rest5ClientOptions.of(options);
@@ -115,7 +113,7 @@ public CompletableFuture<Response> performRequestAsync(
115113

116114
future.cancellable = restClient.performRequestAsync(restRequest, new ResponseListener() {
117115
@Override
118-
public void onSuccess(ESResponse response) {
116+
public void onSuccess(co.elastic.clients.transport.rest5_client.low_level.Response response) {
119117
future.complete(new RestResponse(response));
120118
}
121119

@@ -133,8 +131,8 @@ public void close() throws IOException {
133131
this.restClient.close();
134132
}
135133

136-
private ESRequest createRestRequest(Request request, Rest5ClientOptions options) {
137-
ESRequest clientReq = new ESRequest(
134+
private co.elastic.clients.transport.rest5_client.low_level.Request createRestRequest(Request request, Rest5ClientOptions options) {
135+
co.elastic.clients.transport.rest5_client.low_level.Request clientReq = new co.elastic.clients.transport.rest5_client.low_level.Request(
138136
request.method(), request.path()
139137
);
140138

@@ -180,9 +178,9 @@ private ESRequest createRestRequest(Request request, Rest5ClientOptions options)
180178
}
181179

182180
static class RestResponse implements Response {
183-
private final ESResponse restResponse;
181+
private final co.elastic.clients.transport.rest5_client.low_level.Response restResponse;
184182

185-
RestResponse(ESResponse restResponse) {
183+
RestResponse(co.elastic.clients.transport.rest5_client.low_level.Response restResponse) {
186184
this.restResponse = restResponse;
187185
}
188186

@@ -234,7 +232,7 @@ public BinaryData body() throws IOException {
234232

235233
@Nullable
236234
@Override
237-
public ESResponse originalResponse() {
235+
public co.elastic.clients.transport.rest5_client.low_level.Response originalResponse() {
238236
return this.restResponse;
239237
}
240238

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Cancellable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Represents an operation that can be cancelled.
2828
* Returned when executing async requests through
29-
* {@link Rest5Client#performRequestAsync(ESRequest, ResponseListener)}, so that the request
29+
* {@link Rest5Client#performRequestAsync(Request, ResponseListener)}, so that the request
3030
* can be cancelled if needed. Cancelling a request will result in calling
3131
* {@link HttpUriRequestBase#abort()} on the underlying
3232
* request object, which will in turn cancel its corresponding {@link java.util.concurrent.Future}.

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/ESRequest.java renamed to java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Request.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* HTTP Request to Elasticsearch.
3535
*/
36-
public final class ESRequest {
36+
public final class Request {
3737
private final String method;
3838
private final String endpoint;
3939
private final Map<String, String> parameters = new HashMap<>();
@@ -42,11 +42,11 @@ public final class ESRequest {
4242
private RequestOptions options = RequestOptions.DEFAULT;
4343

4444
/**
45-
* Create the {@linkplain ESRequest}.
45+
* Create the {@linkplain Request}.
4646
* @param method the HTTP method
4747
* @param endpoint the path of the request (without scheme, host, port, or prefix)
4848
*/
49-
public ESRequest(String method, String endpoint) {
49+
public Request(String method, String endpoint) {
5050
this.method = Objects.requireNonNull(method, "method cannot be null");
5151
this.endpoint = Objects.requireNonNull(endpoint, "endpoint cannot be null");
5252
}
@@ -173,7 +173,7 @@ public boolean equals(Object obj) {
173173
return true;
174174
}
175175

176-
ESRequest other = (ESRequest) obj;
176+
Request other = (Request) obj;
177177
return method.equals(other.method)
178178
&& endpoint.equals(other.endpoint)
179179
&& parameters.equals(other.parameters)

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/ESResponse.java renamed to java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Response.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
* Holds an elasticsearch response. It wraps the {@link BasicClassicHttpResponse} returned and associates
3838
* it with its corresponding {@link RequestLine} and {@link HttpHost}.
3939
*/
40-
public class ESResponse {
40+
public class Response {
4141

4242
private final RequestLine requestLine;
4343
private final HttpHost host;
4444
private final ClassicHttpResponse response;
4545

46-
ESResponse(RequestLine requestLine, HttpHost host, ClassicHttpResponse response) {
46+
Response(RequestLine requestLine, HttpHost host, ClassicHttpResponse response) {
4747
Objects.requireNonNull(requestLine, "requestLine cannot be null");
4848
Objects.requireNonNull(host, "host cannot be null");
4949
Objects.requireNonNull(response, "response cannot be null");

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/ResponseException.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
*/
3434
public final class ResponseException extends IOException {
3535

36-
private final ESResponse response;
36+
private final Response response;
3737

38-
public ResponseException(ESResponse response) throws IOException {
38+
public ResponseException(Response response) throws IOException {
3939
super(buildMessage(response));
4040
this.response = response;
4141
}
4242

43-
static String buildMessage(ESResponse response) throws IOException {
43+
static String buildMessage(Response response) throws IOException {
4444
String message = String.format(
4545
Locale.ROOT,
4646
"method [%s], host [%s], URI [%s], status line [%s]",
@@ -70,9 +70,9 @@ static String buildMessage(ESResponse response) throws IOException {
7070
}
7171

7272
/**
73-
* Returns the {@link ESResponse} that caused this exception to be thrown.
73+
* Returns the {@link Response} that caused this exception to be thrown.
7474
*/
75-
public ESResponse getResponse() {
75+
public Response getResponse() {
7676
return response;
7777
}
7878
}

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/ResponseListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface ResponseListener {
3333
/**
3434
* Method invoked if the request yielded a successful response
3535
*/
36-
void onSuccess(ESResponse response);
36+
void onSuccess(Response response);
3737

3838
/**
3939
* Method invoked if the request failed. There are two main categories of failures: connection failures (usually

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
* The hosts that are part of the cluster need to be provided at creation time, but can also be replaced later
9696
* by calling {@link #setNodes(Collection)}.
9797
* <p>
98-
* The method {@link #performRequest(ESRequest)} allows to send a request to the cluster. When
98+
* The method {@link #performRequest(Request)} allows to send a request to the cluster. When
9999
* sending a request, a host gets selected out of the provided ones in a round-robin fashion. Failing hosts
100100
* are marked dead and
101101
* retried after a certain amount of time (minimum 1 minute, maximum 30 minutes), depending on how many
@@ -296,13 +296,13 @@ public boolean isRunning() {
296296
* @throws ResponseException in case Elasticsearch responded with a status code that indicated an
297297
* error
298298
*/
299-
public ESResponse performRequest(ESRequest request) throws IOException {
299+
public Response performRequest(Request request) throws IOException {
300300
InternalRequest internalRequest = new InternalRequest(request);
301301
return performRequest(nextNodes(), internalRequest, null);
302302
}
303303

304-
private ESResponse performRequest(final Iterator<Node> nodes, final InternalRequest request,
305-
Exception previousException)
304+
private Response performRequest(final Iterator<Node> nodes, final InternalRequest request,
305+
Exception previousException)
306306
throws IOException {
307307
RequestContext context = request.createContextForNextAttempt(nodes.next());
308308
ClassicHttpResponse httpResponse;
@@ -360,7 +360,7 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod
360360
}
361361
}
362362

363-
ESResponse response = new ESResponse(new RequestLine(request.httpRequest), node.getHost(), httpResponse);
363+
Response response = new Response(new RequestLine(request.httpRequest), node.getHost(), httpResponse);
364364
if (isCorrectServerResponse(statusCode)) {
365365
onResponse(node);
366366
if (request.warningsHandler.warningsShouldFailRequest(response.getWarnings())) {
@@ -395,7 +395,7 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod
395395
* @param responseListener the {@link ResponseListener} to notify when the
396396
* request is completed or fails
397397
*/
398-
public Cancellable performRequestAsync(ESRequest request, ResponseListener responseListener) {
398+
public Cancellable performRequestAsync(Request request, ResponseListener responseListener) {
399399
try {
400400
FailureTrackingResponseListener failureTrackingResponseListener =
401401
new FailureTrackingResponseListener(responseListener);
@@ -726,7 +726,7 @@ static class FailureTrackingResponseListener {
726726
/**
727727
* Notifies the caller of a response through the wrapped listener
728728
*/
729-
void onSuccess(ESResponse response) {
729+
void onSuccess(Response response) {
730730
responseListener.onSuccess(response);
731731
}
732732

@@ -812,12 +812,12 @@ public void remove() {
812812
}
813813

814814
private class InternalRequest {
815-
private final ESRequest request;
815+
private final Request request;
816816
private final HttpUriRequestBase httpRequest;
817817
private final Cancellable cancellable;
818818
private final WarningsHandler warningsHandler;
819819

820-
InternalRequest(ESRequest request) {
820+
InternalRequest(Request request) {
821821
this.request = request;
822822
Map<String, String> params = new HashMap<>(request.getParameters());
823823
params.putAll(request.getOptions().getParameters());
@@ -897,10 +897,10 @@ private static class RequestContext {
897897
}
898898

899899
private static class ResponseOrResponseException {
900-
private final ESResponse response;
900+
private final Response response;
901901
private final ResponseException responseException;
902902

903-
ResponseOrResponseException(ESResponse response) {
903+
ResponseOrResponseException(Response response) {
904904
this.response = Objects.requireNonNull(response);
905905
this.responseException = null;
906906
}

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/WarningFailureException.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
import static co.elastic.clients.transport.rest5_client.low_level.ResponseException.buildMessage;
2525

2626
/**
27-
* This exception is used to indicate that one or more {@link ESResponse#getWarnings()} exist
27+
* This exception is used to indicate that one or more {@link Response#getWarnings()} exist
2828
* and is typically used when the {@link Rest5Client} is set to fail by setting
2929
* {@link Rest5ClientBuilder#setStrictDeprecationMode(boolean)} to `true`.
3030
*/
3131
// This class extends RuntimeException in order to deal with wrapping that is done in FutureUtils on exception.
3232
// if the exception is not of type ElasticsearchException or RuntimeException it will be wrapped in a UncategorizedExecutionException
3333
public final class WarningFailureException extends RuntimeException {
3434

35-
private final ESResponse response;
35+
private final Response response;
3636

37-
public WarningFailureException(ESResponse response) throws IOException {
37+
public WarningFailureException(Response response) throws IOException {
3838
super(buildMessage(response));
3939
this.response = response;
4040
}
@@ -50,9 +50,9 @@ public WarningFailureException(ESResponse response) throws IOException {
5050
}
5151

5252
/**
53-
* Returns the {@link ESResponse} that caused this exception to be thrown.
53+
* Returns the {@link Response} that caused this exception to be thrown.
5454
*/
55-
public ESResponse getResponse() {
55+
public Response getResponse() {
5656
return response;
5757
}
5858
}

java-client/src/test/java/co/elastic/clients/elasticsearch/_helpers/esql/EsqlAdapterEndToEndTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import co.elastic.clients.elasticsearch._helpers.esql.objects.ObjectsEsqlAdapter;
2727
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
2828
import co.elastic.clients.transport.rest5_client.Rest5ClientTransport;
29-
import co.elastic.clients.transport.rest5_client.low_level.ESRequest;
29+
import co.elastic.clients.transport.rest5_client.low_level.Request;
3030
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
3131
import co.elastic.clients.transport.rest_client.RestClientTransport;
3232
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@@ -35,7 +35,6 @@
3535
import org.apache.commons.io.IOUtils;
3636
import org.apache.http.entity.ByteArrayEntity;
3737
import org.apache.http.entity.ContentType;
38-
import org.elasticsearch.client.Request;
3938
import org.elasticsearch.client.RestClient;
4039
import org.junit.jupiter.api.Assertions;
4140
import org.junit.jupiter.api.BeforeAll;
@@ -64,7 +63,7 @@ public static void setup() throws Exception {
6463

6564
esClient.indices().delete(d -> d.index("employees").ignoreUnavailable(true));
6665

67-
Request request = new Request("POST", "/employees/_bulk?refresh=true");
66+
org.elasticsearch.client.Request request = new org.elasticsearch.client.Request("POST", "/employees/_bulk?refresh=true");
6867

6968
InputStream resourceAsStream = EsqlAdapterTest.class.getResourceAsStream("employees.ndjson");
7069
byte[] bytes = IOUtils.toByteArray(resourceAsStream);
@@ -77,7 +76,7 @@ public static void setup() throws Exception {
7776

7877
esClient.indices().delete(d -> d.index("employees").ignoreUnavailable(true));
7978

80-
ESRequest request = new ESRequest("POST", "/employees/_bulk?refresh=true");
79+
Request request = new Request("POST", "/employees/_bulk?refresh=true");
8180

8281
InputStream resourceAsStream = EsqlAdapterTest.class.getResourceAsStream("employees.ndjson");
8382
byte[] bytes = IOUtils.toByteArray(resourceAsStream);

java-client/src/test/java/co/elastic/clients/transport/rest5_client/TransportTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import co.elastic.clients.transport.TransportException;
2626
import co.elastic.clients.transport.http.RepeatableBodyResponse;
2727
import co.elastic.clients.transport.http.TransportHttpClient;
28-
import co.elastic.clients.transport.rest5_client.low_level.ESResponse;
28+
import co.elastic.clients.transport.rest5_client.low_level.Response;
2929
import co.elastic.clients.transport.rest5_client.low_level.RequestOptions;
3030
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
3131
import co.elastic.clients.util.BinaryData;
@@ -81,7 +81,7 @@ public void testXMLResponse() throws Exception {
8181
assertEquals("es/cat.indices", ex.endpointId());
8282

8383
// Original response is transport-dependent
84-
ESResponse restClientResponse = (ESResponse) ex.response().originalResponse();
84+
Response restClientResponse = (Response) ex.response().originalResponse();
8585
assertEquals(401, restClientResponse.getStatusCode());
8686
}
8787

0 commit comments

Comments
 (0)