2121
2222import co .elastic .clients .elasticsearch .ElasticsearchAsyncClient ;
2323import co .elastic .clients .elasticsearch .ElasticsearchClient ;
24+ import co .elastic .clients .elasticsearch .ElasticsearchTestServer ;
2425import co .elastic .clients .elasticsearch ._types .ElasticsearchException ;
2526import co .elastic .clients .elasticsearch ._types .Refresh ;
2627import co .elastic .clients .elasticsearch ._types .aggregations .HistogramAggregate ;
4243import co .elastic .clients .elasticsearch .indices .GetMappingResponse ;
4344import co .elastic .clients .elasticsearch .indices .IndexState ;
4445import 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 ;
5046import 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 ;
5847import org .junit .Assert ;
5948import org .junit .BeforeClass ;
6049import org .junit .Test ;
61- import org .testcontainers .elasticsearch .ElasticsearchContainer ;
6250
6351import java .io .IOException ;
64- import java .time .Duration ;
6552import java .util .Collections ;
6653import java .util .Map ;
6754import java .util .concurrent .CompletableFuture ;
7057
7158public class RequestTest extends Assert {
7259
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 ;
7861
7962 @ BeforeClass
8063 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 ();
10565 }
10666
10767 @ Test
@@ -112,7 +72,7 @@ public void testCount() throws Exception {
11272
11373 @ Test
11474 public void testIndexCreation () throws Exception {
115- ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient (transport );
75+ ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient (client . _transport () );
11676
11777 // Ping the server
11878 assertTrue (client .ping ().value ());
@@ -222,7 +182,7 @@ public void testDataIngestion() throws Exception {
222182 public void testCatRequest () throws IOException {
223183 // Cat requests should have the "format=json" added by the transport
224184 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 () ));
226186
227187 assertEquals (1 , nodes .valueBody ().size ());
228188 assertEquals ("*" , nodes .valueBody ().get (0 ).master ());
@@ -247,15 +207,25 @@ public void testBulkRequest() throws IOException {
247207 .id ("def" )
248208 .document (appData )
249209 ))
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+ )
250219 );
251220
252221 assertFalse (bulk .errors ());
253- assertEquals (2 , bulk .items ().size ());
222+ assertEquals (3 , bulk .items ().size ());
254223 assertEquals (OperationType .Create , bulk .items ().get (0 ).operationType ());
255224 assertEquals ("foo" , bulk .items ().get (0 ).index ());
256225 assertEquals (1L , bulk .items ().get (0 ).version ().longValue ());
257226 assertEquals ("foo" , bulk .items ().get (1 ).index ());
258227 assertEquals (1L , bulk .items ().get (1 ).version ().longValue ());
228+ assertEquals (42 , client .get (b -> b .index ("foo" ).id ("gh" ), AppData .class ).source ().intValue );
259229 }
260230
261231 @ Test
@@ -291,7 +261,7 @@ public void testRefresh() throws IOException {
291261
292262
293263 ExecutionException ee = assertThrows (ExecutionException .class , () -> {
294- ElasticsearchAsyncClient aClient = new ElasticsearchAsyncClient (transport );
264+ ElasticsearchAsyncClient aClient = new ElasticsearchAsyncClient (client . _transport () );
295265 GetResponse <String > response = aClient .get (
296266 _0 -> _0 .index ("doesnotexist" ).id ("reallynot" ), String .class
297267 ).get ();
@@ -398,30 +368,6 @@ public void testDefaultIndexSettings() throws IOException {
398368 assertNull (settings .get (index ).defaults ());
399369 }
400370
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-
425371 @ Test
426372 public void testValueBodyResponse () throws Exception {
427373 DiskUsageResponse resp = client .indices ().diskUsage (b -> b
0 commit comments