Skip to content

Commit 72a62ae

Browse files
committed
IndexFeatureStats to implement ToXContentObject (#71573)
IndexFeatureStats prints out a whole object, hence it should be a ToXContentObject. This way consumers like Strings#toString automatically know not to wrap it into a new object when printing it out.
1 parent 9aba8d8 commit 72a62ae

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/IndexFeatureStats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.common.io.stream.StreamOutput;
1313
import org.elasticsearch.common.io.stream.Writeable;
14-
import org.elasticsearch.common.xcontent.ToXContent;
14+
import org.elasticsearch.common.xcontent.ToXContentObject;
1515
import org.elasticsearch.common.xcontent.XContentBuilder;
1616

1717
import java.io.IOException;
@@ -20,7 +20,7 @@
2020
/**
2121
* Statistics about an index feature.
2222
*/
23-
public class IndexFeatureStats implements ToXContent, Writeable {
23+
public class IndexFeatureStats implements ToXContentObject, Writeable {
2424

2525
final String name;
2626
int count;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.action.admin.cluster.stats;
10+
11+
import org.elasticsearch.common.Strings;
12+
import org.elasticsearch.common.io.stream.Writeable;
13+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
14+
15+
public class IndexFeatureStatsTests extends AbstractWireSerializingTestCase<IndexFeatureStats> {
16+
@Override
17+
protected Writeable.Reader<IndexFeatureStats> instanceReader() {
18+
return IndexFeatureStats::new;
19+
}
20+
21+
@Override
22+
protected IndexFeatureStats createTestInstance() {
23+
IndexFeatureStats indexFeatureStats = new IndexFeatureStats(randomAlphaOfLengthBetween(3, 10));
24+
indexFeatureStats.indexCount = randomIntBetween(0, Integer.MAX_VALUE);
25+
indexFeatureStats.count = randomIntBetween(0, Integer.MAX_VALUE);
26+
return indexFeatureStats;
27+
}
28+
29+
public void testToXContent() {
30+
IndexFeatureStats testInstance = createTestInstance();
31+
assertEquals("{\"name\":\"" + testInstance.name +
32+
"\",\"count\":" + testInstance.count +
33+
",\"index_count\":" + testInstance.indexCount + "}", Strings.toString(testInstance));
34+
}
35+
}

0 commit comments

Comments
 (0)