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