32
32
import java .util .function .Consumer ;
33
33
import java .util .stream .Stream ;
34
34
35
+ import org .elasticsearch .action .index .IndexRequest ;
35
36
import org .elasticsearch .client .RequestOptions ;
36
37
import org .elasticsearch .client .RestHighLevelClient ;
38
+ import org .elasticsearch .xcontent .XContentType ;
37
39
import org .junit .jupiter .api .DisplayName ;
38
40
import org .junit .jupiter .api .extension .ExtendWith ;
39
41
import org .junit .jupiter .params .ParameterizedTest ;
@@ -66,10 +68,6 @@ void shouldUseConfiguredProxy(ClientUnderTestFactory clientUnderTestFactory, Hov
66
68
wireMockServer (server -> {
67
69
68
70
// wiremock is the dummy server, hoverfly the proxy
69
- WireMock .configureFor (server .port ());
70
- stubForElasticsearchVersionCheck ();
71
- stubFor (head (urlEqualTo ("/" )).willReturn (aResponse () //
72
- .withHeader ("Content-Type" , "application/json; charset=UTF-8" )));
73
71
74
72
String serviceHost = "localhost:" + server .port ();
75
73
String proxyHost = "localhost:" + hoverfly .getHoverflyConfig ().getProxyPort ();
@@ -95,12 +93,6 @@ void shouldUseConfiguredProxy(ClientUnderTestFactory clientUnderTestFactory, Hov
95
93
void shouldConfigureClientAndSetAllRequiredHeaders (ClientUnderTestFactory clientUnderTestFactory ) {
96
94
wireMockServer (server -> {
97
95
98
- WireMock .configureFor (server .port ());
99
-
100
- stubForElasticsearchVersionCheck ();
101
- stubFor (head (urlEqualTo ("/" )).willReturn (aResponse () //
102
- .withHeader ("Content-Type" , "application/json; charset=UTF-8" )));
103
-
104
96
HttpHeaders defaultHeaders = new HttpHeaders ();
105
97
defaultHeaders .addAll ("def1" , Arrays .asList ("def1-1" , "def1-2" ));
106
98
defaultHeaders .add ("def2" , "def2-1" );
@@ -156,6 +148,63 @@ void shouldConfigureClientAndSetAllRequiredHeaders(ClientUnderTestFactory client
156
148
});
157
149
}
158
150
151
+ @ ParameterizedTest // #2088
152
+ @ MethodSource ("clientUnderTestFactorySource" )
153
+ @ DisplayName ("should set compatibility headers" )
154
+ void shouldSetCompatibilityHeaders (ClientUnderTestFactory clientUnderTestFactory ) {
155
+
156
+ wireMockServer (server -> {
157
+
158
+ stubFor (put (urlMatching ("^/index/_doc/42(\\ ?.*)$?" )) //
159
+ .willReturn (jsonResponse ("{\n " + //
160
+ " \" _id\" : \" 42\" ,\n " + //
161
+ " \" _index\" : \" test\" ,\n " + //
162
+ " \" _primary_term\" : 1,\n " + //
163
+ " \" _seq_no\" : 0,\n " + //
164
+ " \" _shards\" : {\n " + //
165
+ " \" failed\" : 0,\n " + //
166
+ " \" successful\" : 1,\n " + //
167
+ " \" total\" : 2\n " + //
168
+ " },\n " + //
169
+ " \" _type\" : \" _doc\" ,\n " + //
170
+ " \" _version\" : 1,\n " + //
171
+ " \" result\" : \" created\" \n " + //
172
+ "}\n " //
173
+ , 201 ) //
174
+ .withHeader ("Content-Type" , "application/vnd.elasticsearch+json;compatible-with=7" ) //
175
+ .withHeader ("X-Elastic-Product" , "Elasticsearch" )));
176
+
177
+ ClientConfigurationBuilder configurationBuilder = new ClientConfigurationBuilder ();
178
+ configurationBuilder //
179
+ .connectedTo ("localhost:" + server .port ()) //
180
+ .withHeaders (() -> {
181
+ HttpHeaders defaultCompatibilityHeaders = new HttpHeaders ();
182
+ defaultCompatibilityHeaders .add ("Accept" , "application/vnd.elasticsearch+json;compatible-with=7" );
183
+ defaultCompatibilityHeaders .add ("Content-Type" , "application/vnd.elasticsearch+json;compatible-with=7" );
184
+ return defaultCompatibilityHeaders ;
185
+ });
186
+
187
+ ClientConfiguration clientConfiguration = configurationBuilder .build ();
188
+ ClientUnderTest clientUnderTest = clientUnderTestFactory .create (clientConfiguration );
189
+
190
+ class Foo {
191
+ public String id ;
192
+
193
+ Foo (String id ) {
194
+ this .id = id ;
195
+ }
196
+ }
197
+ ;
198
+
199
+ clientUnderTest .save (new Foo ("42" ));
200
+
201
+ verify (putRequestedFor (urlMatching ("^/index/_doc/42(\\ ?.*)$?" )) //
202
+ .withHeader ("Accept" , new EqualToPattern ("application/vnd.elasticsearch+json;compatible-with=7" )) //
203
+ .withHeader ("Content-Type" , new EqualToPattern ("application/vnd.elasticsearch+json;compatible-with=7" )) //
204
+ );
205
+ });
206
+ }
207
+
159
208
private StubMapping stubForElasticsearchVersionCheck () {
160
209
return stubFor (get (urlEqualTo ("/" )) //
161
210
.willReturn (okJson ("{\n " + //
@@ -179,6 +228,12 @@ private StubMapping stubForElasticsearchVersionCheck() {
179
228
.withHeader ("X-Elastic-Product" , "Elasticsearch" )));
180
229
}
181
230
231
+ private StubMapping stubForHead () {
232
+ return stubFor (head (urlEqualTo ("/" )) //
233
+ .willReturn (ok () //
234
+ .withHeader ("X-Elastic-Product" , "Elasticsearch" )));
235
+ }
236
+
182
237
/**
183
238
* Consumer extension that catches checked exceptions and wraps them in a RuntimeException.
184
239
*/
@@ -198,6 +253,8 @@ default void accept(WireMockServer wiremockConsumer) {
198
253
199
254
/**
200
255
* starts a Wiremock server and calls consumer with the server as argument. Stops the server after consumer execution.
256
+ * Before the consumer ids called the {@link #stubForHead()} and {@link #stubForElasticsearchVersionCheck()} are
257
+ * registered.
201
258
*
202
259
* @param consumer the consumer
203
260
*/
@@ -208,6 +265,10 @@ private void wireMockServer(WiremockConsumer consumer) {
208
265
// test/resources/mappings
209
266
try {
210
267
wireMockServer .start ();
268
+ WireMock .configureFor (wireMockServer .port ());
269
+ stubForHead ();
270
+ stubForElasticsearchVersionCheck ();
271
+
211
272
consumer .accept (wireMockServer );
212
273
} finally {
213
274
wireMockServer .shutdown ();
@@ -224,6 +285,8 @@ interface ClientUnderTest {
224
285
* @return true if successful
225
286
*/
226
287
boolean ping () throws Exception ;
288
+
289
+ <T > void save (T entity ) throws IOException ;
227
290
}
228
291
229
292
/**
@@ -253,7 +316,20 @@ protected String getDisplayName() {
253
316
@ Override
254
317
ClientUnderTest create (ClientConfiguration clientConfiguration ) {
255
318
RestHighLevelClient client = RestClients .create (clientConfiguration ).rest ();
256
- return () -> client .ping (RequestOptions .DEFAULT );
319
+ return new ClientUnderTest () {
320
+ @ Override
321
+ public boolean ping () throws Exception {
322
+ return client .ping (RequestOptions .DEFAULT );
323
+ }
324
+
325
+ @ Override
326
+ public <T > void save (T entity ) throws IOException {
327
+ IndexRequest indexRequest = new IndexRequest ("index" );
328
+ indexRequest .id ("42" );
329
+ indexRequest .source (entity , XContentType .JSON );
330
+ client .index (indexRequest , RequestOptions .DEFAULT );
331
+ }
332
+ };
257
333
}
258
334
259
335
}
@@ -271,7 +347,20 @@ protected String getDisplayName() {
271
347
@ Override
272
348
ClientUnderTest create (ClientConfiguration clientConfiguration ) {
273
349
ReactiveElasticsearchClient client = ReactiveRestClients .create (clientConfiguration );
274
- return () -> client .ping ().block ();
350
+ return new ClientUnderTest () {
351
+ @ Override
352
+ public boolean ping () throws Exception {
353
+ return client .ping ().block ();
354
+ }
355
+
356
+ @ Override
357
+ public <T > void save (T entity ) throws IOException {
358
+ IndexRequest indexRequest = new IndexRequest ("index" );
359
+ indexRequest .id ("42" );
360
+ indexRequest .source ("{}" , XContentType .JSON );
361
+ client .index (indexRequest ).block ();
362
+ }
363
+ };
275
364
}
276
365
}
277
366
0 commit comments