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());
}
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());
}
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());
}
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));
}
}
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));
}
Aggregations