Skip to content

Commit 28d910b

Browse files
committed
Use codeinclude #1158
use codeinclude for the elasticsearch module documentation
1 parent 0eb2dea commit 28d910b

File tree

2 files changed

+98
-30
lines changed

2 files changed

+98
-30
lines changed

docs/modules/elasticsearch.md

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,25 @@ 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+
[Using an Elasticsearch container](../../modules/elasticsearch/src/test/java/org/testcontainers/containers/ElasticsearchDocumentationTest.java) inside_block:httpClientContainerStart
14+
[Using an Elasticsearch container](../../modules/elasticsearch/src/test/java/org/testcontainers/containers/ElasticsearchDocumentationTest.java) inside_block:transportClientContainerStart
15+
[Using an Elasticsearch container](../../modules/elasticsearch/src/test/java/org/testcontainers/containers/ElasticsearchDocumentationTest.java) inside_block:httpClientContainerStop
16+
<!--/codeinclude-->
1517

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-
```
3718

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

4223
## Choose your Elasticsearch license
4324

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:
25+
If you prefer to start a Docker image with the pure OSS version (which means with no security in older versions or
26+
other new and advanced features), you can use this instead:
4627

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

5232
## Adding this module to your project dependencies
5333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.testcontainers.elasticsearch;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.apache.http.HttpHost;
8+
import org.apache.http.auth.AuthScope;
9+
import org.apache.http.auth.UsernamePasswordCredentials;
10+
import org.apache.http.client.CredentialsProvider;
11+
import org.apache.http.impl.client.BasicCredentialsProvider;
12+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
13+
import org.elasticsearch.client.Request;
14+
import org.elasticsearch.client.Response;
15+
import org.elasticsearch.client.RestClient;
16+
import org.elasticsearch.client.transport.TransportClient;
17+
import org.elasticsearch.cluster.health.ClusterHealthStatus;
18+
import org.elasticsearch.common.settings.Settings;
19+
import org.elasticsearch.common.transport.TransportAddress;
20+
import org.elasticsearch.transport.client.PreBuiltTransportClient;
21+
import org.junit.Test;
22+
23+
public class ElasticsearchDocumentationTest {
24+
25+
@Test
26+
public void containerWithHttpClient() throws IOException {
27+
// httpClientContainerStart {
28+
// Create the elasticsearch container.
29+
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:6.4.1");
30+
31+
// Start the container. This step might take some time...
32+
container.start();
33+
34+
// Do whatever you want with the rest client ...
35+
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
36+
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
37+
RestClient restClient = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
38+
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
39+
.build();
40+
Response response = restClient.performRequest(new Request("GET", "/"));
41+
// }
42+
// Compare Result
43+
assertEquals(response.getStatusLine().getStatusCode(), 200);
44+
45+
// httpClientContainerStop {
46+
// Stop the container.
47+
container.stop();
48+
// }
49+
}
50+
51+
@Test
52+
public void containerWithTransportClient() {
53+
// Create the elasticsearch container.
54+
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:6.4.1");
55+
56+
// Start the container. This step might take some time...
57+
container.start();
58+
59+
// transportClientContainerStart {
60+
// ... or the transport client
61+
TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
62+
Settings settings = Settings.builder().put("cluster.name", "docker-cluster").build();
63+
TransportClient transportClient = new PreBuiltTransportClient(settings)
64+
.addTransportAddress(transportAddress);
65+
ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
66+
// }
67+
// Compare Result
68+
assertEquals(ClusterHealthStatus.GREEN, healths.getStatus());
69+
70+
// Stop the container.
71+
container.stop();
72+
}
73+
74+
@Test
75+
public void containerWithOpenSourceImage() {
76+
// oosContainer {
77+
// Create the elasticsearch container.
78+
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.4.1");
79+
// }
80+
81+
// Start the container. This step might take some time...
82+
container.start();
83+
84+
// Stop the container.
85+
container.stop();
86+
}
87+
88+
}

0 commit comments

Comments
 (0)