11package org .testcontainers .elasticsearch ;
22
3+ import static org .hamcrest .CoreMatchers .containsString ;
4+ import static org .hamcrest .CoreMatchers .is ;
5+ import static org .hamcrest .MatcherAssert .assertThat ;
6+ import static org .rnorth .visibleassertions .VisibleAssertions .assertThrows ;
7+ import static org .testcontainers .elasticsearch .ElasticsearchContainer .ELASTICSEARCH_DEFAULT_VERSION ;
8+
9+ import java .io .IOException ;
310
411import org .apache .http .HttpHost ;
512import org .apache .http .auth .AuthScope ;
613import org .apache .http .auth .UsernamePasswordCredentials ;
714import org .apache .http .client .CredentialsProvider ;
815import org .apache .http .impl .client .BasicCredentialsProvider ;
916import org .apache .http .util .EntityUtils ;
17+ import org .elasticsearch .Version ;
1018import org .elasticsearch .action .admin .cluster .health .ClusterHealthResponse ;
1119import org .elasticsearch .client .Request ;
1220import org .elasticsearch .client .Response ;
1927import org .junit .After ;
2028import org .junit .Test ;
2129
22- import java .io .IOException ;
23-
24- import static org .hamcrest .CoreMatchers .containsString ;
25- import static org .hamcrest .CoreMatchers .is ;
26- import static org .hamcrest .MatcherAssert .assertThat ;
27- import static org .rnorth .visibleassertions .VisibleAssertions .assertThrows ;
28- import static org .testcontainers .elasticsearch .ElasticsearchContainer .ELASTICSEARCH_DEFAULT_VERSION ;
29-
3030public class ElasticsearchContainerTest {
3131
32+ /**
33+ * Elasticsearch version which should be used for the Tests
34+ */
35+ private static final String ELASTICSEARCH_VERSION = Version .CURRENT .toString ();
36+
3237 /**
3338 * Elasticsearch default username, when secured with a license > basic
3439 */
@@ -51,10 +56,14 @@ public void stopRestClient() throws IOException {
5156
5257 @ Test
5358 public void elasticsearchDefaultTest () throws IOException {
59+ // Create the elasticsearch container.
5460 try (ElasticsearchContainer container = new ElasticsearchContainer ()
5561 .withEnv ("foo" , "bar" ) // dummy env for compiler checking correct generics usage
5662 ) {
63+ // Start the container. This step might take some time...
5764 container .start ();
65+
66+ // Do whatever you want with the rest client ...
5867 Response response = getClient (container ).performRequest (new Request ("GET" , "/" ));
5968 assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
6069 assertThat (EntityUtils .toString (response .getEntity ()), containsString (ELASTICSEARCH_DEFAULT_VERSION ));
@@ -69,19 +78,22 @@ public void elasticsearchDefaultTest() throws IOException {
6978
7079 @ Test
7180 public void elasticsearchVersion () throws IOException {
72- try (ElasticsearchContainer container = new ElasticsearchContainer ("docker.elastic.co/elasticsearch/elasticsearch:5.6.12" )) {
81+ try (ElasticsearchContainer container = new ElasticsearchContainer ("docker.elastic.co/elasticsearch/elasticsearch:" + ELASTICSEARCH_VERSION )) {
7382 container .start ();
7483 Response response = getClient (container ).performRequest (new Request ("GET" , "/" ));
7584 assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
7685 String responseAsString = EntityUtils .toString (response .getEntity ());
77- assertThat (responseAsString , containsString ("5.6.12" ));
86+ assertThat (responseAsString , containsString (ELASTICSEARCH_VERSION ));
7887 }
7988 }
8089
8190 @ Test
8291 public void elasticsearchOssImage () throws IOException {
83- try (ElasticsearchContainer container =
84- new ElasticsearchContainer ("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_DEFAULT_VERSION )) {
92+ try (
93+ // oosContainer {
94+ ElasticsearchContainer container = new ElasticsearchContainer ("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_VERSION )
95+ // }
96+ ) {
8597 container .start ();
8698 Response response = getClient (container ).performRequest (new Request ("GET" , "/" ));
8799 assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
@@ -92,21 +104,54 @@ public void elasticsearchOssImage() throws IOException {
92104 }
93105 }
94106
107+ @ Test
108+ public void restClientClusterHealth () throws IOException {
109+ // httpClientContainer {
110+ // Create the elasticsearch container.
111+ try (ElasticsearchContainer container = new ElasticsearchContainer ()) {
112+ // Start the container. This step might take some time...
113+ container .start ();
114+
115+ // Do whatever you want with the rest client ...
116+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
117+ credentialsProvider .setCredentials (AuthScope .ANY ,
118+ new UsernamePasswordCredentials (ELASTICSEARCH_USERNAME , ELASTICSEARCH_PASSWORD ));
119+
120+ client = RestClient .builder (HttpHost .create (container .getHttpHostAddress ()))
121+ .setHttpClientConfigCallback (httpClientBuilder -> httpClientBuilder .setDefaultCredentialsProvider (credentialsProvider ))
122+ .build ();
123+
124+ Response response = client .performRequest (new Request ("GET" , "/_cluster/health" ));
125+ // }}
126+ assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
127+ assertThat (EntityUtils .toString (response .getEntity ()), containsString ("cluster_name" ));
128+ // httpClientContainer {{
129+ }
130+ // }
131+ }
132+
95133 @ Test
96134 public void transportClientClusterHealth () {
135+ // transportClientContainer {
136+ // Create the elasticsearch container.
97137 try (ElasticsearchContainer container = new ElasticsearchContainer ()) {
138+ // Start the container. This step might take some time...
98139 container .start ();
99140
141+ // Do whatever you want with the transport client
100142 TransportAddress transportAddress = new TransportAddress (container .getTcpHost ());
101143 String expectedClusterName = "docker-cluster" ;
102144 Settings settings = Settings .builder ().put ("cluster.name" , expectedClusterName ).build ();
103145 try (TransportClient transportClient = new PreBuiltTransportClient (settings )
104146 .addTransportAddress (transportAddress )) {
105147 ClusterHealthResponse healths = transportClient .admin ().cluster ().prepareHealth ().get ();
106148 String clusterName = healths .getClusterName ();
149+ // }}}
107150 assertThat (clusterName , is (expectedClusterName ));
151+ // transportClientContainer {{{
108152 }
109153 }
154+ // }
110155 }
111156
112157 private RestClient getClient (ElasticsearchContainer container ) {
0 commit comments