Search in sources :

Example 21 with ParseField

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

the class InstantiatingObjectParserTests method testNoAnnotationWrongArgumentNumber.

public void testNoAnnotationWrongArgumentNumber() {
    InstantiatingObjectParser.Builder<NoAnnotations, Void> builder = InstantiatingObjectParser.builder("foo", NoAnnotations.class);
    builder.declareInt(constructorArg(), new ParseField("a"));
    builder.declareString(constructorArg(), new ParseField("b"));
    builder.declareLong(constructorArg(), new ParseField("c"));
    builder.declareLong(constructorArg(), new ParseField("d"));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::build);
    assertThat(e.getMessage(), containsString("No public constructors with 4 parameters exist in the class"));
}
Also used : ParseField(org.opensearch.common.ParseField)

Example 22 with ParseField

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

the class ObjectParserTests method testParseNested.

public void testParseNested() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : 1, \"object\" : { \"test\": 2}}");
    class TestStruct {

        public int test;

        TestStruct object;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    TestStruct s = new TestStruct();
    s.object = new TestStruct();
    objectParser.declareField((i, c, x) -> c.test = i.intValue(), new ParseField("test"), ValueType.INT);
    objectParser.declareField((i, c, x) -> objectParser.parse(parser, c.object, null), new ParseField("object"), ValueType.OBJECT);
    objectParser.parse(parser, s, null);
    assertEquals(s.test, 1);
    assertEquals(s.object.test, 2);
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.opensearch.common.ParseField)

Example 23 with ParseField

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

the class ObjectParserTests method testDeprecationWarnings.

public void testDeprecationWarnings() throws IOException {
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    TestStruct s = new TestStruct();
    XContentParser parser = createParser(XContentType.JSON.xContent(), "{\"old_test\" : \"foo\"}");
    objectParser.declareField((i, v, c) -> v.test = i.text(), new ParseField("test", "old_test"), ObjectParser.ValueType.STRING);
    objectParser.parse(parser, s, null);
    assertEquals("foo", s.test);
    assertWarnings(false, "[foo][1:15] Deprecated field [old_test] used, expected [test] instead");
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField)

Example 24 with ParseField

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

the class ObjectParserTests method testEmptyObjectInArray.

public void testEmptyObjectInArray() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object_array\" : [{}]}");
    ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
    objectParser.declareObjectArray(StaticTestStruct::setObjectArray, objectParser, new ParseField("object_array"));
    StaticTestStruct s = objectParser.parse(parser, null);
    assertNotNull(s.objectArray);
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.opensearch.common.ParseField)

Example 25 with ParseField

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

the class ObjectParserTests method testExceptions.

public void testExceptions() throws IOException {
    class TestStruct {

        public void setTest(int test) {
        }
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("the_parser");
    TestStruct s = new TestStruct();
    objectParser.declareInt(TestStruct::setTest, new ParseField("test"));
    {
        XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"test\" : \"foo\"}");
        XContentParseException ex = expectThrows(XContentParseException.class, () -> objectParser.parse(parser, s, null));
        assertThat(ex.getMessage(), containsString("[the_parser] failed to parse field [test]"));
        assertTrue(ex.getCause() instanceof NumberFormatException);
    }
    {
        XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"not_supported_field\" : \"foo\"}");
        XContentParseException ex = expectThrows(XContentParseException.class, () -> objectParser.parse(parser, s, null));
        assertEquals(ex.getMessage(), "[1:2] [the_parser] unknown field [not_supported_field]");
    }
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) 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