Search in sources :

Example 1 with NoOpNodeClient

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));
}
Also used : MapBuilder(org.opensearch.common.collect.MapBuilder) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient)

Example 2 with NoOpNodeClient

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));
    }
}
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 3 with NoOpNodeClient

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));
    }
}
Also used : RestSearchScrollAction(org.opensearch.rest.action.search.RestSearchScrollAction) NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) BytesArray(org.opensearch.common.bytes.BytesArray) SetOnce(org.apache.lucene.util.SetOnce) HashMap(java.util.HashMap) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) ActionListener(org.opensearch.action.ActionListener) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel)

Example 4 with NoOpNodeClient

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));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) AggregationUsageService(org.opensearch.search.aggregations.support.AggregationUsageService) BaseRestHandler(org.opensearch.rest.BaseRestHandler) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient)

Example 5 with NoOpNodeClient

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();
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) UsageService(org.opensearch.usage.UsageService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) HttpServerTransport(org.opensearch.http.HttpServerTransport) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) Before(org.junit.Before)

Aggregations

NoOpNodeClient (org.opensearch.test.client.NoOpNodeClient)7 NodeClient (org.opensearch.client.node.NodeClient)5 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)5 BytesArray (org.opensearch.common.bytes.BytesArray)4 RestRequest (org.opensearch.rest.RestRequest)4 SetOnce (org.apache.lucene.util.SetOnce)3 ActionListener (org.opensearch.action.ActionListener)3 FakeRestChannel (org.opensearch.test.rest.FakeRestChannel)3 HashMap (java.util.HashMap)2 IOException (java.io.IOException)1 Before (org.junit.Before)1 BulkRequest (org.opensearch.action.bulk.BulkRequest)1 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)1 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)1 UpdateRequest (org.opensearch.action.update.UpdateRequest)1 MapBuilder (org.opensearch.common.collect.MapBuilder)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 HttpServerTransport (org.opensearch.http.HttpServerTransport)1 HierarchyCircuitBreakerService (org.opensearch.indices.breaker.HierarchyCircuitBreakerService)1 BaseRestHandler (org.opensearch.rest.BaseRestHandler)1