Search in sources :

Example 21 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestControllerTests method testDispatchWorksWithNewlineDelimitedJson.

public void testDispatchWorksWithNewlineDelimitedJson() {
    final String mimeType = "application/x-ndjson";
    String content = randomAlphaOfLength((int) Math.round(BREAKER_LIMIT.getBytes() / inFlightRequestsBreaker.getOverhead()));
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray(content), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList(mimeType))).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);
    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));
        }

        @Override
        public boolean supportsContentStream() {
            return true;
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, client.threadPool().getThreadContext());
    assertTrue(channel.getSendResponseCalled());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) IOException(java.io.IOException)

Example 22 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestControllerTests method testNonStreamingXContentCausesErrorResponse.

public void testNonStreamingXContentCausesErrorResponse() throws IOException {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(BytesReference.bytes(YamlXContent.contentBuilder().startObject().endObject()), XContentType.YAML).withPath("/foo").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));
        }

        @Override
        public boolean supportsContentStream() {
            return true;
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, client.threadPool().getThreadContext());
    assertTrue(channel.getSendResponseCalled());
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) IOException(java.io.IOException)

Example 23 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestControllerTests method testDispatchWithContentStreamNoContentType.

public void testDispatchWithContentStreamNoContentType() {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray("{}"), null).withPath("/foo").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));
        }

        @Override
        public boolean supportsContentStream() {
            return true;
        }
    });
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, client.threadPool().getThreadContext());
    assertTrue(channel.getSendResponseCalled());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) IOException(java.io.IOException)

Example 24 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestClearScrollActionTests method testBodyParamsOverrideQueryStringParams.

public void testBodyParamsOverrideQueryStringParams() throws Exception {
    SetOnce<Boolean> scrollCalled = new SetOnce<>();
    try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName()) {

        @Override
        public void clearScroll(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener) {
            scrollCalled.set(true);
            assertThat(request.getScrollIds(), hasSize(1));
            assertThat(request.getScrollIds().get(0), equalTo("BODY"));
        }
    }) {
        RestClearScrollAction action = new RestClearScrollAction();
        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);
        assertThat(scrollCalled.get(), equalTo(true));
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) BytesArray(org.opensearch.common.bytes.BytesArray) SetOnce(org.apache.lucene.util.SetOnce) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestClearScrollAction(org.opensearch.rest.action.search.RestClearScrollAction) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) ActionListener(org.opensearch.action.ActionListener) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 25 with NodeClient

use of org.opensearch.client.node.NodeClient in project ml-commons by opensearch-project.

the class RestMLDeleteTaskAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient nodeClient) throws IOException {
    String taskId = restRequest.param(PARAMETER_TASK_ID);
    MLTaskDeleteRequest mlModelDeleteRequest = new MLTaskDeleteRequest(taskId);
    return channel -> nodeClient.execute(MLTaskDeleteAction.INSTANCE, mlModelDeleteRequest, new RestToXContentListener<>(channel));
}
Also used : MLTaskDeleteRequest(org.opensearch.ml.common.transport.task.MLTaskDeleteRequest) List(java.util.List) PARAMETER_TASK_ID(org.opensearch.ml.utils.RestActionUtils.PARAMETER_TASK_ID) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) ImmutableList(com.google.common.collect.ImmutableList) MLTaskDeleteAction(org.opensearch.ml.common.transport.task.MLTaskDeleteAction) Locale(java.util.Locale) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ML_BASE_URI(org.opensearch.ml.plugin.MachineLearningPlugin.ML_BASE_URI) MLTaskDeleteRequest(org.opensearch.ml.common.transport.task.MLTaskDeleteRequest)

Aggregations

NodeClient (org.opensearch.client.node.NodeClient)182 RestRequest (org.opensearch.rest.RestRequest)142 List (java.util.List)140 IOException (java.io.IOException)137 BaseRestHandler (org.opensearch.rest.BaseRestHandler)111 Arrays.asList (java.util.Arrays.asList)76 Collections.unmodifiableList (java.util.Collections.unmodifiableList)76 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)70 Strings (org.opensearch.common.Strings)66 GET (org.opensearch.rest.RestRequest.Method.GET)64 POST (org.opensearch.rest.RestRequest.Method.POST)47 RestResponse (org.opensearch.rest.RestResponse)39 IndicesOptions (org.opensearch.action.support.IndicesOptions)35 Settings (org.opensearch.common.settings.Settings)34 Set (java.util.Set)31 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)31 BytesRestResponse (org.opensearch.rest.BytesRestResponse)29 Collections (java.util.Collections)28 Collections.singletonList (java.util.Collections.singletonList)28 RestStatus (org.opensearch.rest.RestStatus)28