Search in sources :

Example 1 with ActionRequest

use of org.opensearch.action.ActionRequest in project OpenSearch by opensearch-project.

the class RestHighLevelClientTests method testRequestValidation.

public void testRequestValidation() {
    ActionRequestValidationException validationException = new ActionRequestValidationException();
    validationException.addValidationError("validation error");
    ActionRequest request = new ActionRequest() {

        @Override
        public ActionRequestValidationException validate() {
            return validationException;
        }
    };
    {
        ActionRequestValidationException actualException = expectThrows(ActionRequestValidationException.class, () -> restHighLevelClient.performRequest(request, null, RequestOptions.DEFAULT, null, null));
        assertSame(validationException, actualException);
    }
    {
        TrackingActionListener trackingActionListener = new TrackingActionListener();
        restHighLevelClient.performRequestAsync(request, null, RequestOptions.DEFAULT, null, trackingActionListener, null);
        assertSame(validationException, trackingActionListener.exception.get());
    }
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ActionRequest(org.opensearch.action.ActionRequest)

Example 2 with ActionRequest

use of org.opensearch.action.ActionRequest in project OpenSearch by opensearch-project.

the class TransportBulkActionTookTests method createAction.

private TransportBulkAction createAction(boolean controlled, AtomicLong expected) {
    CapturingTransport capturingTransport = new CapturingTransport();
    TransportService transportService = capturingTransport.createTransportService(clusterService.getSettings(), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    IndexNameExpressionResolver resolver = new Resolver();
    ActionFilters actionFilters = new ActionFilters(new HashSet<>());
    NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {

        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
            listener.onResponse((Response) new CreateIndexResponse(false, false, null));
        }
    };
    if (controlled) {
        return new TestTransportBulkAction(threadPool, transportService, clusterService, null, client, actionFilters, resolver, null, expected::get) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
                expected.set(1000000);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses, indicesThatCannotBeCreated);
            }
        };
    } else {
        return new TestTransportBulkAction(threadPool, transportService, clusterService, null, client, actionFilters, resolver, null, System::nanoTime) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
                long elapsed = spinForAtLeastOneMillisecond();
                expected.set(elapsed);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses, indicesThatCannotBeCreated);
            }
        };
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) Task(org.opensearch.tasks.Task) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ActionType(org.opensearch.action.ActionType) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CapturingTransport(org.opensearch.test.transport.CapturingTransport) ActionRequest(org.opensearch.action.ActionRequest) IndicesRequest(org.opensearch.action.IndicesRequest) ActionFilters(org.opensearch.action.support.ActionFilters) ActionResponse(org.opensearch.action.ActionResponse) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ActionRequest(org.opensearch.action.ActionRequest) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 3 with ActionRequest

use of org.opensearch.action.ActionRequest in project OpenSearch by opensearch-project.

the class RestValidateQueryActionTests method stubValidateQueryAction.

/**
 * Configures {@link NodeClient} to stub {@link ValidateQueryAction} transport action.
 * <p>
 * This lower level of validation is out of the scope of this test.
 */
@BeforeClass
public static void stubValidateQueryAction() {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    final TransportAction transportAction = new TransportAction(ValidateQueryAction.NAME, new ActionFilters(Collections.emptySet()), taskManager) {

        @Override
        protected void doExecute(Task task, ActionRequest request, ActionListener listener) {
        }
    };
    final Map<ActionType, TransportAction> actions = new HashMap<>();
    actions.put(ValidateQueryAction.INSTANCE, transportAction);
    client.initialize(actions, () -> "local", null, new NamedWriteableRegistry(Collections.emptyList()));
    controller.registerHandler(action);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Task(org.opensearch.tasks.Task) TaskManager(org.opensearch.tasks.TaskManager) ActionType(org.opensearch.action.ActionType) ActionListener(org.opensearch.action.ActionListener) ActionRequest(org.opensearch.action.ActionRequest) HashMap(java.util.HashMap) TransportAction(org.opensearch.action.support.TransportAction) ActionFilters(org.opensearch.action.support.ActionFilters) BeforeClass(org.junit.BeforeClass)

Example 4 with ActionRequest

use of org.opensearch.action.ActionRequest in project ml-commons by opensearch-project.

the class MLTaskGetRequestTest method fromActionRequest_IOException.

@Test(expected = UncheckedIOException.class)
public void fromActionRequest_IOException() {
    ActionRequest actionRequest = new ActionRequest() {

        @Override
        public ActionRequestValidationException validate() {
            return null;
        }

        @Override
        public void writeTo(StreamOutput out) throws IOException {
            throw new IOException("test");
        }
    };
    MLTaskGetRequest.fromActionRequest(actionRequest);
}
Also used : ActionRequest(org.opensearch.action.ActionRequest) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 5 with ActionRequest

use of org.opensearch.action.ActionRequest in project ml-commons by opensearch-project.

the class MLModelDeleteRequestTest method fromActionRequest_Success.

@Test
public void fromActionRequest_Success() {
    MLModelDeleteRequest mlModelDeleteRequest = MLModelDeleteRequest.builder().modelId(modelId).build();
    ActionRequest actionRequest = new ActionRequest() {

        @Override
        public ActionRequestValidationException validate() {
            return null;
        }

        @Override
        public void writeTo(StreamOutput out) throws IOException {
            mlModelDeleteRequest.writeTo(out);
        }
    };
    MLModelDeleteRequest result = MLModelDeleteRequest.fromActionRequest(actionRequest);
    assertNotSame(result, mlModelDeleteRequest);
    assertEquals(result.getModelId(), mlModelDeleteRequest.getModelId());
}
Also used : ActionRequest(org.opensearch.action.ActionRequest) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Aggregations

ActionRequest (org.opensearch.action.ActionRequest)19 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)10 StreamOutput (org.opensearch.common.io.stream.StreamOutput)10 Test (org.junit.Test)9 ActionListener (org.opensearch.action.ActionListener)7 IOException (java.io.IOException)6 UncheckedIOException (java.io.UncheckedIOException)5 Task (org.opensearch.tasks.Task)5 ActionResponse (org.opensearch.action.ActionResponse)4 ActionType (org.opensearch.action.ActionType)4 ActionFilters (org.opensearch.action.support.ActionFilters)4 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)3 Map (java.util.Map)2 Log4j2 (lombok.extern.log4j.Log4j2)2 ActionRequestValidationException (org.opensearch.action.ActionRequestValidationException)2 BulkRequest (org.opensearch.action.bulk.BulkRequest)2 GetRequest (org.opensearch.action.get.GetRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 HandledTransportAction (org.opensearch.action.support.HandledTransportAction)2