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"));
}
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);
}
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");
}
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);
}
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]");
}
}
Aggregations