summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/search/profile
diff options
context:
space:
mode:
authorChristoph Büscher <christoph@elastic.co>2017-06-08 16:40:00 +0200
committerGitHub <noreply@github.com>2017-06-08 16:40:00 +0200
commita0afa917acf05c7699d56665f52df41a9dfe7560 (patch)
tree17facc2348bc793819700f9bcec6828f65c2faf9 /core/src/test/java/org/elasticsearch/search/profile
parent4a8c09c5f143e584b7d51a510f15a61cd3f59560 (diff)
[Tests] Check QueryProfileShardResult parser robustness for new fields (#25130)
When parsing resonses we should be ignoring any new unknown fields or inner objects in most cases to be forward compatible with changes in core on the client side. This change adds test for this for QueryProfileShardResult and nested substructures and changes the parsing code where necessary to be able to ignore new fields and objects in the xContent.
Diffstat (limited to 'core/src/test/java/org/elasticsearch/search/profile')
-rw-r--r--core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java24
-rw-r--r--core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java27
-rw-r--r--core/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java23
3 files changed, 67 insertions, 7 deletions
diff --git a/core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java b/core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java
index 77b41b062d..5174267815 100644
--- a/core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java
+++ b/core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java
@@ -33,9 +33,11 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
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 ProfileResultTests extends ESTestCase {
@@ -62,12 +64,32 @@ public class ProfileResultTests extends ESTestCase {
}
public void testFromXContent() throws IOException {
+ doFromXContentTestWithRandomFields(false);
+ }
+
+ /**
+ * This test adds random fields and objects to the xContent rendered out to ensure we can parse it
+ * back to be forward compatible with additions to the xContent
+ */
+ public void testFromXContentWithRandomFields() throws IOException {
+ doFromXContentTestWithRandomFields(true);
+ }
+
+ private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
ProfileResult profileResult = createTestItem(2);
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toShuffledXContent(profileResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
+ BytesReference mutated;
+ if (addRandomFields) {
+ // "breakdown" just consists of key/value pairs, we shouldn't add anything random there
+ Predicate<String> excludeFilter = (s) -> s.endsWith(ProfileResult.BREAKDOWN.getPreferredName());
+ mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
+ } else {
+ mutated = originalBytes;
+ }
ProfileResult parsed;
- try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
+ try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
parsed = ProfileResult.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
diff --git a/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java b/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java
index 853e7cd13a..7bc9b18860 100644
--- a/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java
+++ b/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java
@@ -34,10 +34,12 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName;
+import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;;
public class SearchProfileShardResultsTests extends ESTestCase {
@@ -58,20 +60,43 @@ public class SearchProfileShardResultsTests extends ESTestCase {
}
public void testFromXContent() throws IOException {
+ doFromXContentTestWithRandomFields(false);
+ }
+
+ /**
+ * This test adds random fields and objects to the xContent rendered out to ensure we can parse it
+ * back to be forward compatible with additions to the xContent
+ */
+ public void testFromXContentWithRandomFields() throws IOException {
+ doFromXContentTestWithRandomFields(true);
+ }
+
+ private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
SearchProfileShardResults shardResult = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toShuffledXContent(shardResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
+ BytesReference mutated;
+ if (addRandomFields) {
+ // The ProfileResults "breakdown" section just consists of key/value pairs, we shouldn't add anything random there
+ // also we don't want to insert into the root object here, its just the PROFILE_FIELD itself
+ Predicate<String> excludeFilter = (s) -> (s.isEmpty() || s.endsWith(ProfileResult.BREAKDOWN.getPreferredName()));
+ mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
+ } else {
+ mutated = originalBytes;
+ }
SearchProfileShardResults parsed;
- try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
+ try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
ensureFieldName(parser, parser.nextToken(), SearchProfileShardResults.PROFILE_FIELD);
ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
parsed = SearchProfileShardResults.fromXContent(parser);
+ assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
+
}
}
diff --git a/core/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java b/core/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java
index 8d87f19360..10bf8e2a30 100644
--- a/core/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java
+++ b/core/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java
@@ -34,6 +34,7 @@ import java.util.List;
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 CollectorResultTests extends ESTestCase {
@@ -57,18 +58,30 @@ public class CollectorResultTests extends ESTestCase {
}
public void testFromXContent() throws IOException {
+ doFromXContentTestWithRandomFields(false);
+ }
+
+ public void testFromXContentWithRandomFields() throws IOException {
+ doFromXContentTestWithRandomFields(true);
+ }
+
+ private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
CollectorResult collectorResult = createTestItem(1);
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toShuffledXContent(collectorResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
-
- CollectorResult parsed;
- try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
+ BytesReference mutated;
+ if (addRandomFields) {
+ mutated = insertRandomFields(xContentType, originalBytes, null, random());
+ } else {
+ mutated = originalBytes;
+ }
+ try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
- parsed = CollectorResult.fromXContent(parser);
+ CollectorResult parsed = CollectorResult.fromXContent(parser);
assertNull(parser.nextToken());
+ assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
- assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
public void testToXContent() throws IOException {