Search in sources :

Example 6 with NoOpNodeClient

use of org.opensearch.test.client.NoOpNodeClient in project OpenSearch by opensearch-project.

the class RestBulkActionTests method testBulkPipelineUpsert.

public void testBulkPipelineUpsert() throws Exception {
    SetOnce<Boolean> bulkCalled = new SetOnce<>();
    try (NodeClient verifyingClient = new NoOpNodeClient(this.getTestName()) {

        @Override
        public void bulk(BulkRequest request, ActionListener<BulkResponse> listener) {
            bulkCalled.set(true);
            assertThat(request.requests(), hasSize(2));
            UpdateRequest updateRequest = (UpdateRequest) request.requests().get(1);
            assertThat(updateRequest.upsertRequest().getPipeline(), equalTo("timestamps"));
        }
    }) {
        final Map<String, String> params = new HashMap<>();
        params.put("pipeline", "timestamps");
        new RestBulkAction(settings(Version.CURRENT).build()).handleRequest(new FakeRestRequest.Builder(xContentRegistry()).withPath("my_index/_bulk").withParams(params).withContent(new BytesArray("{\"index\":{\"_id\":\"1\"}}\n" + "{\"field1\":\"val1\"}\n" + "{\"update\":{\"_id\":\"2\"}}\n" + "{\"script\":{\"source\":\"ctx._source.counter++;\"},\"upsert\":{\"field1\":\"upserted_val\"}}\n"), XContentType.JSON).withMethod(RestRequest.Method.POST).build(), mock(RestChannel.class), verifyingClient);
        assertThat(bulkCalled.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) UpdateRequest(org.opensearch.action.update.UpdateRequest) HashMap(java.util.HashMap) RestChannel(org.opensearch.rest.RestChannel) ActionListener(org.opensearch.action.ActionListener) BulkRequest(org.opensearch.action.bulk.BulkRequest) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient)

Example 7 with NoOpNodeClient

use of org.opensearch.test.client.NoOpNodeClient in project OpenSearch by opensearch-project.

the class RestAnalyzeActionTests method testParseXContentForAnalyzeRequestWithInvalidJsonThrowsException.

public void testParseXContentForAnalyzeRequestWithInvalidJsonThrowsException() {
    RestAnalyzeAction action = new RestAnalyzeAction();
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray("{invalid_json}"), XContentType.JSON).build();
    try (NodeClient client = new NoOpNodeClient(this.getClass().getSimpleName())) {
        IOException e = expectThrows(IOException.class, () -> action.handleRequest(request, null, client));
        assertThat(e.getMessage(), containsString("expecting double-quote"));
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) IOException(java.io.IOException) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient)

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