1111// Some portions generated by Codex
1212package org .eclipse .rdf4j .sail .elasticsearch ;
1313
14- import java .net .InetAddress ;
1514import java .util .concurrent .TimeUnit ;
1615
17- import org .elasticsearch .action .admin .cluster .health .ClusterHealthRequest ;
18- import org .elasticsearch .action .admin .cluster .health .ClusterHealthResponse ;
19- import org .elasticsearch .client .Client ;
20- import org .elasticsearch .client .transport .TransportClient ;
21- import org .elasticsearch .common .settings .Settings ;
22- import org .elasticsearch .common .transport .TransportAddress ;
23- import org .elasticsearch .core .TimeValue ;
24- import org .elasticsearch .transport .client .PreBuiltTransportClient ;
16+ import org .apache .hc .core5 .http .HttpHost ;
2517import org .junit .jupiter .api .AfterAll ;
2618import org .junit .jupiter .api .Assumptions ;
2719import org .junit .jupiter .api .BeforeAll ;
3022import org .testcontainers .junit .jupiter .Testcontainers ;
3123import org .testcontainers .utility .DockerImageName ;
3224
25+ import co .elastic .clients .elasticsearch .ElasticsearchClient ;
26+ import co .elastic .clients .elasticsearch ._types .HealthStatus ;
27+ import co .elastic .clients .elasticsearch .cluster .HealthResponse ;
28+ import co .elastic .clients .json .jackson .JacksonJsonpMapper ;
29+ import co .elastic .clients .transport .ElasticsearchTransport ;
30+ import co .elastic .clients .transport .rest5_client .Rest5ClientTransport ;
31+ import co .elastic .clients .transport .rest5_client .low_level .Rest5Client ;
32+
3333@ Testcontainers (disabledWithoutDocker = true )
3434public abstract class AbstractElasticsearchTest {
3535
@@ -39,6 +39,7 @@ public abstract class AbstractElasticsearchTest {
3939 public static final GenericContainer <?> elasticsearch = new GenericContainer <>(dockerImageName ())
4040 .withEnv ("discovery.type" , "single-node" )
4141 .withEnv ("cluster.name" , CLUSTER_NAME )
42+ .withEnv ("xpack.security.enabled" , "false" )
4243 .withEnv ("ES_JAVA_OPTS" ,
4344 "-Djdk.disableLastUsageTracking=true -XX:-UseContainerSupport -Xms512m -Xmx512m" )
4445 .withEnv ("JDK_JAVA_OPTIONS" ,
@@ -47,7 +48,11 @@ public abstract class AbstractElasticsearchTest {
4748 "-Djdk.disableLastUsageTracking=true -XX:-UseContainerSupport -Xms512m -Xmx512m" )
4849 .withExposedPorts (9200 , 9300 );
4950
50- protected static TransportClient client ;
51+ protected static Rest5Client lowLevelClient ;
52+ protected static ElasticsearchTransport transport ;
53+ protected static ElasticsearchClient client ;
54+ protected static String host ;
55+ protected static int httpPort ;
5156
5257 @ BeforeAll
5358 public static void setUpCluster () throws Exception {
@@ -59,37 +64,40 @@ public static void setUpCluster() throws Exception {
5964 Assumptions .assumeTrue (elasticsearch .isRunning (),
6065 "Elasticsearch test container failed to start:\n " + safeLogs ());
6166
62- Settings settings = Settings .builder ().put ("cluster.name" , CLUSTER_NAME ).build ();
63-
64- String host = elasticsearch .getHost ();
65- int transportPort = elasticsearch .getMappedPort (9300 );
67+ host = elasticsearch .getHost ();
68+ httpPort = elasticsearch .getMappedPort (9200 );
6669
67- TransportClient transportClient = new PreBuiltTransportClient (settings )
68- .addTransportAddress (new TransportAddress (InetAddress .getByName (host ), transportPort ));
70+ lowLevelClient = Rest5Client .builder (new HttpHost ("http" , host , httpPort )).build ();
71+ transport = new Rest5ClientTransport (lowLevelClient , new JacksonJsonpMapper ());
72+ client = new ElasticsearchClient (transport );
6973
70- waitForClusterReady (transportClient );
71-
72- client = transportClient ;
74+ waitForClusterReady (client );
7375 }
7476
7577 @ AfterAll
7678 public static void tearDownCluster () {
7779 if (client != null ) {
78- client .close ();
80+ try {
81+ lowLevelClient .close ();
82+ } catch (Exception e ) {
83+ // ignore during shutdown
84+ }
7985 client = null ;
86+ transport = null ;
87+ lowLevelClient = null ;
8088 }
8189 }
8290
8391 private static DockerImageName dockerImageName () {
8492 String esVersion = System .getProperty ("elasticsearch.docker.version" ,
85- System .getProperty ("elasticsearch.version" , "7.15.2 " ));
93+ System .getProperty ("elasticsearch.version" , "9.2.4 " ));
8694
8795 return DockerImageName
8896 .parse ("docker.elastic.co/elasticsearch/elasticsearch:" + esVersion )
8997 .asCompatibleSubstituteFor ("docker.elastic.co/elasticsearch/elasticsearch" );
9098 }
9199
92- private static void waitForClusterReady (Client client ) {
100+ private static void waitForClusterReady (ElasticsearchClient client ) {
93101 if (!elasticsearch .isRunning ()) {
94102 throw new IllegalStateException ("Elasticsearch test container stopped before health check:\n " + safeLogs ());
95103 }
@@ -103,12 +111,9 @@ private static void waitForClusterReady(Client client) {
103111 "Elasticsearch test container stopped during health check:\n " + safeLogs ());
104112 }
105113 try {
106- ClusterHealthRequest request = new ClusterHealthRequest ()
107- .waitForYellowStatus ()
108- .timeout (TimeValue .timeValueSeconds (1 ));
109-
110- ClusterHealthResponse response = client .admin ().cluster ().health (request ).actionGet ();
111- if (!response .isTimedOut ()) {
114+ HealthResponse response = client .cluster ()
115+ .health (h -> h .waitForStatus (HealthStatus .Yellow ).timeout (t -> t .time ("100ms" )));
116+ if (!response .timedOut ()) {
112117 return ;
113118 }
114119 lastFailure = new IllegalStateException ("Cluster health timed out waiting for YELLOW status" );
@@ -117,7 +122,7 @@ private static void waitForClusterReady(Client client) {
117122 }
118123
119124 try {
120- Thread .sleep (100 );
125+ Thread .sleep (10 );
121126 } catch (InterruptedException ie ) {
122127 Thread .currentThread ().interrupt ();
123128 throw new IllegalStateException ("Interrupted while waiting for Elasticsearch test cluster" , ie );
0 commit comments