Skip to content

Commit

Permalink
Include human readable responses in response parsing tests (#22717)
Browse files Browse the repository at this point in the history
As a follow up to #22649, this changes the resent tests for parsing parts of search 
responses to randomly set the humanReadable() flag of the XContentBuilder that 
is used to render the responses. This should help to test that we can parse back 
thoses classes if the user specifies `?human=true` in the request url.
  • Loading branch information
cbuescher authored Jan 24, 2017
1 parent 12b6ff5 commit 59aefe5
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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());
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public void testToAndFromXContent() throws Exception {
Tuple<GetResult, GetResult> 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)) {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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<String, Object> actual) {
assertEquals(expected.getIndex(), actual.get("_index"));
assertEquals(expected.getType(), actual.get("_type"));
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)) {
Expand All @@ -175,15 +176,15 @@ 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);
}

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());
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,17 @@ public void testToXContent() throws IOException {
public void testToAndFromXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final Tuple<UpdateResponse, UpdateResponse> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void testToAndFromXContent() throws Exception {
Tuple<GetField, GetField> 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)) {
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit 59aefe5

Please sign in to comment.