Skip to content

Commit b115194

Browse files
swallezl-trotta
authored andcommitted
Add ElasticsearchTestClient that randomly chooses an implementation
1 parent f966a11 commit b115194

File tree

4 files changed

+96
-67
lines changed

4 files changed

+96
-67
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public final class Rest5ClientBuilder {
161161
*
162162
* @throws NullPointerException if {@code httpAsyncClient} is {@code null}.
163163
*/
164-
public RestClientBuilder setHttpClient(CloseableHttpAsyncClient httpAsyncClient) {
164+
public Rest5ClientBuilder setHttpClient(CloseableHttpAsyncClient httpAsyncClient) {
165165
Objects.requireNonNull(httpAsyncClient, "custom rest client must not be null");
166166
this.httpClient = httpAsyncClient;
167167
return this;
@@ -172,7 +172,7 @@ public RestClientBuilder setHttpClient(CloseableHttpAsyncClient httpAsyncClient)
172172
*
173173
* @throws NullPointerException if {@code httpAsyncClient} is {@code null}.
174174
*/
175-
public RestClientBuilder setSSLContext(SSLContext sslContext) {
175+
public Rest5ClientBuilder setSSLContext(SSLContext sslContext) {
176176
Objects.requireNonNull(sslContext, "ssl context must not be null");
177177
this.sslContext = sslContext;
178178
return this;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* 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+
20+
package co.elastic.clients.elasticsearch;
21+
22+
import co.elastic.clients.json.JsonpMapper;
23+
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
24+
import co.elastic.clients.transport.rest5_client.Rest5ClientTransport;
25+
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
26+
import co.elastic.clients.transport.rest_client.RestClientTransport;
27+
import org.apache.hc.core5.http.Header;
28+
import org.apache.hc.core5.http.message.BasicHeader;
29+
import org.apache.http.HttpHost;
30+
import org.apache.http.auth.AuthScope;
31+
import org.apache.http.auth.UsernamePasswordCredentials;
32+
import org.apache.http.impl.client.BasicCredentialsProvider;
33+
import org.elasticsearch.client.RestClient;
34+
35+
import javax.annotation.Nullable;
36+
import javax.net.ssl.SSLContext;
37+
import java.net.URI;
38+
import java.util.Base64;
39+
import java.util.Random;
40+
41+
public class ElasticsearchTestClient {
42+
43+
// Same value for all tests in a test run
44+
private static final int RAND = new Random().nextInt(100);
45+
46+
private static JsonpMapper mapper(JsonpMapper mapper) {
47+
return mapper != null ? mapper : new JsonbJsonpMapper();
48+
}
49+
50+
public static ElasticsearchClient createClient(String url, @Nullable JsonpMapper mapper, @Nullable SSLContext sslContext) {
51+
if(RAND % 2 == 0) {
52+
System.out.println("Using a Rest4 client");
53+
return createRest4Client(url, mapper, sslContext);
54+
} else {
55+
System.out.println("Using a Rest5 client");
56+
return createRest5Client(url, mapper, sslContext);
57+
}
58+
}
59+
60+
public static ElasticsearchClient createRest4Client(String url, @Nullable JsonpMapper mapper, @Nullable SSLContext sslContext) {
61+
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
62+
credsProv.setCredentials(
63+
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")
64+
);
65+
var restClient = RestClient.builder(HttpHost.create(url))
66+
.setHttpClientConfigCallback(hc -> hc
67+
.setDefaultCredentialsProvider(credsProv)
68+
.setSSLContext(sslContext)
69+
)
70+
.build();
71+
var transport = new RestClientTransport(restClient, mapper(mapper));
72+
return new ElasticsearchClient(transport);
73+
}
74+
75+
public static ElasticsearchClient createRest5Client(String url, @Nullable JsonpMapper mapper, @Nullable SSLContext sslContext) {
76+
var pwd = Base64.getEncoder().encodeToString("elastic:changeme".getBytes());
77+
var restClientBuilder = Rest5Client.builder(org.apache.hc.core5.http.HttpHost.create(URI.create(url)))
78+
.setDefaultHeaders(new Header[]{
79+
new BasicHeader("Authorization", "Basic " + pwd)
80+
});
81+
82+
if (sslContext != null) {
83+
restClientBuilder.setSSLContext(sslContext);
84+
}
85+
86+
var transport = new Rest5ClientTransport(restClientBuilder.build(), mapper(mapper));
87+
return new ElasticsearchClient(transport);
88+
}
89+
}

java-client/src/test/java/co/elastic/clients/elasticsearch/ElasticsearchTestServer.java

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,10 @@
2222
import co.elastic.clients.elasticsearch._types.ErrorResponse;
2323
import co.elastic.clients.json.JsonData;
2424
import co.elastic.clients.json.JsonpDeserializer;
25-
import co.elastic.clients.json.JsonpMapper;
26-
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
27-
import co.elastic.clients.transport.ElasticsearchTransport;
2825
import co.elastic.clients.transport.JsonEndpoint;
2926
import co.elastic.clients.transport.Version;
3027
import co.elastic.clients.transport.endpoints.DelegatingJsonEndpoint;
31-
import co.elastic.clients.transport.rest_client.RestClientTransport;
3228
import org.apache.commons.io.FileUtils;
33-
import org.apache.hc.core5.http.Header;
34-
import org.apache.hc.core5.http.message.BasicHeader;
35-
import org.apache.http.HttpHost;
36-
import org.apache.http.auth.AuthScope;
37-
import org.apache.http.auth.UsernamePasswordCredentials;
38-
import org.apache.http.impl.client.BasicCredentialsProvider;
39-
import org.elasticsearch.client.RestClient;
4029
import org.testcontainers.elasticsearch.ElasticsearchContainer;
4130
import org.testcontainers.images.builder.ImageFromDockerfile;
4231
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
@@ -53,13 +42,12 @@
5342
import java.time.Instant;
5443
import java.time.temporal.ChronoUnit;
5544
import java.util.Base64;
56-
import java.util.Optional;
45+
import java.util.Random;
5746

5847
public class ElasticsearchTestServer implements AutoCloseable {
5948

6049
private final String[] plugins;
6150
private volatile ElasticsearchContainer container;
62-
private final JsonpMapper mapper = new JsonbJsonpMapper();
6351
private ElasticsearchClient client;
6452

6553
private static ElasticsearchTestServer global;
@@ -103,55 +91,11 @@ public ElasticsearchTestServer(String... plugins) {
10391
this.plugins = plugins;
10492
}
10593

106-
public ElasticsearchClient createRest4Client(String url, SSLContext sslContext) {
107-
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
108-
credsProv.setCredentials(
109-
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")
110-
);
111-
var restClient = RestClient.builder(HttpHost.create(url))
112-
.setHttpClientConfigCallback(hc -> hc
113-
.setDefaultCredentialsProvider(credsProv)
114-
.setSSLContext(sslContext)
115-
)
116-
.build();
117-
var transport = new RestClientTransport(restClient, mapper);
118-
return new ElasticsearchClient(transport);
119-
}
120-
121-
// protected void setup(String url, SSLContext sslContext) {
122-
// try {
123-
// org.apache.hc.core5.http.HttpHost host = org.apache.hc.core5.http.HttpHost.create(url);
124-
//
125-
// var pwd = Base64.getEncoder().encodeToString("elastic:changeme".getBytes());
126-
//
127-
//
128-
// restClient = Rest5Client.builder(org.apache.hc.core5.http.HttpHost.create(url))
129-
// .setSslContext(Optional.ofNullable(sslContext).orElse(SSLContext.getDefault()))
130-
// .setDefaultHeaders(new Header[]{
131-
// new BasicHeader("Authorization", "Basic " + pwd)
132-
// })
133-
// //.setCompressionEnabled(true)
134-
// .build();
135-
// } catch (Exception e) {
136-
// throw new RuntimeException(e);
137-
// }
138-
// transport = new Rest5ClientTransport(restClient, mapper);
139-
// client = new ElasticsearchClient(transport);
140-
// }
94+
// Same value for all tests in a test run
95+
private static final int RAND = new Random().nextInt(100);
14196

14297
protected void setup(String url, SSLContext sslContext) {
143-
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
144-
credsProv.setCredentials(
145-
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")
146-
);
147-
var restClient = RestClient.builder(HttpHost.create(url))
148-
.setHttpClientConfigCallback(hc -> hc
149-
.setDefaultCredentialsProvider(credsProv)
150-
.setSSLContext(sslContext)
151-
)
152-
.build();
153-
var transport = new RestClientTransport(restClient, mapper);
154-
client = new ElasticsearchClient(transport);
98+
this.client = ElasticsearchTestClient.createClient(url, null, sslContext);
15599
}
156100

157101
private Version selectLatestVersion(Version version, String info) {
@@ -300,10 +244,6 @@ public ElasticsearchContainer container() {
300244
return this.container;
301245
}
302246

303-
public JsonpMapper mapper() {
304-
return mapper;
305-
}
306-
307247
public ElasticsearchClient client() {
308248
return client;
309249
}

java-client/src/test/java/co/elastic/clients/elasticsearch/spec_issues/SpecIssuesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public void gettingVersionFromNodes() throws Exception {
302302
.nodes().info().nodes().entrySet().forEach(node ->
303303
assertNotNull(node.getValue().version()));
304304
}
305-
305+
306306
private <T> T loadRsrc(String res, JsonpDeserializer<T> deser) {
307307
InputStream is = this.getClass().getResourceAsStream(res);
308308
assertNotNull(is, "Resource not found: " + res);

0 commit comments

Comments
 (0)