diff --git a/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java b/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java index 18a24e00116..df4c6898cc7 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java @@ -36,8 +36,10 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.function.Predicate; import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; +import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; public class CompletionSuggestionOptionTests extends ESTestCase { @@ -67,17 +69,31 @@ public class CompletionSuggestionOptionTests extends ESTestCase { } public void testFromXContent() throws IOException { + doTestFromXContent(false); + } + + public void testFromXContentWithRandomFields() throws IOException { + doTestFromXContent(true); + } + + private void doTestFromXContent(boolean addRandomFields) throws IOException { Option option = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); BytesReference originalBytes = toShuffledXContent(option, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - if (randomBoolean()) { - try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { - originalBytes = shuffleXContent(parser, randomBoolean()).bytes(); - } + BytesReference mutated; + if (addRandomFields) { + // "contexts" is an object consisting of key/array pairs, we shouldn't add anything random there + // also there can be inner search hits fields inside this option, we need to exclude another couple of paths + // where we cannot add random stuff + Predicate excludeFilter = (path) -> (path.endsWith(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName()) + || path.endsWith("highlight") || path.endsWith("fields") || path.contains("_source") || path.contains("inner_hits")); + mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random()); + } else { + mutated = originalBytes; } Option parsed; - try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { + try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { parsed = Option.fromXContent(parser); assertNull(parser.nextToken()); } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java b/core/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java index c565836adb6..8c938caa479 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java @@ -90,7 +90,7 @@ public class SuggestTests extends ESTestCase { Suggest suggest = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(suggest, xContentType, params, humanReadable); + BytesReference originalBytes = toShuffledXContent(suggest, xContentType, params, humanReadable); Suggest parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java b/core/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java index 770fd2f6e6c..46971a5537b 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java @@ -36,10 +36,12 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; +import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; public class SuggestionEntryTests extends ESTestCase { @@ -80,15 +82,35 @@ public class SuggestionEntryTests extends ESTestCase { return entry; } - @SuppressWarnings("unchecked") public void testFromXContent() throws IOException { + doTestFromXContent(false); + } + + public void testFromXContentWithRandomFields() throws IOException { + doTestFromXContent(true); + } + + @SuppressWarnings("unchecked") + private void doTestFromXContent(boolean addRandomFields) throws IOException { for (Class entryType : ENTRY_PARSERS.keySet()) { Entry