[Test] Always check the XContent equivalent when parsing aggregations (#24208)

In InternalAggregationTestCase, we can check that the internal aggregation and the parsed aggregation always produce the same XContent even if the original internal aggregation has been shuffled or not.
This commit is contained in:
Tanguy Leroux 2017-04-20 13:22:50 +02:00 committed by GitHub
parent 6e22a1e9ba
commit d0df1ed193
2 changed files with 11 additions and 14 deletions

View File

@ -203,15 +203,7 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
assumeTrue("This test does not support the aggregation type yet",
getNamedXContents().stream().filter(entry -> entry.name.match(aggregation.getType())).count() > 0);
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
final boolean humanReadable = randomBoolean();
final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);
final Aggregation parsedAggregation = parse(aggregation, xContentType, humanReadable, randomBoolean());
final BytesReference parsedBytes = toXContent((ToXContent) parsedAggregation, xContentType, params, humanReadable);
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
final Aggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean());
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
}
@ -220,12 +212,13 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
}
@SuppressWarnings("unchecked")
protected <P extends ParsedAggregation> P parse(final InternalAggregation aggregation,
final XContentType xContentType,
final boolean humanReadable,
protected <P extends ParsedAggregation> P parseAndAssert(final InternalAggregation aggregation,
final boolean shuffled) 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);
@ -255,6 +248,10 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
assertTrue(parsedAggregation instanceof ParsedAggregation);
assertEquals(aggregation.getType(), ((ParsedAggregation) parsedAggregation).getType());
}
BytesReference parsedBytes = toXContent((ToXContent) parsedAggregation, xContentType, params, humanReadable);
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
return (P) parsedAggregation;
}

View File

@ -62,7 +62,7 @@ public abstract class AbstractPercentilesTestCase<T extends InternalAggregation
public void testPercentilesIterators() throws IOException {
final T aggregation = createTestInstance();
final Iterable<Percentile> parsedAggregation = parse(aggregation, randomFrom(XContentType.values()), randomBoolean(), false);
final Iterable<Percentile> parsedAggregation = parseAndAssert(aggregation, false);
Iterator<Percentile> it = aggregation.iterator();
Iterator<Percentile> parsedIt = parsedAggregation.iterator();