Search in sources :

Example 1 with FakeRestChannel

use of org.opensearch.test.rest.FakeRestChannel in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testDefaultResponseParameters.

public void testDefaultResponseParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            return channel -> executed.set(true);
        }

        @Override
        public String getName() {
            return "test_default_response_action";
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("format", randomAlphaOfLength(8));
    params.put("filter_path", randomAlphaOfLength(8));
    params.put("pretty", randomFrom("true", "false", "", null));
    params.put("human", null);
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mockClient);
    assertTrue(executed.get());
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 2 with FakeRestChannel

use of org.opensearch.test.rest.FakeRestChannel in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testUnconsumedParametersDidYouMean.

public void testUnconsumedParametersDidYouMean() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            request.param("consumed");
            request.param("field");
            request.param("tokenizer");
            request.param("very_close_to_parameter_1");
            request.param("very_close_to_parameter_2");
            return channel -> executed.set(true);
        }

        @Override
        protected Set<String> responseParams() {
            return Collections.singleton("response_param");
        }

        @Override
        public String getName() {
            return "test_unconsumed_did_you_mean_response_action";
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAlphaOfLength(8));
    params.put("flied", randomAlphaOfLength(8));
    params.put("respones_param", randomAlphaOfLength(8));
    params.put("tokenzier", randomAlphaOfLength(8));
    params.put("very_close_to_parametre", randomAlphaOfLength(8));
    params.put("very_far_from_every_consumed_parameter", randomAlphaOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mockClient));
    assertThat(e, hasToString(containsString("request [/] contains unrecognized parameters: " + "[flied] -> did you mean [field]?, " + "[respones_param] -> did you mean [response_param]?, " + "[tokenzier] -> did you mean [tokenizer]?, " + "[very_close_to_parametre] -> did you mean any of [very_close_to_parameter_1, very_close_to_parameter_2]?, " + "[very_far_from_every_consumed_parameter]")));
    assertFalse(executed.get());
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 3 with FakeRestChannel

use of org.opensearch.test.rest.FakeRestChannel in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testConsumedBody.

public void testConsumedBody() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    final BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
            request.content();
            return channel -> executed.set(true);
        }

        @Override
        public String getName() {
            return "test_consumed_body";
        }
    };
    try (XContentBuilder builder = JsonXContent.contentBuilder().startObject().endObject()) {
        final RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(builder.toString()), XContentType.JSON).build();
        final RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
        handler.handleRequest(request, channel, mockClient);
        assertTrue(executed.get());
    }
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) BytesArray(org.opensearch.common.bytes.BytesArray) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with FakeRestChannel

use of org.opensearch.test.rest.FakeRestChannel in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testUnconsumedResponseParameters.

public void testUnconsumedResponseParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            request.param("consumed");
            return channel -> executed.set(true);
        }

        @Override
        protected Set<String> responseParams() {
            return Collections.singleton("response_param");
        }

        @Override
        public String getName() {
            return "test_unconsumed_response_action";
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAlphaOfLength(8));
    params.put("response_param", randomAlphaOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mockClient);
    assertTrue(executed.get());
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 5 with FakeRestChannel

use of org.opensearch.test.rest.FakeRestChannel in project OpenSearch by opensearch-project.

the class BaseRestHandlerTests method testMultipleUnconsumedParameters.

public void testMultipleUnconsumedParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler() {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            request.param("consumed");
            return channel -> executed.set(true);
        }

        @Override
        public String getName() {
            return "test_multiple_unconsumed_response_action";
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAlphaOfLength(8));
    params.put("unconsumed-first", randomAlphaOfLength(8));
    params.put("unconsumed-second", randomAlphaOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mockClient));
    assertThat(e, hasToString(containsString("request [/] contains unrecognized parameters: [unconsumed-first], [unconsumed-second]")));
    assertFalse(executed.get());
}
Also used : Arrays(java.util.Arrays) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Table(org.opensearch.common.Table) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Method(org.opensearch.rest.RestRequest.Method) NodeClient(org.opensearch.client.node.NodeClient) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ReplacedRoute(org.opensearch.rest.RestHandler.ReplacedRoute) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Route(org.opensearch.rest.RestHandler.Route) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Aggregations

FakeRestChannel (org.opensearch.test.rest.FakeRestChannel)21 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)21 NodeClient (org.opensearch.client.node.NodeClient)12 BytesArray (org.opensearch.common.bytes.BytesArray)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)12 HashMap (java.util.HashMap)10 Settings (org.opensearch.common.settings.Settings)10 IOException (java.io.IOException)9 Arrays (java.util.Arrays)9 Collections (java.util.Collections)9 List (java.util.List)9 Set (java.util.Set)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 StringContains.containsString (org.hamcrest.core.StringContains.containsString)9 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)9 Table (org.opensearch.common.Table)9 XContentType (org.opensearch.common.xcontent.XContentType)9 JsonXContent (org.opensearch.common.xcontent.json.JsonXContent)9 ReplacedRoute (org.opensearch.rest.RestHandler.ReplacedRoute)9 Route (org.opensearch.rest.RestHandler.Route)9