Search in sources :

Example 1 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class NodesResponse method declareCommonNodesResponseParsing.

public static <T extends NodesResponse> void declareCommonNodesResponseParsing(ConstructingObjectParser<T, Void> parser) {
    parser.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes"));
    parser.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name"));
}
Also used : ParseField(org.opensearch.common.ParseField)

Example 2 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ConstructingObjectParserTests method testParseNamedObjectInOrderNotSupported.

public void testParseNamedObjectInOrderNotSupported() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + "  {\"a\": {}}" + "],\"named_in_constructor\": {\"b\": {}}" + "}");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    @SuppressWarnings("unchecked") ConstructingObjectParser<NamedObjectHolder, Void> objectParser = new ConstructingObjectParser<>("named_object_holder", a -> new NamedObjectHolder(((List<NamedObject>) a[0])));
    objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, new ParseField("named_in_constructor"));
    objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
    // Now firing the xml through it fails
    XContentParseException e = expectThrows(XContentParseException.class, () -> objectParser.apply(parser, null));
    assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]"));
    assertEquals("[named] doesn't support arrays. Use a single object with multiple fields.", e.getCause().getMessage());
}
Also used : NamedObject(org.opensearch.common.xcontent.ObjectParserTests.NamedObject) ParseField(org.opensearch.common.ParseField)

Example 3 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ConstructingObjectParserTests method testNullDeclares.

public void testNullDeclares() {
    ConstructingObjectParser<Void, Void> objectParser = new ConstructingObjectParser<>("foo", a -> null);
    Exception e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[consumer] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (ContextParser<Void, Object>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (CheckedFunction<XContentParser, Object, IOException>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, null, ObjectParser.ValueType.STRING));
    assertEquals("[parseField] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, new ParseField("test"), null));
    assertEquals("[type] is required", e.getMessage());
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collections.unmodifiableList(java.util.Collections.unmodifiableList) CheckedFunction(org.opensearch.common.CheckedFunction) NamedObject(org.opensearch.common.xcontent.ObjectParserTests.NamedObject) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) ParseField(org.opensearch.common.ParseField) Nullable(org.opensearch.common.Nullable) ConstructingObjectParser.constructorArg(org.opensearch.common.xcontent.ConstructingObjectParser.constructorArg) List(java.util.List) Matcher(org.hamcrest.Matcher) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ConstructingObjectParser.optionalConstructorArg(org.opensearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Matchers.containsString(org.hamcrest.Matchers.containsString) NamedObject(org.opensearch.common.xcontent.ObjectParserTests.NamedObject) IOException(java.io.IOException) IOException(java.io.IOException) ParseField(org.opensearch.common.ParseField)

Example 4 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ConstructingObjectParserTests method testRequiredAndExclusiveFields.

public void testRequiredAndExclusiveFields() throws IOException {
    class TestStruct {

        final String a;

        final long b;

        TestStruct(String a) {
            this.a = a;
            this.b = 0;
        }

        TestStruct(long b) {
            this.a = null;
            this.b = b;
        }
    }
    XContentParser ok = createParser(JsonXContent.jsonXContent, "{ \"a\" : \"a\" }");
    XContentParser toomany = createParser(JsonXContent.jsonXContent, "{ \"a\" : \"a\", \"b\" : 1 }");
    XContentParser notenough = createParser(JsonXContent.jsonXContent, "{ }");
    ConstructingObjectParser<TestStruct, Void> parser = new ConstructingObjectParser<>("teststruct", args -> {
        if (args[0] != null) {
            return new TestStruct((String) args[0]);
        }
        return new TestStruct((Long) args[1]);
    });
    parser.declareString(optionalConstructorArg(), new ParseField("a"));
    parser.declareLong(optionalConstructorArg(), new ParseField("b"));
    parser.declareExclusiveFieldSet("a", "b");
    parser.declareRequiredFieldSet("a", "b");
    TestStruct actual = parser.parse(ok, null);
    assertThat(actual.a, equalTo("a"));
    assertThat(actual.b, equalTo(0L));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse(toomany, null));
    assertThat(e.getMessage(), containsString("allowed together: [a, b]"));
    e = expectThrows(IllegalArgumentException.class, () -> parser.parse(notenough, null));
    assertThat(e.getMessage(), containsString("Required one of fields [a, b], but none were specified."));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField)

Example 5 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ConstructingObjectParserTests method testParseNamedObjectInOrderNotSupportedConstructorArg.

public void testParseNamedObjectInOrderNotSupportedConstructorArg() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": {\"a\": {}}, \"named_in_constructor\": [ {\"b\": {}} ]}");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    @SuppressWarnings("unchecked") ConstructingObjectParser<NamedObjectHolder, Void> objectParser = new ConstructingObjectParser<>("named_object_holder", a -> new NamedObjectHolder(((List<NamedObject>) a[0])));
    objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, new ParseField("named_in_constructor"));
    objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
    // Now firing the xml through it fails
    XContentParseException e = expectThrows(XContentParseException.class, () -> objectParser.apply(parser, null));
    assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]"));
    assertThat(e.getCause().getMessage(), containsString("[named_in_constructor] doesn't support arrays. Use a single object with multiple fields."));
}
Also used : NamedObject(org.opensearch.common.xcontent.ObjectParserTests.NamedObject) ParseField(org.opensearch.common.ParseField)

Aggregations

ParseField (org.opensearch.common.ParseField)64 NamedObjectParser (org.opensearch.common.xcontent.ObjectParser.NamedObjectParser)26 Matchers.containsString (org.hamcrest.Matchers.containsString)15 List (java.util.List)13 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)9 Map (java.util.Map)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 UncheckedIOException (java.io.UncheckedIOException)5 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)5 Aggregation (org.opensearch.search.aggregations.Aggregation)5 HashMap (java.util.HashMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Arrays (java.util.Arrays)3 CheckedFunction (org.opensearch.common.CheckedFunction)3 ConstructingObjectParser (org.opensearch.common.xcontent.ConstructingObjectParser)3 NamedObject (org.opensearch.common.xcontent.ObjectParserTests.NamedObject)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 JsonXContent (org.opensearch.common.xcontent.json.JsonXContent)3 URI (java.net.URI)2