32
32
import java .util .function .Consumer ;
33
33
import java .util .stream .Stream ;
34
34
35
- import org .elasticsearch .action .index .IndexRequest ;
36
35
import org .elasticsearch .client .RequestOptions ;
37
36
import org .elasticsearch .client .RestHighLevelClient ;
38
- import org .elasticsearch .xcontent .XContentType ;
39
37
import org .junit .jupiter .api .DisplayName ;
40
38
import org .junit .jupiter .api .extension .ExtendWith ;
41
39
import org .junit .jupiter .params .ParameterizedTest ;
@@ -68,6 +66,10 @@ void shouldUseConfiguredProxy(ClientUnderTestFactory clientUnderTestFactory, Hov
68
66
wireMockServer (server -> {
69
67
70
68
// 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" )));
71
73
72
74
String serviceHost = "localhost:" + server .port ();
73
75
String proxyHost = "localhost:" + hoverfly .getHoverflyConfig ().getProxyPort ();
@@ -93,6 +95,12 @@ void shouldUseConfiguredProxy(ClientUnderTestFactory clientUnderTestFactory, Hov
93
95
void shouldConfigureClientAndSetAllRequiredHeaders (ClientUnderTestFactory clientUnderTestFactory ) {
94
96
wireMockServer (server -> {
95
97
98
+ WireMock .configureFor (server .port ());
99
+
100
+ stubForElasticsearchVersionCheck ();
101
+ stubFor (head (urlEqualTo ("/" )).willReturn (aResponse () //
102
+ .withHeader ("Content-Type" , "application/json; charset=UTF-8" )));
103
+
96
104
HttpHeaders defaultHeaders = new HttpHeaders ();
97
105
defaultHeaders .addAll ("def1" , Arrays .asList ("def1-1" , "def1-2" ));
98
106
defaultHeaders .add ("def2" , "def2-1" );
@@ -148,63 +156,6 @@ void shouldConfigureClientAndSetAllRequiredHeaders(ClientUnderTestFactory client
148
156
});
149
157
}
150
158
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
-
208
159
private StubMapping stubForElasticsearchVersionCheck () {
209
160
return stubFor (get (urlEqualTo ("/" )) //
210
161
.willReturn (okJson ("{\n " + //
@@ -228,12 +179,6 @@ private StubMapping stubForElasticsearchVersionCheck() {
228
179
.withHeader ("X-Elastic-Product" , "Elasticsearch" )));
229
180
}
230
181
231
- private StubMapping stubForHead () {
232
- return stubFor (head (urlEqualTo ("/" )) //
233
- .willReturn (ok () //
234
- .withHeader ("X-Elastic-Product" , "Elasticsearch" )));
235
- }
236
-
237
182
/**
238
183
* Consumer extension that catches checked exceptions and wraps them in a RuntimeException.
239
184
*/
@@ -253,8 +198,6 @@ default void accept(WireMockServer wiremockConsumer) {
253
198
254
199
/**
255
200
* 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.
258
201
*
259
202
* @param consumer the consumer
260
203
*/
@@ -265,10 +208,6 @@ private void wireMockServer(WiremockConsumer consumer) {
265
208
// test/resources/mappings
266
209
try {
267
210
wireMockServer .start ();
268
- WireMock .configureFor (wireMockServer .port ());
269
- stubForHead ();
270
- stubForElasticsearchVersionCheck ();
271
-
272
211
consumer .accept (wireMockServer );
273
212
} finally {
274
213
wireMockServer .shutdown ();
@@ -285,8 +224,6 @@ interface ClientUnderTest {
285
224
* @return true if successful
286
225
*/
287
226
boolean ping () throws Exception ;
288
-
289
- <T > void save (T entity ) throws IOException ;
290
227
}
291
228
292
229
/**
@@ -316,20 +253,7 @@ protected String getDisplayName() {
316
253
@ Override
317
254
ClientUnderTest create (ClientConfiguration clientConfiguration ) {
318
255
RestHighLevelClient client = RestClients .create (clientConfiguration ).rest ();
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
- };
256
+ return () -> client .ping (RequestOptions .DEFAULT );
333
257
}
334
258
335
259
}
@@ -347,20 +271,7 @@ protected String getDisplayName() {
347
271
@ Override
348
272
ClientUnderTest create (ClientConfiguration clientConfiguration ) {
349
273
ReactiveElasticsearchClient client = ReactiveRestClients .create (clientConfiguration );
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
- };
274
+ return () -> client .ping ().block ();
364
275
}
365
276
}
366
277
0 commit comments