use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class XContentHelperTests method testToXContent.
public void testToXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final ToXContent toXContent;
final boolean error;
if (randomBoolean()) {
if (randomBoolean()) {
error = false;
toXContent = (builder, params) -> builder.field("field", "value");
} else {
error = true;
toXContent = (builder, params) -> builder.startObject().field("field", "value").endObject();
}
} else {
if (randomBoolean()) {
error = false;
toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("field", "value").endObject();
} else {
error = true;
toXContent = (ToXContentObject) (builder, params) -> builder.field("field", "value");
}
}
if (error) {
expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType, randomBoolean()));
} else {
BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean());
try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertTrue(parser.nextToken().isValue());
assertEquals("value", parser.text());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
}
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class XContentMapValuesTests method testThatFilterIncludesEmptyObjectWhenUsingExcludes.
public void testThatFilterIncludesEmptyObjectWhenUsingExcludes() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[] { "nonExistingField" });
assertThat(mapTuple.v2(), equalTo(filteredSource));
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class XContentMapValuesTests method testIncludingObjectWithNestedIncludedObject.
@SuppressWarnings({ "unchecked" })
public void testIncludingObjectWithNestedIncludedObject() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj1").startObject("obj2").endObject().endObject().endObject();
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[] { "*.obj2" }, Strings.EMPTY_ARRAY);
assertThat(filteredSource.size(), equalTo(1));
assertThat(filteredSource, hasKey("obj1"));
assertThat(((Map) filteredSource.get("obj1")).size(), equalTo(1));
assertThat(((Map<String, Object>) filteredSource.get("obj1")), hasKey("obj2"));
assertThat(((Map) ((Map) filteredSource.get("obj1")).get("obj2")).size(), equalTo(0));
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class XContentMapValuesTests method testThatFilterIncludesEmptyObjectWhenUsingIncludes.
public void testThatFilterIncludesEmptyObjectWhenUsingIncludes() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[] { "obj" }, Strings.EMPTY_ARRAY);
assertThat(mapTuple.v2(), equalTo(filteredSource));
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class TemplateQueryBuilderTests method testUnknownField.
/**
* Override superclass test since template query doesn't support boost and queryName, so
* we need to mutate other existing field in the test query.
*/
@Override
public void testUnknownField() throws IOException {
TemplateQueryBuilder testQuery = createTestQueryBuilder();
XContentType xContentType = randomFrom(XContentType.JSON, XContentType.YAML);
String testQueryAsString = toXContent(testQuery, xContentType).string();
String queryAsString = testQueryAsString.replace("inline", "bogusField");
try {
parseQuery(createParser(xContentType.xContent(), queryAsString));
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("[script] unknown field [bogusField], parser not found"));
}
}
Aggregations