[Transform] simplify TransformConfigUpdate (#55224)

removes the unnecessary ToXContent method in TransformConfigUpdate
This commit is contained in:
Hendrik Muhs 2020-04-15 13:21:58 +02:00
parent e164c9aaee
commit 9ec9866acb
2 changed files with 63 additions and 62 deletions

View File

@ -9,14 +9,11 @@ package org.elasticsearch.xpack.core.transform.transforms;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.rest.RestStatus;
@ -33,22 +30,24 @@ import static org.elasticsearch.xpack.core.transform.transforms.TransformConfig.
/**
* This class holds the mutable configuration items for a data frame transform
*/
public class TransformConfigUpdate implements Writeable, ToXContentObject {
public class TransformConfigUpdate implements Writeable {
public static final String NAME = "data_frame_transform_config_update";
private static final ConstructingObjectParser<TransformConfigUpdate, String> PARSER = new ConstructingObjectParser<>(NAME,
private static final ConstructingObjectParser<TransformConfigUpdate, String> PARSER = new ConstructingObjectParser<>(
NAME,
false,
(args) -> {
SourceConfig source = (SourceConfig) args[0];
DestConfig dest = (DestConfig) args[1];
TimeValue frequency = args[2] == null ?
null :
TimeValue.parseTimeValue((String) args[2], TransformField.FREQUENCY.getPreferredName());
TimeValue frequency = args[2] == null
? null
: TimeValue.parseTimeValue((String) args[2], TransformField.FREQUENCY.getPreferredName());
SyncConfig syncConfig = (SyncConfig) args[3];
String description = (String) args[4];
return new TransformConfigUpdate(source, dest, frequency, syncConfig, description);
});
}
);
static {
PARSER.declareObject(optionalConstructorArg(), (p, c) -> SourceConfig.fromXContent(p, false), TransformField.SOURCE);
@ -73,11 +72,13 @@ public class TransformConfigUpdate implements Writeable, ToXContentObject {
private final String description;
private Map<String, String> headers;
public TransformConfigUpdate(final SourceConfig source,
final DestConfig dest,
final TimeValue frequency,
final SyncConfig syncConfig,
final String description){
public TransformConfigUpdate(
final SourceConfig source,
final DestConfig dest,
final TimeValue frequency,
final SyncConfig syncConfig,
final String description
) {
this.source = source;
this.dest = dest;
this.frequency = frequency;
@ -143,33 +144,6 @@ public class TransformConfigUpdate implements Writeable, ToXContentObject {
}
}
@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
builder.startObject();
if (source != null) {
builder.field(TransformField.SOURCE.getPreferredName(), source);
}
if (dest != null) {
builder.field(TransformField.DESTINATION.getPreferredName(), dest);
}
if (frequency != null) {
builder.field(TransformField.FREQUENCY.getPreferredName(), frequency.getStringRep());
}
if (syncConfig != null) {
builder.startObject(TransformField.SYNC.getPreferredName());
builder.field(syncConfig.getWriteableName(), syncConfig);
builder.endObject();
}
if (description != null) {
builder.field(TransformField.DESCRIPTION.getPreferredName(), description);
}
if (headers != null) {
builder.field(TransformConfig.HEADERS.getPreferredName(), headers);
}
builder.endObject();
return builder;
}
@Override
public boolean equals(Object other) {
if (this == other) {
@ -183,23 +157,18 @@ public class TransformConfigUpdate implements Writeable, ToXContentObject {
final TransformConfigUpdate that = (TransformConfigUpdate) other;
return Objects.equals(this.source, that.source)
&& Objects.equals(this.dest, that.dest)
&& Objects.equals(this.frequency, that.frequency)
&& Objects.equals(this.syncConfig, that.syncConfig)
&& Objects.equals(this.description, that.description)
&& Objects.equals(this.headers, that.headers);
&& Objects.equals(this.dest, that.dest)
&& Objects.equals(this.frequency, that.frequency)
&& Objects.equals(this.syncConfig, that.syncConfig)
&& Objects.equals(this.description, that.description)
&& Objects.equals(this.headers, that.headers);
}
@Override
public int hashCode(){
public int hashCode() {
return Objects.hash(source, dest, frequency, syncConfig, description, headers);
}
@Override
public String toString() {
return Strings.toString(this, true, true);
}
public static TransformConfigUpdate fromXContent(final XContentParser parser) {
return PARSER.apply(parser, null);
}
@ -235,11 +204,14 @@ public class TransformConfigUpdate implements Writeable, ToXContentObject {
String currentConfigName = config.getSyncConfig() == null ? "null" : config.getSyncConfig().getWriteableName();
if (syncConfig.getWriteableName().equals(currentConfigName) == false) {
throw new ElasticsearchStatusException(
TransformMessages.getMessage(TransformMessages.TRANSFORM_UPDATE_CANNOT_CHANGE_SYNC_METHOD,
TransformMessages.getMessage(
TransformMessages.TRANSFORM_UPDATE_CANNOT_CHANGE_SYNC_METHOD,
config.getId(),
currentConfigName,
syncConfig.getWriteableName()),
RestStatus.BAD_REQUEST);
syncConfig.getWriteableName()
),
RestStatus.BAD_REQUEST
);
}
builder.setSyncConfig(syncConfig);
}

View File

@ -12,8 +12,9 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.xpack.core.transform.TransformField;
import org.elasticsearch.xpack.core.transform.action.AbstractWireSerializingTransformTestCase;
import org.elasticsearch.xpack.core.transform.transforms.pivot.PivotConfigTests;
import java.io.IOException;
@ -21,12 +22,13 @@ import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
import static org.elasticsearch.xpack.core.transform.transforms.DestConfigTests.randomDestConfig;
import static org.elasticsearch.xpack.core.transform.transforms.SourceConfigTests.randomSourceConfig;
import static org.elasticsearch.xpack.core.transform.transforms.TransformConfigTests.randomTransformConfig;
import static org.hamcrest.Matchers.equalTo;
public class TransformConfigUpdateTests extends AbstractSerializingTransformTestCase<TransformConfigUpdate> {
public class TransformConfigUpdateTests extends AbstractWireSerializingTransformTestCase<TransformConfigUpdate> {
public static TransformConfigUpdate randomTransformConfigUpdate() {
return new TransformConfigUpdate(
@ -42,11 +44,6 @@ public class TransformConfigUpdateTests extends AbstractSerializingTransformTest
return TimeSyncConfigTests.randomTimeSyncConfig();
}
@Override
protected TransformConfigUpdate doParseInstance(XContentParser parser) throws IOException {
return TransformConfigUpdate.fromXContent(parser);
}
@Override
protected TransformConfigUpdate createTestInstance() {
return randomTransformConfigUpdate();
@ -162,6 +159,38 @@ public class TransformConfigUpdateTests extends AbstractSerializingTransformTest
}
public void testFromXContent() throws IOException {
xContentTester(
this::createParser,
TransformConfigUpdateTests::randomTransformConfigUpdate,
this::toXContent,
TransformConfigUpdate::fromXContent
).supportsUnknownFields(false).assertEqualsConsumer(this::assertEqualInstances).test();
}
private void toXContent(TransformConfigUpdate update, XContentBuilder builder) throws IOException {
builder.startObject();
if (update.getSource() != null) {
builder.field(TransformField.SOURCE.getPreferredName(), update.getSource());
}
if (update.getDestination() != null) {
builder.field(TransformField.DESTINATION.getPreferredName(), update.getDestination());
}
if (update.getFrequency() != null) {
builder.field(TransformField.FREQUENCY.getPreferredName(), update.getFrequency().getStringRep());
}
if (update.getSyncConfig() != null) {
builder.startObject(TransformField.SYNC.getPreferredName());
builder.field(update.getSyncConfig().getWriteableName(), update.getSyncConfig());
builder.endObject();
}
if (update.getDescription() != null) {
builder.field(TransformField.DESCRIPTION.getPreferredName(), update.getDescription());
}
builder.endObject();
}
static class FooSync implements SyncConfig {
@Override