-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
[Test] Add test for custom requests in High Level Rest Client #25106
Conversation
This commit adds a test that tests and demonstrates how {@link RestHighLevelClient} can be extended to support custom requests and responses.
private CustomRestClient restHighLevelClient; | ||
|
||
@Before | ||
public void iniClients() 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.
initClients
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.
left a couple of comments
assertEquals(expectedStatus(length), customResponse.status()); | ||
} | ||
|
||
private Void performRequestAsync(HttpEntity httpEntity, ResponseListener responseListener) { |
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.
why Void instead of void?
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.
Because Mockito's doAnswer() method needs a type to be able to stub a method that returns void (ie RestClient's performRequestAsync method).
return null; | ||
} | ||
|
||
private Response performRequest(HttpEntity httpEntity) 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.
why do we need these two methods? can we use the internal RestClient instead?
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.
Why do you mean by using the internal RestClient? The test uses a mocked RestClient and executes these methods when the RestClient's performRequest and performRequestAsync methods are called with parameters that match the Matchers configured in initClient(). I might be missing something, but I think this test mocks the right RestClient object ?
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.
right, I had totally misunderstood how the test works
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Test and demonstrates how {@link RestHighLevelClient} can be extended to support custom requests and responses. |
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.
add "against custom endpoints" at the end of the sentence?
} | ||
} | ||
|
||
static class CustomRequest extends ActionRequest implements ToXContentObject { |
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.
maybe we could trim down the test and just reuse existing requests? would it make the test less useful? I think the important bit is that you can add the methods to the custom client class, and that those methods can make use of the existing infra.
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.
I'd like to keep the custom request and custom response because it echoes the custom/customAsync methods. But I can trim down the test by not printing out xcontent request and response body which are not the point here. I'll try that and you'll tell me if you still want to reuse existing requests (I'm fine with reusing MainRequest/MainResponse for example)
} | ||
|
||
public void customAsync(CustomRequest customRequest, ActionListener<CustomResponse> listener, Header... headers) { | ||
performRequestAsync(customRequest, this::toRequest, this::toResponse, listener, emptySet(), headers); |
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.
I think that what confuses me here is that we call performRequest and performRequestAsync here, why do we mock the rest client then? Wouldn't it be better to test that RestHighLevelClient subclasses can use performRequestAndParseEntity and performRequestAsyncAndParseEntity (which are private at the moment)
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.
I think that what confuses me here is that we call performRequest and performRequestAsync here, why do we mock the rest client then?
I agree, this is confusing. I you noticed it calls the performRequest method from the RestHighLevelClient and not the method from the test. I renamed the test method so that it's not confusing.
Wouldn't it be better to test that RestHighLevelClient subclasses can use performRequestAndParseEntity and performRequestAsyncAndParseEntity (which are private at the moment)
I don't know, it really depends of what the client want to do with the HTTP response. The performRequest/performRequestAsync are nice because we get back the Response from the low level rest client and so we can extract headers and still parse the entity with the parseEntity method. On the other side, exposing performRequestAndParseEntity and performRequestAsyncAndParseEntity would be useful too, so maybe both of them should be protected
.
@javanna Thanks for your review! I applied your feedback and trimmed down the test, please let me know what you think. |
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.
I left a couple of small comments
super(restClient); | ||
} | ||
|
||
public CustomResponse custom(CustomRequest customRequest, Header... headers) 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.
shall we also have two additional methods that use performRequestAsyncAndParseEntity and performRequestAndParseEntity (which should be made protected for that)?
} | ||
|
||
public CustomResponse custom(CustomRequest customRequest, Header... headers) throws IOException { | ||
return performRequest(customRequest, this::toRequest, this::toResponse, emptySet(), headers); |
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.
I think that we need to make this method and performRequestAsync protected rather than package private. Can you add for this and the other needed methods a test like this one: https://github.com/elastic/elasticsearch/blob/master/client/rest/src/test/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumerTests.java#L121 ?
@javanna Thanks for your review. I changed the code and dropped the custom objects and reused existing request/response as you suggested before. |
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.
LGTM thanks @tlrx
This commit adds a test that tests and demonstrates how {@link RestHighLevelClient} can be extended to support custom endpoint.
Thanks @javanna! I added a missing |
* master: (53 commits) Log checkout so SHA is known Add link to community Rust Client (elastic#22897) "shard started" should show index and shard ID (elastic#25157) await fix testWithRandomException Change BWC versions on create index response Return the index name on a create index response Remove incorrect bwc branch logic from master Correctly format arrays in output [Test] Extending parsing checks for SearchResponse (elastic#25148) Scripting: Change keys for inline/stored scripts to source/id (elastic#25127) [Test] Add test for custom requests in High Level Rest Client (elastic#25106) nested: In case of a single type the _id field should be added to the nested document instead of _uid field. `type` and `id` are lost upon serialization of `Translog.Delete`. (elastic#24586) fix highlighting docs Fix NPE in token_count datatype with null value (elastic#25046) Remove the postings highlighter and make unified the default highlighter choice (elastic#25028) [Test] Adding test for parsing SearchShardFailure leniently (elastic#25144) Fix typo in shards.asciidoc (elastic#25143) List Hibernate Search (elastic#25145) [DOCS] update maxRetryTimeout in java REST client usage page ...
* master: (80 commits) Test: remove faling test that relies on merge order Log checkout so SHA is known Add link to community Rust Client (elastic#22897) "shard started" should show index and shard ID (elastic#25157) await fix testWithRandomException Change BWC versions on create index response Return the index name on a create index response Remove incorrect bwc branch logic from master Correctly format arrays in output [Test] Extending parsing checks for SearchResponse (elastic#25148) Scripting: Change keys for inline/stored scripts to source/id (elastic#25127) [Test] Add test for custom requests in High Level Rest Client (elastic#25106) nested: In case of a single type the _id field should be added to the nested document instead of _uid field. `type` and `id` are lost upon serialization of `Translog.Delete`. (elastic#24586) fix highlighting docs Fix NPE in token_count datatype with null value (elastic#25046) Remove the postings highlighter and make unified the default highlighter choice (elastic#25028) [Test] Adding test for parsing SearchShardFailure leniently (elastic#25144) Fix typo in shards.asciidoc (elastic#25143) List Hibernate Search (elastic#25145) ...
* master: (1889 commits) Test: remove faling test that relies on merge order Log checkout so SHA is known Add link to community Rust Client (elastic#22897) "shard started" should show index and shard ID (elastic#25157) await fix testWithRandomException Change BWC versions on create index response Return the index name on a create index response Remove incorrect bwc branch logic from master Correctly format arrays in output [Test] Extending parsing checks for SearchResponse (elastic#25148) Scripting: Change keys for inline/stored scripts to source/id (elastic#25127) [Test] Add test for custom requests in High Level Rest Client (elastic#25106) nested: In case of a single type the _id field should be added to the nested document instead of _uid field. `type` and `id` are lost upon serialization of `Translog.Delete`. (elastic#24586) fix highlighting docs Fix NPE in token_count datatype with null value (elastic#25046) Remove the postings highlighter and make unified the default highlighter choice (elastic#25028) [Test] Adding test for parsing SearchShardFailure leniently (elastic#25144) Fix typo in shards.asciidoc (elastic#25143) List Hibernate Search (elastic#25145) ...
This pull request adds a test that tests and demonstrates how
RestHighLevelClient
can be extended to support custom requests and responses.