Skip to content

Commit 8e2c84a

Browse files
authored
Clean up XPackInfoResponse class and related tests (#35547)
Response classes in Elasticsearch (and xpack) only need to implement ToXContent, which is needed to print their output put in the REST layer and return the response in json (or others) format. On the other hand, response classes that are added to the high-level REST client, need to do the opposite: parse xcontent and create a new object based on that. This commit removes the parsing code from the XPackInfoResponse server variant, and the toXContent portion from the corresponding client variant. It also removes a client specific test class that looks redundant now that we have a single test class for both classes.
1 parent 928ab6a commit 8e2c84a

File tree

5 files changed

+47
-275
lines changed

5 files changed

+47
-275
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java

Lines changed: 46 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,27 @@
1818
*/
1919
package org.elasticsearch.client.xpack;
2020

21+
import org.elasticsearch.client.license.LicenseStatus;
2122
import org.elasticsearch.common.Nullable;
2223
import org.elasticsearch.common.ParseField;
23-
import org.elasticsearch.common.Strings;
2424
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2525
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
26-
import org.elasticsearch.common.xcontent.ToXContentObject;
27-
import org.elasticsearch.common.xcontent.XContentBuilder;
2826
import org.elasticsearch.common.xcontent.XContentParser;
29-
import org.elasticsearch.client.license.LicenseStatus;
3027

3128
import java.io.IOException;
32-
import java.util.ArrayList;
3329
import java.util.Collections;
34-
import java.util.EnumSet;
3530
import java.util.HashMap;
3631
import java.util.HashSet;
3732
import java.util.List;
3833
import java.util.Map;
3934
import java.util.Objects;
4035
import java.util.Set;
4136
import java.util.concurrent.TimeUnit;
42-
import java.util.stream.Collectors;
4337

4438
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
4539
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
4640

47-
public class XPackInfoResponse implements ToXContentObject {
41+
public class XPackInfoResponse {
4842
/**
4943
* Value of the license's expiration time if it should never expire.
5044
*/
@@ -102,7 +96,11 @@ public int hashCode() {
10296

10397
@Override
10498
public String toString() {
105-
return Strings.toString(this, true, false);
99+
return "XPackInfoResponse{" +
100+
"buildInfo=" + buildInfo +
101+
", licenseInfo=" + licenseInfo +
102+
", featureSetsInfo=" + featureSetsInfo +
103+
'}';
106104
}
107105

108106
private static final ConstructingObjectParser<XPackInfoResponse, Void> PARSER = new ConstructingObjectParser<>(
@@ -131,41 +129,12 @@ public String toString() {
131129
(p, c, name) -> FeatureSetsInfo.FeatureSet.PARSER.parse(p, name),
132130
new ParseField("features"));
133131
}
132+
134133
public static XPackInfoResponse fromXContent(XContentParser parser) throws IOException {
135134
return PARSER.parse(parser, null);
136135
}
137136

138-
@Override
139-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
140-
builder.startObject();
141-
142-
if (buildInfo != null) {
143-
builder.field("build", buildInfo, params);
144-
}
145-
146-
EnumSet<XPackInfoRequest.Category> categories = XPackInfoRequest.Category
147-
.toSet(Strings.splitStringByCommaToArray(params.param("categories", "_all")));
148-
if (licenseInfo != null) {
149-
builder.field("license", licenseInfo, params);
150-
} else if (categories.contains(XPackInfoRequest.Category.LICENSE)) {
151-
// if the user requested the license info, and there is no license, we should send
152-
// back an explicit null value (indicating there is no license). This is different
153-
// than not adding the license info at all
154-
builder.nullField("license");
155-
}
156-
157-
if (featureSetsInfo != null) {
158-
builder.field("features", featureSetsInfo, params);
159-
}
160-
161-
if (params.paramAsBoolean("human", true)) {
162-
builder.field("tagline", "You know, for X");
163-
}
164-
165-
return builder.endObject();
166-
}
167-
168-
public static class LicenseInfo implements ToXContentObject {
137+
public static class LicenseInfo {
169138
private final String uid;
170139
private final String type;
171140
private final String mode;
@@ -217,6 +186,17 @@ public int hashCode() {
217186
return Objects.hash(uid, type, mode, status, expiryDate);
218187
}
219188

189+
@Override
190+
public String toString() {
191+
return "LicenseInfo{" +
192+
"uid='" + uid + '\'' +
193+
", type='" + type + '\'' +
194+
", mode='" + mode + '\'' +
195+
", status=" + status +
196+
", expiryDate=" + expiryDate +
197+
'}';
198+
}
199+
220200
private static final ConstructingObjectParser<LicenseInfo, Void> PARSER = new ConstructingObjectParser<>(
221201
"license_info", true, (a, v) -> {
222202
String uid = (String) a[0];
@@ -234,22 +214,9 @@ public int hashCode() {
234214
PARSER.declareString(constructorArg(), new ParseField("status"));
235215
PARSER.declareLong(optionalConstructorArg(), new ParseField("expiry_date_in_millis"));
236216
}
237-
238-
@Override
239-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
240-
builder.startObject()
241-
.field("uid", uid)
242-
.field("type", type)
243-
.field("mode", mode)
244-
.field("status", status.label());
245-
if (expiryDate != BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) {
246-
builder.timeField("expiry_date_in_millis", "expiry_date", expiryDate);
247-
}
248-
return builder.endObject();
249-
}
250217
}
251218

252-
public static class BuildInfo implements ToXContentObject {
219+
public static class BuildInfo {
253220
private final String hash;
254221
private final String timestamp;
255222

@@ -280,23 +247,23 @@ public int hashCode() {
280247
return Objects.hash(hash, timestamp);
281248
}
282249

250+
@Override
251+
public String toString() {
252+
return "BuildInfo{" +
253+
"hash='" + hash + '\'' +
254+
", timestamp='" + timestamp + '\'' +
255+
'}';
256+
}
257+
283258
private static final ConstructingObjectParser<BuildInfo, Void> PARSER = new ConstructingObjectParser<>(
284259
"build_info", true, (a, v) -> new BuildInfo((String) a[0], (String) a[1]));
285260
static {
286261
PARSER.declareString(constructorArg(), new ParseField("hash"));
287262
PARSER.declareString(constructorArg(), new ParseField("date"));
288263
}
289-
290-
@Override
291-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
292-
return builder.startObject()
293-
.field("hash", hash)
294-
.field("date", timestamp)
295-
.endObject();
296-
}
297264
}
298265

299-
public static class FeatureSetsInfo implements ToXContentObject {
266+
public static class FeatureSetsInfo {
300267
private final Map<String, FeatureSet> featureSets;
301268

302269
public FeatureSetsInfo(Set<FeatureSet> featureSets) {
@@ -325,16 +292,13 @@ public int hashCode() {
325292
}
326293

327294
@Override
328-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
329-
builder.startObject();
330-
List<String> names = new ArrayList<>(this.featureSets.keySet()).stream().sorted().collect(Collectors.toList());
331-
for (String name : names) {
332-
builder.field(name, featureSets.get(name), params);
333-
}
334-
return builder.endObject();
295+
public String toString() {
296+
return "FeatureSetsInfo{" +
297+
"featureSets=" + featureSets +
298+
'}';
335299
}
336300

337-
public static class FeatureSet implements ToXContentObject {
301+
public static class FeatureSet {
338302
private final String name;
339303
@Nullable private final String description;
340304
private final boolean available;
@@ -389,6 +353,17 @@ public int hashCode() {
389353
return Objects.hash(name, description, available, enabled, nativeCodeInfo);
390354
}
391355

356+
@Override
357+
public String toString() {
358+
return "FeatureSet{" +
359+
"name='" + name + '\'' +
360+
", description='" + description + '\'' +
361+
", available=" + available +
362+
", enabled=" + enabled +
363+
", nativeCodeInfo=" + nativeCodeInfo +
364+
'}';
365+
}
366+
392367
private static final ConstructingObjectParser<FeatureSet, String> PARSER = new ConstructingObjectParser<>(
393368
"feature_set", true, (a, name) -> {
394369
String description = (String) a[0];
@@ -404,20 +379,6 @@ public int hashCode() {
404379
PARSER.declareBoolean(constructorArg(), new ParseField("enabled"));
405380
PARSER.declareObject(optionalConstructorArg(), (p, name) -> p.map(), new ParseField("native_code_info"));
406381
}
407-
408-
@Override
409-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
410-
builder.startObject();
411-
if (description != null) {
412-
builder.field("description", description);
413-
}
414-
builder.field("available", available);
415-
builder.field("enabled", enabled);
416-
if (nativeCodeInfo != null) {
417-
builder.field("native_code_info", nativeCodeInfo);
418-
}
419-
return builder.endObject();
420-
}
421382
}
422383
}
423384
}

client/rest-high-level/src/test/java/org/elasticsearch/client/xpack/XPackInfoResponseTests.java

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)