Search in sources :

Example 1 with ActionRequestValidationException

use of org.opensearch.action.ActionRequestValidationException 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 ActionRequestValidationException

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

the class PutIndexTemplateRequestTests method testValidateErrorMessage.

public void testValidateErrorMessage() throws Exception {
    expectThrows(IllegalArgumentException.class, () -> new PutIndexTemplateRequest(null));
    expectThrows(IllegalArgumentException.class, () -> new PutIndexTemplateRequest("test").name(null));
    PutIndexTemplateRequest request = new PutIndexTemplateRequest("test");
    ActionRequestValidationException withoutPattern = request.validate();
    assertThat(withoutPattern.getMessage(), containsString("index patterns are missing"));
    request.name("foo");
    ActionRequestValidationException withoutIndexPatterns = request.validate();
    assertThat(withoutIndexPatterns.validationErrors(), hasSize(1));
    assertThat(withoutIndexPatterns.getMessage(), containsString("index patterns are missing"));
    request.patterns(Collections.singletonList("test-*"));
    ActionRequestValidationException noError = request.validate();
    assertThat(noError, is(nullValue()));
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException)

Example 3 with ActionRequestValidationException

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

the class RankEvalRequest method validate.

@Override
public ActionRequestValidationException validate() {
    ActionRequestValidationException e = null;
    if (rankingEvaluationSpec == null) {
        e = new ActionRequestValidationException();
        e.addValidationError("missing ranking evaluation specification");
    }
    return e;
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException)

Example 4 with ActionRequestValidationException

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

the class TransportAction method execute.

/**
 * Use this method when the transport action should continue to run in the context of the current task
 */
public final void execute(Task task, Request request, ActionListener<Response> listener) {
    ActionRequestValidationException validationException = request.validate();
    if (validationException != null) {
        listener.onFailure(validationException);
        return;
    }
    if (task != null && request.getShouldStoreResult()) {
        listener = new TaskResultStoringActionListener<>(taskManager, task, listener);
    }
    RequestFilterChain<Request, Response> requestFilterChain = new RequestFilterChain<>(this, logger);
    requestFilterChain.proceed(task, actionName, request, listener);
}
Also used : ActionResponse(org.opensearch.action.ActionResponse) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ActionRequest(org.opensearch.action.ActionRequest)

Example 5 with ActionRequestValidationException

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

the class SearchRequest method validate.

@Override
public ActionRequestValidationException validate() {
    ActionRequestValidationException validationException = null;
    boolean scroll = scroll() != null;
    if (scroll) {
        if (source != null) {
            if (source.trackTotalHitsUpTo() != null && source.trackTotalHitsUpTo() != SearchContext.TRACK_TOTAL_HITS_ACCURATE) {
                validationException = addValidationError("disabling [track_total_hits] is not allowed in a scroll context", validationException);
            }
            if (source.from() > 0) {
                validationException = addValidationError("using [from] is not allowed in a scroll context", validationException);
            }
            if (source.size() == 0) {
                validationException = addValidationError("[size] cannot be [0] in a scroll context", validationException);
            }
            if (source.rescores() != null && source.rescores().isEmpty() == false) {
                validationException = addValidationError("using [rescore] is not allowed in a scroll context", validationException);
            }
        }
        if (requestCache != null && requestCache) {
            validationException = addValidationError("[request_cache] cannot be used in a scroll context", validationException);
        }
    }
    if (source != null) {
        if (source.aggregations() != null) {
            validationException = source.aggregations().validate(validationException);
        }
    }
    if (pointInTimeBuilder() != null) {
        if (scroll) {
            validationException = addValidationError("using [point in time] is not allowed in a scroll context", validationException);
        }
    }
    return validationException;
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException)

Aggregations

ActionRequestValidationException (org.opensearch.action.ActionRequestValidationException)53 Test (org.junit.Test)5 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)4 IOException (java.io.IOException)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 UpdateRequest (org.opensearch.action.update.UpdateRequest)3 NodeClient (org.opensearch.client.node.NodeClient)3 Template (org.opensearch.cluster.metadata.Template)3 BaseRestHandler (org.opensearch.rest.BaseRestHandler)3 RestRequest (org.opensearch.rest.RestRequest)3 Arrays.asList (java.util.Arrays.asList)2 Collections.unmodifiableList (java.util.Collections.unmodifiableList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ActionRequest (org.opensearch.action.ActionRequest)2 Request (org.opensearch.action.admin.indices.datastream.CreateDataStreamAction.Request)2 Request (org.opensearch.action.admin.indices.datastream.DeleteDataStreamAction.Request)2 PutComposableIndexTemplateAction (org.opensearch.action.admin.indices.template.put.PutComposableIndexTemplateAction)2 SearchRequest (org.opensearch.action.search.SearchRequest)2