|
8 | 8 |
|
9 | 9 | package org.elasticsearch.snapshots;
|
10 | 10 |
|
| 11 | +import org.elasticsearch.Version; |
11 | 12 | import org.elasticsearch.cluster.ClusterModule;
|
12 | 13 | import org.elasticsearch.cluster.ClusterState.Custom;
|
13 | 14 | import org.elasticsearch.cluster.Diff;
|
14 | 15 | import org.elasticsearch.cluster.SnapshotsInProgress;
|
15 | 16 | import org.elasticsearch.cluster.SnapshotsInProgress.Entry;
|
16 | 17 | import org.elasticsearch.cluster.SnapshotsInProgress.ShardState;
|
17 | 18 | import org.elasticsearch.cluster.SnapshotsInProgress.State;
|
| 19 | +import org.elasticsearch.common.Strings; |
18 | 20 | import org.elasticsearch.common.collect.ImmutableOpenMap;
|
19 | 21 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
20 | 22 | import org.elasticsearch.common.io.stream.Writeable;
|
| 23 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
21 | 25 | import org.elasticsearch.index.Index;
|
22 | 26 | import org.elasticsearch.index.shard.ShardId;
|
23 | 27 | import org.elasticsearch.repositories.IndexId;
|
24 | 28 | import org.elasticsearch.test.AbstractDiffableWireSerializationTestCase;
|
25 | 29 | import org.elasticsearch.test.ESTestCase;
|
26 | 30 | import org.elasticsearch.test.VersionUtils;
|
27 | 31 |
|
| 32 | +import java.io.IOException; |
28 | 33 | import java.util.ArrayList;
|
29 | 34 | import java.util.Arrays;
|
| 35 | +import java.util.Collections; |
30 | 36 | import java.util.HashMap;
|
31 | 37 | import java.util.List;
|
32 | 38 | import java.util.Map;
|
33 | 39 | import java.util.stream.Collectors;
|
34 | 40 | import java.util.stream.Stream;
|
35 | 41 |
|
| 42 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 43 | +import static org.hamcrest.Matchers.equalTo; |
| 44 | + |
36 | 45 | public class SnapshotsInProgressSerializationTests extends AbstractDiffableWireSerializationTestCase<Custom> {
|
37 | 46 |
|
38 | 47 | @Override
|
@@ -222,6 +231,32 @@ private Entry mutateEntry(Entry entry) {
|
222 | 231 | }
|
223 | 232 | }
|
224 | 233 |
|
| 234 | + public void testXContent() throws IOException { |
| 235 | + SnapshotsInProgress sip = |
| 236 | + SnapshotsInProgress.of(Collections.singletonList(new Entry( |
| 237 | + new Snapshot("repo", new SnapshotId("name", "uuid")), true, true, State.SUCCESS, |
| 238 | + Collections.singletonList(new IndexId("index", "uuid")), Collections.emptyList(), Collections.emptyList(), 1234567, 0, |
| 239 | + ImmutableOpenMap.<ShardId, SnapshotsInProgress.ShardSnapshotStatus>builder() |
| 240 | + .fPut(new ShardId("index", "uuid", 0), |
| 241 | + new SnapshotsInProgress.ShardSnapshotStatus("nodeId", ShardState.SUCCESS, "reason", "generation")) |
| 242 | + .build(), null, null, Version.CURRENT))); |
| 243 | + |
| 244 | + try (XContentBuilder builder = jsonBuilder()) { |
| 245 | + builder.humanReadable(true); |
| 246 | + builder.startObject(); |
| 247 | + sip.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 248 | + builder.endObject(); |
| 249 | + String json = Strings.toString(builder); |
| 250 | + assertThat(json, |
| 251 | + equalTo("{\"snapshots\":[{\"repository\":\"repo\",\"snapshot\":\"name\",\"uuid\":\"uuid\"," + |
| 252 | + "\"include_global_state\":true,\"partial\":true,\"state\":\"SUCCESS\"," + |
| 253 | + "\"indices\":[{\"name\":\"index\",\"id\":\"uuid\"}],\"start_time\":\"1970-01-01T00:20:34.567Z\"," + |
| 254 | + "\"start_time_millis\":1234567,\"repository_state_id\":0," + |
| 255 | + "\"shards\":[{\"index\":{\"index_name\":\"index\",\"index_uuid\":\"uuid\"}," + |
| 256 | + "\"shard\":0,\"state\":\"SUCCESS\",\"node\":\"nodeId\"}],\"feature_states\":[],\"data_streams\":[]}]}")); |
| 257 | + } |
| 258 | + } |
| 259 | + |
225 | 260 | public static State randomState(ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards) {
|
226 | 261 | return SnapshotsInProgress.completed(shards.values())
|
227 | 262 | ? randomFrom(State.SUCCESS, State.FAILED) : randomFrom(State.STARTED, State.INIT, State.ABORTED);
|
|
0 commit comments