use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class NestedIdentityTests method testFromXContent.
public void testFromXContent() throws IOException {
NestedIdentity nestedIdentity = createTestItem(randomInt(3));
XContentType xcontentType = randomFrom(XContentType.values());
XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
if (randomBoolean()) {
builder.prettyPrint();
}
builder = nestedIdentity.innerToXContent(builder, ToXContent.EMPTY_PARAMS);
try (XContentParser parser = createParser(builder)) {
NestedIdentity parsedNestedIdentity = NestedIdentity.fromXContent(parser);
assertEquals(nestedIdentity, parsedNestedIdentity);
assertNull(parser.nextToken());
}
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class AggregatorFactoriesTests method testTwoTypes.
public void testTwoTypes() throws Exception {
XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("in_stock").startObject("filter").startObject("range").startObject("stock").field("gt", 0).endObject().endObject().endObject().startObject("terms").field("field", "stock").endObject().endObject().endObject();
XContentParser parser = createParser(source);
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
assertThat(e.toString(), containsString("Found two aggregation type definitions in [in_stock]: [filter] and [terms]"));
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class AggregatorFactoriesTests method testInvalidType.
public void testInvalidType() throws Exception {
XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("calendar_interval", "month").endObject().startObject("aggs").startObject("tags").startObject("term").field("field", "tag").endObject().endObject().endObject().endObject().endObject();
XContentParser parser = createParser(source);
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
assertThat(e.toString(), containsString("Unknown aggregation type [term] did you mean [terms]?"));
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class AggregatorFactoriesTests method testMissingType.
public void testMissingType() throws Exception {
XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("calendar_interval", "month").endObject().startObject("aggs").startObject("tag_count").field("field", "tag").endObject().endObject().endObject().endObject();
XContentParser parser = createParser(source);
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [tag_count]"));
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class AggregatorFactoriesTests method testMissingName.
public void testMissingName() throws Exception {
XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("calendar_interval", "month").endObject().startObject("aggs").startObject("cardinality").field("field", "tag").endObject().endObject().endObject().endObject();
XContentParser parser = createParser(source);
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [cardinality]"));
}
Aggregations