summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/action/support
diff options
context:
space:
mode:
authorTanguy Leroux <tlrx.dev@gmail.com>2017-01-10 20:25:32 +0100
committerGitHub <noreply@github.com>2017-01-10 20:25:32 +0100
commit2dcb05fca877763969baad8d9c80cdb3dfecf2b6 (patch)
tree73a306ba2d556bcaca36da1c5858dce318b03a5e /core/src/test/java/org/elasticsearch/action/support
parentd6a23de00273aa19884bdd98ff54d5158940afeb (diff)
Add fromxcontent methods to index response (#22229)
This commit adds the parsing fromXContent() methods to the IndexResponse class. The method is based on a ObjectParser because it is easier to use when parsing parent abstract classes like DocWriteResponse. It also changes the ReplicationResponse.ShardInfo so that it now implements ToXContentObject. This way, the ShardInfo.fromXContent() method can be used by the IndexResponse's ObjectParser.
Diffstat (limited to 'core/src/test/java/org/elasticsearch/action/support')
-rw-r--r--core/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java65
1 files changed, 15 insertions, 50 deletions
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 658853f959..0972a91c8e 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
@@ -20,17 +20,17 @@
package org.elasticsearch.action.support.replication;
import org.elasticsearch.ElasticsearchException;
-import org.elasticsearch.action.RoutingMissingException;
import org.elasticsearch.common.bytes.BytesReference;
+import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.Index;
-import org.elasticsearch.index.shard.IndexShardRecoveringException;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
+import org.elasticsearch.test.RandomObjects;
import java.io.IOException;
import java.util.ArrayList;
@@ -42,6 +42,7 @@ import java.util.Set;
import java.util.function.Supplier;
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
+import static org.hamcrest.Matchers.instanceOf;
public class ReplicationResponseTests extends ESTestCase {
@@ -64,12 +65,13 @@ public class ReplicationResponseTests extends ESTestCase {
new ReplicationResponse.ShardInfo(shardInfo.getTotal(), shardInfo.getSuccessful() + 1, shardInfo.getFailures()));
mutations.add(() -> {
int nbFailures = randomIntBetween(1, 5);
- return new ReplicationResponse.ShardInfo(shardInfo.getTotal(), shardInfo.getSuccessful(), randomFailures(nbFailures));
+ ReplicationResponse.ShardInfo.Failure[] randomFailures = RandomObjects.randomShardInfoFailures(random(), nbFailures);
+ return new ReplicationResponse.ShardInfo(shardInfo.getTotal(), shardInfo.getSuccessful(), randomFailures);
});
return randomFrom(mutations).get();
};
- checkEqualsAndHashCode(randomShardInfo(), copy, mutate);
+ checkEqualsAndHashCode(RandomObjects.randomShardInfo(random(), randomBoolean()), copy, mutate);
}
public void testFailureEqualsAndHashcode() {
@@ -127,7 +129,7 @@ public class ReplicationResponseTests extends ESTestCase {
return randomFrom(mutations).get();
};
- checkEqualsAndHashCode(randomFailure(), copy, mutate);
+ checkEqualsAndHashCode(RandomObjects.randomShardInfoFailure(random()), copy, mutate);
}
public void testShardInfoToXContent() throws IOException {
@@ -136,13 +138,11 @@ public class ReplicationResponseTests extends ESTestCase {
final ReplicationResponse.ShardInfo shardInfo = new ReplicationResponse.ShardInfo(5, 3);
final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType);
- // Expected JSON is {"_shards":{"total":5,"successful":3,"failed":0}}
+ // Expected JSON is {"total":5,"successful":3,"failed":0}
+ assertThat(shardInfo, instanceOf(ToXContentObject.class));
try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
- assertEquals("_shards", parser.currentName());
- assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
- assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("total", parser.currentName());
assertEquals(XContentParser.Token.VALUE_NUMBER, parser.nextToken());
assertEquals(shardInfo.getTotal(), parser.intValue());
@@ -155,7 +155,6 @@ public class ReplicationResponseTests extends ESTestCase {
assertEquals(XContentParser.Token.VALUE_NUMBER, parser.nextToken());
assertEquals(shardInfo.getFailed(), parser.intValue());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
- assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
}
@@ -168,10 +167,9 @@ public class ReplicationResponseTests extends ESTestCase {
ReplicationResponse.ShardInfo parsedShardInfo;
try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) {
- // Move to the start object that was manually added when building the object
+ // Move to the first start object
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsedShardInfo = ReplicationResponse.ShardInfo.fromXContent(parser);
- assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
// We can use assertEquals because the shardInfo doesn't have a failure (and exceptions)
@@ -184,15 +182,12 @@ public class ReplicationResponseTests extends ESTestCase {
public void testShardInfoWithFailureToXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
- final ReplicationResponse.ShardInfo shardInfo = randomShardInfo();
+ final ReplicationResponse.ShardInfo shardInfo = RandomObjects.randomShardInfo(random(), true);
final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType);
try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
- assertEquals("_shards", parser.currentName());
- assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
- assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("total", parser.currentName());
assertEquals(XContentParser.Token.VALUE_NUMBER, parser.nextToken());
assertEquals(shardInfo.getTotal(), parser.intValue());
@@ -217,7 +212,6 @@ public class ReplicationResponseTests extends ESTestCase {
}
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
- assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
}
@@ -225,15 +219,14 @@ public class ReplicationResponseTests extends ESTestCase {
public void testRandomShardInfoFromXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
- final ReplicationResponse.ShardInfo shardInfo = randomShardInfo();
+ final ReplicationResponse.ShardInfo shardInfo = RandomObjects.randomShardInfo(random(), randomBoolean());
final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfo, xContentType);
ReplicationResponse.ShardInfo parsedShardInfo;
try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) {
- // Move to the start object that was manually added when building the object
+ // Move to the first start object
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsedShardInfo = ReplicationResponse.ShardInfo.fromXContent(parser);
- assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
@@ -266,7 +259,7 @@ public class ReplicationResponseTests extends ESTestCase {
public void testRandomFailureToXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
- final ReplicationResponse.ShardInfo.Failure shardInfoFailure = randomFailure();
+ final ReplicationResponse.ShardInfo.Failure shardInfoFailure = RandomObjects.randomShardInfoFailure(random());
final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfoFailure, xContentType);
try (XContentParser parser = createParser(xContentType.xContent(), shardInfoBytes)) {
@@ -277,7 +270,7 @@ public class ReplicationResponseTests extends ESTestCase {
public void testRandomFailureToAndFromXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
- final ReplicationResponse.ShardInfo.Failure shardInfoFailure = randomFailure();
+ final ReplicationResponse.ShardInfo.Failure shardInfoFailure = RandomObjects.randomShardInfoFailure(random());;
final BytesReference shardInfoBytes = XContentHelper.toXContent(shardInfoFailure, xContentType);
ReplicationResponse.ShardInfo.Failure parsedFailure;
@@ -358,32 +351,4 @@ public class ReplicationResponseTests extends ESTestCase {
}
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
}
-
- private static ReplicationResponse.ShardInfo randomShardInfo() {
- int total = randomIntBetween(1, 10);
- int successful = randomIntBetween(0, total);
- return new ReplicationResponse.ShardInfo(total, successful, randomFailures(Math.max(0, (total - successful))));
- }
-
- private static ReplicationResponse.ShardInfo.Failure[] randomFailures(int nbFailures) {
- List<ReplicationResponse.ShardInfo.Failure> randomFailures = new ArrayList<>(nbFailures);
- for (int i = 0; i < nbFailures; i++) {
- randomFailures.add(randomFailure());
- }
- return randomFailures.toArray(new ReplicationResponse.ShardInfo.Failure[nbFailures]);
- }
-
- private static ReplicationResponse.ShardInfo.Failure randomFailure() {
- return new ReplicationResponse.ShardInfo.Failure(
- new ShardId(randomAsciiOfLength(5), randomAsciiOfLength(5), randomIntBetween(0, 5)),
- randomAsciiOfLength(3),
- randomFrom(
- new IndexShardRecoveringException(new ShardId("_test", "_0", 5)),
- new ElasticsearchException(new IllegalArgumentException("argument is wrong")),
- new RoutingMissingException("_test", "_type", "_id")
- ),
- randomFrom(RestStatus.values()),
- randomBoolean()
- );
- }
}