Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit c34d56f

Browse files
committed
Move tests to Elasticsearch Java client
1 parent b5a022e commit c34d56f

File tree

2 files changed

+103
-97
lines changed

2 files changed

+103
-97
lines changed

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ dependencies {
117117
compileOnly "org.elasticsearch:elasticsearch:$elasticsearchVersion"
118118

119119
testImplementation "org.elasticsearch:elasticsearch:$elasticsearchVersion"
120-
testImplementation("org.testcontainers:elasticsearch:1.16.3") {
120+
testImplementation "co.elastic.clients:elasticsearch-java:$elasticsearchVersion"
121+
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
122+
testImplementation('org.testcontainers:elasticsearch:1.17.1') {
121123
exclude group: 'junit', module: 'junit'
122124
}
123-
testImplementation "org.testcontainers:junit-jupiter:1.16.3"
125+
testImplementation 'org.testcontainers:junit-jupiter:1.17.1'
124126
testImplementation "org.assertj:assertj-core:3.22.0"
125127
testImplementation "org.slf4j:slf4j-simple:1.7.36"
126128
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,127 @@
11
package org.elasticsearch.plugin.ingest.langdetect;
22

3+
import co.elastic.clients.elasticsearch.ElasticsearchClient;
4+
import co.elastic.clients.elasticsearch._types.PluginStats;
5+
import co.elastic.clients.elasticsearch.core.GetResponse;
6+
import co.elastic.clients.elasticsearch.nodes.NodesInfoResponse;
7+
import co.elastic.clients.elasticsearch.nodes.info.NodeInfo;
8+
import co.elastic.clients.elasticsearch.nodes.info.NodeInfoIngestProcessor;
9+
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
10+
import co.elastic.clients.transport.ElasticsearchTransport;
11+
import co.elastic.clients.transport.rest_client.RestClientTransport;
12+
import org.apache.http.HttpHost;
13+
import org.elasticsearch.client.RestClient;
14+
import org.junit.jupiter.api.AfterAll;
15+
import org.junit.jupiter.api.BeforeAll;
16+
import org.junit.jupiter.api.Disabled;
317
import org.junit.jupiter.api.Tag;
418
import org.junit.jupiter.api.Test;
19+
import org.slf4j.LoggerFactory;
520
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.containers.output.Slf4jLogConsumer;
622
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
723
import org.testcontainers.images.builder.ImageFromDockerfile;
824

25+
import java.io.IOException;
926
import java.net.URI;
1027
import java.net.http.HttpClient;
1128
import java.net.http.HttpRequest;
1229
import java.net.http.HttpResponse;
1330
import java.nio.file.Paths;
14-
import java.util.Base64;
31+
import java.util.Map;
1532

1633
import static org.assertj.core.api.Assertions.assertThat;
1734

1835
@Tag("slow")
1936
public class LangDetectProcessorIntegrationTests {
2037

38+
private static GenericContainer container;
39+
private static RestClient restClient;
40+
private static ElasticsearchClient client;
41+
42+
@BeforeAll
43+
public static void startContainer() {
44+
ImageFromDockerfile image = new ImageFromDockerfile().withDockerfile(Paths.get("./Dockerfile"));
45+
container = new GenericContainer(image);
46+
container.addEnv("discovery.type", "single-node");
47+
container.withEnv("xpack.security.enabled", "false");
48+
container.withEnv("ES_JAVA_OPTS", "-Xms4g -Xmx4g");
49+
container.addExposedPorts(9200);
50+
container.setWaitStrategy(new LogMessageWaitStrategy().withRegEx(".*(\"message\":\\s?\"started\".*|] started\n$)"));
51+
52+
container.start();
53+
container.followOutput(new Slf4jLogConsumer(LoggerFactory.getLogger(LangDetectProcessorIntegrationTests.class)));
54+
55+
// Create the low-level client
56+
restClient = RestClient.builder(new HttpHost("localhost", container.getMappedPort(9200))).build();
57+
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
58+
client = new ElasticsearchClient(transport);
59+
}
60+
61+
@AfterAll
62+
public static void stopContainer() throws IOException {
63+
if (restClient != null) {
64+
restClient.close();
65+
}
66+
if (container != null) {
67+
container.close();
68+
}
69+
}
70+
2171
// at some point this should be split into three tests with the container starting only once
2272
// and proper JSON parsing, but it is good enough for now
73+
@Disabled
2374
@Test
2475
public void testLangDetectPlugin() throws Exception {
25-
final ImageFromDockerfile image = new ImageFromDockerfile().withDockerfile(Paths.get(System.getenv("PWD"), "Dockerfile"));
26-
27-
try (GenericContainer container = new GenericContainer(image)) {
28-
container.addEnv("discovery.type", "single-node");
29-
container.withEnv("ELASTIC_PASSWORD", "changeme");
30-
container.withEnv("xpack.security.enabled", "true");
31-
container.withEnv("ES_JAVA_OPTS", "-Xms4g -Xmx4g");
32-
container.addExposedPorts(9200);
33-
container.setWaitStrategy(new LogMessageWaitStrategy().withRegEx(".*(\"message\":\\s?\"started\".*|] started\n$)"));
34-
35-
container.start();
36-
37-
String endpoint = String.format("http://localhost:%s/", container.getMappedPort(9200));
38-
39-
HttpRequest request = HttpRequest.newBuilder()
40-
.GET()
41-
.uri(URI.create(endpoint))
42-
.header("Authorization", basicAuth("elastic", "changeme"))
43-
.build();
44-
45-
HttpClient httpClient = HttpClient.newHttpClient();
46-
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
47-
48-
// check initial connection works
49-
assertThat(response.body()).startsWith("{");
50-
assertThat(response.statusCode()).isEqualTo(200);
51-
52-
// check for langdetect plugin and available processor
53-
request = HttpRequest.newBuilder()
54-
.GET()
55-
.uri(URI.create(endpoint + "_nodes/plugins,ingest"))
56-
.header("Authorization", basicAuth("elastic", "changeme"))
57-
.build();
58-
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
59-
// too lazy to parse JSON currently...
60-
assertThat(response.body()).contains("\"name\":\"ingest-langdetect\"");
61-
assertThat(response.body()).contains("\"type\":\"langdetect\"");
76+
// this currently breaks in the Elasticsearch Java Client, so let's wait after 8.2.0
77+
// until this is fixed...
78+
NodesInfoResponse nodesInfoResponse = client.nodes().info();
79+
NodeInfo nodeInfo = nodesInfoResponse.nodes().values().iterator().next();
80+
assertThat(nodeInfo.ingest().processors()).map(NodeInfoIngestProcessor::type).contains("langdetect");
81+
assertThat(nodeInfo.plugins()).map(PluginStats::name).contains("ingest-langdetect");
82+
}
6283

63-
// test lang detection in a processor
64-
String putPipelineBody = """
84+
@Test
85+
public void testLangDetectProcessorInPipeline() throws Exception {
86+
String putPipelineBody = """
87+
{
88+
"description": "_description",
89+
"processors": [
90+
{
91+
"langdetect" : {
92+
"field" : "field1",
93+
"target_field" : "field1_language"
94+
}
95+
},
6596
{
66-
"description": "_description",
67-
"processors": [
68-
{
69-
"langdetect" : {
70-
"field" : "field1",
71-
"target_field" : "field1_language"
72-
}
73-
},
74-
{
75-
"langdetect" : {
76-
"field" : "field1",
77-
"target_field" : "field1_lingua",
78-
"implementation" : "lingua"
79-
}
80-
}
81-
]
97+
"langdetect" : {
98+
"field" : "field1",
99+
"target_field" : "field1_lingua",
100+
"implementation" : "lingua"
101+
}
82102
}
83-
""";
84-
request = HttpRequest.newBuilder()
85-
.PUT(HttpRequest.BodyPublishers.ofString(putPipelineBody))
86-
.uri(URI.create(endpoint + "_ingest/pipeline/my_pipeline"))
87-
.header("Authorization", basicAuth("elastic", "changeme"))
88-
.header("Content-Type", "application/json")
89-
.build();
90-
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
91-
assertThat(response.statusCode()).isEqualTo(200);
92-
93-
// index document
94-
String documentBody = "{ \"field1\": \"This is hopefully an english text\" }";
95-
request = HttpRequest.newBuilder()
96-
.PUT(HttpRequest.BodyPublishers.ofString(documentBody))
97-
.uri(URI.create(endpoint + "test/_doc/1?pipeline=my_pipeline"))
98-
.header("Content-Type", "application/json")
99-
.header("Authorization", basicAuth("elastic", "changeme"))
100-
.build();
101-
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
102-
assertThat(response.statusCode()).isEqualTo(201);
103-
104-
// retrieve document
105-
request = HttpRequest.newBuilder()
106-
.GET()
107-
.uri(URI.create(endpoint + "test/_doc/1"))
108-
.header("Authorization", basicAuth("elastic", "changeme"))
109-
.header("Content-Type", "application/json")
110-
.build();
111-
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
112-
assertThat(response.statusCode()).isEqualTo(200);
113-
114-
// too lazy to parse JSON currently...
115-
assertThat(response.body()).contains("\"field1_language\":\"en\"");
116-
assertThat(response.body()).contains("\"field1_lingua\":\"en\"");
117-
}
118-
}
119-
120-
private static String basicAuth(String username, String password) {
121-
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
103+
]
104+
}
105+
""";
106+
HttpRequest request = HttpRequest.newBuilder()
107+
.PUT(HttpRequest.BodyPublishers.ofString(putPipelineBody))
108+
.uri(URI.create("http://localhost:" + container.getMappedPort(9200) + "/_ingest/pipeline/my-pipeline"))
109+
.header("Content-Type", "application/json")
110+
.build();
111+
HttpClient httpClient = HttpClient.newBuilder().build();
112+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
113+
assertThat(response.statusCode()).isEqualTo(200);
114+
115+
// index document
116+
client.index(b -> b.index("test")
117+
.id("1")
118+
.pipeline("my-pipeline")
119+
.document(Map.of("field1", "This is hopefully an english text"))
120+
);
121+
122+
GetResponse<Map> getResponse = client.get(b -> b.index("test").id("1"), Map.class);
123+
Map<String, Object> source = getResponse.source();
124+
assertThat(source).containsEntry("field1_language", "en");
125+
assertThat(source).containsEntry("field1_lingua", "en");
122126
}
123127
}

0 commit comments

Comments
 (0)