summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/action/support
diff options
context:
space:
mode:
authorLee Hinman <lee@writequit.org>2017-01-11 10:08:46 -0700
committerLee Hinman <lee@writequit.org>2017-01-11 10:08:46 -0700
commite93fdb846083a29dc65505e111e32553d6f7be6c (patch)
tree4764f937f71ace9a619cb628f07bf7808d8a18e0 /core/src/test/java/org/elasticsearch/action/support
parent692ddac02036f473d8bb0a8b967647fdc688cfa2 (diff)
parentfed2a1a8225996f9dbad2ebd5cd3f8c4242f87a8 (diff)
Merge branch 'master' into enhancement/use_shard_bulk_for_single_ops
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()
- );
- }
}