use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class ObjectParserTests method testExclusiveFieldSet.
public void testExclusiveFieldSet() throws IOException {
XContentParser goodA = createParser(JsonXContent.jsonXContent, "{\"a\" : 1, \"c\" : 4}");
XContentParser bad = createParser(JsonXContent.jsonXContent, "{\"a\" : 1, \"b\" : 2}");
XContentParser badmulti = createParser(JsonXContent.jsonXContent, "{\"a\" : 1, \"b\" : 2, \"c\" : 3, \"d\" : 4 }");
ObjectParser<TestStruct, Void> parser = new ObjectParser<>("foo", TestStruct::new);
parser.declareLong(TestStruct::setA, new ParseField("a"));
parser.declareLong(TestStruct::setB, new ParseField("b"));
parser.declareLong(TestStruct::setC, new ParseField("c"));
parser.declareLong(TestStruct::setD, new ParseField("d"));
parser.declareExclusiveFieldSet("a", "b");
parser.declareExclusiveFieldSet("c", "d");
TestStruct actualA = parser.parse(goodA, null);
assertThat(actualA.a, equalTo(1L));
assertThat(actualA.c, equalTo(4L));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse(bad, null));
assertThat(e.getMessage(), containsString("The following fields are not allowed together: [a, b]"));
e = expectThrows(IllegalArgumentException.class, () -> parser.parse(badmulti, null));
assertThat(e.getMessage(), containsString("allowed together: [a, b] The following fields are not allowed together: [c, d]"));
}
use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class ObjectParserTests method testConsumeUnknownFields.
public void testConsumeUnknownFields() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"test_number\" : 2,\n" + " \"name\" : \"geoff\",\n" + " \"test_boolean\" : true,\n" + " \"test_null\" : null,\n" + " \"test_array\": [1,2,3,4],\n" + " \"test_nested\": { \"field\" : \"value\", \"field2\" : [ \"list1\", \"list2\" ] }\n" + "}");
ObjectParser<ObjectWithArbitraryFields, Void> op = new ObjectParser<>("unknown", ObjectWithArbitraryFields::setField, ObjectWithArbitraryFields::new);
op.declareString(ObjectWithArbitraryFields::setName, new ParseField("name"));
ObjectWithArbitraryFields o = op.parse(parser, null);
assertEquals("geoff", o.name);
assertEquals(6, o.fields.size());
assertEquals("foo", o.fields.get("test"));
assertEquals(2, o.fields.get("test_number"));
assertEquals(true, o.fields.get("test_boolean"));
assertNull(o.fields.get("test_null"));
assertThat(o.fields.get("test_array"), instanceOf(List.class));
assertThat(o.fields.get("test_nested"), instanceOf(Map.class));
}
use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class ObjectParserTests method testEmptyObject.
public void testEmptyObject() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object\" : {}}");
ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
objectParser.declareObject(StaticTestStruct::setObject, objectParser, new ParseField("object"));
StaticTestStruct s = objectParser.parse(parser, null);
assertNotNull(s.object);
}
use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class ObjectParserTests method testIgnoreUnknownFields.
public void testIgnoreUnknownFields() throws IOException {
XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
b.startObject();
{
b.field("test", "foo");
b.field("junk", 2);
}
b.endObject();
b = shuffleXContent(b);
b.flush();
byte[] bytes = ((ByteArrayOutputStream) b.getOutputStream()).toByteArray();
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
class TestStruct {
public String test;
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
TestStruct s = objectParser.parse(parser, new TestStruct(), null);
assertEquals(s.test, "foo");
}
use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class ObjectParserTests method testIgnoreUnknownObjects.
public void testIgnoreUnknownObjects() throws IOException {
XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
b.startObject();
{
b.field("test", "foo");
b.startObject("junk");
{
b.field("really", "junk");
}
b.endObject();
}
b.endObject();
b = shuffleXContent(b);
b.flush();
byte[] bytes = ((ByteArrayOutputStream) b.getOutputStream()).toByteArray();
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
class TestStruct {
public String test;
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
TestStruct s = objectParser.parse(parser, new TestStruct(), null);
assertEquals(s.test, "foo");
}
Aggregations