|
20 | 20 | package org.elasticsearch.client.core;
|
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.support.DefaultShardOperationFailedException;
|
23 |
| -import org.elasticsearch.cluster.ClusterModule; |
24 |
| -import org.elasticsearch.common.bytes.BytesReference; |
25 |
| -import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; |
26 |
| -import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
27 |
| -import org.elasticsearch.common.xcontent.ToXContent; |
28 |
| -import org.elasticsearch.common.xcontent.XContent; |
29 |
| -import org.elasticsearch.common.xcontent.XContentFactory; |
| 23 | +import org.elasticsearch.client.AbstractResponseTestCase; |
30 | 24 | import org.elasticsearch.common.xcontent.XContentParser;
|
31 |
| -import org.elasticsearch.common.xcontent.XContentType; |
32 | 25 | import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException;
|
33 |
| -import org.elasticsearch.test.ESTestCase; |
34 | 26 |
|
35 | 27 | import java.io.IOException;
|
36 | 28 | import java.util.ArrayList;
|
|
43 | 35 | import static org.hamcrest.Matchers.hasSize;
|
44 | 36 | import static org.hamcrest.Matchers.isIn;
|
45 | 37 |
|
46 |
| -public class BroadcastResponseTests extends ESTestCase { |
| 38 | +public class BroadcastResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.support.broadcast.BroadcastResponse, |
| 39 | + BroadcastResponse> { |
47 | 40 |
|
48 |
| - public void testFromXContent() throws IOException { |
49 |
| - final String index = randomAlphaOfLength(8); |
50 |
| - final String id = randomAlphaOfLength(8); |
| 41 | + private String index; |
| 42 | + private String id; |
| 43 | + private Set<Integer> shardIds; |
| 44 | + |
| 45 | + @Override |
| 46 | + protected org.elasticsearch.action.support.broadcast.BroadcastResponse createServerTestInstance() { |
| 47 | + index = randomAlphaOfLength(8); |
| 48 | + id = randomAlphaOfLength(8); |
51 | 49 | final int total = randomIntBetween(1, 16);
|
52 | 50 | final int successful = total - scaledRandomIntBetween(0, total);
|
53 | 51 | final int failed = scaledRandomIntBetween(0, total - successful);
|
54 | 52 | final List<DefaultShardOperationFailedException> failures = new ArrayList<>();
|
55 |
| - final Set<Integer> shardIds = new HashSet<>(); |
| 53 | + shardIds = new HashSet<>(); |
56 | 54 | for (int i = 0; i < failed; i++) {
|
57 | 55 | final DefaultShardOperationFailedException failure = new DefaultShardOperationFailedException(
|
58 |
| - index, |
59 |
| - randomValueOtherThanMany(shardIds::contains, () -> randomIntBetween(0, total - 1)), |
60 |
| - new RetentionLeaseNotFoundException(id)); |
| 56 | + index, |
| 57 | + randomValueOtherThanMany(shardIds::contains, () -> randomIntBetween(0, total - 1)), |
| 58 | + new RetentionLeaseNotFoundException(id)); |
61 | 59 | failures.add(failure);
|
62 | 60 | shardIds.add(failure.shardId());
|
63 | 61 | }
|
64 | 62 |
|
65 |
| - final org.elasticsearch.action.support.broadcast.BroadcastResponse to = |
66 |
| - new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures); |
| 63 | + return new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures); |
| 64 | + } |
67 | 65 |
|
68 |
| - final XContentType xContentType = randomFrom(XContentType.values()); |
69 |
| - final BytesReference bytes = toShuffledXContent(to, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); |
| 66 | + @Override |
| 67 | + protected BroadcastResponse doParseToClientInstance(XContentParser parser) throws IOException { |
| 68 | + return BroadcastResponse.fromXContent(parser); |
| 69 | + } |
70 | 70 |
|
71 |
| - final XContent xContent = XContentFactory.xContent(xContentType); |
72 |
| - final XContentParser parser = xContent.createParser( |
73 |
| - new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), |
74 |
| - LoggingDeprecationHandler.INSTANCE, |
75 |
| - bytes.streamInput()); |
76 |
| - final BroadcastResponse from = BroadcastResponse.fromXContent(parser); |
77 |
| - assertThat(from.shards().total(), equalTo(total)); |
78 |
| - assertThat(from.shards().successful(), equalTo(successful)); |
79 |
| - assertThat(from.shards().skipped(), equalTo(0)); |
80 |
| - assertThat(from.shards().failed(), equalTo(failed)); |
81 |
| - assertThat(from.shards().failures(), hasSize(failed == 0 ? failed : 1)); // failures are grouped |
82 |
| - if (failed > 0) { |
83 |
| - final DefaultShardOperationFailedException groupedFailure = from.shards().failures().iterator().next(); |
| 71 | + @Override |
| 72 | + protected void assertInstances(org.elasticsearch.action.support.broadcast.BroadcastResponse serverTestInstance, |
| 73 | + BroadcastResponse clientInstance) { |
| 74 | + assertThat(clientInstance.shards().total(), equalTo(serverTestInstance.getTotalShards())); |
| 75 | + assertThat(clientInstance.shards().successful(), equalTo(serverTestInstance.getSuccessfulShards())); |
| 76 | + assertThat(clientInstance.shards().skipped(), equalTo(0)); |
| 77 | + assertThat(clientInstance.shards().failed(), equalTo(serverTestInstance.getFailedShards())); |
| 78 | + assertThat(clientInstance.shards().failures(), hasSize(clientInstance.shards().failed() == 0 ? 0 : 1)); // failures are grouped |
| 79 | + if (clientInstance.shards().failed() > 0) { |
| 80 | + final DefaultShardOperationFailedException groupedFailure = clientInstance.shards().failures().iterator().next(); |
84 | 81 | assertThat(groupedFailure.index(), equalTo(index));
|
85 | 82 | assertThat(groupedFailure.shardId(), isIn(shardIds));
|
86 | 83 | assertThat(groupedFailure.reason(), containsString("reason=retention lease with ID [" + id + "] not found"));
|
|
0 commit comments