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