Skip to content

Commit ade7e0f

Browse files
authored
Update to latest API spec, update ES test server to use SSL (#384) (#387)
1 parent 537c16a commit ade7e0f

File tree

3 files changed

+132
-9
lines changed

3 files changed

+132
-9
lines changed

java-client/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ dependencies {
225225

226226
// MIT
227227
// https://www.testcontainers.org/
228-
testImplementation("org.testcontainers", "testcontainers", "1.17.2")
229-
testImplementation("org.testcontainers", "elasticsearch", "1.17.2")
228+
testImplementation("org.testcontainers", "testcontainers", "1.17.3")
229+
testImplementation("org.testcontainers", "elasticsearch", "1.17.3")
230230
}
231231

232232

java-client/src/test/java/co/elastic/clients/elasticsearch/ElasticsearchTestServer.java

+48-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
2727
import co.elastic.clients.transport.ElasticsearchTransport;
2828
import co.elastic.clients.transport.JsonEndpoint;
29+
import co.elastic.clients.transport.Version;
2930
import co.elastic.clients.transport.endpoints.DelegatingJsonEndpoint;
3031
import co.elastic.clients.transport.rest_client.RestClientTransport;
3132
import org.apache.http.HttpHost;
@@ -34,12 +35,16 @@
3435
import org.apache.http.impl.client.BasicCredentialsProvider;
3536
import org.elasticsearch.client.RestClient;
3637
import org.testcontainers.elasticsearch.ElasticsearchContainer;
38+
import org.testcontainers.images.builder.ImageFromDockerfile;
39+
import org.testcontainers.utility.DockerImageName;
3740

41+
import javax.net.ssl.SSLContext;
3842
import java.io.IOException;
3943
import java.time.Duration;
4044

4145
public class ElasticsearchTestServer implements AutoCloseable {
4246

47+
private final String[] plugins;
4348
private volatile ElasticsearchContainer container;
4449
private int port;
4550
private final JsonpMapper mapper = new JsonbJsonpMapper();
@@ -54,7 +59,7 @@ public static synchronized ElasticsearchTestServer global() {
5459
System.out.println("Starting global ES test server.");
5560
global = new ElasticsearchTestServer();
5661
try {
57-
global.setup();
62+
global.start();
5863
} catch (Exception e) {
5964
e.printStackTrace();
6065
throw e;
@@ -67,24 +72,62 @@ public static synchronized ElasticsearchTestServer global() {
6772
return global;
6873
}
6974

70-
private synchronized void setup() {
71-
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.17.4")
75+
public ElasticsearchTestServer(String... plugins) {
76+
this.plugins = plugins;
77+
}
78+
79+
public synchronized ElasticsearchTestServer start() {
80+
Version version = Version.VERSION.major() < 8 ? new Version(7,17,5,false) : new Version(8,3,3,false);
81+
82+
// Note we could use version.major() + "." + version.minor() + "-SNAPSHOT" but plugins won't install on a snapshot version
83+
String esImage = "docker.elastic.co/elasticsearch/elasticsearch:" + version;
84+
85+
DockerImageName image;
86+
if (plugins.length == 0) {
87+
image = DockerImageName.parse(esImage);
88+
} else {
89+
String esWithPluginsImage = new ImageFromDockerfile()
90+
.withDockerfileFromBuilder(b -> {
91+
b.from(esImage);
92+
for (String plugin : plugins) {
93+
b.run("/usr/share/elasticsearch/bin/elasticsearch-plugin", "install", plugin);
94+
}
95+
}
96+
).get();
97+
98+
image = DockerImageName
99+
.parse(esWithPluginsImage)
100+
.asCompatibleSubstituteFor("docker.elastic.co/elasticsearch/elasticsearch");
101+
}
102+
103+
container = new ElasticsearchContainer(image)
72104
.withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m")
73105
.withEnv("path.repo", "/tmp") // for snapshots
74106
.withStartupTimeout(Duration.ofSeconds(60))
75107
.withPassword("changeme");
76108
container.start();
109+
77110
port = container.getMappedPort(9200);
78111

112+
boolean useTLS = version.major() >= 8;
113+
HttpHost host = new HttpHost("localhost", port, useTLS ? "https": "http");
114+
115+
SSLContext sslContext = useTLS ? container.createSslContextFromCa() : null;
116+
79117
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
80118
credsProv.setCredentials(
81119
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")
82120
);
83-
restClient = RestClient.builder(new HttpHost("localhost", port))
84-
.setHttpClientConfigCallback(hc -> hc.setDefaultCredentialsProvider(credsProv))
121+
restClient = RestClient.builder(host)
122+
.setHttpClientConfigCallback(hc -> hc
123+
.setDefaultCredentialsProvider(credsProv)
124+
.setSSLContext(sslContext)
125+
)
85126
.build();
86127
transport = new RestClientTransport(restClient, mapper);
87128
client = new ElasticsearchClient(transport);
129+
130+
return this;
88131
}
89132

90133
/**

java-client/src/test/java/co/elastic/clients/elasticsearch/spec_issues/SpecIssuesTest.java

+82-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919

2020
package co.elastic.clients.elasticsearch.spec_issues;
2121

22+
import co.elastic.clients.documentation.usage.Product;
2223
import co.elastic.clients.elasticsearch.ElasticsearchClient;
2324
import co.elastic.clients.elasticsearch.ElasticsearchTestServer;
2425
import co.elastic.clients.elasticsearch._types.ErrorResponse;
26+
import co.elastic.clients.elasticsearch._types.Script;
2527
import co.elastic.clients.elasticsearch._types.analysis.LimitTokenCountTokenFilter;
28+
import co.elastic.clients.elasticsearch._types.mapping.Property;
29+
import co.elastic.clients.elasticsearch._types.mapping.RuntimeField;
30+
import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
2631
import co.elastic.clients.elasticsearch.cluster.ClusterStatsResponse;
2732
import co.elastic.clients.elasticsearch.core.SearchRequest;
2833
import co.elastic.clients.elasticsearch.core.SearchResponse;
34+
import co.elastic.clients.elasticsearch.core.search.Suggester;
2935
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
3036
import co.elastic.clients.elasticsearch.indices.GetFieldMappingRequest;
3137
import co.elastic.clients.elasticsearch.indices.GetFieldMappingResponse;
@@ -75,11 +81,40 @@ public void i0201_restoreResponse() throws Exception {
7581
RestoreResponse restoreResponse = fromJson("{\"acknowledged\":true}", RestoreResponse.class);
7682
}
7783

84+
@Test
85+
public void i0298_runtimeMappings() throws Exception {
86+
ElasticsearchClient client = ElasticsearchTestServer.global().client();
87+
88+
String index = "i0298";
89+
90+
Product p = new Product("p1", "p2", 42.0);
91+
92+
client.index(ir -> ir
93+
.index(index)
94+
.document(p));
95+
96+
client.indices().flush(f -> f.index(index));
97+
98+
RuntimeField runtimeField = RuntimeField.of(rf -> rf
99+
.type(RuntimeFieldType.Double)
100+
.script(Script.of(s -> s
101+
.inline(i -> i.
102+
source("emit(doc['price'].value * 1.19)")
103+
)
104+
))
105+
);
106+
107+
client.search(sr -> sr
108+
.index(index)
109+
.runtimeMappings("priceWithTax", runtimeField), // NOTE: the builder accepts only lists here
110+
Product.class);
111+
}
112+
78113
@Test
79114
public void i0297_mappingSettings() {
80115

81116
CreateIndexRequest request = CreateIndexRequest.of(r -> r
82-
.index("name")
117+
.index("i0297")
83118
.settings(s -> s
84119
// This is "mapping" and not "mappings"
85120
.mapping(m -> m.totalFields(totalFields -> totalFields.limit(1001)))
@@ -118,11 +153,56 @@ public void i0295_mappingSettings() {
118153
"}";
119154

120155
CreateIndexRequest request = CreateIndexRequest.of(r -> r
121-
.index("name")
156+
.index("i0295")
122157
.withJson(new StringReader(json))
123158
);
124159
}
125160

161+
@Test
162+
public void i0254_suggesterTest() throws Exception {
163+
new Suggester.Builder().suggesters("song-suggest", s -> s.completion(c->c.field("suggest"))).build();
164+
}
165+
166+
@Test
167+
public void i0249_variantKind() throws Exception {
168+
try (ElasticsearchTestServer server = new ElasticsearchTestServer("analysis-icu").start()) {
169+
170+
ElasticsearchClient esClient = server.client();
171+
172+
esClient.indices().create(r -> r
173+
.index("i0249")
174+
.withJson(new StringReader("{\n" +
175+
" \"mappings\": {\n" +
176+
" \"properties\": {\n" +
177+
" \"name\": { \n" +
178+
" \"type\": \"text\",\n" +
179+
" \"fields\": {\n" +
180+
" \"sort\": { \n" +
181+
" \"type\": \"icu_collation_keyword\",\n" +
182+
" \"index\": false,\n" +
183+
" \"language\": \"de\",\n" +
184+
" \"country\": \"DE\",\n" +
185+
" \"variant\": \"@collation=phonebook\"\n" +
186+
" }\n" +
187+
" }\n" +
188+
" }\n" +
189+
" }\n" +
190+
" }\n" +
191+
"}\n"))
192+
);
193+
194+
GetFieldMappingResponse fm = esClient.indices().getFieldMapping(b -> b
195+
.index("i0249")
196+
.fields("*")
197+
);
198+
199+
Property property = fm.get("i0249").mappings().get("name").mapping().get("name").text().fields().get("sort");
200+
201+
assertTrue(property._isCustom());
202+
assertEquals("icu_collation_keyword", property._customKind());
203+
}
204+
}
205+
126206
@Test
127207
public void i0199_consumeAllTokensOptional() {
128208
// https://github.com/elastic/elasticsearch-java/issues/199

0 commit comments

Comments
 (0)