|
22 | 22 | import co.elastic.clients.elasticsearch._types.ErrorResponse; |
23 | 23 | import co.elastic.clients.json.JsonData; |
24 | 24 | 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; |
28 | 25 | import co.elastic.clients.transport.JsonEndpoint; |
29 | 26 | import co.elastic.clients.transport.Version; |
30 | 27 | import co.elastic.clients.transport.endpoints.DelegatingJsonEndpoint; |
31 | | -import co.elastic.clients.transport.rest_client.RestClientTransport; |
32 | 28 | 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; |
40 | 29 | import org.testcontainers.elasticsearch.ElasticsearchContainer; |
41 | 30 | import org.testcontainers.images.builder.ImageFromDockerfile; |
42 | 31 | import org.testcontainers.shaded.org.apache.commons.io.IOUtils; |
|
53 | 42 | import java.time.Instant; |
54 | 43 | import java.time.temporal.ChronoUnit; |
55 | 44 | import java.util.Base64; |
56 | | -import java.util.Optional; |
| 45 | +import java.util.Random; |
57 | 46 |
|
58 | 47 | public class ElasticsearchTestServer implements AutoCloseable { |
59 | 48 |
|
60 | 49 | private final String[] plugins; |
61 | 50 | private volatile ElasticsearchContainer container; |
62 | | - private final JsonpMapper mapper = new JsonbJsonpMapper(); |
63 | 51 | private ElasticsearchClient client; |
64 | 52 |
|
65 | 53 | private static ElasticsearchTestServer global; |
@@ -103,55 +91,11 @@ public ElasticsearchTestServer(String... plugins) { |
103 | 91 | this.plugins = plugins; |
104 | 92 | } |
105 | 93 |
|
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); |
141 | 96 |
|
142 | 97 | 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); |
155 | 99 | } |
156 | 100 |
|
157 | 101 | private Version selectLatestVersion(Version version, String info) { |
@@ -300,10 +244,6 @@ public ElasticsearchContainer container() { |
300 | 244 | return this.container; |
301 | 245 | } |
302 | 246 |
|
303 | | - public JsonpMapper mapper() { |
304 | | - return mapper; |
305 | | - } |
306 | | - |
307 | 247 | public ElasticsearchClient client() { |
308 | 248 | return client; |
309 | 249 | } |
|
0 commit comments