Skip to content

Commit

Permalink
Move more test-only x-content parsing logic out of production codebase (
Browse files Browse the repository at this point in the history
#113774)

Just moving over a few more since they're not used in production code any longer.
  • Loading branch information
original-brownbear committed Sep 30, 2024
1 parent d21e0ef commit 99830f9
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.apache.http.util.EntityUtils;
import org.elasticsearch.Build;
import org.elasticsearch.action.admin.cluster.settings.RestClusterGetSettingsResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
Expand Down Expand Up @@ -43,6 +42,7 @@
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.test.rest.RestTestLegacyFeatures;
import org.elasticsearch.test.rest.TestResponseParsers;
import org.elasticsearch.transport.Compression;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -1861,7 +1861,7 @@ public void testTransportCompressionSetting() throws IOException {
final Request getSettingsRequest = new Request("GET", "/_cluster/settings");
final Response getSettingsResponse = client().performRequest(getSettingsRequest);
try (XContentParser parser = createParser(JsonXContent.jsonXContent, getSettingsResponse.getEntity().getContent())) {
final Settings settings = RestClusterGetSettingsResponse.fromXContent(parser).getPersistentSettings();
final Settings settings = TestResponseParsers.parseClusterSettingsResponse(parser).getPersistentSettings();
assertThat(REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace("foo").get(settings), equalTo(Compression.Enabled.TRUE));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;

/**
* This response is specific to the REST client. {@link org.elasticsearch.action.admin.cluster.state.ClusterStateResponse}
* is used on the transport layer.
Expand All @@ -33,23 +27,9 @@ public class RestClusterGetSettingsResponse implements ToXContentObject {
private final Settings transientSettings;
private final Settings defaultSettings;

static final String PERSISTENT_FIELD = "persistent";
static final String TRANSIENT_FIELD = "transient";
static final String DEFAULTS_FIELD = "defaults";

private static final ConstructingObjectParser<RestClusterGetSettingsResponse, Void> PARSER = new ConstructingObjectParser<>(
"cluster_get_settings_response",
true,
a -> {
Settings defaultSettings = a[2] == null ? Settings.EMPTY : (Settings) a[2];
return new RestClusterGetSettingsResponse((Settings) a[0], (Settings) a[1], defaultSettings);
}
);
static {
PARSER.declareObject(constructorArg(), (p, c) -> Settings.fromXContent(p), new ParseField(PERSISTENT_FIELD));
PARSER.declareObject(constructorArg(), (p, c) -> Settings.fromXContent(p), new ParseField(TRANSIENT_FIELD));
PARSER.declareObject(optionalConstructorArg(), (p, c) -> Settings.fromXContent(p), new ParseField(DEFAULTS_FIELD));
}
public static final String PERSISTENT_FIELD = "persistent";
public static final String TRANSIENT_FIELD = "transient";
public static final String DEFAULTS_FIELD = "defaults";

public RestClusterGetSettingsResponse(Settings persistentSettings, Settings transientSettings, Settings defaultSettings) {
this.persistentSettings = Objects.requireNonNullElse(persistentSettings, Settings.EMPTY);
Expand Down Expand Up @@ -120,10 +100,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static RestClusterGetSettingsResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,18 @@
import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;

/**
* A response for a create index action.
*/
public class CreateIndexResponse extends ShardsAcknowledgedResponse {

private static final ParseField INDEX = new ParseField("index");

private static final ConstructingObjectParser<CreateIndexResponse, Void> PARSER = new ConstructingObjectParser<>(
"create_index",
true,
args -> new CreateIndexResponse((boolean) args[0], (boolean) args[1], (String) args[2])
);

static {
declareFields(PARSER);
}

protected static <T extends CreateIndexResponse> void declareFields(ConstructingObjectParser<T, Void> objectParser) {
declareAcknowledgedAndShardsAcknowledgedFields(objectParser);
objectParser.declareField(constructorArg(), (parser, context) -> parser.textOrNull(), INDEX, ObjectParser.ValueType.STRING);
}
public static final ParseField INDEX = new ParseField("index");

private final String index;

Expand Down Expand Up @@ -74,10 +54,6 @@ protected void addCustomFields(XContentBuilder builder, Params params) throws IO
builder.field(INDEX.getPreferredName(), index());
}

public static CreateIndexResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

@Override
public boolean equals(Object o) {
if (super.equals(o)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;

/**
* A response to an action which updated the cluster state, but needs to report whether any relevant nodes failed to apply the update. For
* instance, a {@link org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest} may update a mapping in the index metadata, but
Expand All @@ -39,16 +33,6 @@ public class AcknowledgedResponse extends ActionResponse implements IsAcknowledg
public static final AcknowledgedResponse FALSE = new AcknowledgedResponse(false);

public static final String ACKNOWLEDGED_KEY = "acknowledged";
private static final ParseField ACKNOWLEDGED = new ParseField(ACKNOWLEDGED_KEY);

public static <T extends AcknowledgedResponse> void declareAcknowledgedField(ConstructingObjectParser<T, Void> objectParser) {
objectParser.declareField(
constructorArg(),
(parser, context) -> parser.booleanValue(),
ACKNOWLEDGED,
ObjectParser.ValueType.BOOLEAN
);
}

protected final boolean acknowledged;

Expand Down Expand Up @@ -93,28 +77,6 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params)

protected void addCustomFields(XContentBuilder builder, Params params) throws IOException {}

/**
* A generic parser that simply parses the acknowledged flag
*/
private static final ConstructingObjectParser<Boolean, Void> ACKNOWLEDGED_FLAG_PARSER = new ConstructingObjectParser<>(
"acknowledged_flag",
true,
args -> (Boolean) args[0]
);

static {
ACKNOWLEDGED_FLAG_PARSER.declareField(
constructorArg(),
(parser, context) -> parser.booleanValue(),
ACKNOWLEDGED,
ObjectParser.ValueType.BOOLEAN
);
}

public static AcknowledgedResponse fromXContent(XContentParser parser) throws IOException {
return AcknowledgedResponse.of(ACKNOWLEDGED_FLAG_PARSER.apply(parser, null));
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,15 @@

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;

public class ShardsAcknowledgedResponse extends AcknowledgedResponse {

protected static final ParseField SHARDS_ACKNOWLEDGED = new ParseField("shards_acknowledged");

public static <T extends ShardsAcknowledgedResponse> void declareAcknowledgedAndShardsAcknowledgedFields(
ConstructingObjectParser<T, Void> objectParser
) {
declareAcknowledgedField(objectParser);
objectParser.declareField(
constructorArg(),
(parser, context) -> parser.booleanValue(),
SHARDS_ACKNOWLEDGED,
ObjectParser.ValueType.BOOLEAN
);
}
public static final ParseField SHARDS_ACKNOWLEDGED = new ParseField("shards_acknowledged");

public static final ShardsAcknowledgedResponse NOT_ACKNOWLEDGED = new ShardsAcknowledgedResponse(false, false);
private static final ShardsAcknowledgedResponse SHARDS_NOT_ACKNOWLEDGED = new ShardsAcknowledgedResponse(true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.test.rest.TestResponseParsers;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.XContentParser;

import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import static org.elasticsearch.action.support.master.AcknowledgedResponse.declareAcknowledgedField;
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;

public class ClusterUpdateSettingsResponseTests extends AbstractXContentSerializingTestCase<ClusterUpdateSettingsResponse> {
Expand All @@ -33,7 +33,7 @@ public class ClusterUpdateSettingsResponseTests extends AbstractXContentSerializ
args -> new ClusterUpdateSettingsResponse((boolean) args[0], (Settings) args[1], (Settings) args[2])
);
static {
declareAcknowledgedField(PARSER);
TestResponseParsers.declareAcknowledgedField(PARSER);
PARSER.declareObject(constructorArg(), (p, c) -> Settings.fromXContent(p), ClusterUpdateSettingsResponse.TRANSIENT);
PARSER.declareObject(constructorArg(), (p, c) -> Settings.fromXContent(p), ClusterUpdateSettingsResponse.PERSISTENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.AbstractXContentTestCase;
import org.elasticsearch.test.rest.TestResponseParsers;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
Expand All @@ -20,7 +21,7 @@ public class RestClusterGetSettingsResponseTests extends AbstractXContentTestCas

@Override
protected RestClusterGetSettingsResponse doParseInstance(XContentParser parser) throws IOException {
return RestClusterGetSettingsResponse.fromXContent(parser);
return TestResponseParsers.parseClusterSettingsResponse(parser);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.test.rest.TestResponseParsers;
import org.elasticsearch.xcontent.XContentParser;

public class CreateIndexResponseTests extends AbstractXContentSerializingTestCase<CreateIndexResponse> {
Expand Down Expand Up @@ -52,7 +53,7 @@ protected CreateIndexResponse mutateInstance(CreateIndexResponse response) {

@Override
protected CreateIndexResponse doParseInstance(XContentParser parser) {
return CreateIndexResponse.fromXContent(parser);
return TestResponseParsers.parseCreateIndexResponse(parser);
}

public void testToXContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.test.rest.TestResponseParsers;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.XContentParser;

import static org.elasticsearch.action.support.master.ShardsAcknowledgedResponse.declareAcknowledgedAndShardsAcknowledgedFields;

public class OpenIndexResponseTests extends AbstractXContentSerializingTestCase<OpenIndexResponse> {

private static final ConstructingObjectParser<OpenIndexResponse, Void> PARSER = new ConstructingObjectParser<>(
Expand All @@ -25,7 +24,7 @@ public class OpenIndexResponseTests extends AbstractXContentSerializingTestCase<
);

static {
declareAcknowledgedAndShardsAcknowledgedFields(PARSER);
TestResponseParsers.declareAcknowledgedAndShardsAcknowledgedFields(PARSER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ public static CreateIndexResponse createIndex(RestClient client, String name, Se

final Response response = client.performRequest(request);
try (var parser = responseAsParser(response)) {
return CreateIndexResponse.fromXContent(parser);
return TestResponseParsers.parseCreateIndexResponse(parser);
}
}

Expand All @@ -1867,7 +1867,7 @@ protected static AcknowledgedResponse deleteIndex(RestClient restClient, String
Request request = new Request("DELETE", "/" + name);
Response response = restClient.performRequest(request);
try (var parser = responseAsParser(response)) {
return AcknowledgedResponse.fromXContent(parser);
return TestResponseParsers.parseAcknowledgedResponse(parser);
}
}

Expand Down
Loading

0 comments on commit 99830f9

Please sign in to comment.