use of org.opensearch.test.client.NoOpNodeClient in project OpenSearch by opensearch-project.
the class RestTasksActionTests method testConsumesParameters.
public void testConsumesParameters() throws Exception {
RestTasksAction action = new RestTasksAction(() -> DiscoveryNodes.EMPTY_NODES);
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(MapBuilder.<String, String>newMapBuilder().put("parent_task_id", "the node:3").put("nodes", "node1,node2").put("actions", "*").map()).build();
FakeRestChannel fakeRestChannel = new FakeRestChannel(fakeRestRequest, false, 1);
try (NoOpNodeClient nodeClient = buildNodeClient()) {
action.handleRequest(fakeRestRequest, fakeRestChannel, nodeClient);
}
assertThat(fakeRestChannel.errors().get(), is(0));
assertThat(fakeRestChannel.responses().get(), is(1));
}
use of org.opensearch.test.client.NoOpNodeClient 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.test.client.NoOpNodeClient in project OpenSearch by opensearch-project.
the class RestSearchScrollActionTests method testBodyParamsOverrideQueryStringParams.
public void testBodyParamsOverrideQueryStringParams() throws Exception {
SetOnce<Boolean> scrollCalled = new SetOnce<>();
try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName()) {
@Override
public void searchScroll(SearchScrollRequest request, ActionListener<SearchResponse> listener) {
scrollCalled.set(true);
assertThat(request.scrollId(), equalTo("BODY"));
assertThat(request.scroll().keepAlive().getStringRep(), equalTo("1m"));
}
}) {
RestSearchScrollAction action = new RestSearchScrollAction();
Map<String, String> params = new HashMap<>();
params.put("scroll_id", "QUERY_STRING");
params.put("scroll", "1000m");
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).withContent(new BytesArray("{\"scroll_id\":\"BODY\", \"scroll\":\"1m\"}"), XContentType.JSON).build();
FakeRestChannel channel = new FakeRestChannel(request, false, 0);
action.handleRequest(request, channel, nodeClient);
assertThat(scrollCalled.get(), equalTo(true));
}
}
use of org.opensearch.test.client.NoOpNodeClient in project OpenSearch by opensearch-project.
the class UsageServiceTests method testRestUsage.
public void testRestUsage() throws Exception {
RestRequest restRequest = new FakeRestRequest();
BaseRestHandler handlerA = new MockRestHandler("a");
BaseRestHandler handlerB = new MockRestHandler("b");
BaseRestHandler handlerC = new MockRestHandler("c");
BaseRestHandler handlerD = new MockRestHandler("d");
BaseRestHandler handlerE = new MockRestHandler("e");
BaseRestHandler handlerF = new MockRestHandler("f");
UsageService usageService = new UsageService();
usageService.addRestHandler(handlerA);
usageService.addRestHandler(handlerB);
usageService.addRestHandler(handlerC);
usageService.addRestHandler(handlerD);
usageService.addRestHandler(handlerE);
usageService.addRestHandler(handlerF);
try (NodeClient client = new NoOpNodeClient(this.getClass().getSimpleName() + "TestClient")) {
handlerA.handleRequest(restRequest, null, client);
handlerB.handleRequest(restRequest, null, client);
handlerA.handleRequest(restRequest, null, client);
handlerA.handleRequest(restRequest, null, client);
handlerB.handleRequest(restRequest, null, client);
handlerC.handleRequest(restRequest, null, client);
handlerC.handleRequest(restRequest, null, client);
handlerD.handleRequest(restRequest, null, client);
handlerA.handleRequest(restRequest, null, client);
handlerB.handleRequest(restRequest, null, client);
handlerE.handleRequest(restRequest, null, client);
handlerF.handleRequest(restRequest, null, client);
handlerC.handleRequest(restRequest, null, client);
handlerD.handleRequest(restRequest, null, client);
}
Map<String, Long> restUsage = usageService.getRestUsageStats();
assertThat(restUsage, notNullValue());
assertThat(restUsage.size(), equalTo(6));
assertThat(restUsage.get("a"), equalTo(4L));
assertThat(restUsage.get("b"), equalTo(3L));
assertThat(restUsage.get("c"), equalTo(3L));
assertThat(restUsage.get("d"), equalTo(2L));
assertThat(restUsage.get("e"), equalTo(1L));
assertThat(restUsage.get("f"), equalTo(1L));
}
use of org.opensearch.test.client.NoOpNodeClient in project OpenSearch by opensearch-project.
the class RestControllerTests method setup.
@Before
public void setup() {
circuitBreakerService = new HierarchyCircuitBreakerService(Settings.builder().put(HierarchyCircuitBreakerService.IN_FLIGHT_REQUESTS_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), BREAKER_LIMIT).put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false).build(), Collections.emptyList(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
usageService = new UsageService();
// we can do this here only because we know that we don't adjust breaker settings dynamically in the test
inFlightRequestsBreaker = circuitBreakerService.getBreaker(CircuitBreaker.IN_FLIGHT_REQUESTS);
HttpServerTransport httpServerTransport = new TestHttpServerTransport();
client = new NoOpNodeClient(this.getTestName());
restController = new RestController(Collections.emptySet(), null, client, circuitBreakerService, usageService);
restController.registerHandler(RestRequest.Method.GET, "/", (request, channel, client) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)));
restController.registerHandler(RestRequest.Method.GET, "/error", (request, channel, client) -> {
throw new IllegalArgumentException("test error");
});
httpServerTransport.start();
}
Aggregations