Search in sources :

Example 56 with ParseField

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

the class ObjectParserTests method testMultipleRequiredFieldSet.

public void testMultipleRequiredFieldSet() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"unrelated\": \"123\"}");
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, TestStruct::new);
    objectParser.declareLong(TestStruct::setA, new ParseField("a"));
    objectParser.declareLong(TestStruct::setB, new ParseField("b"));
    objectParser.declareLong(TestStruct::setC, new ParseField("c"));
    objectParser.declareLong(TestStruct::setD, new ParseField("d"));
    objectParser.declareRequiredFieldSet(new String[] { "a", "b" });
    objectParser.declareRequiredFieldSet(new String[] { "c", "d" });
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null));
    assertThat(e.getMessage(), equalTo("Required one of fields [a, b], but none were specified. " + "Required one of fields [c, d], but none were specified. "));
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.opensearch.common.ParseField)

Example 57 with ParseField

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

the class ObjectParserTests method testFailOnValueType.

public void testFailOnValueType() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"numeric_value\" : false}");
    class TestStruct {

        @SuppressWarnings("unused")
        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    TestStruct s = new TestStruct();
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("numeric_value"), ObjectParser.ValueType.FLOAT);
    Exception e = expectThrows(XContentParseException.class, () -> objectParser.parse(parser, s, null));
    assertThat(e.getMessage(), containsString("[foo] numeric_value doesn't support values of type: VALUE_BOOLEAN"));
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 58 with ParseField

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

the class ObjectParserTests method testParseNamedObjectsInOrderNotSupported.

public void testParseNamedObjectsInOrderNotSupported() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ] }");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    ObjectParser<NamedObjectsHolder, Void> objectParser = new ObjectParser<>("named_object_holder", NamedObjectsHolder::new);
    objectParser.declareNamedObjects(NamedObjectsHolder::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 : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.opensearch.common.ParseField)

Example 59 with ParseField

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

the class ConstructingObjectParserTests method testCalledOneTime.

/**
 * Tests the non-constructor fields are only set on time.
 */
public void testCalledOneTime() throws IOException {
    boolean ctorArgOptional = randomBoolean();
    class CalledOneTime {

        CalledOneTime(String yeah) {
            Matcher<String> yeahMatcher = equalTo("!");
            if (ctorArgOptional) {
                // either(yeahMatcher).or(nullValue) is broken by https://github.com/hamcrest/JavaHamcrest/issues/49
                yeahMatcher = anyOf(yeahMatcher, nullValue());
            }
            assertThat(yeah, yeahMatcher);
        }

        boolean fooSet = false;

        void setFoo(String foo) {
            assertFalse(fooSet);
            fooSet = true;
        }
    }
    ConstructingObjectParser<CalledOneTime, Void> parser = new ConstructingObjectParser<>("one_time_test", (a) -> new CalledOneTime((String) a[0]));
    parser.declareString(CalledOneTime::setFoo, new ParseField("foo"));
    parser.declareString(ctorArgOptional ? optionalConstructorArg() : constructorArg(), new ParseField("yeah"));
    // ctor arg first so we can test for the bug we found one time
    XContentParser xcontent = createParser(JsonXContent.jsonXContent, "{ \"yeah\": \"!\", \"foo\": \"foo\" }");
    CalledOneTime result = parser.apply(xcontent, null);
    assertTrue(result.fooSet);
    // and ctor arg second just in case
    xcontent = createParser(JsonXContent.jsonXContent, "{ \"foo\": \"foo\",  \"yeah\": \"!\" }");
    result = parser.apply(xcontent, null);
    assertTrue(result.fooSet);
    if (ctorArgOptional) {
        // and without the constructor arg if we've made it optional
        xcontent = createParser(JsonXContent.jsonXContent, "{ \"foo\": \"foo\" }");
        result = parser.apply(xcontent, null);
    }
    assertTrue(result.fooSet);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField)

Example 60 with ParseField

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

the class ConstructingObjectParserTests method testIgnoreUnknownFields.

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

        public final String test;

        TestStruct(String test) {
            this.test = test;
        }
    }
    ConstructingObjectParser<TestStruct, Void> objectParser = new ConstructingObjectParser<>("foo", true, a -> new TestStruct((String) a[0]));
    objectParser.declareString(constructorArg(), new ParseField("test"));
    TestStruct s = objectParser.apply(parser, null);
    assertEquals(s.test, "foo");
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField)

Aggregations

ParseField (org.opensearch.common.ParseField)67 NamedObjectParser (org.opensearch.common.xcontent.ObjectParser.NamedObjectParser)26 List (java.util.List)15 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)9 Map (java.util.Map)8 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)7 Aggregation (org.opensearch.search.aggregations.Aggregation)7 HashMap (java.util.HashMap)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 UncheckedIOException (java.io.UncheckedIOException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Collectors (java.util.stream.Collectors)3 OpenSearchException (org.opensearch.OpenSearchException)3 ExplainResponse (org.opensearch.action.explain.ExplainResponse)3 ContextParser (org.opensearch.common.xcontent.ContextParser)3 NamedObject (org.opensearch.common.xcontent.ObjectParserTests.NamedObject)3