diff --git a/core/src/main/java/org/elasticsearch/action/delete/DeleteResponse.java b/core/src/main/java/org/elasticsearch/action/delete/DeleteResponse.java index b5a4d74d620ea..9ab06a941eb20 100644 --- a/core/src/main/java/org/elasticsearch/action/delete/DeleteResponse.java +++ b/core/src/main/java/org/elasticsearch/action/delete/DeleteResponse.java @@ -20,7 +20,6 @@ package org.elasticsearch.action.delete; import org.elasticsearch.action.DocWriteResponse; -import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ConstructingObjectParser; @@ -85,7 +84,7 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t PARSER.declareBoolean(constructorArg(), new ParseField(FOUND)); } - public static DeleteResponse fromXContent(XContentParser parser) throws IOException { + public static DeleteResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } 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 030dac8eb24e9..6cf8d12c21c8d 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -376,10 +376,6 @@ public static void writeRawField(String field, BytesReference source, XContentBu } } - public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType) throws IOException { - return toXContent(toXContent, xContentType, false); - } - /** * 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. 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 93d9589b61392..c78fec37f8837 100644 --- a/core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.seqno.SequenceNumbersService; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.RandomObjects; @@ -37,7 +38,7 @@ public class DeleteResponseTests extends ESTestCase { - public void testToXContent() throws IOException { + public void testToXContent() { { DeleteResponse response = new DeleteResponse(new ShardId("index", "index_uuid", 0), "type", "id", 3, 5, true); String output = Strings.toString(response); @@ -59,7 +60,8 @@ public void testToAndFromXContent() throws IOException { // Create a random DeleteResponse and converts it to XContent in bytes DeleteResponse deleteResponse = randomDeleteResponse(); - BytesReference deleteResponseBytes = toXContent(deleteResponse, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference deleteResponseBytes = toXContent(deleteResponse, xContentType, humanReadable); // Parse the XContent bytes to obtain a parsed DeleteResponse parsedDeleteResponse; @@ -73,7 +75,7 @@ public void testToAndFromXContent() throws IOException { // and those exceptions are not parsed back with the same types. // Print the parsed object out and test that the output is the same as the original output - BytesReference parsedDeleteResponseBytes = toXContent(parsedDeleteResponse, xContentType); + BytesReference parsedDeleteResponseBytes = toXContent(parsedDeleteResponse, xContentType, humanReadable); try (XContentParser parser = createParser(xContentType.xContent(), parsedDeleteResponseBytes)) { assertDeleteResponse(deleteResponse, parser.map()); } @@ -92,8 +94,8 @@ private static DeleteResponse randomDeleteResponse() { ShardId shardId = new ShardId(randomAsciiOfLength(5), randomAsciiOfLength(5), randomIntBetween(0, 5)); String type = randomAsciiOfLength(5); String id = randomAsciiOfLength(5); - long seqNo = randomIntBetween(-2, 5); - long version = (long) randomIntBetween(0, 5); + long seqNo = randomFrom(SequenceNumbersService.UNASSIGNED_SEQ_NO, randomNonNegativeLong(), (long) randomIntBetween(0, 10000)); + long version = randomBoolean() ? randomNonNegativeLong() : randomIntBetween(0, 10000); boolean found = randomBoolean(); DeleteResponse response = new DeleteResponse(shardId, type, id, seqNo, version, found); 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 05d51875cf407..37824ef2b0315 100644 --- a/core/src/test/java/org/elasticsearch/action/get/GetResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/get/GetResponseTests.java @@ -46,7 +46,8 @@ public void testToAndFromXContent() throws Exception { Tuple tuple = randomGetResult(xContentType); GetResponse getResponse = new GetResponse(tuple.v1()); GetResponse expectedGetResponse = new GetResponse(tuple.v2()); - BytesReference originalBytes = toXContent(getResponse, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(getResponse, xContentType, humanReadable); //test that we can parse what we print out GetResponse parsedGetResponse; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { @@ -55,7 +56,7 @@ public void testToAndFromXContent() throws Exception { } assertEquals(expectedGetResponse, parsedGetResponse); //print the parsed object out and test that the output is the same as the original output - BytesReference finalBytes = toXContent(parsedGetResponse, xContentType); + BytesReference finalBytes = toXContent(parsedGetResponse, xContentType, humanReadable); assertToXContentEquivalent(originalBytes, finalBytes, xContentType); //check that the source stays unchanged, no shuffling of keys nor anything like that assertEquals(expectedGetResponse.getSourceAsString(), parsedGetResponse.getSourceAsString()); 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 8be880009fa0f..696a836a37ad8 100644 --- a/core/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.seqno.SequenceNumbersService; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; @@ -40,7 +41,7 @@ public class IndexResponseTests extends ESTestCase { - public void testToXContent() throws IOException { + public void testToXContent() { { IndexResponse indexResponse = new IndexResponse(new ShardId("index", "index_uuid", 0), "type", "id", 3, 5, true); String output = Strings.toString(indexResponse); @@ -62,7 +63,8 @@ public void testToAndFromXContent() throws IOException { // Create a random IndexResponse and converts it to XContent in bytes IndexResponse indexResponse = randomIndexResponse(); - BytesReference indexResponseBytes = toXContent(indexResponse, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference indexResponseBytes = toXContent(indexResponse, xContentType, humanReadable); // Parse the XContent bytes to obtain a parsed IndexResponse parsedIndexResponse; @@ -76,12 +78,13 @@ public void testToAndFromXContent() throws IOException { // and those exceptions are not parsed back with the same types. // Print the parsed object out and test that the output is the same as the original output - BytesReference parsedIndexResponseBytes = toXContent(parsedIndexResponse, xContentType); + BytesReference parsedIndexResponseBytes = toXContent(parsedIndexResponse, xContentType, humanReadable); try (XContentParser parser = createParser(xContentType.xContent(), parsedIndexResponseBytes)) { assertIndexResponse(indexResponse, parser.map()); } } + @SuppressWarnings("unchecked") public static void assertDocWriteResponse(DocWriteResponse expected, Map actual) { assertEquals(expected.getIndex(), actual.get("_index")); assertEquals(expected.getType(), actual.get("_type")); @@ -165,8 +168,8 @@ private static IndexResponse randomIndexResponse() { ShardId shardId = new ShardId(randomAsciiOfLength(5), randomAsciiOfLength(5), randomIntBetween(0, 5)); String type = randomAsciiOfLength(5); String id = randomAsciiOfLength(5); - long seqNo = randomIntBetween(-2, 5); - long version = (long) randomIntBetween(0, 5); + long seqNo = randomFrom(SequenceNumbersService.UNASSIGNED_SEQ_NO, randomNonNegativeLong(), (long) randomIntBetween(0, 10000)); + long version = randomBoolean() ? randomNonNegativeLong() : randomIntBetween(0, 10000); boolean created = randomBoolean(); IndexResponse indexResponse = new IndexResponse(shardId, type, id, seqNo, version, created); diff --git a/core/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java b/core/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java index d73046934d7e1..94a7e1d44bb81 100644 --- a/core/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java +++ b/core/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java @@ -74,7 +74,7 @@ public void testFromXContent() throws IOException { public void testToXContent() throws IOException { ShardSearchFailure failure = new ShardSearchFailure(new ParsingException(0, 0, "some message", null), new SearchShardTarget("nodeId", new ShardId(new Index("indexName", "indexUuid"), 123))); - BytesReference xContent = toXContent(failure, XContentType.JSON); + BytesReference xContent = toXContent(failure, XContentType.JSON, randomBoolean()); assertEquals( "{\"shard\":123," + "\"index\":\"indexName\"," 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 0972a91c8ec1e..3ff113fc0fb1d 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 @@ -136,7 +136,7 @@ public void testShardInfoToXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final ReplicationResponse.ShardInfo shardInfo = new ReplicationResponse.ShardInfo(5, 3); - final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType); + final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType, randomBoolean()); // Expected JSON is {"total":5,"successful":3,"failed":0} assertThat(shardInfo, instanceOf(ToXContentObject.class)); @@ -163,7 +163,8 @@ public void testShardInfoToAndFromXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final ReplicationResponse.ShardInfo shardInfo = new ReplicationResponse.ShardInfo(randomIntBetween(1, 5), randomIntBetween(1, 5)); - final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType); + boolean humanReadable = randomBoolean(); + final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType, humanReadable); ReplicationResponse.ShardInfo parsedShardInfo; try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) { @@ -175,7 +176,7 @@ public void testShardInfoToAndFromXContent() throws IOException { // We can use assertEquals because the shardInfo doesn't have a failure (and exceptions) assertEquals(shardInfo, parsedShardInfo); - BytesReference parsedShardInfoBytes = XContentHelper.toXContent(parsedShardInfo, xContentType); + BytesReference parsedShardInfoBytes = XContentHelper.toXContent(parsedShardInfo, xContentType, humanReadable); assertEquals(shardInfoBytes, parsedShardInfoBytes); } @@ -183,7 +184,7 @@ public void testShardInfoWithFailureToXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final ReplicationResponse.ShardInfo shardInfo = RandomObjects.randomShardInfo(random(), true); - final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType); + final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType, randomBoolean()); try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -220,7 +221,7 @@ public void testRandomShardInfoFromXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final ReplicationResponse.ShardInfo shardInfo = RandomObjects.randomShardInfo(random(), randomBoolean()); - final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType); + final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType, randomBoolean()); ReplicationResponse.ShardInfo parsedShardInfo; try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) { @@ -260,7 +261,7 @@ public void testRandomFailureToXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final ReplicationResponse.ShardInfo.Failure shardInfoFailure = RandomObjects.randomShardInfoFailure(random()); - final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfoFailure, xContentType); + final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfoFailure, xContentType, randomBoolean()); try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) { assertFailure(parser, shardInfoFailure); @@ -271,7 +272,7 @@ public void testRandomFailureToAndFromXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final ReplicationResponse.ShardInfo.Failure shardInfoFailure = RandomObjects.randomShardInfoFailure(random());; - final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfoFailure, xContentType); + final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfoFailure, xContentType, randomBoolean()); ReplicationResponse.ShardInfo.Failure parsedFailure; try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) { 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 487686c84cdf2..35d9eb8edf18b 100644 --- a/core/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java +++ b/core/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java @@ -82,16 +82,17 @@ public void testToXContent() throws IOException { public void testToAndFromXContent() throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final Tuple tuple = randomUpdateResponse(xContentType); + boolean humanReadable = randomBoolean(); // Parse the XContent bytes to obtain a parsed UpdateResponse UpdateResponse parsedUpdateResponse; - try (XContentParser parser = createParser(xContentType.xContent(), toXContent(tuple.v1(), xContentType))) { + try (XContentParser parser = createParser(xContentType.xContent(), toXContent(tuple.v1(), xContentType, humanReadable))) { parsedUpdateResponse = UpdateResponse.fromXContent(parser); assertNull(parser.nextToken()); } final UpdateResponse expectedUpdateResponse = tuple.v2(); - try (XContentParser parser = createParser(xContentType.xContent(), toXContent(parsedUpdateResponse, xContentType))) { + try (XContentParser parser = createParser(xContentType.xContent(), toXContent(parsedUpdateResponse, xContentType, humanReadable))) { IndexResponseTests.assertDocWriteResponse(expectedUpdateResponse, parser.map()); } assertEquals(expectedUpdateResponse.getGetResult(), parsedUpdateResponse.getGetResult()); diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java index 0ae86f7dc55f2..219bd12daeaea 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java @@ -90,9 +90,9 @@ public void testToXContent() throws IOException { } } if (error) { - expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType)); + expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType, randomBoolean())); } else { - BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType); + BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean()); try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes)) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); 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 6ce3d80276520..3fd6ca87e9bf3 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetFieldTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetFieldTests.java @@ -57,7 +57,8 @@ public void testToAndFromXContent() throws Exception { Tuple tuple = randomGetField(xContentType); GetField getField = tuple.v1(); GetField expectedGetField = tuple.v2(); - BytesReference originalBytes = toXContent(getField, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(getField, xContentType, humanReadable); //test that we can parse what we print out GetField parsedGetField; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { @@ -70,7 +71,7 @@ public void testToAndFromXContent() throws Exception { assertNull(parser.nextToken()); } assertEquals(expectedGetField, parsedGetField); - BytesReference finalBytes = toXContent(parsedGetField, xContentType); + BytesReference finalBytes = toXContent(parsedGetField, xContentType, humanReadable); assertToXContentEquivalent(originalBytes, finalBytes, xContentType); } 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 106e063f38cf7..4e5b94d9c9877 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -52,7 +52,8 @@ public void testToAndFromXContent() throws Exception { Tuple tuple = randomGetResult(xContentType); GetResult getResult = tuple.v1(); GetResult expectedGetResult = tuple.v2(); - BytesReference originalBytes = toXContent(getResult, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(getResult, xContentType, humanReadable); //test that we can parse what we print out GetResult parsedGetResult; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { @@ -61,7 +62,7 @@ public void testToAndFromXContent() throws Exception { } assertEquals(expectedGetResult, parsedGetResult); //print the parsed object out and test that the output is the same as the original output - BytesReference finalBytes = toXContent(parsedGetResult, xContentType); + BytesReference finalBytes = toXContent(parsedGetResult, xContentType, humanReadable); assertToXContentEquivalent(originalBytes, finalBytes, xContentType); //check that the source stays unchanged, no shuffling of keys nor anything like that assertEquals(expectedGetResult.sourceAsString(), parsedGetResult.sourceAsString()); @@ -93,7 +94,8 @@ public void testToAndFromXContentEmbedded() throws Exception { GetResult expectedGetResult = new GetResult(null, null, null, -1, tuple.v2().isExists(), tuple.v2().sourceRef(), tuple.v2().getFields()); - BytesReference originalBytes = toXContentEmbedded(getResult, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContentEmbedded(getResult, xContentType, humanReadable); // Test that we can parse the result of toXContentEmbedded() GetResult parsedEmbeddedGetResult; @@ -105,7 +107,7 @@ public void testToAndFromXContentEmbedded() throws Exception { assertEquals(expectedGetResult, parsedEmbeddedGetResult); //print the parsed object out and test that the output is the same as the original output - BytesReference finalBytes = toXContentEmbedded(parsedEmbeddedGetResult, xContentType); + BytesReference finalBytes = toXContentEmbedded(parsedEmbeddedGetResult, xContentType, humanReadable); assertToXContentEquivalent(originalBytes, finalBytes, xContentType); //check that the source stays unchanged, no shuffling of keys nor anything like that assertEquals(expectedGetResult.sourceAsString(), parsedEmbeddedGetResult.sourceAsString()); @@ -119,7 +121,7 @@ public void testToXContentEmbedded() throws IOException { GetResult getResult = new GetResult("index", "type", "id", 2, true, new BytesArray("{\"foo\":\"bar\",\"baz\":[\"baz_0\",\"baz_1\"]}"), fields); - BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON); + BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON, false); assertEquals("{\"found\":true,\"_source\":{\"foo\":\"bar\",\"baz\":[\"baz_0\",\"baz_1\"]}," + "\"fields\":{\"foo\":[\"bar\"],\"baz\":[\"baz_0\",\"baz_1\"]}}", originalBytes.utf8ToString()); } @@ -127,7 +129,7 @@ public void testToXContentEmbedded() throws IOException { public void testToXContentEmbeddedNotFound() throws IOException { GetResult getResult = new GetResult("index", "type", "id", 1, false, null, null); - BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON); + BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON, false); assertEquals("{\"found\":false}", originalBytes.utf8ToString()); } @@ -213,7 +215,8 @@ private static Tuple,Map> randomGetField return Tuple.tuple(fields, expectedFields); } - private static BytesReference toXContentEmbedded(GetResult getResult, XContentType xContentType) throws IOException { - return XContentHelper.toXContent(getResult::toXContentEmbedded, xContentType); + private static BytesReference toXContentEmbedded(GetResult getResult, XContentType xContentType, boolean humanReadable) + throws IOException { + return XContentHelper.toXContent(getResult::toXContentEmbedded, xContentType, humanReadable); } } diff --git a/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitTests.java b/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitTests.java index f42b79670c128..ea2fb1d0db311 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitTests.java @@ -21,6 +21,7 @@ import org.apache.lucene.search.Explanation; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.InputStreamStreamInput; @@ -28,7 +29,6 @@ import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -137,16 +137,18 @@ public static InternalSearchHit createTestItem(boolean withOptionalInnerHits) { public void testFromXContent() throws IOException { InternalSearchHit searchHit = createTestItem(true); - XContentType xcontentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(xcontentType); - builder = searchHit.toXContent(builder, ToXContent.EMPTY_PARAMS); - - XContentParser parser = createParser(builder); - parser.nextToken(); // jump to first START_OBJECT - InternalSearchHit parsed = InternalSearchHit.fromXContent(parser); - assertToXContentEquivalent(builder.bytes(), toXContent(parsed, xcontentType), xcontentType); - assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); - assertNull(parser.nextToken()); + boolean humanReadable = randomBoolean(); + XContentType xContentType = randomFrom(XContentType.values()); + BytesReference originalBytes = toXContent(searchHit, xContentType, humanReadable); + + InternalSearchHit parsed; + try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { + parser.nextToken(); // jump to first START_OBJECT + parsed = InternalSearchHit.fromXContent(parser); + assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); + assertNull(parser.nextToken()); + } + assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType); } public void testToXContent() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitsTests.java b/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitsTests.java index 5301c566d1447..2474893edf506 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitsTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/InternalSearchHitsTests.java @@ -19,10 +19,10 @@ package org.elasticsearch.search.internal; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -49,19 +49,17 @@ public static InternalSearchHits createTestItem() { public void testFromXContent() throws IOException { InternalSearchHits searchHits = createTestItem(); - XContentType xcontentType = XContentType.JSON; //randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(xcontentType); - builder.startObject(); - builder = searchHits.toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.endObject(); - - XContentParser parser = createParser(builder); - InternalSearchHits parsed = InternalSearchHits.fromXContent(parser); - assertToXContentEquivalent(builder.bytes(), toXContent(parsed, xcontentType), xcontentType); - assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); - parser.nextToken(); - assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); - assertNull(parser.nextToken()); + XContentType xcontentType = randomFrom(XContentType.values()); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(searchHits, xcontentType, humanReadable); + InternalSearchHits parsed; + try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) { + parsed = InternalSearchHits.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); } public void testToXContent() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/search/internal/SearchSortValuesTests.java b/core/src/test/java/org/elasticsearch/search/internal/SearchSortValuesTests.java index ddff230b4bc40..3cff30cab1cc4 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/SearchSortValuesTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/SearchSortValuesTests.java @@ -19,11 +19,11 @@ package org.elasticsearch.search.internal; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -66,25 +66,20 @@ public static SearchSortValues createTestItem() { public void testFromXContent() throws IOException { SearchSortValues sortValues = createTestItem(); XContentType xcontentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(xcontentType); - if (randomBoolean()) { - builder.prettyPrint(); - } - builder.startObject(); // we need to wrap xContent output in proper object to create a parser for it - builder = sortValues.toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.endObject(); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(sortValues, xcontentType, humanReadable); - XContentParser parser = createParser(builder); - parser.nextToken(); // skip to the elements start array token, fromXContent advances from there if called - parser.nextToken(); - parser.nextToken(); - if (sortValues.sortValues().length > 0) { - SearchSortValues parsed = SearchSortValues.fromXContent(parser); - assertToXContentEquivalent(builder.bytes(), toXContent(parsed, xcontentType), xcontentType); + SearchSortValues parsed; + try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) { + parser.nextToken(); // skip to the elements start array token, fromXContent advances from there if called + parser.nextToken(); + parser.nextToken(); + parsed = SearchSortValues.fromXContent(parser); parser.nextToken(); + assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); + assertNull(parser.nextToken()); } - assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); - assertNull(parser.nextToken()); + assertToXContentEquivalent(originalBytes, toXContent(parsed, xcontentType, humanReadable), xcontentType); } public void testToXContent() throws IOException { 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 2f4bf50daab72..2d9cf847861ac 100644 --- a/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java +++ b/core/src/test/java/org/elasticsearch/search/profile/SearchProfileShardResultsTests.java @@ -59,16 +59,18 @@ public static SearchProfileShardResults createTestItem() { public void testFromXContent() throws IOException { SearchProfileShardResults shardResult = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toXContent(shardResult, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(shardResult, xContentType, humanReadable); + SearchProfileShardResults parsed = null; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { 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); - SearchProfileShardResults parsed = SearchProfileShardResults.fromXContent(parser); - assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType), xContentType); + parsed = SearchProfileShardResults.fromXContent(parser); 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/aggregation/AggregationProfileShardResultTests.java b/core/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java index 5d1b7f973c953..ca5f792e7291e 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 @@ -74,7 +74,7 @@ public void testToXContent() throws IOException { ProfileResult profileResult = new ProfileResult("someType", "someDescription", timings, Collections.emptyList()); profileResults.add(profileResult); AggregationProfileShardResult aggProfileResults = new AggregationProfileShardResult(profileResults); - BytesReference xContent = toXContent(aggProfileResults, XContentType.JSON); + BytesReference xContent = toXContent(aggProfileResults, XContentType.JSON, false); assertEquals("{\"aggregations\":[" + "{\"type\":\"someType\"," + "\"description\":\"someDescription\"," @@ -82,6 +82,16 @@ public void testToXContent() throws IOException { + "\"breakdown\":{\"timing1\":2000,\"timing2\":4000}" + "}" + "]}", xContent.utf8ToString()); + + xContent = toXContent(aggProfileResults, XContentType.JSON, true); + assertEquals("{\"aggregations\":[" + + "{\"type\":\"someType\"," + + "\"description\":\"someDescription\"," + + "\"time\":\"6micros\"," + + "\"time_in_nanos\":6000," + + "\"breakdown\":{\"timing1\":2000,\"timing2\":4000}" + + "}" + + "]}", xContent.utf8ToString()); } } 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 5c72f15ac4ad9..1542ce2eca0e6 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 @@ -53,7 +53,8 @@ public static QueryProfileShardResult createTestItem() { public void testFromXContent() throws IOException { QueryProfileShardResult profileResult = createTestItem(); XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toXContent(profileResult, xContentType); + boolean humanReadable = randomBoolean(); + BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable); QueryProfileShardResult parsed; try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { @@ -62,7 +63,7 @@ public void testFromXContent() throws IOException { assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); assertNull(parser.nextToken()); } - assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType), xContentType); + assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType); } }