-
Notifications
You must be signed in to change notification settings - Fork 25.6k
HLRest: Allow caller to set per request options #30490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
1964066
647f4ca
d2f56a2
2642f01
2952791
fad1513
c9f2320
afb800f
4f2bd82
5330e72
07c3bd5
d1a72a4
f64db06
f83454c
0cb6502
bb4f6f3
f4630b5
c1b3321
364913e
74be65c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ | |
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.entity.ByteArrayEntity; | ||
| import org.apache.http.entity.ContentType; | ||
| import org.apache.http.message.BasicHeader; | ||
| import org.apache.http.message.BasicRequestLine; | ||
| import org.apache.http.message.BasicStatusLine; | ||
| import org.apache.lucene.util.BytesRef; | ||
|
|
@@ -48,11 +47,13 @@ | |
| import java.lang.reflect.Modifier; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static java.util.Collections.emptySet; | ||
| import static org.hamcrest.Matchers.containsInAnyOrder; | ||
| import static org.hamcrest.Matchers.contains; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
| import static org.mockito.Matchers.any; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
@@ -73,12 +74,12 @@ public void initClients() throws IOException { | |
| final RestClient restClient = mock(RestClient.class); | ||
| restHighLevelClient = new CustomRestClient(restClient); | ||
|
|
||
| doAnswer(inv -> mockPerformRequest(((Request) inv.getArguments()[0]).getHeaders().iterator().next())) | ||
| doAnswer(inv -> mockPerformRequest((Request) inv.getArguments()[0])) | ||
| .when(restClient) | ||
| .performRequest(any(Request.class)); | ||
|
|
||
| doAnswer(inv -> mockPerformRequestAsync( | ||
| ((Request) inv.getArguments()[0]).getHeaders().iterator().next(), | ||
| ((Request) inv.getArguments()[0]), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you fix the javadocs link for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| (ResponseListener) inv.getArguments()[1])) | ||
| .when(restClient) | ||
| .performRequestAsync(any(Request.class), any(ResponseListener.class)); | ||
|
|
@@ -87,26 +88,32 @@ public void initClients() throws IOException { | |
|
|
||
| public void testCustomEndpoint() throws IOException { | ||
| final MainRequest request = new MainRequest(); | ||
| final Header header = new BasicHeader("node_name", randomAlphaOfLengthBetween(1, 10)); | ||
| String nodeName = randomAlphaOfLengthBetween(1, 10); | ||
|
|
||
| MainResponse response = restHighLevelClient.custom(request, header); | ||
| assertEquals(header.getValue(), response.getNodeName()); | ||
| MainResponse response = restHighLevelClient.custom(request, optionsForNodeName(nodeName)); | ||
| assertEquals(nodeName, response.getNodeName()); | ||
|
|
||
| response = restHighLevelClient.customAndParse(request, header); | ||
| assertEquals(header.getValue(), response.getNodeName()); | ||
| response = restHighLevelClient.customAndParse(request, optionsForNodeName(nodeName)); | ||
| assertEquals(nodeName, response.getNodeName()); | ||
| } | ||
|
|
||
| public void testCustomEndpointAsync() throws Exception { | ||
| final MainRequest request = new MainRequest(); | ||
| final Header header = new BasicHeader("node_name", randomAlphaOfLengthBetween(1, 10)); | ||
| String nodeName = randomAlphaOfLengthBetween(1, 10); | ||
|
|
||
| PlainActionFuture<MainResponse> future = PlainActionFuture.newFuture(); | ||
| restHighLevelClient.customAsync(request, future, header); | ||
| assertEquals(header.getValue(), future.get().getNodeName()); | ||
| restHighLevelClient.customAsync(request, optionsForNodeName(nodeName), future); | ||
| assertEquals(nodeName, future.get().getNodeName()); | ||
|
|
||
| future = PlainActionFuture.newFuture(); | ||
| restHighLevelClient.customAndParseAsync(request, future, header); | ||
| assertEquals(header.getValue(), future.get().getNodeName()); | ||
| restHighLevelClient.customAndParseAsync(request, optionsForNodeName(nodeName), future); | ||
| assertEquals(nodeName, future.get().getNodeName()); | ||
| } | ||
|
|
||
| private RequestOptions optionsForNodeName(String nodeName) { | ||
|
||
| RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); | ||
| options.addHeader("node_name", nodeName); | ||
| return options.build(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -115,27 +122,27 @@ public void testCustomEndpointAsync() throws Exception { | |
| */ | ||
| @SuppressForbidden(reason = "We're forced to uses Class#getDeclaredMethods() here because this test checks protected methods") | ||
| public void testMethodsVisibility() throws ClassNotFoundException { | ||
| final String[] methodNames = new String[]{"performRequest", | ||
| "performRequestAsync", | ||
| final String[] methodNames = new String[]{"parseEntity", | ||
| "parseResponseException", | ||
| "performRequest", | ||
| "performRequestAndParseEntity", | ||
| "performRequestAsyncAndParseEntity", | ||
| "parseEntity", | ||
| "parseResponseException"}; | ||
| "performRequestAsync", | ||
| "performRequestAsyncAndParseEntity"}; | ||
|
|
||
| final List<String> protectedMethods = Arrays.stream(RestHighLevelClient.class.getDeclaredMethods()) | ||
| final Set<String> protectedMethods = Arrays.stream(RestHighLevelClient.class.getDeclaredMethods()) | ||
| .filter(method -> Modifier.isProtected(method.getModifiers())) | ||
| .map(Method::getName) | ||
| .collect(Collectors.toList()); | ||
| .collect(Collectors.toCollection(TreeSet::new)); | ||
|
|
||
| assertThat(protectedMethods, containsInAnyOrder(methodNames)); | ||
| assertThat(protectedMethods, contains(methodNames)); | ||
| } | ||
|
|
||
| /** | ||
| * Mocks the asynchronous request execution by calling the {@link #mockPerformRequest(Header)} method. | ||
| */ | ||
| private Void mockPerformRequestAsync(Header httpHeader, ResponseListener responseListener) { | ||
| private Void mockPerformRequestAsync(Request request, ResponseListener responseListener) { | ||
| try { | ||
| responseListener.onSuccess(mockPerformRequest(httpHeader)); | ||
| responseListener.onSuccess(mockPerformRequest(request)); | ||
| } catch (IOException e) { | ||
| responseListener.onFailure(e); | ||
| } | ||
|
|
@@ -145,7 +152,9 @@ private Void mockPerformRequestAsync(Header httpHeader, ResponseListener respons | |
| /** | ||
| * Mocks the synchronous request execution like if it was executed by Elasticsearch. | ||
| */ | ||
| private Response mockPerformRequest(Header httpHeader) throws IOException { | ||
| private Response mockPerformRequest(Request request) throws IOException { | ||
| assertThat(request.getOptions().getHeaders(), hasSize(1)); | ||
| Header httpHeader = request.getOptions().getHeaders().get(0); | ||
| final Response mockResponse = mock(Response.class); | ||
| when(mockResponse.getHost()).thenReturn(new HttpHost("localhost", 9200)); | ||
|
|
||
|
|
@@ -171,20 +180,20 @@ private CustomRestClient(RestClient restClient) { | |
| super(restClient, RestClient::close, Collections.emptyList()); | ||
| } | ||
|
|
||
| MainResponse custom(MainRequest mainRequest, Header... headers) throws IOException { | ||
| return performRequest(mainRequest, this::toRequest, this::toResponse, emptySet(), headers); | ||
| MainResponse custom(MainRequest mainRequest, RequestOptions options) throws IOException { | ||
| return performRequest(mainRequest, this::toRequest, options, this::toResponse, emptySet()); | ||
| } | ||
|
|
||
| MainResponse customAndParse(MainRequest mainRequest, Header... headers) throws IOException { | ||
| return performRequestAndParseEntity(mainRequest, this::toRequest, MainResponse::fromXContent, emptySet(), headers); | ||
| MainResponse customAndParse(MainRequest mainRequest, RequestOptions options) throws IOException { | ||
| return performRequestAndParseEntity(mainRequest, this::toRequest, options, MainResponse::fromXContent, emptySet()); | ||
| } | ||
|
|
||
| void customAsync(MainRequest mainRequest, ActionListener<MainResponse> listener, Header... headers) { | ||
| performRequestAsync(mainRequest, this::toRequest, this::toResponse, listener, emptySet(), headers); | ||
| void customAsync(MainRequest mainRequest, RequestOptions options, ActionListener<MainResponse> listener) { | ||
| performRequestAsync(mainRequest, this::toRequest, options, this::toResponse, listener, emptySet()); | ||
| } | ||
|
|
||
| void customAndParseAsync(MainRequest mainRequest, ActionListener<MainResponse> listener, Header... headers) { | ||
| performRequestAsyncAndParseEntity(mainRequest, this::toRequest, MainResponse::fromXContent, listener, emptySet(), headers); | ||
| void customAndParseAsync(MainRequest mainRequest, RequestOptions options, ActionListener<MainResponse> listener) { | ||
| performRequestAsyncAndParseEntity(mainRequest, this::toRequest, options, MainResponse::fromXContent, listener, emptySet()); | ||
| } | ||
|
|
||
| Request toRequest(MainRequest mainRequest) throws IOException { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍