diff options
author | Luca Cavanna <javanna@users.noreply.github.com> | 2017-04-05 11:12:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-05 11:12:34 +0200 |
commit | 318d365b12648daf4231430ae0c5257f83d1da80 (patch) | |
tree | 45d5fc75523535060507aaa181f14235a29a6849 /core/src | |
parent | afd45c14327cd0f8d155e5ac9740f48e8e39b09c (diff) |
[TEST] make sure that fromXContent doesn't rely on keys ordering (#23901)
We shuffle the keys before we parse our responses for the high level client so that we make sure we never rely on keys ordering.
Diffstat (limited to 'core/src')
26 files changed, 61 insertions, 81 deletions
diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 0c6cb1c5cd..3d69dd38b3 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -445,7 +445,8 @@ public class XContentHelper { /** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided - * {@link XContentType}. Wraps the output into a new anonymous object. + * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned + * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, boolean humanReadable) throws IOException { return toXContent(toXContent, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); @@ -453,7 +454,8 @@ public class XContentHelper { /** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided - * {@link XContentType}. Wraps the output into a new anonymous object. + * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned + * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { diff --git a/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java b/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java index 8b4f65ab49..a64acbf9a3 100644 --- a/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java +++ b/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java @@ -58,7 +58,6 @@ import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.TestSearchContext; import org.elasticsearch.transport.RemoteTransportException; -import org.hamcrest.Matcher; import java.io.EOFException; import java.io.FileNotFoundException; @@ -427,6 +426,7 @@ public class ElasticsearchExceptionTests extends ESTestCase { .field("stack_trace", "...") .endObject(); + builder = shuffleXContent(builder); ElasticsearchException parsed; try (XContentParser parser = createParser(xContent, builder.bytes())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -447,6 +447,7 @@ public class ElasticsearchExceptionTests extends ESTestCase { final XContent xContent = randomFrom(XContentType.values()).xContent(); XContentBuilder builder = XContentBuilder.builder(xContent).startObject().value(e).endObject(); + builder = shuffleXContent(builder); ElasticsearchException parsed; try (XContentParser parser = createParser(builder)) { @@ -491,6 +492,7 @@ public class ElasticsearchExceptionTests extends ESTestCase { final XContent xContent = randomFrom(XContentType.values()).xContent(); XContentBuilder builder = XContentBuilder.builder(xContent).startObject().value(foo).endObject(); + builder = shuffleXContent(builder); ElasticsearchException parsed; try (XContentParser parser = createParser(builder)) { @@ -570,8 +572,9 @@ public class ElasticsearchExceptionTests extends ESTestCase { .field("object_field", "value") .endObject() .endObject(); - - originalBytes = builder.bytes(); + try (XContentBuilder shuffledBuilder = shuffleXContent(builder)) { + originalBytes = shuffledBuilder.bytes(); + } } ElasticsearchException parsedException; @@ -597,10 +600,10 @@ public class ElasticsearchExceptionTests extends ESTestCase { final Tuple<Throwable, ElasticsearchException> exceptions = randomExceptions(); final Throwable throwable = exceptions.v1(); - BytesReference throwableBytes = XContentHelper.toXContent((builder, params) -> { + BytesReference throwableBytes = toShuffledXContent((builder, params) -> { ElasticsearchException.generateThrowableXContent(builder, params, throwable); return builder; - }, xContent.type(), randomBoolean()); + }, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean()); ElasticsearchException parsedException; try (XContentParser parser = createParser(xContent, throwableBytes)) { @@ -615,11 +618,11 @@ public class ElasticsearchExceptionTests extends ESTestCase { public void testUnknownFailureToAndFromXContent() throws IOException { final XContent xContent = randomFrom(XContentType.values()).xContent(); - BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> { + BytesReference failureBytes = toShuffledXContent((builder, params) -> { // Prints a null failure using generateFailureXContent() ElasticsearchException.generateFailureXContent(builder, params, null, randomBoolean()); return builder; - }, xContent.type(), randomBoolean()); + }, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean()); ElasticsearchException parsedFailure; try (XContentParser parser = createParser(xContent, failureBytes)) { @@ -640,10 +643,10 @@ public class ElasticsearchExceptionTests extends ESTestCase { final XContent xContent = randomFrom(XContentType.values()).xContent(); final Exception failure = (Exception) randomExceptions().v1(); - BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> { + BytesReference failureBytes = toShuffledXContent((builder, params) -> { ElasticsearchException.generateFailureXContent(builder, params, failure, false); return builder; - }, xContent.type(), randomBoolean()); + }, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean()); try (XContentParser parser = createParser(xContent, failureBytes)) { failureBytes = shuffleXContent(parser, randomBoolean()).bytes(); @@ -785,10 +788,10 @@ public class ElasticsearchExceptionTests extends ESTestCase { } Exception finalFailure = failure; - BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> { + BytesReference failureBytes = toShuffledXContent((builder, params) -> { ElasticsearchException.generateFailureXContent(builder, params, finalFailure, true); return builder; - }, xContent.type(), randomBoolean()); + }, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean()); try (XContentParser parser = createParser(xContent, failureBytes)) { failureBytes = shuffleXContent(parser, randomBoolean()).bytes(); @@ -807,7 +810,7 @@ public class ElasticsearchExceptionTests extends ESTestCase { } /** - * Builds a {@link ToXContent} using a JSON XContentBuilder and check the resulting string with the given {@link Matcher}. + * Builds a {@link ToXContent} using a JSON XContentBuilder and compares the result to the given json in string format. * * By default, the stack trace of the exception is not rendered. The parameter `errorTrace` forces the stack trace to * be rendered like the REST API does when the "error_trace" parameter is set to true. diff --git a/core/src/test/java/org/elasticsearch/action/bulk/BulkItemResponseTests.java b/core/src/test/java/org/elasticsearch/action/bulk/BulkItemResponseTests.java index f2d51539c6..24f3afdd79 100644 --- a/core/src/test/java/org/elasticsearch/action/bulk/BulkItemResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/bulk/BulkItemResponseTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.action.index.IndexResponseTests; import org.elasticsearch.action.update.UpdateResponseTests; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; @@ -37,7 +38,6 @@ import java.io.IOException; import static org.elasticsearch.ElasticsearchExceptionTests.assertDeepEquals; import static org.elasticsearch.ElasticsearchExceptionTests.randomExceptions; -import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; import static org.hamcrest.Matchers.containsString; public class BulkItemResponseTests extends ESTestCase { @@ -70,14 +70,7 @@ public class BulkItemResponseTests extends ESTestCase { BulkItemResponse bulkItemResponse = new BulkItemResponse(bulkItemId, opType, randomDocWriteResponses.v1()); BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(bulkItemId, opType, randomDocWriteResponses.v2()); - BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, humanReadable); - - // Shuffle the XContent fields - if (randomBoolean()) { - try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { - originalBytes = shuffleXContent(parser, randomBoolean()).bytes(); - } - } + BytesReference originalBytes = toShuffledXContent(bulkItemResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); BulkItemResponse parsedBulkItemResponse; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { @@ -105,7 +98,7 @@ public class BulkItemResponseTests extends ESTestCase { BulkItemResponse bulkItemResponse = new BulkItemResponse(itemId, opType, bulkItemFailure); Failure expectedBulkItemFailure = new Failure(index, type, id, exceptions.v2(), ExceptionsHelper.status(bulkItemCause)); BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(itemId, opType, expectedBulkItemFailure); - BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, randomBoolean()); + BytesReference originalBytes = toShuffledXContent(bulkItemResponse, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); // Shuffle the XContent fields if (randomBoolean()) { diff --git a/core/src/test/java/org/elasticsearch/action/bulk/BulkResponseTests.java b/core/src/test/java/org/elasticsearch/action/bulk/BulkResponseTests.java index 08622b327e..dd8f92bb2e 100644 --- a/core/src/test/java/org/elasticsearch/action/bulk/BulkResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/bulk/BulkResponseTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.action.index.IndexResponseTests; import org.elasticsearch.action.update.UpdateResponseTests; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; @@ -86,13 +87,7 @@ public class BulkResponseTests extends ESTestCase { } BulkResponse bulkResponse = new BulkResponse(bulkItems, took, ingestTook); - BytesReference originalBytes = toXContent(bulkResponse, xContentType, humanReadable); - - if (randomBoolean()) { - try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { - originalBytes = shuffleXContent(parser, randomBoolean()).bytes(); - } - } + BytesReference originalBytes = toShuffledXContent(bulkResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); BulkResponse parsedBulkResponse; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java b/core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java index cbf514375c..2b13b2b8b4 100644 --- a/core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.action.support.replication.ReplicationResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.seqno.SequenceNumbersService; @@ -34,7 +35,6 @@ import java.io.IOException; import static org.elasticsearch.action.index.IndexResponseTests.assertDocWriteResponse; import static org.elasticsearch.cluster.metadata.IndexMetaData.INDEX_UUID_NA_VALUE; -import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; public class DeleteResponseTests extends ESTestCase { @@ -62,16 +62,8 @@ public class DeleteResponseTests extends ESTestCase { boolean humanReadable = randomBoolean(); final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference deleteResponseBytes = toXContent(deleteResponse, xContentType, humanReadable); + BytesReference deleteResponseBytes = toShuffledXContent(deleteResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - // Shuffle the XContent fields - if (randomBoolean()) { - try (XContentParser parser = createParser(xContentType.xContent(), deleteResponseBytes)) { - deleteResponseBytes = shuffleXContent(parser, randomBoolean()).bytes(); - } - } - - // Parse the XContent bytes to obtain a parsed DeleteResponse DeleteResponse parsedDeleteResponse; try (XContentParser parser = createParser(xContentType.xContent(), deleteResponseBytes)) { parsedDeleteResponse = DeleteResponse.fromXContent(parser); diff --git a/core/src/test/java/org/elasticsearch/action/get/GetResponseTests.java b/core/src/test/java/org/elasticsearch/action/get/GetResponseTests.java index c81250ea8c..f755d05fc7 100644 --- a/core/src/test/java/org/elasticsearch/action/get/GetResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/get/GetResponseTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.get.GetField; @@ -46,7 +47,7 @@ public class GetResponseTests extends ESTestCase { GetResponse getResponse = new GetResponse(tuple.v1()); GetResponse expectedGetResponse = new GetResponse(tuple.v2()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(getResponse, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(getResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable, "_source"); //test that we can parse what we print out GetResponse parsedGetResponse; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java b/core/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java index 4afa0f7298..c222d2d896 100644 --- a/core/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.action.support.replication.ReplicationResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.seqno.SequenceNumbersService; @@ -35,7 +36,6 @@ import java.io.IOException; import static org.elasticsearch.action.support.replication.ReplicationResponseTests.assertShardInfo; import static org.elasticsearch.cluster.metadata.IndexMetaData.INDEX_UUID_NA_VALUE; -import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; public class IndexResponseTests extends ESTestCase { @@ -63,16 +63,8 @@ public class IndexResponseTests extends ESTestCase { boolean humanReadable = randomBoolean(); XContentType xContentType = randomFrom(XContentType.values()); - BytesReference indexResponseBytes = toXContent(indexResponse, xContentType, humanReadable); + BytesReference indexResponseBytes = toShuffledXContent(indexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - // Shuffle the XContent fields - if (randomBoolean()) { - try (XContentParser parser = createParser(xContentType.xContent(), indexResponseBytes)) { - indexResponseBytes = shuffleXContent(parser, randomBoolean()).bytes(); - } - } - - // Parse the XContent bytes to obtain a parsed IndexResponse parsedIndexResponse; try (XContentParser parser = createParser(xContentType.xContent(), indexResponseBytes)) { parsedIndexResponse = IndexResponse.fromXContent(parser); diff --git a/core/src/test/java/org/elasticsearch/action/main/MainResponseTests.java b/core/src/test/java/org/elasticsearch/action/main/MainResponseTests.java index 429fe5cac7..c30e14c302 100644 --- a/core/src/test/java/org/elasticsearch/action/main/MainResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/main/MainResponseTests.java @@ -54,7 +54,7 @@ public class MainResponseTests extends ESTestCase { MainResponse mainResponse = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(mainResponse, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(mainResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); MainResponse parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { parsed = MainResponse.fromXContent(parser); diff --git a/core/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java b/core/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java index 1c523d6503..31bcb1e9ed 100644 --- a/core/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java +++ b/core/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java @@ -27,7 +27,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.Index; @@ -157,7 +156,7 @@ public class SearchPhaseExecutionExceptionTests extends ESTestCase { final String phase = randomFrom("query", "search", "other"); SearchPhaseExecutionException actual = new SearchPhaseExecutionException(phase, "unexpected failures", shardSearchFailures); - BytesReference exceptionBytes = XContentHelper.toXContent(actual, xContent.type(), randomBoolean()); + BytesReference exceptionBytes = toShuffledXContent(actual, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean()); ElasticsearchException parsedException; try (XContentParser parser = createParser(xContent, exceptionBytes)) { diff --git a/core/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java b/core/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java index 58f77db339..fd61bbc6d7 100644 --- a/core/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.breaker.CircuitBreakingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.shard.ShardId; @@ -76,7 +77,7 @@ public class ReplicationResponseTests extends ESTestCase { final XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(shardInfo, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(shardInfo, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); // Shuffle the XContent fields if (randomBoolean()) { diff --git a/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java b/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java index 4958e1c78a..d6a087bec4 100644 --- a/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java +++ b/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; @@ -459,7 +460,7 @@ public class UpdateRequestTests extends ESTestCase { XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = XContentHelper.toXContent(updateRequest, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(updateRequest, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); if (randomBoolean()) { try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java b/core/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java index bd9e355182..153c27b370 100644 --- a/core/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.get.GetField; @@ -44,7 +45,6 @@ import static org.elasticsearch.action.DocWriteResponse.Result.DELETED; import static org.elasticsearch.action.DocWriteResponse.Result.NOT_FOUND; import static org.elasticsearch.action.DocWriteResponse.Result.UPDATED; import static org.elasticsearch.cluster.metadata.IndexMetaData.INDEX_UUID_NA_VALUE; -import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; public class UpdateResponseTests extends ESTestCase { @@ -87,16 +87,8 @@ public class UpdateResponseTests extends ESTestCase { UpdateResponse expectedUpdateResponse = tuple.v2(); boolean humanReadable = randomBoolean(); - BytesReference updateResponseBytes = toXContent(updateResponse, xContentType, humanReadable); + BytesReference updateResponseBytes = toShuffledXContent(updateResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - // Shuffle the XContent fields - if (randomBoolean()) { - try (XContentParser parser = createParser(xContentType.xContent(), updateResponseBytes)) { - updateResponseBytes = shuffleXContent(parser, randomBoolean()).bytes(); - } - } - - // Parse the XContent bytes to obtain a parsed UpdateResponse UpdateResponse parsedUpdateResponse; try (XContentParser parser = createParser(xContentType.xContent(), updateResponseBytes)) { parsedUpdateResponse = UpdateResponse.fromXContent(parser); diff --git a/core/src/test/java/org/elasticsearch/index/get/GetFieldTests.java b/core/src/test/java/org/elasticsearch/index/get/GetFieldTests.java index d2968b6d8a..62cd45508d 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetFieldTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetFieldTests.java @@ -22,6 +22,7 @@ package org.elasticsearch.index.get; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.ParentFieldMapper; @@ -58,7 +59,7 @@ public class GetFieldTests extends ESTestCase { GetField getField = tuple.v1(); GetField expectedGetField = tuple.v2(); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(getField, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(getField, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); //test that we can parse what we print out GetField parsedGetField; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java index 93ba260ee4..c23a648aff 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; @@ -53,7 +54,7 @@ public class GetResultTests extends ESTestCase { GetResult getResult = tuple.v1(); GetResult expectedGetResult = tuple.v2(); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(getResult, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(getResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable, "_source"); //test that we can parse what we print out GetResult parsedGetResult; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/search/SearchHitTests.java b/core/src/test/java/org/elasticsearch/search/SearchHitTests.java index 84d40f0b9b..51fffc3e95 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchHitTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchHitTests.java @@ -137,7 +137,7 @@ public class SearchHitTests extends ESTestCase { SearchHit searchHit = createTestItem(true); boolean humanReadable = randomBoolean(); XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toXContent(searchHit, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(searchHit, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); SearchHit parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/search/SearchHitsTests.java b/core/src/test/java/org/elasticsearch/search/SearchHitsTests.java index 9cb808a5d0..4c41e6fbcd 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchHitsTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchHitsTests.java @@ -51,7 +51,7 @@ public class SearchHitsTests extends ESTestCase { SearchHits searchHits = createTestItem(); XContentType xcontentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(searchHits, xcontentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(searchHits, xcontentType, ToXContent.EMPTY_PARAMS, humanReadable); SearchHits parsed; try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) { parsed = SearchHits.fromXContent(parser); diff --git a/core/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java b/core/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java index 654b3c9861..316ef3d455 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java @@ -67,7 +67,7 @@ public class SearchSortValuesTests extends ESTestCase { SearchSortValues sortValues = createTestItem(); XContentType xcontentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(sortValues, xcontentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(sortValues, xcontentType, ToXContent.EMPTY_PARAMS, humanReadable); SearchSortValues parsed; try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) { 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 70375c6d20..77b41b062d 100644 --- a/core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java +++ b/core/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java @@ -65,7 +65,7 @@ public class ProfileResultTests extends ESTestCase { ProfileResult profileResult = createTestItem(2); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(profileResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); ProfileResult 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/profile/SearchProfileShardResultsTests.java b/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java index 82ed118f55..ea851427c0 100644 --- a/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java +++ b/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.profile; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.profile.aggregation.AggregationProfileShardResult; @@ -60,7 +61,7 @@ public class SearchProfileShardResultsTests extends ESTestCase { SearchProfileShardResults shardResult = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(shardResult, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(shardResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); SearchProfileShardResults parsed = null; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation); diff --git a/core/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java b/core/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java index ca5f792e72..a21eeedb96 100644 --- a/core/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java +++ b/core/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.profile.aggregation; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.common.xcontent.XContentType; @@ -52,7 +53,7 @@ public class AggregationProfileShardResultTests extends ESTestCase { AggregationProfileShardResult profileResult = createTestItem(2); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(profileResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); AggregationProfileShardResult parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { 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 698e4a387a..8d87f19360 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 @@ -60,7 +60,7 @@ public class CollectorResultTests extends ESTestCase { CollectorResult collectorResult = createTestItem(1); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(collectorResult, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(collectorResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); CollectorResult parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { diff --git a/core/src/test/java/org/elasticsearch/search/profile/query/QueryProfileShardResultTests.java b/core/src/test/java/org/elasticsearch/search/profile/query/QueryProfileShardResultTests.java index 1542ce2eca..6ffece354f 100644 --- a/core/src/test/java/org/elasticsearch/search/profile/query/QueryProfileShardResultTests.java +++ b/core/src/test/java/org/elasticsearch/search/profile/query/QueryProfileShardResultTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.profile.query; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.common.xcontent.XContentType; @@ -54,7 +55,7 @@ public class QueryProfileShardResultTests extends ESTestCase { QueryProfileShardResult profileResult = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(profileResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); QueryProfileShardResult parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { 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 9aa4d9dc2b..18a24e0011 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.suggest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.SearchHit; @@ -69,7 +70,7 @@ public class CompletionSuggestionOptionTests extends ESTestCase { Option option = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(option, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(option, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); if (randomBoolean()) { try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { originalBytes = shuffleXContent(parser, randomBoolean()).bytes(); 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 ddb4904827..770fd2f6e6 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.suggest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry; @@ -85,7 +86,7 @@ public class SuggestionEntryTests extends ESTestCase { Entry<Option> entry = createTestItem(entryType); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(entry, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(entry, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); Entry<Option> 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/SuggestionOptionTests.java b/core/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java index 268f7429d7..32a3d92584 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.suggest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option; @@ -46,7 +47,7 @@ public class SuggestionOptionTests extends ESTestCase { Option option = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(option, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(option, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); Option 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/TermSuggestionOptionTests.java b/core/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java index 668629fe57..75ced3cb80 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.suggest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option; @@ -45,7 +46,7 @@ public class TermSuggestionOptionTests extends ESTestCase { Option option = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); boolean humanReadable = randomBoolean(); - BytesReference originalBytes = toXContent(option, xContentType, humanReadable); + BytesReference originalBytes = toShuffledXContent(option, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); Option parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); |