All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.action.support.ActionTestUtils Maven / Gradle / Ivy

There is a newer version: 8.16.0
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.action.support;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.tasks.TaskManager;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.Transport;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.elasticsearch.test.ESTestCase.fail;

public class ActionTestUtils {

    private ActionTestUtils() { /* no construction */ }

    public static  Response executeBlocking(
        TransportAction action,
        Request request
    ) {
        return PlainActionFuture.get(
            future -> action.execute(request.createTask(1L, "direct", action.actionName, TaskId.EMPTY_TASK_ID, Map.of()), request, future),
            10,
            TimeUnit.SECONDS
        );
    }

    public static  Response executeBlockingWithTask(
        TaskManager taskManager,
        Transport.Connection localConnection,
        TransportAction action,
        Request request
    ) {
        return PlainActionFuture.get(
            future -> taskManager.registerAndExecute("transport", action, request, localConnection, future),
            10,
            TimeUnit.SECONDS
        );
    }

    /**
     * Executes the given action.
     *
     * This is a shim method to make execution publicly available in tests.
     */
    public static  void execute(
        TransportAction action,
        Task task,
        Request request,
        ActionListener listener
    ) {
        action.execute(task, request, listener);
    }

    public static  void execute(
        TransportAction action,
        Request request,
        ActionListener listener
    ) {
        action.execute(request.createTask(1L, "direct", action.actionName, TaskId.EMPTY_TASK_ID, Map.of()), request, listener);
    }

    public static  ActionListener assertNoFailureListener(CheckedConsumer consumer) {
        return ActionListener.wrap(consumer, ESTestCase::fail);
    }

    public static  ActionListener assertNoSuccessListener(Consumer consumer) {
        return new ActionListener<>() {
            @Override
            public void onResponse(T result) {
                fail(null, "unexpected success with result [%s] while expecting to handle failure with [%s]", result, consumer);
            }

            @Override
            public void onFailure(Exception e) {
                try {
                    consumer.accept(e);
                } catch (Exception e2) {
                    if (e2 != e) {
                        e2.addSuppressed(e);
                    }
                    fail(e2, "unexpected failure in onFailure handler for [%s]", consumer);
                }
            }
        };
    }

    public static ResponseListener wrapAsRestResponseListener(ActionListener listener) {
        return new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                listener.onResponse(response);
            }

            @Override
            public void onFailure(Exception exception) {
                listener.onFailure(exception);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy