Search in sources :

Example 86 with NodeClient

use of org.elasticsearch.client.node.NodeClient 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 87 with NodeClient

use of org.elasticsearch.client.node.NodeClient 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 88 with NodeClient

use of org.elasticsearch.client.node.NodeClient 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)

Example 89 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class DeprecationRestHandlerTests method testHandleRequestLogsWarningThenForwards.

public void testHandleRequestLogsWarningThenForwards() throws Exception {
    RestRequest request = mock(RestRequest.class);
    RestChannel channel = mock(RestChannel.class);
    NodeClient client = mock(NodeClient.class);
    DeprecationRestHandler deprecatedHandler = new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger);
    // test it
    deprecatedHandler.handleRequest(request, channel, client);
    InOrder inOrder = inOrder(handler, request, channel, deprecationLogger);
    // log, then forward
    inOrder.verify(deprecationLogger).deprecated(deprecationMessage);
    inOrder.verify(handler).handleRequest(request, channel, client);
    inOrder.verifyNoMoreInteractions();
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) InOrder(org.mockito.InOrder)

Example 90 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestControllerTests method testDispatchFailsWithPlainText.

public void testDispatchFailsWithPlainText() {
    String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray(content), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList("text/plain"))).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.NOT_ACCEPTABLE);
    restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Aggregations

NodeClient (org.elasticsearch.client.node.NodeClient)125 Settings (org.elasticsearch.common.settings.Settings)115 RestRequest (org.elasticsearch.rest.RestRequest)108 RestController (org.elasticsearch.rest.RestController)107 IOException (java.io.IOException)103 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)88 Strings (org.elasticsearch.common.Strings)61 GET (org.elasticsearch.rest.RestRequest.Method.GET)57 RestResponse (org.elasticsearch.rest.RestResponse)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 POST (org.elasticsearch.rest.RestRequest.Method.POST)34 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)30 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)27 Set (java.util.Set)24 Table (org.elasticsearch.common.Table)24 OK (org.elasticsearch.rest.RestStatus.OK)23 AcknowledgedRestListener (org.elasticsearch.rest.action.AcknowledgedRestListener)23 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 Collections (java.util.Collections)18