Search in sources :

Example 86 with XContentParser

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();
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 87 with XContentParser

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);
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 88 with XContentParser

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());
    }
}
Also used : FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 89 with XContentParser

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);
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) ArrayList(java.util.ArrayList) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo) ParameterInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo.ParameterInfo) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 90 with XContentParser

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));
        }
    }
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) Settings(org.opensearch.common.settings.Settings)

Aggregations

XContentParser (org.opensearch.common.xcontent.XContentParser)767 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)263 IOException (java.io.IOException)159 BytesReference (org.opensearch.common.bytes.BytesReference)118 ParsingException (org.opensearch.common.ParsingException)112 XContentType (org.opensearch.common.xcontent.XContentType)107 ArrayList (java.util.ArrayList)86 List (java.util.List)59 OpenSearchParseException (org.opensearch.OpenSearchParseException)55 HashMap (java.util.HashMap)51 Matchers.containsString (org.hamcrest.Matchers.containsString)51 Map (java.util.Map)48 ToXContent (org.opensearch.common.xcontent.ToXContent)46 OpenSearchException (org.opensearch.OpenSearchException)45 XContentParseException (org.opensearch.common.xcontent.XContentParseException)40 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)35 SearchResponse (org.opensearch.action.search.SearchResponse)29 BytesArray (org.opensearch.common.bytes.BytesArray)27 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)26 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)25