Search in sources :

Example 6 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class BaseRestHandlerTests method testDefaultResponseParameters.

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

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            return channel -> executed.set(true);
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("format", randomAsciiOfLength(8));
    params.put("filter_path", randomAsciiOfLength(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, mock(NodeClient.class));
    assertTrue(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 7 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class BaseRestHandlerTests method testUnconsumedResponseParameters.

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

        @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");
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAsciiOfLength(8));
    params.put("response_param", randomAsciiOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mock(NodeClient.class));
    assertTrue(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 8 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class BaseRestHandlerTests method testOneUnconsumedParameters.

public void testOneUnconsumedParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            request.param("consumed");
            return channel -> executed.set(true);
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAsciiOfLength(8));
    params.put("unconsumed", randomAsciiOfLength(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, mock(NodeClient.class)));
    assertThat(e, hasToString(containsString("request [/] contains unrecognized parameter: [unconsumed]")));
    assertFalse(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 9 with FakeRestChannel

use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.

the class BaseRestHandlerTests method testUnconsumedParametersDidYouMean.

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

        @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");
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAsciiOfLength(8));
    params.put("flied", randomAsciiOfLength(8));
    params.put("respones_param", randomAsciiOfLength(8));
    params.put("tokenzier", randomAsciiOfLength(8));
    params.put("very_close_to_parametre", randomAsciiOfLength(8));
    params.put("very_far_from_every_consumed_parameter", randomAsciiOfLength(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, mock(NodeClient.class)));
    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 : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Aggregations

FakeRestChannel (org.elasticsearch.test.rest.FakeRestChannel)9 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)9 IOException (java.io.IOException)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Set (java.util.Set)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 NodeClient (org.elasticsearch.client.node.NodeClient)6 Table (org.elasticsearch.common.Table)6 Settings (org.elasticsearch.common.settings.Settings)6 AbstractCatAction (org.elasticsearch.rest.action.cat.AbstractCatAction)6 ESTestCase (org.elasticsearch.test.ESTestCase)6 StringContains.containsString (org.hamcrest.core.StringContains.containsString)6 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)6 Mockito.mock (org.mockito.Mockito.mock)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)3 Empty (org.elasticsearch.transport.TransportResponse.Empty)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2