Search in sources :

Example 1 with ContextSetup

use of org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup in project OpenSearch by opensearch-project.

the class PainlessExecuteRequestTests method testFromXContent.

// Testing XContent serialization manually here, because the xContentType field in ContextSetup determines
// how the request needs to parse and the xcontent serialization framework randomizes that. The XContentType
// is not known and accessable when the test request instance is created in the xcontent serialization framework.
// Changing that is a big change. Writing a custom xcontent test here is the best option for now, because as far
// as I know this request class is the only case where this is a problem.
public final void testFromXContent() throws Exception {
    for (int i = 0; i < 20; i++) {
        PainlessExecuteAction.Request testInstance = createTestInstance();
        ContextSetup contextSetup = testInstance.getContextSetup();
        XContent xContent = randomFrom(XContentType.values()).xContent();
        if (contextSetup != null && contextSetup.getXContentType() != null) {
            xContent = contextSetup.getXContentType().xContent();
        }
        try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
            builder.value(testInstance);
            StreamInput instanceInput = BytesReference.bytes(builder).streamInput();
            try (XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, instanceInput)) {
                PainlessExecuteAction.Request result = PainlessExecuteAction.Request.parse(parser);
                assertThat(result, equalTo(testInstance));
            }
        }
    }
}
Also used : ContextSetup(org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup) XContent(org.opensearch.common.xcontent.XContent) StreamInput(org.opensearch.common.io.stream.StreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with ContextSetup

use of org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup in project OpenSearch by opensearch-project.

the class PainlessExecuteRequestTests method randomContextSetup.

private static ContextSetup randomContextSetup() {
    String index = randomBoolean() ? randomAlphaOfLength(4) : null;
    QueryBuilder query = randomBoolean() ? new MatchAllQueryBuilder() : null;
    BytesReference doc = null;
    XContentType xContentType = randomFrom(XContentType.values());
    if (randomBoolean()) {
        try {
            XContentBuilder xContentBuilder = XContentBuilder.builder(xContentType.xContent());
            xContentBuilder.startObject();
            xContentBuilder.endObject();
            doc = BytesReference.bytes(xContentBuilder);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    ContextSetup contextSetup = new ContextSetup(index, doc, query);
    contextSetup.setXContentType(xContentType);
    return contextSetup;
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ContextSetup(org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup) XContentType(org.opensearch.common.xcontent.XContentType) UncheckedIOException(java.io.UncheckedIOException) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 3 with ContextSetup

use of org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup in project OpenSearch by opensearch-project.

the class PainlessExecuteRequestTests method createTestInstance.

@Override
protected PainlessExecuteAction.Request createTestInstance() {
    Script script = new Script(randomAlphaOfLength(10));
    ScriptContext<?> context = randomBoolean() ? randomFrom(PainlessExecuteAction.Request.SUPPORTED_CONTEXTS.values()) : null;
    ContextSetup contextSetup = randomBoolean() ? randomContextSetup() : null;
    return new PainlessExecuteAction.Request(script, context != null ? context.name : null, contextSetup);
}
Also used : ContextSetup(org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup) Script(org.opensearch.script.Script)

Aggregations

ContextSetup (org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup)3 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1 XContent (org.opensearch.common.xcontent.XContent)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 XContentType (org.opensearch.common.xcontent.XContentType)1 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1 Script (org.opensearch.script.Script)1