1919
2020package org .elasticsearch .client ;
2121
22- import org .apache .lucene .util .BytesRef ;
23- import org .elasticsearch .Build ;
24- import org .elasticsearch .Version ;
25- import org .elasticsearch .action .ActionListener ;
26- import org .elasticsearch .action .main .MainRequest ;
27- import org .elasticsearch .action .main .MainResponse ;
2822import org .apache .http .Header ;
2923import org .apache .http .HttpEntity ;
3024import org .apache .http .HttpHost ;
31- import org .apache .http .HttpResponse ;
3225import org .apache .http .ProtocolVersion ;
3326import org .apache .http .RequestLine ;
3427import org .apache .http .client .methods .HttpGet ;
3528import org .apache .http .entity .ByteArrayEntity ;
3629import org .apache .http .entity .ContentType ;
3730import org .apache .http .message .BasicHeader ;
38- import org .apache .http .message .BasicHttpResponse ;
3931import org .apache .http .message .BasicRequestLine ;
4032import org .apache .http .message .BasicStatusLine ;
33+ import org .apache .lucene .util .BytesRef ;
34+ import org .elasticsearch .Build ;
35+ import org .elasticsearch .Version ;
36+ import org .elasticsearch .action .ActionListener ;
37+ import org .elasticsearch .action .main .MainRequest ;
38+ import org .elasticsearch .action .main .MainResponse ;
39+ import org .elasticsearch .action .support .PlainActionFuture ;
40+ import org .elasticsearch .client .Request ;
41+ import org .elasticsearch .client .Response ;
42+ import org .elasticsearch .client .ResponseListener ;
43+ import org .elasticsearch .client .RestClient ;
44+ import org .elasticsearch .client .RestHighLevelClient ;
4145import org .elasticsearch .cluster .ClusterName ;
4246import org .elasticsearch .common .SuppressForbidden ;
4347import org .elasticsearch .common .xcontent .XContentHelper ;
4852import java .io .IOException ;
4953import java .lang .reflect .Method ;
5054import java .lang .reflect .Modifier ;
55+ import java .util .Arrays ;
5156import java .util .Collections ;
57+ import java .util .List ;
58+ import java .util .stream .Collectors ;
5259
5360import static java .util .Collections .emptyMap ;
5461import static java .util .Collections .emptySet ;
55- import static org .elasticsearch . client . ESRestHighLevelClientTestCase . execute ;
62+ import static org .hamcrest . Matchers . containsInAnyOrder ;
5663import static org .mockito .Matchers .any ;
5764import static org .mockito .Matchers .anyMapOf ;
5865import static org .mockito .Matchers .anyObject ;
5966import static org .mockito .Matchers .anyVararg ;
6067import static org .mockito .Matchers .eq ;
6168import static org .mockito .Mockito .doAnswer ;
6269import static org .mockito .Mockito .mock ;
70+ import static org .mockito .Mockito .when ;
6371
6472/**
6573 * Test and demonstrates how {@link RestHighLevelClient} can be extended to support custom endpoints.
@@ -92,31 +100,45 @@ public void testCustomEndpoint() throws IOException {
92100 final MainRequest request = new MainRequest ();
93101 final Header header = new BasicHeader ("node_name" , randomAlphaOfLengthBetween (1 , 10 ));
94102
95- MainResponse response = execute ( request , restHighLevelClient :: custom , restHighLevelClient :: customAsync , header );
103+ MainResponse response = restHighLevelClient . custom ( request , header );
96104 assertEquals (header .getValue (), response .getNodeName ());
97105
98- response = execute ( request , restHighLevelClient :: customAndParse , restHighLevelClient :: customAndParseAsync , header );
106+ response = restHighLevelClient . customAndParse ( request , header );
99107 assertEquals (header .getValue (), response .getNodeName ());
100108 }
101109
110+ public void testCustomEndpointAsync () throws Exception {
111+ final MainRequest request = new MainRequest ();
112+ final Header header = new BasicHeader ("node_name" , randomAlphaOfLengthBetween (1 , 10 ));
113+
114+ PlainActionFuture <MainResponse > future = PlainActionFuture .newFuture ();
115+ restHighLevelClient .customAsync (request , future , header );
116+ assertEquals (header .getValue (), future .get ().getNodeName ());
117+
118+ future = PlainActionFuture .newFuture ();
119+ restHighLevelClient .customAndParseAsync (request , future , header );
120+ assertEquals (header .getValue (), future .get ().getNodeName ());
121+ }
122+
102123 /**
103124 * The {@link RestHighLevelClient} must declare the following execution methods using the <code>protected</code> modifier
104125 * so that they can be used by subclasses to implement custom logic.
105126 */
106127 @ SuppressForbidden (reason = "We're forced to uses Class#getDeclaredMethods() here because this test checks protected methods" )
107128 public void testMethodsVisibility () throws ClassNotFoundException {
108- String [] methodNames = new String []{"performRequest" , "performRequestAndParseEntity" , "performRequestAsync" ,
109- "performRequestAsyncAndParseEntity" };
110- for (String methodName : methodNames ) {
111- boolean found = false ;
112- for (Method method : RestHighLevelClient .class .getDeclaredMethods ()) {
113- if (method .getName ().equals (methodName )) {
114- assertTrue ("Method " + methodName + " must be protected" , Modifier .isProtected (method .getModifiers ()));
115- found = true ;
116- }
117- }
118- assertTrue ("Failed to find method " + methodName , found );
119- }
129+ final String [] methodNames = new String []{"performRequest" ,
130+ "performRequestAsync" ,
131+ "performRequestAndParseEntity" ,
132+ "performRequestAsyncAndParseEntity" ,
133+ "parseEntity" ,
134+ "parseResponseException" };
135+
136+ final List <String > protectedMethods = Arrays .stream (RestHighLevelClient .class .getDeclaredMethods ())
137+ .filter (method -> Modifier .isProtected (method .getModifiers ()))
138+ .map (Method ::getName )
139+ .collect (Collectors .toList ());
140+
141+ assertThat (protectedMethods , containsInAnyOrder (methodNames ));
120142 }
121143
122144 /**
@@ -135,15 +157,20 @@ private Void mockPerformRequestAsync(Header httpHeader, ResponseListener respons
135157 * Mocks the synchronous request execution like if it was executed by Elasticsearch.
136158 */
137159 private Response mockPerformRequest (Header httpHeader ) throws IOException {
160+ final Response mockResponse = mock (Response .class );
161+ when (mockResponse .getHost ()).thenReturn (new HttpHost ("localhost" , 9200 ));
162+
138163 ProtocolVersion protocol = new ProtocolVersion ("HTTP" , 1 , 1 );
139- HttpResponse httpResponse = new BasicHttpResponse (new BasicStatusLine (protocol , 200 , "OK" ));
164+ when ( mockResponse . getStatusLine ()). thenReturn (new BasicStatusLine (protocol , 200 , "OK" ));
140165
141166 MainResponse response = new MainResponse (httpHeader .getValue (), Version .CURRENT , ClusterName .DEFAULT , "_na" , Build .CURRENT , true );
142167 BytesRef bytesRef = XContentHelper .toXContent (response , XContentType .JSON , false ).toBytesRef ();
143- httpResponse . setEntity (new ByteArrayEntity (bytesRef .bytes , ContentType .APPLICATION_JSON ));
168+ when ( mockResponse . getEntity ()). thenReturn (new ByteArrayEntity (bytesRef .bytes , ContentType .APPLICATION_JSON ));
144169
145170 RequestLine requestLine = new BasicRequestLine (HttpGet .METHOD_NAME , ENDPOINT , protocol );
146- return new Response (requestLine , new HttpHost ("localhost" , 9200 ), httpResponse );
171+ when (mockResponse .getRequestLine ()).thenReturn (requestLine );
172+
173+ return mockResponse ;
147174 }
148175
149176 /**
0 commit comments