use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class ScriptMetadataTests method testLoadEmptyScripts.
public void testLoadEmptyScripts() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject().field("mustache#empty", "").endObject();
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
ScriptMetadata.fromXContent(parser);
assertWarnings("empty templates should no longer be used");
builder = XContentFactory.jsonBuilder();
builder.startObject().field("lang#empty", "").endObject();
parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
ScriptMetadata.fromXContent(parser);
assertWarnings("empty scripts should no longer be used");
builder = XContentFactory.jsonBuilder();
builder.startObject().startObject("script").field("lang", "lang").field("source", "").endObject().endObject();
parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
ScriptMetadata.fromXContent(parser);
assertNoDeprecationWarnings();
builder = XContentFactory.jsonBuilder();
builder.startObject().startObject("script").field("lang", "mustache").field("source", "").endObject().endObject();
parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
ScriptMetadata.fromXContent(parser);
assertNoDeprecationWarnings();
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class ScriptMetadataTests method testFromXContentLoading.
public void testFromXContentLoading() throws Exception {
// failure to load to old namespace scripts with the same id but different langs
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject().field("lang0#id0", "script0").field("lang1#id0", "script1").endObject();
XContentParser parser0 = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
expectThrows(IllegalArgumentException.class, () -> ScriptMetadata.fromXContent(parser0));
// failure to load a new namespace script and old namespace script with the same id but different langs
builder = XContentFactory.jsonBuilder();
builder.startObject().field("lang0#id0", "script0").startObject("id0").field("lang", "lang1").field("source", "script1").endObject().endObject();
XContentParser parser1 = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
expectThrows(IllegalArgumentException.class, () -> ScriptMetadata.fromXContent(parser1));
// failure to load a new namespace script and old namespace script with the same id but different langs with additional scripts
builder = XContentFactory.jsonBuilder();
builder.startObject().field("lang0#id0", "script0").field("lang0#id1", "script1").startObject("id1").field("lang", "lang0").field("source", "script0").endObject().startObject("id0").field("lang", "lang1").field("source", "script1").endObject().endObject();
XContentParser parser2 = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
expectThrows(IllegalArgumentException.class, () -> ScriptMetadata.fromXContent(parser2));
// okay to load the same script from the new and old namespace if the lang is the same
builder = XContentFactory.jsonBuilder();
builder.startObject().field("lang0#id0", "script0").startObject("id0").field("lang", "lang0").field("source", "script1").endObject().endObject();
XContentParser parser3 = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput());
ScriptMetadata.fromXContent(parser3);
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class FiltersTests method testOtherBucket.
public void testOtherBucket() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.startObject();
builder.startArray("filters").startObject().startObject("term").field("field", "foo").endObject().endObject().endArray();
builder.endObject();
try (XContentParser parser = createParser(shuffleXContent(builder))) {
parser.nextToken();
FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", parser);
// The other bucket is disabled by default
assertFalse(filters.otherBucket());
builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.startObject();
builder.startArray("filters").startObject().startObject("term").field("field", "foo").endObject().endObject().endArray();
builder.field("other_bucket_key", "some_key");
builder.endObject();
}
try (XContentParser parser = createParser(shuffleXContent(builder))) {
parser.nextToken();
FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", parser);
// but setting a key enables it automatically
assertTrue(filters.otherBucket());
builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.startObject();
builder.startArray("filters").startObject().startObject("term").field("field", "foo").endObject().endObject().endArray();
builder.field("other_bucket", false);
builder.field("other_bucket_key", "some_key");
builder.endObject();
}
try (XContentParser parser = createParser(shuffleXContent(builder))) {
parser.nextToken();
FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", parser);
// unless the other bucket is explicitly disabled
assertFalse(filters.otherBucket());
}
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class ScriptContextInfoTests method testScriptMethodInfoParser.
public void testScriptMethodInfoParser() throws IOException {
String json = "{\"name\": \"fooFunc\", \"return_type\": \"int\", \"params\": [{\"type\": \"int\", \"name\": \"fooParam\"}, " + "{\"type\": \"java.util.Map\", \"name\": \"barParam\"}]}";
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(json).streamInput());
ScriptContextInfo.ScriptMethodInfo info = ScriptContextInfo.ScriptMethodInfo.fromXContent(parser);
assertEquals(new ScriptContextInfo.ScriptMethodInfo("fooFunc", "int", new ArrayList<>(Arrays.asList(new ScriptContextInfo.ScriptMethodInfo.ParameterInfo("int", "fooParam"), new ScriptContextInfo.ScriptMethodInfo.ParameterInfo("java.util.Map", "barParam")))), info);
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class ScriptTests method testParse.
public void testParse() throws IOException {
Script expectedScript = createScript();
try (XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()))) {
expectedScript.toXContent(builder, ToXContent.EMPTY_PARAMS);
try (XContentParser xParser = createParser(builder)) {
Settings settings = Settings.fromXContent(xParser);
Script actualScript = Script.parse(settings);
assertThat(actualScript, equalTo(expectedScript));
}
}
}
Aggregations