Skip to content

Commit 7278c49

Browse files
authored
Use codeinclude for Elasticsearch Module (#2828)
Co-authored-by: Simon Schneider <[email protected]>
1 parent 1955de4 commit 7278c49

File tree

2 files changed

+66
-42
lines changed

2 files changed

+66
-42
lines changed

docs/modules/elasticsearch.md

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,24 @@ Note that it's based on the [official Docker image](https://www.elastic.co/guide
99

1010
You can start an elasticsearch container instance from any Java application by using:
1111

12-
```java
13-
// Create the elasticsearch container.
14-
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:6.4.1");
12+
<!--codeinclude-->
13+
[HttpClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer
14+
[TransportClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:transportClientContainer
15+
<!--/codeinclude-->
1516

16-
// Start the container. This step might take some time...
17-
container.start();
18-
19-
// Do whatever you want with the rest client ...
20-
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
21-
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
22-
RestClient restClient = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
23-
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
24-
.build();
25-
Response response = restClient.performRequest(new Request("GET", "/"));
26-
27-
// ... or the transport client
28-
TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
29-
Settings settings = Settings.builder().put("cluster.name", "docker-cluster").build();
30-
TransportClient transportClient = new PreBuiltTransportClient(settings)
31-
.addTransportAddress(transportAddress);
32-
ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
33-
34-
// Stop the container.
35-
container.stop();
36-
```
3717

3818
Note that if you are still using the [TransportClient](https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.3/transport-client.html)
3919
(not recommended as it is deprecated), the default cluster name is set to `docker-cluster` so you need to change `cluster.name` setting
4020
or set `client.transport.ignore_cluster_name` to `true`.
4121

4222
## Choose your Elasticsearch license
4323

44-
If you prefer to start a Docker image with the pure OSS version (which means with no security or
45-
other advanced features), you can use this instead:
24+
If you prefer to start a Docker image with the pure OSS version (which means with no security in older versions or
25+
other new and advanced features), you can use this instead:
4626

47-
```java
48-
// Create the elasticsearch container.
49-
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.4.1");
50-
```
27+
<!--codeinclude-->
28+
[Elasticsearch OSS](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:oosContainer
29+
<!--/codeinclude-->
5130

5231
## Adding this module to your project dependencies
5332

modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package 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

411
import org.apache.http.HttpHost;
512
import org.apache.http.auth.AuthScope;
613
import org.apache.http.auth.UsernamePasswordCredentials;
714
import org.apache.http.client.CredentialsProvider;
815
import org.apache.http.impl.client.BasicCredentialsProvider;
916
import org.apache.http.util.EntityUtils;
17+
import org.elasticsearch.Version;
1018
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
1119
import org.elasticsearch.client.Request;
1220
import org.elasticsearch.client.Response;
@@ -19,16 +27,13 @@
1927
import org.junit.After;
2028
import 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-
3030
public 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

Comments
 (0)