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));
}
}
}
}
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;
}
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);
}
Aggregations