|
49 | 49 | import java.util.List;
|
50 | 50 |
|
51 | 51 | import static java.util.Collections.singletonMap;
|
| 52 | +import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; |
52 | 53 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
|
53 | 54 |
|
54 | 55 | public class SearchResponseTests extends ESTestCase {
|
@@ -78,31 +79,71 @@ protected NamedXContentRegistry xContentRegistry() {
|
78 | 79 | }
|
79 | 80 |
|
80 | 81 | private SearchResponse createTestItem(ShardSearchFailure... shardSearchFailures) {
|
81 |
| - SearchHits hits = SearchHitsTests.createTestItem(); |
| 82 | + return createTestItem(false, shardSearchFailures); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * This SearchResponse doesn't include SearchHits, Aggregations, Suggestions, ShardSearchFailures, SearchProfileShardResults |
| 87 | + * to make it possible to only test properties of the SearchResponse itself |
| 88 | + */ |
| 89 | + private SearchResponse createMinimalTestItem() { |
| 90 | + return createTestItem(true); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * if minimal is set, don't include search hits, aggregations, suggest etc... to make test simpler |
| 95 | + */ |
| 96 | + private SearchResponse createTestItem(boolean minimal, ShardSearchFailure... shardSearchFailures) { |
82 | 97 | boolean timedOut = randomBoolean();
|
83 | 98 | Boolean terminatedEarly = randomBoolean() ? null : randomBoolean();
|
84 | 99 | int numReducePhases = randomIntBetween(1, 10);
|
85 | 100 | long tookInMillis = randomNonNegativeLong();
|
86 | 101 | int successfulShards = randomInt();
|
87 | 102 | int totalShards = randomInt();
|
88 |
| - |
89 |
| - InternalAggregations aggregations = aggregationsTests.createTestInstance(); |
90 |
| - Suggest suggest = SuggestTests.createTestItem(); |
91 |
| - SearchProfileShardResults profileShardResults = SearchProfileShardResultsTests.createTestItem(); |
92 |
| - |
93 |
| - InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, aggregations, suggest, profileShardResults, |
| 103 | + InternalSearchResponse internalSearchResponse; |
| 104 | + if (minimal == false) { |
| 105 | + SearchHits hits = SearchHitsTests.createTestItem(); |
| 106 | + InternalAggregations aggregations = aggregationsTests.createTestInstance(); |
| 107 | + Suggest suggest = SuggestTests.createTestItem(); |
| 108 | + SearchProfileShardResults profileShardResults = SearchProfileShardResultsTests.createTestItem(); |
| 109 | + internalSearchResponse = new InternalSearchResponse(hits, aggregations, suggest, profileShardResults, |
94 | 110 | timedOut, terminatedEarly, numReducePhases);
|
| 111 | + } else { |
| 112 | + internalSearchResponse = InternalSearchResponse.empty(); |
| 113 | + } |
95 | 114 | return new SearchResponse(internalSearchResponse, null, totalShards, successfulShards, tookInMillis, shardSearchFailures);
|
96 | 115 | }
|
97 | 116 |
|
| 117 | + /** |
| 118 | + * the "_shard/total/failures" section makes it impossible to directly |
| 119 | + * compare xContent, so we omit it here |
| 120 | + */ |
98 | 121 | public void testFromXContent() throws IOException {
|
99 |
| - // the "_shard/total/failures" section makes if impossible to directly compare xContent, so we omit it here |
100 |
| - SearchResponse response = createTestItem(); |
| 122 | + doFromXContentTestWithRandomFields(createTestItem(), false); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * This test adds random fields and objects to the xContent rendered out to |
| 127 | + * ensure we can parse it back to be forward compatible with additions to |
| 128 | + * the xContent. We test this with a "minimal" SearchResponse, adding random |
| 129 | + * fields to SearchHits, Aggregations etc... is tested in their own tests |
| 130 | + */ |
| 131 | + public void testFromXContentWithRandomFields() throws IOException { |
| 132 | + doFromXContentTestWithRandomFields(createMinimalTestItem(), true); |
| 133 | + } |
| 134 | + |
| 135 | + private void doFromXContentTestWithRandomFields(SearchResponse response, boolean addRandomFields) throws IOException { |
101 | 136 | XContentType xcontentType = randomFrom(XContentType.values());
|
102 | 137 | boolean humanReadable = randomBoolean();
|
103 | 138 | final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
104 | 139 | BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, humanReadable);
|
105 |
| - try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) { |
| 140 | + BytesReference mutated; |
| 141 | + if (addRandomFields) { |
| 142 | + mutated = insertRandomFields(xcontentType, originalBytes, null, random()); |
| 143 | + } else { |
| 144 | + mutated = originalBytes; |
| 145 | + } |
| 146 | + try (XContentParser parser = createParser(xcontentType.xContent(), mutated)) { |
106 | 147 | SearchResponse parsed = SearchResponse.fromXContent(parser);
|
107 | 148 | assertToXContentEquivalent(originalBytes, XContentHelper.toXContent(parsed, xcontentType, params, humanReadable), xcontentType);
|
108 | 149 | assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
|
|
0 commit comments