3232import java .util .function .Consumer ;
3333import java .util .stream .Stream ;
3434
35- import org .elasticsearch .action .index .IndexRequest ;
3635import org .elasticsearch .client .RequestOptions ;
3736import org .elasticsearch .client .RestHighLevelClient ;
38- import org .elasticsearch .xcontent .XContentType ;
3937import org .junit .jupiter .api .DisplayName ;
4038import org .junit .jupiter .api .extension .ExtendWith ;
4139import org .junit .jupiter .params .ParameterizedTest ;
@@ -68,6 +66,10 @@ void shouldUseConfiguredProxy(ClientUnderTestFactory clientUnderTestFactory, Hov
6866 wireMockServer (server -> {
6967
7068 // 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" )));
7173
7274 String serviceHost = "localhost:" + server .port ();
7375 String proxyHost = "localhost:" + hoverfly .getHoverflyConfig ().getProxyPort ();
@@ -93,6 +95,12 @@ void shouldUseConfiguredProxy(ClientUnderTestFactory clientUnderTestFactory, Hov
9395 void shouldConfigureClientAndSetAllRequiredHeaders (ClientUnderTestFactory clientUnderTestFactory ) {
9496 wireMockServer (server -> {
9597
98+ WireMock .configureFor (server .port ());
99+
100+ stubForElasticsearchVersionCheck ();
101+ stubFor (head (urlEqualTo ("/" )).willReturn (aResponse () //
102+ .withHeader ("Content-Type" , "application/json; charset=UTF-8" )));
103+
96104 HttpHeaders defaultHeaders = new HttpHeaders ();
97105 defaultHeaders .addAll ("def1" , Arrays .asList ("def1-1" , "def1-2" ));
98106 defaultHeaders .add ("def2" , "def2-1" );
@@ -148,63 +156,6 @@ void shouldConfigureClientAndSetAllRequiredHeaders(ClientUnderTestFactory client
148156 });
149157 }
150158
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-
208159 private StubMapping stubForElasticsearchVersionCheck () {
209160 return stubFor (get (urlEqualTo ("/" )) //
210161 .willReturn (okJson ("{\n " + //
@@ -228,12 +179,6 @@ private StubMapping stubForElasticsearchVersionCheck() {
228179 .withHeader ("X-Elastic-Product" , "Elasticsearch" )));
229180 }
230181
231- private StubMapping stubForHead () {
232- return stubFor (head (urlEqualTo ("/" )) //
233- .willReturn (ok () //
234- .withHeader ("X-Elastic-Product" , "Elasticsearch" )));
235- }
236-
237182 /**
238183 * Consumer extension that catches checked exceptions and wraps them in a RuntimeException.
239184 */
@@ -253,8 +198,6 @@ default void accept(WireMockServer wiremockConsumer) {
253198
254199 /**
255200 * 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.
258201 *
259202 * @param consumer the consumer
260203 */
@@ -265,10 +208,6 @@ private void wireMockServer(WiremockConsumer consumer) {
265208 // test/resources/mappings
266209 try {
267210 wireMockServer .start ();
268- WireMock .configureFor (wireMockServer .port ());
269- stubForHead ();
270- stubForElasticsearchVersionCheck ();
271-
272211 consumer .accept (wireMockServer );
273212 } finally {
274213 wireMockServer .shutdown ();
@@ -285,8 +224,6 @@ interface ClientUnderTest {
285224 * @return true if successful
286225 */
287226 boolean ping () throws Exception ;
288-
289- <T > void save (T entity ) throws IOException ;
290227 }
291228
292229 /**
@@ -316,20 +253,7 @@ protected String getDisplayName() {
316253 @ Override
317254 ClientUnderTest create (ClientConfiguration clientConfiguration ) {
318255 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 );
333257 }
334258
335259 }
@@ -347,20 +271,7 @@ protected String getDisplayName() {
347271 @ Override
348272 ClientUnderTest create (ClientConfiguration clientConfiguration ) {
349273 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 ();
364275 }
365276 }
366277
0 commit comments