diff options
author | Luca Cavanna <javanna@users.noreply.github.com> | 2017-05-29 11:00:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-29 11:00:20 +0200 |
commit | ea00d343aeb33b5b3437c577456b0567b172e283 (patch) | |
tree | 03a6672b60931fc882e51736c046cd282c8fa8a0 /core/src/test/java/org/elasticsearch/search | |
parent | 7be5758e6d9fc37b878e9f736d280a76c35348f2 (diff) |
ClearScrollRequest to implement ToXContentObject (#24907)
ClearScrollRequest can be created from a request body, but it doesn't support the opposite, meaning printing out its content to an XContentBuilder. This is useful to the high level REST client and allows for better testing of what we parse.
Moved parsing method from RestClearScrollAction to ClearScrollRequest so that fromXContent and toXContent sit close to each other. Added unit tests to verify that body parameters override query_string parameters when both present (there is already a yaml test for this but unit test is even better)
Diffstat (limited to 'core/src/test/java/org/elasticsearch/search')
-rw-r--r-- | core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java b/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java index ae8ad66ac8..905bddbcf1 100644 --- a/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java +++ b/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java @@ -20,32 +20,29 @@ package org.elasticsearch.search.scroll; import org.elasticsearch.action.search.ClearScrollRequest; +import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.search.RestClearScrollAction; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.rest.FakeRestChannel; import org.elasticsearch.test.rest.FakeRestRequest; +import org.mockito.ArgumentCaptor; + +import java.util.Collections; +import java.util.List; -import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.startsWith; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class RestClearScrollActionTests extends ESTestCase { - public void testParseClearScrollRequest() throws Exception { - XContentParser content = createParser(XContentFactory.jsonBuilder() - .startObject() - .array("scroll_id", "value_1", "value_2") - .endObject()); - ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); - RestClearScrollAction.buildFromContent(content, clearScrollRequest); - assertThat(clearScrollRequest.scrollIds(), contains("value_1", "value_2")); - } public void testParseClearScrollRequestWithInvalidJsonThrowsException() throws Exception { RestClearScrollAction action = new RestClearScrollAction(Settings.EMPTY, mock(RestController.class)); @@ -55,17 +52,22 @@ public class RestClearScrollActionTests extends ESTestCase { assertThat(e.getMessage(), equalTo("Failed to parse request body")); } - public void testParseClearScrollRequestWithUnknownParamThrowsException() throws Exception { - XContentParser invalidContent = createParser(XContentFactory.jsonBuilder() - .startObject() - .array("scroll_id", "value_1", "value_2") - .field("unknown", "keyword") - .endObject()); - ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); + public void testBodyParamsOverrideQueryStringParams() throws Exception { + NodeClient nodeClient = mock(NodeClient.class); + doNothing().when(nodeClient).searchScroll(any(), any()); - Exception e = expectThrows(IllegalArgumentException.class, - () -> RestClearScrollAction.buildFromContent(invalidContent, clearScrollRequest)); - assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]")); - } + RestClearScrollAction action = new RestClearScrollAction(Settings.EMPTY, mock(RestController.class)); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) + .withParams(Collections.singletonMap("scroll_id", "QUERY_STRING")) + .withContent(new BytesArray("{\"scroll_id\": [\"BODY\"]}"), XContentType.JSON).build(); + FakeRestChannel channel = new FakeRestChannel(request, false, 0); + action.handleRequest(request, channel, nodeClient); + ArgumentCaptor<ClearScrollRequest> argument = ArgumentCaptor.forClass(ClearScrollRequest.class); + verify(nodeClient).clearScroll(argument.capture(), anyObject()); + ClearScrollRequest clearScrollRequest = argument.getValue(); + List<String> scrollIds = clearScrollRequest.getScrollIds(); + assertEquals(1, scrollIds.size()); + assertEquals("BODY", scrollIds.get(0)); + } } |