21
21
22
22
import co .elastic .clients .elasticsearch .ElasticsearchAsyncClient ;
23
23
import co .elastic .clients .elasticsearch .ElasticsearchClient ;
24
+ import co .elastic .clients .elasticsearch .ElasticsearchTestServer ;
24
25
import co .elastic .clients .elasticsearch ._types .ElasticsearchException ;
25
26
import co .elastic .clients .elasticsearch ._types .Refresh ;
26
27
import co .elastic .clients .elasticsearch ._types .aggregations .HistogramAggregate ;
42
43
import co .elastic .clients .elasticsearch .indices .GetMappingResponse ;
43
44
import co .elastic .clients .elasticsearch .indices .IndexState ;
44
45
import co .elastic .clients .elasticsearch .model .ModelTestCase ;
45
- import co .elastic .clients .elasticsearch .snapshot .CreateRepositoryResponse ;
46
- import co .elastic .clients .elasticsearch .snapshot .CreateSnapshotResponse ;
47
- import co .elastic .clients .json .JsonpMapper ;
48
- import co .elastic .clients .json .jsonb .JsonbJsonpMapper ;
49
- import co .elastic .clients .transport .ElasticsearchTransport ;
50
46
import co .elastic .clients .transport .endpoints .BooleanResponse ;
51
- import co .elastic .clients .transport .rest_client .RestClientTransport ;
52
- import org .apache .http .HttpHost ;
53
- import org .apache .http .auth .AuthScope ;
54
- import org .apache .http .auth .UsernamePasswordCredentials ;
55
- import org .apache .http .impl .client .BasicCredentialsProvider ;
56
- import org .elasticsearch .client .RestClient ;
57
- import org .junit .AfterClass ;
58
47
import org .junit .Assert ;
59
48
import org .junit .BeforeClass ;
60
49
import org .junit .Test ;
61
- import org .testcontainers .elasticsearch .ElasticsearchContainer ;
62
50
63
51
import java .io .IOException ;
64
- import java .time .Duration ;
65
52
import java .util .Collections ;
66
53
import java .util .Map ;
67
54
import java .util .concurrent .CompletableFuture ;
70
57
71
58
public class RequestTest extends Assert {
72
59
73
- private static ElasticsearchContainer container ;
74
- private static final JsonpMapper mapper = new JsonbJsonpMapper ();
75
- private static RestClient restClient ;
76
- private static ElasticsearchTransport transport ;
77
- private static ElasticsearchClient client ;
60
+ static ElasticsearchClient client ;
78
61
79
62
@ BeforeClass
80
63
public static void setup () {
81
- container = new ElasticsearchContainer ("docker.elastic.co/elasticsearch/elasticsearch:7.16.2" )
82
- .withEnv ("ES_JAVA_OPTS" , "-Xms256m -Xmx256m" )
83
- .withEnv ("path.repo" , "/tmp" ) // for snapshots
84
- .withStartupTimeout (Duration .ofSeconds (30 ))
85
- .withPassword ("changeme" );
86
- container .start ();
87
- int port = container .getMappedPort (9200 );
88
-
89
- BasicCredentialsProvider credsProv = new BasicCredentialsProvider ();
90
- credsProv .setCredentials (
91
- AuthScope .ANY , new UsernamePasswordCredentials ("elastic" , "changeme" )
92
- );
93
- restClient = RestClient .builder (new HttpHost ("localhost" , port ))
94
- .setHttpClientConfigCallback (hc -> hc .setDefaultCredentialsProvider (credsProv ))
95
- .build ();
96
- transport = new RestClientTransport (restClient , mapper );
97
- client = new ElasticsearchClient (transport );
98
- }
99
-
100
- @ AfterClass
101
- public static void tearDown () {
102
- if (container != null ) {
103
- container .stop ();
104
- }
64
+ client = ElasticsearchTestServer .global ().client ();
105
65
}
106
66
107
67
@ Test
@@ -112,7 +72,7 @@ public void testCount() throws Exception {
112
72
113
73
@ Test
114
74
public void testIndexCreation () throws Exception {
115
- ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient (transport );
75
+ ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient (client . _transport () );
116
76
117
77
// Ping the server
118
78
assertTrue (client .ping ().value ());
@@ -222,7 +182,7 @@ public void testDataIngestion() throws Exception {
222
182
public void testCatRequest () throws IOException {
223
183
// Cat requests should have the "format=json" added by the transport
224
184
NodesResponse nodes = client .cat ().nodes (_0 -> _0 );
225
- System .out .println (ModelTestCase .toJson (nodes , mapper ));
185
+ System .out .println (ModelTestCase .toJson (nodes , client . _transport (). jsonpMapper () ));
226
186
227
187
assertEquals (1 , nodes .valueBody ().size ());
228
188
assertEquals ("*" , nodes .valueBody ().get (0 ).master ());
@@ -247,15 +207,25 @@ public void testBulkRequest() throws IOException {
247
207
.id ("def" )
248
208
.document (appData )
249
209
))
210
+ .operations (_1 -> _1
211
+ .update (_2 -> _2
212
+ .index ("foo" )
213
+ .id ("gh" )
214
+ .action (_3 -> _3
215
+ .docAsUpsert (true )
216
+ .doc (appData ))
217
+ )
218
+ )
250
219
);
251
220
252
221
assertFalse (bulk .errors ());
253
- assertEquals (2 , bulk .items ().size ());
222
+ assertEquals (3 , bulk .items ().size ());
254
223
assertEquals (OperationType .Create , bulk .items ().get (0 ).operationType ());
255
224
assertEquals ("foo" , bulk .items ().get (0 ).index ());
256
225
assertEquals (1L , bulk .items ().get (0 ).version ().longValue ());
257
226
assertEquals ("foo" , bulk .items ().get (1 ).index ());
258
227
assertEquals (1L , bulk .items ().get (1 ).version ().longValue ());
228
+ assertEquals (42 , client .get (b -> b .index ("foo" ).id ("gh" ), AppData .class ).source ().intValue );
259
229
}
260
230
261
231
@ Test
@@ -291,7 +261,7 @@ public void testRefresh() throws IOException {
291
261
292
262
293
263
ExecutionException ee = assertThrows (ExecutionException .class , () -> {
294
- ElasticsearchAsyncClient aClient = new ElasticsearchAsyncClient (transport );
264
+ ElasticsearchAsyncClient aClient = new ElasticsearchAsyncClient (client . _transport () );
295
265
GetResponse <String > response = aClient .get (
296
266
_0 -> _0 .index ("doesnotexist" ).id ("reallynot" ), String .class
297
267
).get ();
@@ -398,30 +368,6 @@ public void testDefaultIndexSettings() throws IOException {
398
368
assertNull (settings .get (index ).defaults ());
399
369
}
400
370
401
- @ Test
402
- public void testSnapshotCreation () throws IOException {
403
- // https://github.com/elastic/elasticsearch-java/issues/74
404
- // https://github.com/elastic/elasticsearch/issues/82358
405
-
406
- CreateRepositoryResponse repo = client .snapshot ().createRepository (b1 -> b1
407
- .name ("test" )
408
- .type ("fs" )
409
- .settings (b2 -> b2
410
- .location ("/tmp/test-repo" )
411
- )
412
- );
413
-
414
- assertTrue (repo .acknowledged ());
415
-
416
- CreateSnapshotResponse snapshot = client .snapshot ().create (b -> b
417
- .repository ("test" )
418
- .snapshot ("1" )
419
- .waitForCompletion (true )
420
- );
421
-
422
- assertNotNull (snapshot .snapshot ());
423
- }
424
-
425
371
@ Test
426
372
public void testValueBodyResponse () throws Exception {
427
373
DiskUsageResponse resp = client .indices ().diskUsage (b -> b
0 commit comments