Skip to content

Commit a6fd2fa

Browse files
authored
Test: Handle order change (#70427)
Fixes a error in `TopHitsTests` introduced in #70318 where we were too strict in our round trip tests. We asserted that serialization produced exactly the same `.toString` which is true of most aggregations, but not `top_hits`. In `top_hits` it shuffles some map keys. Which is just fine. We should not consider that a failure.
1 parent 302341a commit a6fd2fa

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
package org.elasticsearch.search.aggregations.metrics;
1010

11+
import org.elasticsearch.common.xcontent.XContentHelper;
1112
import org.elasticsearch.common.xcontent.XContentParser;
13+
import org.elasticsearch.common.xcontent.XContentType;
1214
import org.elasticsearch.common.xcontent.json.JsonXContent;
1315
import org.elasticsearch.search.aggregations.AggregationInitializationException;
1416
import org.elasticsearch.search.aggregations.AggregatorFactories;
@@ -19,7 +21,9 @@
1921
import org.elasticsearch.search.sort.SortBuilders;
2022
import org.elasticsearch.search.sort.SortOrder;
2123
import org.elasticsearch.test.AbstractQueryTestCase;
24+
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
2225

26+
import java.io.IOException;
2327
import java.util.ArrayList;
2428
import java.util.Collections;
2529
import java.util.List;
@@ -189,4 +193,13 @@ public void testFailWithSubAgg() throws Exception {
189193
assertThat(e.toString(), containsString("Aggregator [top_tags_hits] of type [top_hits] cannot accept sub-aggregations"));
190194
}
191195

196+
@Override
197+
protected void assertToXContentAfterSerialization(TopHitsAggregationBuilder original, TopHitsAggregationBuilder deserialized)
198+
throws IOException {
199+
ElasticsearchAssertions.assertToXContentEquivalent(
200+
XContentHelper.toXContent(original, XContentType.JSON, false),
201+
XContentHelper.toXContent(deserialized, XContentType.JSON, false),
202+
XContentType.JSON
203+
);
204+
}
192205
}

test/framework/src/main/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,20 @@ public void testSerialization() throws IOException {
145145
assertEquals(testAgg, deserialized);
146146
assertEquals(testAgg.hashCode(), deserialized.hashCode());
147147
assertNotSame(testAgg, deserialized);
148-
// Make sure serialization preserves toXContent.
149-
assertEquals(Strings.toString(testAgg), Strings.toString(deserialized));
148+
@SuppressWarnings("unchecked") // They are .equal so its safe
149+
AB castDeserialized = (AB) deserialized;
150+
assertToXContentAfterSerialization(testAgg, castDeserialized);
150151
}
151152
}
152153
}
153154

155+
/**
156+
* Make sure serialization preserves toXContent.
157+
*/
158+
protected void assertToXContentAfterSerialization(AB original, AB deserialized) throws IOException {
159+
assertEquals(Strings.toString(original), Strings.toString(deserialized));
160+
}
161+
154162
public void testEqualsAndHashcode() throws IOException {
155163
// TODO we only change name and boost, we should extend by any sub-test supplying a "mutate" method that randomly changes one
156164
// aspect of the object under test

0 commit comments

Comments
 (0)