Skip to content

Commit 2e4d69a

Browse files
kelcbuescher
authored andcommitted
Fix toString() in SnapshotStatus (#26852)
Closes #26851
1 parent 8ccb8c0 commit 2e4d69a

File tree

2 files changed

+137
-11
lines changed

2 files changed

+137
-11
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
package org.elasticsearch.action.admin.cluster.snapshots.status;
2121

2222
import org.elasticsearch.cluster.SnapshotsInProgress.State;
23+
import org.elasticsearch.common.Strings;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.io.stream.StreamOutput;
2526
import org.elasticsearch.common.io.stream.Streamable;
26-
import org.elasticsearch.common.xcontent.ToXContent.Params;
2727
import org.elasticsearch.common.xcontent.ToXContentObject;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
29-
import org.elasticsearch.common.xcontent.XContentFactory;
3029
import org.elasticsearch.snapshots.Snapshot;
3130

3231
import java.io.IOException;
@@ -160,15 +159,7 @@ public static SnapshotStatus readSnapshotStatus(StreamInput in) throws IOExcepti
160159

161160
@Override
162161
public String toString() {
163-
try {
164-
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
165-
builder.startObject();
166-
toXContent(builder, EMPTY_PARAMS);
167-
builder.endObject();
168-
return builder.string();
169-
} catch (IOException e) {
170-
return "{ \"error\" : \"" + e.getMessage() + "\"}";
171-
}
162+
return Strings.toString(this, true, false);
172163
}
173164

174165
/**
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package org.elasticsearch.action.admin.cluster.snapshots.status;
2+
3+
/*
4+
* Licensed to Elasticsearch under one or more contributor
5+
* license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright
7+
* ownership. Elasticsearch licenses this file to you under
8+
* the Apache License, Version 2.0 (the "License"); you may
9+
* not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.elasticsearch.cluster.SnapshotsInProgress;
23+
import org.elasticsearch.common.UUIDs;
24+
import org.elasticsearch.index.shard.ShardId;
25+
import org.elasticsearch.snapshots.Snapshot;
26+
import org.elasticsearch.snapshots.SnapshotId;
27+
import org.elasticsearch.test.ESTestCase;
28+
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
33+
public class SnapshotStatusTests extends ESTestCase {
34+
35+
36+
public void testToString() throws Exception {
37+
SnapshotsInProgress.State state = randomFrom(SnapshotsInProgress.State.values());
38+
String uuid = UUIDs.randomBase64UUID();
39+
SnapshotId id = new SnapshotId("test-snap", uuid);
40+
Snapshot snapshot = new Snapshot("test-repo", id);
41+
42+
String indexName = randomAlphaOfLengthBetween(3, 50);
43+
int shardId = randomInt();
44+
ShardId testShardId = ShardId.fromString("[" + indexName + "][" + shardId + "]");
45+
SnapshotIndexShardStage shardStage = randomFrom(SnapshotIndexShardStage.values());
46+
SnapshotIndexShardStatus snapshotIndexShardStatus = new SnapshotIndexShardStatus(testShardId, shardStage);
47+
List<SnapshotIndexShardStatus> snapshotIndexShardStatuses = new ArrayList<>();
48+
snapshotIndexShardStatuses.add(snapshotIndexShardStatus);
49+
SnapshotStatus status = new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses);
50+
51+
int initializingShards = 0;
52+
int startedShards = 0;
53+
int finalizingShards = 0;
54+
int doneShards = 0;
55+
int failedShards = 0;
56+
int totalShards = 1;
57+
58+
switch (shardStage) {
59+
case INIT:
60+
initializingShards++;
61+
break;
62+
case STARTED:
63+
startedShards++;
64+
break;
65+
case FINALIZE:
66+
finalizingShards++;
67+
break;
68+
case DONE:
69+
doneShards++;
70+
break;
71+
case FAILURE:
72+
failedShards++;
73+
break;
74+
default:
75+
break;
76+
}
77+
78+
String expected = "{\n" +
79+
" \"snapshot\" : \"test-snap\",\n" +
80+
" \"repository\" : \"test-repo\",\n" +
81+
" \"uuid\" : \"" + uuid + "\",\n" +
82+
" \"state\" : \"" + state.toString() + "\",\n" +
83+
" \"shards_stats\" : {\n" +
84+
" \"initializing\" : " + initializingShards + ",\n" +
85+
" \"started\" : " + startedShards + ",\n" +
86+
" \"finalizing\" : " + finalizingShards + ",\n" +
87+
" \"done\" : " + doneShards + ",\n" +
88+
" \"failed\" : " + failedShards + ",\n" +
89+
" \"total\" : " + totalShards + "\n" +
90+
" },\n" +
91+
" \"stats\" : {\n" +
92+
" \"number_of_files\" : 0,\n" +
93+
" \"processed_files\" : 0,\n" +
94+
" \"total_size_in_bytes\" : 0,\n" +
95+
" \"processed_size_in_bytes\" : 0,\n" +
96+
" \"start_time_in_millis\" : 0,\n" +
97+
" \"time_in_millis\" : 0\n" +
98+
" },\n" +
99+
" \"indices\" : {\n" +
100+
" \"" + indexName + "\" : {\n" +
101+
" \"shards_stats\" : {\n" +
102+
" \"initializing\" : " + initializingShards + ",\n" +
103+
" \"started\" : " + startedShards + ",\n" +
104+
" \"finalizing\" : " + finalizingShards + ",\n" +
105+
" \"done\" : " + doneShards + ",\n" +
106+
" \"failed\" : " + failedShards + ",\n" +
107+
" \"total\" : " + totalShards + "\n" +
108+
" },\n" +
109+
" \"stats\" : {\n" +
110+
" \"number_of_files\" : 0,\n" +
111+
" \"processed_files\" : 0,\n" +
112+
" \"total_size_in_bytes\" : 0,\n" +
113+
" \"processed_size_in_bytes\" : 0,\n" +
114+
" \"start_time_in_millis\" : 0,\n" +
115+
" \"time_in_millis\" : 0\n" +
116+
" },\n" +
117+
" \"shards\" : {\n" +
118+
" \"" + shardId + "\" : {\n" +
119+
" \"stage\" : \"" + shardStage.toString() + "\",\n" +
120+
" \"stats\" : {\n" +
121+
" \"number_of_files\" : 0,\n" +
122+
" \"processed_files\" : 0,\n" +
123+
" \"total_size_in_bytes\" : 0,\n" +
124+
" \"processed_size_in_bytes\" : 0,\n" +
125+
" \"start_time_in_millis\" : 0,\n" +
126+
" \"time_in_millis\" : 0\n" +
127+
" }\n" +
128+
" }\n" +
129+
" }\n" +
130+
" }\n" +
131+
" }\n" +
132+
"}";
133+
assertEquals(expected, status.toString());
134+
}
135+
}

0 commit comments

Comments
 (0)