use of org.opensearch.search.aggregations.ParsedAggregation in project OpenSearch by opensearch-project.
the class InternalAggregationTestCase method parseAndAssert.
@SuppressWarnings("unchecked")
protected <P extends ParsedAggregation> P parseAndAssert(final InternalAggregation aggregation, final boolean shuffled, final boolean addRandomFields) throws IOException {
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
final XContentType xContentType = randomFrom(XContentType.values());
final boolean humanReadable = randomBoolean();
final BytesReference originalBytes;
if (shuffled) {
originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);
} else {
originalBytes = toXContent(aggregation, xContentType, params, humanReadable);
}
BytesReference mutated;
if (addRandomFields) {
/*
* - we don't add to the root object because it should only contain
* the named aggregation to test - we don't want to insert into the
* "meta" object, because we pass on everything we find there
*
* - we don't want to directly insert anything random into "buckets"
* objects, they are used with "keyed" aggregations and contain
* named bucket objects. Any new named object on this level should
* also be a bucket and be parsed as such.
*
* we also exclude top_hits that contain SearchHits, as all unknown fields
* on a root level of SearchHit are interpreted as meta-fields and will be kept.
*/
Predicate<String> basicExcludes = path -> path.isEmpty() || path.endsWith(Aggregation.CommonFields.META.getPreferredName()) || path.endsWith(Aggregation.CommonFields.BUCKETS.getPreferredName()) || path.contains("top_hits");
Predicate<String> excludes = basicExcludes.or(excludePathsFromXContentInsertion());
mutated = XContentTestUtils.insertRandomFields(xContentType, originalBytes, excludes, random());
} else {
mutated = originalBytes;
}
SetOnce<Aggregation> parsedAggregation = new SetOnce<>();
try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class, parsedAggregation::set);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
Aggregation agg = parsedAggregation.get();
assertEquals(aggregation.getName(), agg.getName());
assertEquals(aggregation.getMetadata(), agg.getMetadata());
assertTrue(agg instanceof ParsedAggregation);
assertEquals(aggregation.getType(), agg.getType());
BytesReference parsedBytes = toXContent(agg, xContentType, params, humanReadable);
OpenSearchAssertions.assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
return (P) agg;
}
}
use of org.opensearch.search.aggregations.ParsedAggregation in project OpenSearch by opensearch-project.
the class InternalAggregationTestCase method testFromXContent.
public final void testFromXContent() throws IOException {
final T aggregation = createTestInstanceForXContent();
final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), false);
assertFromXContent(aggregation, parsedAggregation);
}
use of org.opensearch.search.aggregations.ParsedAggregation in project OpenSearch by opensearch-project.
the class InternalAggregationTestCase method testFromXContentWithRandomFields.
public final void testFromXContentWithRandomFields() throws IOException {
final T aggregation = createTestInstanceForXContent();
final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), true);
assertFromXContent(aggregation, parsedAggregation);
}
Aggregations