From 7d3f9d6692a7671841e500d7bd70f9776987eb8a Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Sun, 14 Oct 2018 20:26:14 +0200 Subject: [PATCH 01/26] HLRC XPack Protocol clean up: Licence, Misc Relates #34451 --- .../elasticsearch/client/LicenseClient.java | 10 +- .../client/LicenseRequestConverters.java | 19 ++- .../client/RequestConverters.java | 8 +- .../elasticsearch/client/TimedRequest.java | 9 +- .../org/elasticsearch/client/XPackClient.java | 8 +- .../client/XPackRequestConverters.java | 4 +- .../client}/license/DeleteLicenseRequest.java | 12 +- .../client/license/GetLicenseRequest.java | 21 ++- .../client}/license/GetLicenseResponse.java | 6 +- .../client}/license/LicenseStatus.java | 19 +-- .../client}/license/LicensesStatus.java | 2 +- .../client}/license/PutLicenseRequest.java | 13 +- .../client}/license/PutLicenseResponse.java | 77 ++------- .../client}/license/package-info.java | 2 +- .../client}/xpack/XPackInfoRequest.java | 34 +--- .../client}/xpack/XPackInfoResponse.java | 37 +--- .../client/xpack/XPackUsageRequest.java | 11 +- .../client}/xpack/XPackUsageResponse.java | 2 +- .../client/LicenseRequestConvertersTests.java | 86 ++++++++++ .../org/elasticsearch/client/LicensingIT.java | 55 ++++++ .../elasticsearch/client/PingAndInfoIT.java | 8 +- .../client/RequestConvertersTests.java | 17 +- .../client/XPackRequestConvertersTests.java | 11 +- .../LicensingDocumentationIT.java | 12 +- .../MiscellaneousDocumentationIT.java | 14 +- x-pack/protocol-test/build.gradle | 21 +++ .../xpack/license/LicenseStatusTests.java | 26 +++ .../license/PutLicenseResponseTests.java | 33 ++-- .../xpack/license/GetLicenseRequest.java | 41 ----- .../xpack/XPackInfoResponseTests.java | 160 ------------------ 30 files changed, 319 insertions(+), 459 deletions(-) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/DeleteLicenseRequest.java (68%) rename x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageRequest.java => client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java (69%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/GetLicenseResponse.java (86%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/LicenseStatus.java (73%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/LicensesStatus.java (97%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/PutLicenseRequest.java (78%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/PutLicenseResponse.java (70%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/license/package-info.java (94%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol => client/rest-high-level/src/main/java/org/elasticsearch/client}/xpack/XPackInfoRequest.java (65%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol => client/rest-high-level/src/main/java/org/elasticsearch/client}/xpack/XPackInfoResponse.java (93%) rename x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java => client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageRequest.java (66%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol => client/rest-high-level/src/main/java/org/elasticsearch/client}/xpack/XPackUsageResponse.java (97%) create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java create mode 100644 x-pack/protocol-test/build.gradle create mode 100644 x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java rename x-pack/{protocol => protocol-test}/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java (81%) delete mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java delete mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java index bf8abc21fe135..258231c8be378 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java @@ -30,11 +30,11 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseResponse; -import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; -import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; +import org.elasticsearch.client.license.DeleteLicenseRequest; +import org.elasticsearch.client.license.GetLicenseRequest; +import org.elasticsearch.client.license.GetLicenseResponse; +import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.client.license.PutLicenseResponse; import java.io.IOException; import java.io.InputStream; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java index 7c2c049324eab..1595f238fecc1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java @@ -22,15 +22,14 @@ import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; -import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; -import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; +import org.elasticsearch.client.license.DeleteLicenseRequest; +import org.elasticsearch.client.license.GetLicenseRequest; +import org.elasticsearch.client.license.PutLicenseRequest; public class LicenseRequestConverters { static Request putLicense(PutLicenseRequest putLicenseRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("license") + .addPathPartAsIs("_xpack", "license") .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(request); @@ -45,17 +44,19 @@ static Request putLicense(PutLicenseRequest putLicenseRequest) { static Request getLicense(GetLicenseRequest getLicenseRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("license") + .addPathPartAsIs("_xpack", "license") .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(request); - parameters.withLocal(getLicenseRequest.local()); + parameters.withLocal(getLicenseRequest.isLocal()); return request; } static Request deleteLicense(DeleteLicenseRequest deleteLicenseRequest) { - Request request = new Request(HttpDelete.METHOD_NAME, "/_xpack/license"); + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_xpack", "license") + .build(); + Request request = new Request(HttpDelete.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(request); parameters.withTimeout(deleteLicenseRequest.timeout()); parameters.withMasterTimeout(deleteLicenseRequest.masterNodeTimeout()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 9c461a404cf8f..57e63791fd30f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -980,9 +980,11 @@ EndpointBuilder addCommaSeparatedPathParts(String[] parts) { return this; } - EndpointBuilder addPathPartAsIs(String part) { - if (Strings.hasLength(part)) { - joiner.add(part); + EndpointBuilder addPathPartAsIs(String ... parts) { + for (String part : parts) { + if (Strings.hasLength(part)) { + joiner.add(part); + } } return this; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java index af8fbe3e72b37..6ae4e1567a655 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TimedRequest.java @@ -20,6 +20,8 @@ import org.elasticsearch.common.unit.TimeValue; +import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; + /** * A base request for any requests that supply timeouts. * @@ -28,8 +30,11 @@ */ public class TimedRequest implements Validatable { - private TimeValue timeout; - private TimeValue masterTimeout; + public static final TimeValue DEFAULT_ACK_TIMEOUT = timeValueSeconds(30); + public static final TimeValue DEFAULT_MASTER_NODE_TIMEOUT = timeValueSeconds(30); + + private TimeValue timeout = DEFAULT_ACK_TIMEOUT; + private TimeValue masterTimeout = DEFAULT_MASTER_NODE_TIMEOUT; public void setTimeout(TimeValue timeout) { this.timeout = timeout; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java index 9cd8413fa7917..a20dfd1ba328a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java @@ -20,10 +20,10 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.protocol.xpack.XPackInfoRequest; -import org.elasticsearch.protocol.xpack.XPackInfoResponse; -import org.elasticsearch.protocol.xpack.XPackUsageRequest; -import org.elasticsearch.protocol.xpack.XPackUsageResponse; +import org.elasticsearch.client.xpack.XPackInfoRequest; +import org.elasticsearch.client.xpack.XPackInfoResponse; +import org.elasticsearch.client.xpack.XPackUsageRequest; +import org.elasticsearch.client.xpack.XPackUsageResponse; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java index 1e2e15ad97c2e..1d5b9a418586c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java @@ -20,8 +20,8 @@ package org.elasticsearch.client; import org.apache.http.client.methods.HttpGet; -import org.elasticsearch.protocol.xpack.XPackInfoRequest; -import org.elasticsearch.protocol.xpack.XPackUsageRequest; +import org.elasticsearch.client.xpack.XPackInfoRequest; +import org.elasticsearch.client.xpack.XPackUsageRequest; import java.util.EnumSet; import java.util.Locale; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/DeleteLicenseRequest.java similarity index 68% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/DeleteLicenseRequest.java index abcc4a68eb88a..a014b85c72cb2 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/DeleteLicenseRequest.java @@ -16,16 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.license; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.support.master.AcknowledgedRequest; +import org.elasticsearch.client.TimedRequest; +public class DeleteLicenseRequest extends TimedRequest { -public class DeleteLicenseRequest extends AcknowledgedRequest { - - @Override - public ActionRequestValidationException validate() { - return null; - } } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java similarity index 69% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java index f5f6d9d949b7f..bb0468fd9d572 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java @@ -16,16 +16,23 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack; +package org.elasticsearch.client.license; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.support.master.MasterNodeRequest; +import org.elasticsearch.client.Validatable; -public class XPackUsageRequest extends MasterNodeRequest { - @Override - public ActionRequestValidationException validate() { - return null; +public class GetLicenseRequest implements Validatable { + + protected boolean local = false; + + public GetLicenseRequest() { } + public boolean isLocal() { + return local; + } + + public void setLocal(boolean local) { + this.local = local; + } } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java similarity index 86% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java index 7232e185a7ef5..5afbaa2afa8e9 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java @@ -16,11 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.license; -import org.elasticsearch.action.ActionResponse; - -public class GetLicenseResponse extends ActionResponse { +public class GetLicenseResponse { private String license; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicenseStatus.java similarity index 73% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicenseStatus.java index ea3e4f8a8965b..99751dcb6f266 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicenseStatus.java @@ -16,18 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.license; - -import java.io.IOException; - -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Writeable; +package org.elasticsearch.client.license; /** * Status of an X-Pack license. */ -public enum LicenseStatus implements Writeable { +public enum LicenseStatus { ACTIVE("active"), INVALID("invalid"), @@ -43,15 +37,6 @@ public String label() { return label; } - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(label); - } - - public static LicenseStatus readFrom(StreamInput in) throws IOException { - return fromString(in.readString()); - } - public static LicenseStatus fromString(String value) { switch (value) { case "active": diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java similarity index 97% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java index 2a3ed924fbe4b..7a2c48e9d49d3 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.license; import java.util.Locale; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java similarity index 78% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java index 97101a3ccd483..62b223458c558 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java @@ -17,23 +17,16 @@ * under the License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.license; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.support.master.AcknowledgedRequest; +import org.elasticsearch.client.TimedRequest; -public class PutLicenseRequest extends AcknowledgedRequest { +public class PutLicenseRequest extends TimedRequest { private String licenseDefinition; private boolean acknowledge = false; public PutLicenseRequest() { - - } - - @Override - public ActionRequestValidationException validate() { - return null; } public void setLicenseDefinition(String licenseDefinition) { diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java similarity index 70% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java index 9c4ff51d92a10..1b50ef10eb950 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java @@ -17,16 +17,11 @@ * under the License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.license; -import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Tuple; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParseException; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.protocol.xpack.common.ProtocolUtils; @@ -42,7 +37,9 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class PutLicenseResponse extends AcknowledgedResponse { +public final class PutLicenseResponse { + + private static final ParseField ACKNOWLEDGED = new ParseField("acknowledged"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "put_license_response", true, (a, v) -> { @@ -58,7 +55,7 @@ public class PutLicenseResponse extends AcknowledgedResponse { }); static { - PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged")); + PARSER.declareBoolean(constructorArg(), ACKNOWLEDGED); PARSER.declareString(constructorArg(), new ParseField("license_status")); PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { Map acknowledgeMessages = new HashMap<>(); @@ -97,6 +94,7 @@ public class PutLicenseResponse extends AcknowledgedResponse { new ParseField("acknowledge")); } + private boolean acknowledged; private LicensesStatus status; private Map acknowledgeMessages; private String acknowledgeHeader; @@ -110,12 +108,16 @@ public PutLicenseResponse(boolean acknowledged, LicensesStatus status) { public PutLicenseResponse(boolean acknowledged, LicensesStatus status, String acknowledgeHeader, Map acknowledgeMessages) { - super(acknowledged); + this.acknowledged = acknowledged; this.status = status; this.acknowledgeHeader = acknowledgeHeader; this.acknowledgeMessages = acknowledgeMessages; } + public boolean isAcknowledged() { + return acknowledged; + } + public LicensesStatus status() { return status; } @@ -128,62 +130,6 @@ public String acknowledgeHeader() { return acknowledgeHeader; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - status = LicensesStatus.fromId(in.readVInt()); - acknowledgeHeader = in.readOptionalString(); - int size = in.readVInt(); - Map acknowledgeMessages = new HashMap<>(size); - for (int i = 0; i < size; i++) { - String feature = in.readString(); - int nMessages = in.readVInt(); - String[] messages = new String[nMessages]; - for (int j = 0; j < nMessages; j++) { - messages[j] = in.readString(); - } - acknowledgeMessages.put(feature, messages); - } - this.acknowledgeMessages = acknowledgeMessages; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeVInt(status.id()); - out.writeOptionalString(acknowledgeHeader); - out.writeVInt(acknowledgeMessages.size()); - for (Map.Entry entry : acknowledgeMessages.entrySet()) { - out.writeString(entry.getKey()); - out.writeVInt(entry.getValue().length); - for (String message : entry.getValue()) { - out.writeString(message); - } - } - } - - @Override - protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { - builder.field("license_status", status.toString()); - if (!acknowledgeMessages.isEmpty()) { - builder.startObject("acknowledge"); - builder.field("message", acknowledgeHeader); - for (Map.Entry entry : acknowledgeMessages.entrySet()) { - builder.startArray(entry.getKey()); - for (String message : entry.getValue()) { - builder.value(message); - } - builder.endArray(); - } - builder.endObject(); - } - } - - @Override - public String toString() { - return Strings.toString(this, true, true); - } - public static PutLicenseResponse fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } @@ -205,5 +151,4 @@ public int hashCode() { return Objects.hash(super.hashCode(), status, ProtocolUtils.hashCode(acknowledgeMessages), acknowledgeHeader); } - } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/package-info.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/package-info.java similarity index 94% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/package-info.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/license/package-info.java index ca859f29e440b..ee0bd22c515de 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/package-info.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/package-info.java @@ -21,4 +21,4 @@ * Request and Response objects for the default distribution's License * APIs. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.license; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java similarity index 65% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java index ce43b763e2313..6f018e07b5de9 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java @@ -16,21 +16,17 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack; +package org.elasticsearch.client.xpack; -import org.elasticsearch.action.ActionRequest; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.client.Validatable; -import java.io.IOException; import java.util.EnumSet; import java.util.Locale; /** * Fetch information about X-Pack from the cluster. */ -public class XPackInfoRequest extends ActionRequest { +public class XPackInfoRequest implements Validatable { public enum Category { BUILD, LICENSE, FEATURES; @@ -72,28 +68,4 @@ public EnumSet getCategories() { return categories; } - @Override - public ActionRequestValidationException validate() { - return null; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - this.verbose = in.readBoolean(); - EnumSet categories = EnumSet.noneOf(Category.class); - int size = in.readVInt(); - for (int i = 0; i < size; i++) { - categories.add(Category.valueOf(in.readString())); - } - this.categories = categories; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeBoolean(verbose); - out.writeVInt(categories.size()); - for (Category category : categories) { - out.writeString(category.name()); - } - } } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java similarity index 93% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java index 1d3e51c11e027..2b7d205447f69 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java @@ -16,9 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack; +package org.elasticsearch.client.xpack; -import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; @@ -30,7 +29,7 @@ import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.license.LicenseStatus; +import org.elasticsearch.client.license.LicenseStatus; import java.io.IOException; import java.util.ArrayList; @@ -48,7 +47,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class XPackInfoResponse extends ActionResponse implements ToXContentObject { +public class XPackInfoResponse implements ToXContentObject { /** * Value of the license's expiration time if it should never expire. */ @@ -89,21 +88,6 @@ public FeatureSetsInfo getFeatureSetsInfo() { return featureSetsInfo; } - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeOptionalWriteable(buildInfo); - out.writeOptionalWriteable(licenseInfo); - out.writeOptionalWriteable(featureSetsInfo); - } - - @Override - public void readFrom(StreamInput in) throws IOException { - this.buildInfo = in.readOptionalWriteable(BuildInfo::new); - this.licenseInfo = in.readOptionalWriteable(LicenseInfo::new); - this.featureSetsInfo = in.readOptionalWriteable(FeatureSetsInfo::new); - } - @Override public boolean equals(Object other) { if (other == null || other.getClass() != getClass()) return false; @@ -184,7 +168,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } - public static class LicenseInfo implements ToXContentObject, Writeable { + public static class LicenseInfo implements ToXContentObject { private final String uid; private final String type; private final String mode; @@ -199,19 +183,6 @@ public LicenseInfo(String uid, String type, String mode, LicenseStatus status, l this.expiryDate = expiryDate; } - public LicenseInfo(StreamInput in) throws IOException { - this(in.readString(), in.readString(), in.readString(), LicenseStatus.readFrom(in), in.readLong()); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(uid); - out.writeString(type); - out.writeString(mode); - status.writeTo(out); - out.writeLong(expiryDate); - } - public String getUid() { return uid; } diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageRequest.java similarity index 66% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageRequest.java index c256e7562f7f8..9308a09de796c 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageRequest.java @@ -16,15 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.client.xpack; -import java.io.IOException; +import org.elasticsearch.client.TimedRequest; -import org.elasticsearch.test.ESTestCase; +public class XPackUsageRequest extends TimedRequest { -public class LicenseStatusTests extends ESTestCase { - public void testSerialization() throws IOException { - LicenseStatus status = randomFrom(LicenseStatus.values()); - assertSame(status, copyWriteable(status, writableRegistry(), LicenseStatus::readFrom)); - } } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java similarity index 97% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java index 3459403bd6124..86e812b4e6933 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.protocol.xpack; +package org.elasticsearch.client. xpack; import org.elasticsearch.common.xcontent.XContentParser; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java new file mode 100644 index 0000000000000..96faef8ee00bb --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java @@ -0,0 +1,86 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client; + +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.action.support.master.AcknowledgedRequest; +import org.elasticsearch.client.license.DeleteLicenseRequest; +import org.elasticsearch.client.license.GetLicenseRequest; +import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.test.ESTestCase; + +import java.util.HashMap; +import java.util.Map; + +import static org.elasticsearch.client.RequestConvertersTests.setRandomMasterTimeout; +import static org.elasticsearch.client.RequestConvertersTests.setRandomTimeout; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; + +public class LicenseRequestConvertersTests extends ESTestCase { + public void testGetLicense() { + final boolean local = randomBoolean(); + final GetLicenseRequest getLicenseRequest = new GetLicenseRequest(); + getLicenseRequest.setLocal(local); + final Map expectedParams = new HashMap<>(); + if (local) { + expectedParams.put("local", Boolean.TRUE.toString()); + } + + Request request = LicenseRequestConverters.getLicense(getLicenseRequest); + assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); + assertThat(request.getEndpoint(), equalTo("/_xpack/license")); + assertThat(request.getParameters(), equalTo(expectedParams)); + assertThat(request.getEntity(), is(nullValue())); + } + + public void testPutLicense() { + final boolean acknowledge = randomBoolean(); + final PutLicenseRequest putLicenseRequest = new PutLicenseRequest(); + putLicenseRequest.setAcknowledge(acknowledge); + final Map expectedParams = new HashMap<>(); + if (acknowledge) { + expectedParams.put("acknowledge", Boolean.TRUE.toString()); + } + setRandomTimeout(putLicenseRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + setRandomMasterTimeout(putLicenseRequest, expectedParams); + + Request request = LicenseRequestConverters.putLicense(putLicenseRequest); + assertThat(request.getMethod(), equalTo(HttpPut.METHOD_NAME)); + assertThat(request.getEndpoint(), equalTo("/_xpack/license")); + assertThat(request.getParameters(), equalTo(expectedParams)); + assertThat(request.getEntity(), is(nullValue())); + } + + public void testDeleteLicense() { + final DeleteLicenseRequest deleteLicenseRequest = new DeleteLicenseRequest(); + final Map expectedParams = new HashMap<>(); + setRandomTimeout(deleteLicenseRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + setRandomMasterTimeout(deleteLicenseRequest, expectedParams); + + Request request = LicenseRequestConverters.deleteLicense(deleteLicenseRequest); + assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME)); + assertThat(request.getEndpoint(), equalTo("/_xpack/license")); + assertThat(request.getParameters(), equalTo(expectedParams)); + assertThat(request.getEntity(), is(nullValue())); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java new file mode 100644 index 0000000000000..e53a2e9d9b7b7 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java @@ -0,0 +1,55 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client; + +import org.elasticsearch.client.license.GetLicenseRequest; +import org.elasticsearch.client.license.GetLicenseResponse; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; + +import java.util.Map; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; + +public class LicensingIT extends ESRestHighLevelClientTestCase { + public void testGetLicense() throws Exception { + final GetLicenseRequest request = new GetLicenseRequest(); + final GetLicenseResponse response = highLevelClient().license().getLicense(request, RequestOptions.DEFAULT); + final String licenseDefinition = response.getLicenseDefinition(); + assertThat(licenseDefinition, notNullValue()); + + final XContentParser parser = createParser(JsonXContent.jsonXContent, licenseDefinition); + final Map map = parser.map(); + assertThat(map.containsKey("license"), equalTo(true)); + + @SuppressWarnings("unchecked") + final Map license = (Map) map.get("license"); + assertThat(license.get("status"), equalTo("active")); + assertThat(license.get("type"), equalTo("trial")); + } + + public void testPutLicense() throws Exception { + // TODO + } + + public void testDeleteLicense() throws Exception { + // TODO + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java index 5f38316fd752d..3d6b2d6634f93 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java @@ -21,10 +21,10 @@ import org.apache.http.client.methods.HttpGet; import org.elasticsearch.action.main.MainResponse; -import org.elasticsearch.protocol.xpack.XPackInfoRequest; -import org.elasticsearch.protocol.xpack.XPackInfoResponse; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; -import org.elasticsearch.protocol.xpack.license.LicenseStatus; +import org.elasticsearch.client.xpack.XPackInfoRequest; +import org.elasticsearch.client.xpack.XPackInfoResponse; +import org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; +import org.elasticsearch.client.license.LicenseStatus; import java.io.IOException; import java.util.EnumSet; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 3801dfe71de9c..7e89a3426e03b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -1572,6 +1572,12 @@ static void setRandomLocal(MasterNodeReadRequest request, Map setRandomLocal(request::local, expectedParams); } + static void setRandomTimeout(TimedRequest request, TimeValue defaultTimeout, Map expectedParams) { + setRandomTimeout(s -> + request.setTimeout(TimeValue.parseTimeValue(s, request.getClass().getName() + ".timeout")), + defaultTimeout, expectedParams); + } + static void setRandomTimeout(Consumer setter, TimeValue defaultTimeout, Map expectedParams) { if (randomBoolean()) { String timeout = randomTimeValue(); @@ -1583,9 +1589,18 @@ static void setRandomTimeout(Consumer setter, TimeValue defaultTimeout, } static void setRandomMasterTimeout(MasterNodeRequest request, Map expectedParams) { + setRandomMasterTimeout(request::masterNodeTimeout, expectedParams); + } + static void setRandomMasterTimeout(TimedRequest request, Map expectedParams) { + setRandomMasterTimeout(s -> + request.setMasterTimeout(TimeValue.parseTimeValue(s, request.getClass().getName() + ".masterNodeTimeout")), + expectedParams); + } + + static void setRandomMasterTimeout(Consumer setter, Map expectedParams) { if (randomBoolean()) { String masterTimeout = randomTimeValue(); - request.masterNodeTimeout(masterTimeout); + setter.accept(masterTimeout); expectedParams.put("master_timeout", masterTimeout); } else { expectedParams.put("master_timeout", MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT.getStringRep()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java index d2f20273d4d3c..b5d4e1d496db4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java @@ -20,9 +20,8 @@ package org.elasticsearch.client; import org.apache.http.client.methods.HttpGet; -import org.elasticsearch.protocol.xpack.XPackInfoRequest; +import org.elasticsearch.client.xpack.XPackInfoRequest; import org.elasticsearch.test.ESTestCase; -import org.junit.Assert; import java.util.EnumSet; import java.util.HashMap; @@ -55,9 +54,9 @@ public void testXPackInfo() { } Request request = XPackRequestConverters.info(infoRequest); - Assert.assertEquals(HttpGet.METHOD_NAME, request.getMethod()); - Assert.assertEquals("/_xpack", request.getEndpoint()); - Assert.assertNull(request.getEntity()); - Assert.assertEquals(expectedParams, request.getParameters()); + assertEquals(HttpGet.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack", request.getEndpoint()); + assertNull(request.getEntity()); + assertEquals(expectedParams, request.getParameters()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java index 3737bd70a8dae..09b249b360992 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java @@ -27,12 +27,12 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.Booleans; -import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseResponse; -import org.elasticsearch.protocol.xpack.license.LicensesStatus; -import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; -import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; +import org.elasticsearch.client.license.DeleteLicenseRequest; +import org.elasticsearch.client.license.GetLicenseRequest; +import org.elasticsearch.client.license.GetLicenseResponse; +import org.elasticsearch.client.license.LicensesStatus; +import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.client.license.PutLicenseResponse; import java.util.Map; import java.util.concurrent.CountDownLatch; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MiscellaneousDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MiscellaneousDocumentationIT.java index a9fe4aba2f75a..d1766552cdfd6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MiscellaneousDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MiscellaneousDocumentationIT.java @@ -30,13 +30,13 @@ import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.protocol.xpack.XPackInfoRequest; -import org.elasticsearch.protocol.xpack.XPackInfoResponse; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo; -import org.elasticsearch.protocol.xpack.XPackUsageRequest; -import org.elasticsearch.protocol.xpack.XPackUsageResponse; +import org.elasticsearch.client.xpack.XPackInfoRequest; +import org.elasticsearch.client.xpack.XPackInfoResponse; +import org.elasticsearch.client.xpack.XPackInfoResponse.BuildInfo; +import org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo; +import org.elasticsearch.client.xpack.XPackInfoResponse.LicenseInfo; +import org.elasticsearch.client.xpack.XPackUsageRequest; +import org.elasticsearch.client.xpack.XPackUsageResponse; import java.io.IOException; import java.time.Instant; diff --git a/x-pack/protocol-test/build.gradle b/x-pack/protocol-test/build.gradle new file mode 100644 index 0000000000000..a1f174b2bc6c5 --- /dev/null +++ b/x-pack/protocol-test/build.gradle @@ -0,0 +1,21 @@ + +apply plugin: 'elasticsearch.build' + +group = 'org.elasticsearch.client' +archivesBaseName = 'x-pack-protocol-test' + +// due to clash of xpack and protocol classes +jarHell.enabled = false +// jarHell is there - run tests manually before clean up is done +test.enabled = false + +dependencies { + testCompile "org.elasticsearch.plugin:x-pack-core:${version}" + testCompile "org.elasticsearch.test:framework:${version}" + testCompile ("org.elasticsearch.client:elasticsearch-rest-high-level-client:${version}") { + exclude group: 'org.elasticsearch.plugin', module: 'protocol' + } + testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" + testCompile "junit:junit:${versions.junit}" + testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" +} \ No newline at end of file diff --git a/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java new file mode 100644 index 0000000000000..f89e15a1f0920 --- /dev/null +++ b/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol.xpack.license; + +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class LicenseStatusTests extends ESTestCase { + public void testCompatibility() { + final LicenseStatus[] values = LicenseStatus.values(); + final org.elasticsearch.client.license.LicenseStatus[] ossValues = + org.elasticsearch.client.license.LicenseStatus.values(); + + assertThat(values.length, equalTo(ossValues.length)); + + for (LicenseStatus value : values) { + final org.elasticsearch.client.license.LicenseStatus licenseStatus = + org.elasticsearch.client.license.LicenseStatus.fromString(value.label()); + assertThat(licenseStatus.label(), equalTo(value.label())); + } + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java similarity index 81% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java rename to x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java index 0980a07b68ad2..d168f65fbfd12 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ b/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -1,20 +1,7 @@ /* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. */ package org.elasticsearch.protocol.xpack.license; @@ -30,6 +17,15 @@ public class PutLicenseResponseTests extends AbstractStreamableXContentTestCase { + @Override + protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { + // the key point - is to read by OSS version of class + final org.elasticsearch.client.license.PutLicenseResponse response = + org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); + return new PutLicenseResponse(response.isAcknowledged(), LicensesStatus.valueOf(response.status().name()), + response.acknowledgeHeader(), response.acknowledgeMessages()); + } + @Override protected boolean supportsUnknownFields() { return true; @@ -77,11 +73,6 @@ private static Map randomAckMessages() { return ackMessages; } - @Override - protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { - return PutLicenseResponse.fromXContent(parser); - } - @Override protected PutLicenseResponse createBlankInstance() { return new PutLicenseResponse(); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java deleted file mode 100644 index c669d3d33770c..0000000000000 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.protocol.xpack.license; - -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.support.master.MasterNodeReadRequest; -import org.elasticsearch.common.io.stream.StreamInput; - -import java.io.IOException; - - -public class GetLicenseRequest extends MasterNodeReadRequest { - - public GetLicenseRequest() { - } - - public GetLicenseRequest(StreamInput in) throws IOException { - super(in); - } - - @Override - public ActionRequestValidationException validate() { - return null; - } -} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java deleted file mode 100644 index 820b8200ed940..0000000000000 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.protocol.xpack; - -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; -import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; -import org.elasticsearch.protocol.xpack.license.LicenseStatus; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import java.util.function.Predicate; -import java.io.IOException; - -public class XPackInfoResponseTests extends AbstractStreamableXContentTestCase { - @Override - protected XPackInfoResponse doParseInstance(XContentParser parser) throws IOException { - return XPackInfoResponse.fromXContent(parser); - } - - @Override - protected XPackInfoResponse createBlankInstance() { - return new XPackInfoResponse(); - } - - @Override - protected Predicate getRandomFieldsExcludeFilter() { - return path -> path.equals("features") - || (path.startsWith("features") && path.endsWith("native_code_info")); - } - - @Override - protected ToXContent.Params getToXContentParams() { - Map params = new HashMap<>(); - if (randomBoolean()) { - params.put("human", randomBoolean() ? "true" : "false"); - } - if (randomBoolean()) { - params.put("categories", "_none"); - } - return new ToXContent.MapParams(params); - } - - @Override - protected XPackInfoResponse createTestInstance() { - return new XPackInfoResponse( - randomBoolean() ? null : randomBuildInfo(), - randomBoolean() ? null : randomLicenseInfo(), - randomBoolean() ? null : randomFeatureSetsInfo()); - } - - @Override - protected XPackInfoResponse mutateInstance(XPackInfoResponse response) { - @SuppressWarnings("unchecked") - Function mutator = randomFrom( - r -> new XPackInfoResponse( - mutateBuildInfo(r.getBuildInfo()), - r.getLicenseInfo(), - r.getFeatureSetsInfo()), - r -> new XPackInfoResponse( - r.getBuildInfo(), - mutateLicenseInfo(r.getLicenseInfo()), - r.getFeatureSetsInfo()), - r -> new XPackInfoResponse( - r.getBuildInfo(), - r.getLicenseInfo(), - mutateFeatureSetsInfo(r.getFeatureSetsInfo()))); - return mutator.apply(response); - } - - private BuildInfo randomBuildInfo() { - return new BuildInfo( - randomAlphaOfLength(10), - randomAlphaOfLength(15)); - } - - private BuildInfo mutateBuildInfo(BuildInfo buildInfo) { - if (buildInfo == null) { - return randomBuildInfo(); - } - return null; - } - - private LicenseInfo randomLicenseInfo() { - return new LicenseInfo( - randomAlphaOfLength(10), - randomAlphaOfLength(4), - randomAlphaOfLength(5), - randomFrom(LicenseStatus.values()), - randomLong()); - } - - private LicenseInfo mutateLicenseInfo(LicenseInfo licenseInfo) { - if (licenseInfo == null) { - return randomLicenseInfo(); - } - return null; - } - - private FeatureSetsInfo randomFeatureSetsInfo() { - int size = between(0, 10); - Set featureSets = new HashSet<>(size); - while (featureSets.size() < size) { - featureSets.add(randomFeatureSet()); - } - return new FeatureSetsInfo(featureSets); - } - - private FeatureSetsInfo mutateFeatureSetsInfo(FeatureSetsInfo featureSetsInfo) { - if (featureSetsInfo == null) { - return randomFeatureSetsInfo(); - } - return null; - } - - private FeatureSet randomFeatureSet() { - return new FeatureSet( - randomAlphaOfLength(5), - randomBoolean() ? null : randomAlphaOfLength(20), - randomBoolean(), - randomBoolean(), - randomNativeCodeInfo()); - } - - private Map randomNativeCodeInfo() { - if (randomBoolean()) { - return null; - } - int size = between(0, 10); - Map nativeCodeInfo = new HashMap<>(size); - while (nativeCodeInfo.size() < size) { - nativeCodeInfo.put(randomAlphaOfLength(5), randomAlphaOfLength(5)); - } - return nativeCodeInfo; - } -} From b7b8f8d837dfb7488d5c7380b743956db2d69583 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 15 Oct 2018 17:49:22 +0200 Subject: [PATCH 02/26] package typo (extra space) --- .../java/org/elasticsearch/client/xpack/XPackUsageResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java index 86e812b4e6933..b51a2d7de9fbd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.client. xpack; +package org.elasticsearch.client.xpack; import org.elasticsearch.common.xcontent.XContentParser; From c5e6c9d092e6590d243c292633c49c680ebb62ce Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 16 Oct 2018 16:41:07 +0200 Subject: [PATCH 03/26] Merge branch 'origin/master' into hlrc_xprotocol_cleanup_1 --- .../src/test/java/org/elasticsearch/client/LicensingIT.java | 6 +++--- .../client/documentation/LicensingDocumentationIT.java | 6 ------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java index 5e9c64d7c229e..10f9d9f5028e5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java @@ -21,15 +21,15 @@ import org.elasticsearch.Build; import org.elasticsearch.client.license.GetLicenseRequest; import org.elasticsearch.client.license.GetLicenseResponse; +import org.elasticsearch.client.license.LicensesStatus; +import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.client.license.PutLicenseResponse; import org.elasticsearch.client.license.StartBasicRequest; import org.elasticsearch.client.license.StartBasicResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.protocol.xpack.license.LicensesStatus; -import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; -import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; import org.junit.After; import org.junit.BeforeClass; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java index 88d290947698f..4655de8d66a76 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java @@ -29,12 +29,6 @@ import org.elasticsearch.client.license.StartBasicRequest; import org.elasticsearch.client.license.StartBasicResponse; import org.elasticsearch.common.Booleans; -import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; -import org.elasticsearch.protocol.xpack.license.GetLicenseResponse; -import org.elasticsearch.protocol.xpack.license.LicensesStatus; -import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; -import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; import org.junit.After; import org.junit.BeforeClass; import org.elasticsearch.client.license.DeleteLicenseRequest; From 32872b474a850b6834bd24d23e7d8ef919c3ab6a Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 16 Oct 2018 16:49:35 +0200 Subject: [PATCH 04/26] inline ParseField as it's used in one place --- .../org/elasticsearch/client/license/PutLicenseResponse.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java index 1b50ef10eb950..d0e2fa469bb71 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java @@ -39,8 +39,6 @@ public final class PutLicenseResponse { - private static final ParseField ACKNOWLEDGED = new ParseField("acknowledged"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "put_license_response", true, (a, v) -> { boolean acknowledged = (Boolean) a[0]; @@ -55,7 +53,7 @@ public final class PutLicenseResponse { }); static { - PARSER.declareBoolean(constructorArg(), ACKNOWLEDGED); + PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged")); PARSER.declareString(constructorArg(), new ParseField("license_status")); PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { Map acknowledgeMessages = new HashMap<>(); From 7e778d718d8bc0d83a29827b5bd45c4e2ae296f0 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 16 Oct 2018 17:15:08 +0200 Subject: [PATCH 05/26] fill the gaps for LicensingIT --- .../test/java/org/elasticsearch/client/LicensingIT.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java index 10f9d9f5028e5..eb76825153629 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicensingIT.java @@ -19,6 +19,8 @@ package org.elasticsearch.client; import org.elasticsearch.Build; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.license.DeleteLicenseRequest; import org.elasticsearch.client.license.GetLicenseRequest; import org.elasticsearch.client.license.GetLicenseResponse; import org.elasticsearch.client.license.LicensesStatus; @@ -150,10 +152,12 @@ public void testGetLicense() throws Exception { } public void testPutLicense() throws Exception { - // TODO + putTrialLicense(); } public void testDeleteLicense() throws Exception { - // TODO + final DeleteLicenseRequest request = new DeleteLicenseRequest(); + final AcknowledgedResponse response = highLevelClient().license().deleteLicense(request, RequestOptions.DEFAULT); + assertThat(response.isAcknowledged(), equalTo(true)); } } From 3a7f138bd64d4d4df47c0444cdc88aa2b83f0228 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 16 Oct 2018 17:31:39 +0200 Subject: [PATCH 06/26] move protocol-test to another PR --- x-pack/protocol-test/build.gradle | 21 ---- .../xpack/license/LicenseStatusTests.java | 26 ---- .../license/PutLicenseResponseTests.java | 116 ------------------ 3 files changed, 163 deletions(-) delete mode 100644 x-pack/protocol-test/build.gradle delete mode 100644 x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java delete mode 100644 x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java diff --git a/x-pack/protocol-test/build.gradle b/x-pack/protocol-test/build.gradle deleted file mode 100644 index a1f174b2bc6c5..0000000000000 --- a/x-pack/protocol-test/build.gradle +++ /dev/null @@ -1,21 +0,0 @@ - -apply plugin: 'elasticsearch.build' - -group = 'org.elasticsearch.client' -archivesBaseName = 'x-pack-protocol-test' - -// due to clash of xpack and protocol classes -jarHell.enabled = false -// jarHell is there - run tests manually before clean up is done -test.enabled = false - -dependencies { - testCompile "org.elasticsearch.plugin:x-pack-core:${version}" - testCompile "org.elasticsearch.test:framework:${version}" - testCompile ("org.elasticsearch.client:elasticsearch-rest-high-level-client:${version}") { - exclude group: 'org.elasticsearch.plugin', module: 'protocol' - } - testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" - testCompile "junit:junit:${versions.junit}" - testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" -} \ No newline at end of file diff --git a/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java deleted file mode 100644 index f89e15a1f0920..0000000000000 --- a/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol.xpack.license; - -import org.elasticsearch.test.ESTestCase; - -import static org.hamcrest.CoreMatchers.equalTo; - -public class LicenseStatusTests extends ESTestCase { - public void testCompatibility() { - final LicenseStatus[] values = LicenseStatus.values(); - final org.elasticsearch.client.license.LicenseStatus[] ossValues = - org.elasticsearch.client.license.LicenseStatus.values(); - - assertThat(values.length, equalTo(ossValues.length)); - - for (LicenseStatus value : values) { - final org.elasticsearch.client.license.LicenseStatus licenseStatus = - org.elasticsearch.client.license.LicenseStatus.fromString(value.label()); - assertThat(licenseStatus.label(), equalTo(value.label())); - } - } -} diff --git a/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java deleted file mode 100644 index d168f65fbfd12..0000000000000 --- a/x-pack/protocol-test/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol.xpack.license; - -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; - -import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.function.Predicate; - -public class PutLicenseResponseTests extends AbstractStreamableXContentTestCase { - - @Override - protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { - // the key point - is to read by OSS version of class - final org.elasticsearch.client.license.PutLicenseResponse response = - org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); - return new PutLicenseResponse(response.isAcknowledged(), LicensesStatus.valueOf(response.status().name()), - response.acknowledgeHeader(), response.acknowledgeMessages()); - } - - @Override - protected boolean supportsUnknownFields() { - return true; - } - - @Override - protected Predicate getRandomFieldsExcludeFilter() { - // The structure of the response is such that unknown fields inside acknowledge cannot be supported since they - // are treated as messages from new services - return p -> p.startsWith("acknowledge"); - } - - @Override - protected PutLicenseResponse createTestInstance() { - boolean acknowledged = randomBoolean(); - LicensesStatus status = randomFrom(LicensesStatus.VALID, LicensesStatus.INVALID, LicensesStatus.EXPIRED); - String messageHeader; - Map ackMessages; - if (randomBoolean()) { - messageHeader = randomAlphaOfLength(10); - ackMessages = randomAckMessages(); - } else { - messageHeader = null; - ackMessages = Collections.emptyMap(); - } - - return new PutLicenseResponse(acknowledged, status, messageHeader, ackMessages); - } - - private static Map randomAckMessages() { - int nFeatures = randomIntBetween(1, 5); - - Map ackMessages = new HashMap<>(); - - for (int i = 0; i < nFeatures; i++) { - String feature = randomAlphaOfLengthBetween(9, 15); - int nMessages = randomIntBetween(1, 5); - String[] messages = new String[nMessages]; - for (int j = 0; j < nMessages; j++) { - messages[j] = randomAlphaOfLengthBetween(10, 30); - } - ackMessages.put(feature, messages); - } - - return ackMessages; - } - - @Override - protected PutLicenseResponse createBlankInstance() { - return new PutLicenseResponse(); - } - - @Override - protected PutLicenseResponse mutateInstance(PutLicenseResponse response) { - @SuppressWarnings("unchecked") - Function mutator = randomFrom( - r -> new PutLicenseResponse( - r.isAcknowledged() == false, - r.status(), - r.acknowledgeHeader(), - r.acknowledgeMessages()), - r -> new PutLicenseResponse( - r.isAcknowledged(), - mutateStatus(r.status()), - r.acknowledgeHeader(), - r.acknowledgeMessages()), - r -> { - if (r.acknowledgeMessages().isEmpty()) { - return new PutLicenseResponse( - r.isAcknowledged(), - r.status(), - randomAlphaOfLength(10), - randomAckMessages() - ); - } else { - return new PutLicenseResponse(r.isAcknowledged(), r.status()); - } - } - - ); - return mutator.apply(response); - } - - private LicensesStatus mutateStatus(LicensesStatus status) { - return randomValueOtherThan(status, () -> randomFrom(LicensesStatus.values())); - } - -} From 10c9bb86984b12071fc815f54f158ffb3f87ce07 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Wed, 17 Oct 2018 11:22:08 +0200 Subject: [PATCH 07/26] XPack/OSS request/response compatibility tests Relates to #34451 --- x-pack/response-tests/build.gradle | 44 +++++++ .../xpack/license/LicenseStatusTests.java | 26 ++++ .../license/PutLicenseResponseTests.java | 116 ++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 x-pack/response-tests/build.gradle create mode 100644 x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java create mode 100644 x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java diff --git a/x-pack/response-tests/build.gradle b/x-pack/response-tests/build.gradle new file mode 100644 index 0000000000000..5677ca9a25e60 --- /dev/null +++ b/x-pack/response-tests/build.gradle @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import org.elasticsearch.gradle.test.RestIntegTestTask +import org.gradle.api.internal.provider.Providers + +apply plugin: 'elasticsearch.build' + +group = 'org.elasticsearch.client' +archivesBaseName = 'response-tests' + +dependencies { + testCompile "org.elasticsearch.plugin:x-pack-core:${version}" + testCompile "org.elasticsearch.test:framework:${version}" + testCompile ("org.elasticsearch.client:elasticsearch-rest-high-level-client:${version}") { + exclude group: 'org.elasticsearch.plugin', module: 'protocol' + } + testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" + testCompile "junit:junit:${versions.junit}" + testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" +} + +// due to clash of xpack and protocol classes +jarHell.enabled = false +// jarHell is there - run tests manually before clean up is done +test.enabled = false + +// Not published so no need to assemble +assemble.enabled = true \ No newline at end of file diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java new file mode 100644 index 0000000000000..f89e15a1f0920 --- /dev/null +++ b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol.xpack.license; + +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class LicenseStatusTests extends ESTestCase { + public void testCompatibility() { + final LicenseStatus[] values = LicenseStatus.values(); + final org.elasticsearch.client.license.LicenseStatus[] ossValues = + org.elasticsearch.client.license.LicenseStatus.values(); + + assertThat(values.length, equalTo(ossValues.length)); + + for (LicenseStatus value : values) { + final org.elasticsearch.client.license.LicenseStatus licenseStatus = + org.elasticsearch.client.license.LicenseStatus.fromString(value.label()); + assertThat(licenseStatus.label(), equalTo(value.label())); + } + } +} diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java new file mode 100644 index 0000000000000..d168f65fbfd12 --- /dev/null +++ b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -0,0 +1,116 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol.xpack.license; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; +import java.util.function.Predicate; + +public class PutLicenseResponseTests extends AbstractStreamableXContentTestCase { + + @Override + protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { + // the key point - is to read by OSS version of class + final org.elasticsearch.client.license.PutLicenseResponse response = + org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); + return new PutLicenseResponse(response.isAcknowledged(), LicensesStatus.valueOf(response.status().name()), + response.acknowledgeHeader(), response.acknowledgeMessages()); + } + + @Override + protected boolean supportsUnknownFields() { + return true; + } + + @Override + protected Predicate getRandomFieldsExcludeFilter() { + // The structure of the response is such that unknown fields inside acknowledge cannot be supported since they + // are treated as messages from new services + return p -> p.startsWith("acknowledge"); + } + + @Override + protected PutLicenseResponse createTestInstance() { + boolean acknowledged = randomBoolean(); + LicensesStatus status = randomFrom(LicensesStatus.VALID, LicensesStatus.INVALID, LicensesStatus.EXPIRED); + String messageHeader; + Map ackMessages; + if (randomBoolean()) { + messageHeader = randomAlphaOfLength(10); + ackMessages = randomAckMessages(); + } else { + messageHeader = null; + ackMessages = Collections.emptyMap(); + } + + return new PutLicenseResponse(acknowledged, status, messageHeader, ackMessages); + } + + private static Map randomAckMessages() { + int nFeatures = randomIntBetween(1, 5); + + Map ackMessages = new HashMap<>(); + + for (int i = 0; i < nFeatures; i++) { + String feature = randomAlphaOfLengthBetween(9, 15); + int nMessages = randomIntBetween(1, 5); + String[] messages = new String[nMessages]; + for (int j = 0; j < nMessages; j++) { + messages[j] = randomAlphaOfLengthBetween(10, 30); + } + ackMessages.put(feature, messages); + } + + return ackMessages; + } + + @Override + protected PutLicenseResponse createBlankInstance() { + return new PutLicenseResponse(); + } + + @Override + protected PutLicenseResponse mutateInstance(PutLicenseResponse response) { + @SuppressWarnings("unchecked") + Function mutator = randomFrom( + r -> new PutLicenseResponse( + r.isAcknowledged() == false, + r.status(), + r.acknowledgeHeader(), + r.acknowledgeMessages()), + r -> new PutLicenseResponse( + r.isAcknowledged(), + mutateStatus(r.status()), + r.acknowledgeHeader(), + r.acknowledgeMessages()), + r -> { + if (r.acknowledgeMessages().isEmpty()) { + return new PutLicenseResponse( + r.isAcknowledged(), + r.status(), + randomAlphaOfLength(10), + randomAckMessages() + ); + } else { + return new PutLicenseResponse(r.isAcknowledged(), r.status()); + } + } + + ); + return mutator.apply(response); + } + + private LicensesStatus mutateStatus(LicensesStatus status) { + return randomValueOtherThan(status, () -> randomFrom(LicensesStatus.values())); + } + +} From 2f0015b0b9063b768055e3e7fc5b2abe7f362e0a Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Wed, 17 Oct 2018 16:16:03 +0200 Subject: [PATCH 08/26] AbstractHLRCStreamableXContentTestCase is added --- ...bstractHLRCStreamableXContentTestCase.java | 47 +++++++++++++++++++ .../xpack/license/LicenseStatusTests.java | 4 +- .../license/PutLicenseResponseTests.java | 21 ++++++--- 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java new file mode 100644 index 0000000000000..a1effb0ccc9bc --- /dev/null +++ b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java @@ -0,0 +1,47 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.protocol.xpack.license; + +import org.elasticsearch.common.io.stream.Streamable; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +public abstract class AbstractHLRCStreamableXContentTestCase + extends AbstractStreamableXContentTestCase { + + /** + * Generic test that creates new instance of HLRC request/response from the test instance and checks + * both for equality and asserts equality on the two queries. + */ + public final void testHLRCFromXContent() throws IOException { + AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, this::createTestInstance, supportsUnknownFields(), + getShuffleFieldsExceptions(), getRandomFieldsExcludeFilter(), this::createParser, + p -> convert(doHLRCParseInstance(p)), + this::assertEqualInstances, true, getToXContentParams()); + } + + public abstract H doHLRCParseInstance(XContentParser parser) throws IOException; + + public abstract T convert(H instance); + +} diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java index f89e15a1f0920..a424a30383d13 100644 --- a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java +++ b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java @@ -12,10 +12,10 @@ public class LicenseStatusTests extends ESTestCase { public void testCompatibility() { final LicenseStatus[] values = LicenseStatus.values(); - final org.elasticsearch.client.license.LicenseStatus[] ossValues = + final org.elasticsearch.client.license.LicenseStatus[] hlrcValues = org.elasticsearch.client.license.LicenseStatus.values(); - assertThat(values.length, equalTo(ossValues.length)); + assertThat(values.length, equalTo(hlrcValues.length)); for (LicenseStatus value : values) { final org.elasticsearch.client.license.LicenseStatus licenseStatus = diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java index d168f65fbfd12..5eb3f1d18c298 100644 --- a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.protocol.xpack.license; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; import java.io.IOException; import java.util.Collections; @@ -15,15 +14,23 @@ import java.util.function.Function; import java.util.function.Predicate; -public class PutLicenseResponseTests extends AbstractStreamableXContentTestCase { +public class PutLicenseResponseTests extends AbstractHLRCStreamableXContentTestCase { @Override protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { - // the key point - is to read by OSS version of class - final org.elasticsearch.client.license.PutLicenseResponse response = - org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); - return new PutLicenseResponse(response.isAcknowledged(), LicensesStatus.valueOf(response.status().name()), - response.acknowledgeHeader(), response.acknowledgeMessages()); + return PutLicenseResponse.fromXContent(parser); + } + + @Override + public org.elasticsearch.client.license.PutLicenseResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); + } + + @Override + public PutLicenseResponse convert(org.elasticsearch.client.license.PutLicenseResponse instance) { + return new PutLicenseResponse(instance.isAcknowledged(), LicensesStatus.valueOf(instance.status().name()), + instance.acknowledgeHeader(), instance.acknowledgeMessages()); } @Override From a12a507e12ad2df639527bbc938d2da1d294457c Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Fri, 19 Oct 2018 13:40:28 +0200 Subject: [PATCH 09/26] moved ProtocolUtils from protocol to client --- .../java/org/elasticsearch/client}/common/ProtocolUtils.java | 2 +- .../org/elasticsearch/client/license/PutLicenseResponse.java | 2 +- .../org/elasticsearch/client}/common/ProtocolUtilsTests.java | 2 +- .../elasticsearch/client/license/StartBasicResponseTests.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/common/ProtocolUtils.java (97%) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/test/java/org/elasticsearch/client}/common/ProtocolUtilsTests.java (98%) diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/common/ProtocolUtils.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/common/ProtocolUtils.java similarity index 97% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/common/ProtocolUtils.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/common/ProtocolUtils.java index e135cdc50e926..9181f4f0c0bbd 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/common/ProtocolUtils.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/common/ProtocolUtils.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.protocol.xpack.common; +package org.elasticsearch.client.common; import java.util.Arrays; import java.util.Map; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java index d0e2fa469bb71..107aec76f974b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java @@ -24,7 +24,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentParseException; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.common.ProtocolUtils; +import org.elasticsearch.client.common.ProtocolUtils; import java.io.IOException; import java.util.ArrayList; diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java similarity index 98% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java index 214708327388f..8402367390263 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.protocol.xpack.common; +package org.elasticsearch.client.common; import org.elasticsearch.test.ESTestCase; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java index 0679af8b6fdc8..8370a6ba9afed 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java @@ -21,7 +21,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.common.ProtocolUtils; +import org.elasticsearch.client.common.ProtocolUtils; import org.elasticsearch.test.ESTestCase; import java.io.IOException; From ad85b75160cf1b30343af60b91da5654447e01c5 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Fri, 19 Oct 2018 13:13:16 +0200 Subject: [PATCH 10/26] HLRC XPack Protocol clean up: Migration; Graph; Watcher Drop protocol module Relates #34451 --- client/rest-high-level/build.gradle | 1 - .../org/elasticsearch/client/GraphClient.java | 4 +- .../client/GraphRequestConverters.java | 2 +- .../elasticsearch/client/MigrationClient.java | 4 +- .../client/MigrationRequestConverters.java | 4 +- .../elasticsearch/client/WatcherClient.java | 8 +- .../client/WatcherRequestConverters.java | 20 +- .../client}/graph/Connection.java | 23 +- .../client}/graph/GraphExploreRequest.java | 70 +----- .../client}/graph/GraphExploreResponse.java | 74 +------ .../org/elasticsearch/client}/graph/Hop.java | 41 +--- .../elasticsearch/client}/graph/Vertex.java | 17 +- .../client}/graph/VertexRequest.java | 61 +----- .../client}/graph/package-info.java | 2 +- .../migration/IndexUpgradeInfoRequest.java | 33 +-- .../migration/IndexUpgradeInfoResponse.java | 46 +--- .../migration/UpgradeActionRequired.java | 2 +- .../client}/migration/package-info.java | 2 +- .../client/watcher/AckWatchRequest.java | 1 - .../client/watcher/ActivateWatchRequest.java | 1 - .../client}/watcher/DeleteWatchRequest.java | 39 +--- .../client}/watcher/DeleteWatchResponse.java | 23 +- .../client}/watcher/PutWatchRequest.java | 51 ++--- .../client}/watcher/PutWatchResponse.java | 30 +-- .../client}/watcher/package-info.java | 2 +- .../client/GrapRequestConvertersTests.java | 23 +- .../org/elasticsearch/client/GraphIT.java | 12 +- .../org/elasticsearch/client/MigrationIT.java | 4 +- .../MigrationRequestConvertersTests.java | 4 +- .../org/elasticsearch/client/WatcherIT.java | 8 +- .../client/WatcherRequestConvertersTests.java | 4 +- .../documentation/GraphDocumentationIT.java | 12 +- .../MigrationClientDocumentationIT.java | 6 +- .../documentation/WatcherDocumentationIT.java | 8 +- .../graph/GraphExploreResponseTests.java | 4 +- .../IndexUpgradeInfoRequestTests.java | 15 +- .../IndexUpgradeInfoResponseTests.java | 11 +- .../watcher/DeleteWatchResponseTests.java | 2 +- .../watcher/PutWatchResponseTests.java | 2 +- .../watcher/WatchRequestValidationTests.java | 44 ++-- x-pack/build.gradle | 20 +- x-pack/protocol/LICENSE.txt | 202 ------------------ .../IndexUpgradeInfoRequestTests.java | 50 ----- .../IndexUpgradeInfoResponseTests.java | 68 ------ 44 files changed, 176 insertions(+), 884 deletions(-) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/Connection.java (89%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/GraphExploreRequest.java (82%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/GraphExploreResponse.java (77%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/Hop.java (76%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/Vertex.java (93%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/VertexRequest.java (78%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/graph/package-info.java (94%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/migration/IndexUpgradeInfoRequest.java (65%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/migration/IndexUpgradeInfoResponse.java (68%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/migration/UpgradeActionRequired.java (97%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/migration/package-info.java (94%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/DeleteWatchRequest.java (56%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/DeleteWatchResponse.java (81%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/PutWatchRequest.java (64%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/PutWatchResponse.java (74%) rename {x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/package-info.java (94%) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/test/java/org/elasticsearch/client}/graph/GraphExploreResponseTests.java (98%) rename x-pack/protocol/build.gradle => client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java (63%) rename x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/package-info.java => client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java (79%) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/test/java/org/elasticsearch/client}/watcher/DeleteWatchResponseTests.java (96%) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/test/java/org/elasticsearch/client}/watcher/PutWatchResponseTests.java (96%) delete mode 100644 x-pack/protocol/LICENSE.txt delete mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequestTests.java delete mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java diff --git a/client/rest-high-level/build.gradle b/client/rest-high-level/build.gradle index b3a8da10eb610..af6c025b38718 100644 --- a/client/rest-high-level/build.gradle +++ b/client/rest-high-level/build.gradle @@ -51,7 +51,6 @@ dependencies { compile "org.elasticsearch.plugin:aggs-matrix-stats-client:${version}" compile "org.elasticsearch.plugin:rank-eval-client:${version}" compile "org.elasticsearch.plugin:lang-mustache-client:${version}" - bundle project(':x-pack:protocol') testCompile "org.elasticsearch.client:test:${version}" testCompile "org.elasticsearch.test:framework:${version}" diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java index 5099bf8d51d32..3d5365fedde5a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java @@ -20,8 +20,8 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; -import org.elasticsearch.protocol.xpack.graph.GraphExploreResponse; +import org.elasticsearch.client.graph.GraphExploreRequest; +import org.elasticsearch.client.graph.GraphExploreResponse; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphRequestConverters.java index c1f1e1d115f15..32a8b6ebb9341 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphRequestConverters.java @@ -20,7 +20,7 @@ package org.elasticsearch.client; import org.apache.http.client.methods.HttpGet; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; +import org.elasticsearch.client.graph.GraphExploreRequest; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java index 8717943d79718..15bf71bc8db8f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java @@ -19,8 +19,8 @@ package org.elasticsearch.client; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoResponse; +import org.elasticsearch.client.migration.IndexUpgradeInfoRequest; +import org.elasticsearch.client.migration.IndexUpgradeInfoResponse; import java.io.IOException; import java.util.Collections; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java index 2f5309350df42..f9b7fab7d9d1a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java @@ -20,13 +20,13 @@ package org.elasticsearch.client; import org.apache.http.client.methods.HttpGet; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; +import org.elasticsearch.client.migration.IndexUpgradeInfoRequest; public class MigrationRequestConverters { static Request getMigrationAssistance(IndexUpgradeInfoRequest indexUpgradeInfoRequest) { RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack/migration/assistance") + .addPathPartAsIs("_xpack", "migration", "assistance") .addCommaSeparatedPathParts(indexUpgradeInfoRequest.indices()); String endpoint = endpointBuilder.build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java index cfa5e6e61fa1f..11a26ee3e0efa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java @@ -23,10 +23,10 @@ import org.elasticsearch.client.watcher.ActivateWatchResponse; import org.elasticsearch.client.watcher.AckWatchRequest; import org.elasticsearch.client.watcher.AckWatchResponse; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchResponse; +import org.elasticsearch.client.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.PutWatchResponse; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java index 3a17056f9bf4c..650e3ba0bb1cb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java @@ -26,16 +26,14 @@ import org.elasticsearch.client.watcher.ActivateWatchRequest; import org.elasticsearch.client.watcher.AckWatchRequest; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.PutWatchRequest; public class WatcherRequestConverters { static Request putWatch(PutWatchRequest putWatchRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("watcher") - .addPathPartAsIs("watch") + .addPathPartAsIs("_xpack", "watcher", "watch") .addPathPart(putWatchRequest.getId()) .build(); @@ -52,9 +50,7 @@ static Request putWatch(PutWatchRequest putWatchRequest) { static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("watcher") - .addPathPartAsIs("watch") + .addPathPartAsIs("_xpack", "watcher", "watch") .addPathPart(deleteWatchRequest.getId()) .build(); @@ -64,9 +60,7 @@ static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) { public static Request ackWatch(AckWatchRequest ackWatchRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("watcher") - .addPathPartAsIs("watch") + .addPathPartAsIs("_xpack", "watcher", "watch") .addPathPart(ackWatchRequest.getWatchId()) .addPathPartAsIs("_ack") .addCommaSeparatedPathParts(ackWatchRequest.getActionIds()) @@ -77,9 +71,7 @@ public static Request ackWatch(AckWatchRequest ackWatchRequest) { static Request activateWatch(ActivateWatchRequest activateWatchRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("watcher") - .addPathPartAsIs("watch") + .addPathPartAsIs("_xpack", "watcher", "watch") .addPathPart(activateWatchRequest.getWatchId()) .addPathPartAsIs("_activate") .build(); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java similarity index 89% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java index 455434f7ac4a9..07aff3888d260 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java @@ -16,22 +16,19 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; import com.carrotsearch.hppc.ObjectIntHashMap; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContent.Params; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.graph.Vertex.VertexId; +import org.elasticsearch.client.graph.Vertex.VertexId; import java.io.IOException; import java.util.List; -import java.util.Map; import java.util.Objects; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; @@ -55,25 +52,9 @@ public Connection(Vertex from, Vertex to, double weight, long docCount) { this.docCount = docCount; } - public Connection(StreamInput in, Map vertices) throws IOException { - from = vertices.get(new VertexId(in.readString(), in.readString())); - to = vertices.get(new VertexId(in.readString(), in.readString())); - weight = in.readDouble(); - docCount = in.readVLong(); - } - Connection() { } - void writeTo(StreamOutput out) throws IOException { - out.writeString(from.getField()); - out.writeString(from.getTerm()); - out.writeString(to.getField()); - out.writeString(to.getTerm()); - out.writeDouble(weight); - out.writeVLong(docCount); - } - public ConnectionId getId() { return new ConnectionId(from.getId(), to.getId()); } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java similarity index 82% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java index 495ea5fd28ac3..4d2a000a00c89 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java @@ -16,13 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; -import org.elasticsearch.action.ActionRequest; -import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; -import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.client.Validatable; +import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -37,14 +36,14 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Iterator; import java.util.List; +import java.util.Optional; /** * Holds the criteria required to guide the exploration of connected terms which * can be returned as a graph. */ -public class GraphExploreRequest extends ActionRequest implements IndicesRequest.Replaceable, ToXContentObject { +public class GraphExploreRequest implements IndicesRequest.Replaceable, ToXContentObject, Validatable { public static final String NO_HOPS_ERROR_MESSAGE = "Graph explore request must have at least one hop"; public static final String NO_VERTICES_ERROR_MESSAGE = "Graph explore hop must have at least one VertexRequest"; @@ -74,15 +73,15 @@ public GraphExploreRequest(String... indices) { } @Override - public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = null; + public Optional validate() { + ValidationException validationException = new ValidationException(); if (hops.size() == 0) { - validationException = ValidateActions.addValidationError(NO_HOPS_ERROR_MESSAGE, validationException); + validationException.addValidationError(NO_HOPS_ERROR_MESSAGE); } for (Hop hop : hops) { - validationException = hop.validate(validationException); + hop.validate(validationException); } - return validationException; + return validationException.validationErrors().isEmpty() ? Optional.empty() : Optional.of(validationException); } @Override @@ -159,55 +158,6 @@ public GraphExploreRequest timeout(String timeout) { return this; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - - indices = in.readStringArray(); - indicesOptions = IndicesOptions.readIndicesOptions(in); - types = in.readStringArray(); - routing = in.readOptionalString(); - timeout = in.readOptionalTimeValue(); - sampleSize = in.readInt(); - sampleDiversityField = in.readOptionalString(); - maxDocsPerDiversityValue = in.readInt(); - - useSignificance = in.readBoolean(); - returnDetailedInfo = in.readBoolean(); - - int numHops = in.readInt(); - Hop parentHop = null; - for (int i = 0; i < numHops; i++) { - Hop hop = new Hop(parentHop); - hop.readFrom(in); - hops.add(hop); - parentHop = hop; - } - - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeStringArray(indices); - indicesOptions.writeIndicesOptions(out); - out.writeStringArray(types); - out.writeOptionalString(routing); - out.writeOptionalTimeValue(timeout); - - out.writeInt(sampleSize); - out.writeOptionalString(sampleDiversityField); - out.writeInt(maxDocsPerDiversityValue); - - out.writeBoolean(useSignificance); - out.writeBoolean(returnDetailedInfo); - out.writeInt(hops.size()); - for (Iterator iterator = hops.iterator(); iterator.hasNext();) { - Hop hop = iterator.next(); - hop.writeTo(out); - } - } - @Override public String toString() { return "graph explore [" + Arrays.toString(indices) + "][" + Arrays.toString(types) + "]"; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java similarity index 77% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java index baaaedf0163ed..2b5d1c7ecf4be 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java @@ -16,24 +16,21 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; import com.carrotsearch.hppc.ObjectIntHashMap; -import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; 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.protocol.xpack.graph.Connection.ConnectionId; -import org.elasticsearch.protocol.xpack.graph.Connection.UnresolvedConnection; -import org.elasticsearch.protocol.xpack.graph.Vertex.VertexId; +import org.elasticsearch.client.graph.Connection.ConnectionId; +import org.elasticsearch.client.graph.Connection.UnresolvedConnection; +import org.elasticsearch.client.graph.Vertex.VertexId; import java.io.IOException; import java.util.Collection; @@ -41,7 +38,6 @@ import java.util.List; import java.util.Map; -import static org.elasticsearch.action.search.ShardSearchFailure.readShardSearchFailure; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; @@ -51,7 +47,7 @@ * * @see GraphExploreRequest */ -public class GraphExploreResponse extends ActionResponse implements ToXContentObject { +public class GraphExploreResponse implements ToXContentObject { private long tookInMillis; private boolean timedOut = false; @@ -94,41 +90,6 @@ public ShardOperationFailedException[] getShardFailures() { return shardFailures; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - tookInMillis = in.readVLong(); - timedOut = in.readBoolean(); - - int size = in.readVInt(); - if (size == 0) { - shardFailures = ShardSearchFailure.EMPTY_ARRAY; - } else { - shardFailures = new ShardSearchFailure[size]; - for (int i = 0; i < shardFailures.length; i++) { - shardFailures[i] = readShardSearchFailure(in); - } - } - // read vertices - size = in.readVInt(); - vertices = new HashMap<>(); - for (int i = 0; i < size; i++) { - Vertex n = Vertex.readFrom(in); - vertices.put(n.getId(), n); - } - - size = in.readVInt(); - - connections = new HashMap<>(); - for (int i = 0; i < size; i++) { - Connection e = new Connection(in, vertices); - connections.put(e.getId(), e); - } - - returnDetailedInfo = in.readBoolean(); - - } - public Collection getConnections() { return connections.values(); } @@ -141,31 +102,6 @@ public Vertex getVertex(VertexId id) { return vertices.get(id); } - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeVLong(tookInMillis); - out.writeBoolean(timedOut); - - out.writeVInt(shardFailures.length); - for (ShardOperationFailedException shardSearchFailure : shardFailures) { - shardSearchFailure.writeTo(out); - } - - out.writeVInt(vertices.size()); - for (Vertex vertex : vertices.values()) { - vertex.writeTo(out); - } - - out.writeVInt(connections.size()); - for (Connection connection : connections.values()) { - connection.writeTo(out); - } - - out.writeBoolean(returnDetailedInfo); - - } - private static final ParseField TOOK = new ParseField("took"); private static final ParseField TIMED_OUT = new ParseField("timed_out"); private static final ParseField VERTICES = new ParseField("vertices"); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java similarity index 76% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java index 70ec61067f5b8..83196aada7061 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java @@ -16,12 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.ValidateActions; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; @@ -56,7 +53,7 @@ *

* */ -public class Hop implements ToXContentFragment{ +public class Hop implements ToXContentFragment { final Hop parentHop; List vertices = null; QueryBuilder guidingQuery = null; @@ -65,44 +62,16 @@ public Hop(Hop parent) { this.parentHop = parent; } - public ActionRequestValidationException validate(ActionRequestValidationException validationException) { - + public void validate(ValidationException validationException) { if (getEffectiveVertexRequests().size() == 0) { - validationException = ValidateActions.addValidationError(GraphExploreRequest.NO_VERTICES_ERROR_MESSAGE, validationException); + validationException.addValidationError(GraphExploreRequest.NO_VERTICES_ERROR_MESSAGE); } - return validationException; - } public Hop getParentHop() { return parentHop; } - void writeTo(StreamOutput out) throws IOException { - out.writeOptionalNamedWriteable(guidingQuery); - if (vertices == null) { - out.writeVInt(0); - } else { - out.writeVInt(vertices.size()); - for (VertexRequest vr : vertices) { - vr.writeTo(out); - } - } - } - - void readFrom(StreamInput in) throws IOException { - guidingQuery = in.readOptionalNamedWriteable(QueryBuilder.class); - int size = in.readVInt(); - if (size > 0) { - vertices = new ArrayList<>(); - for (int i = 0; i < size; i++) { - VertexRequest vr = new VertexRequest(); - vr.readFrom(in); - vertices.add(vr); - } - } - } - public QueryBuilder guidingQuery() { if (guidingQuery != null) { return guidingQuery; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java similarity index 93% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java index cfc26f44fac04..852372209da9c 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java @@ -16,11 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -64,19 +62,6 @@ public Vertex(String field, String term, double weight, int depth, long bg, long this.bg = bg; this.fg = fg; } - - static Vertex readFrom(StreamInput in) throws IOException { - return new Vertex(in.readString(), in.readString(), in.readDouble(), in.readVInt(), in.readVLong(), in.readVLong()); - } - - void writeTo(StreamOutput out) throws IOException { - out.writeString(field); - out.writeString(term); - out.writeDouble(weight); - out.writeVInt(depth); - out.writeVLong(bg); - out.writeVLong(fg); - } @Override public int hashCode() { diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java similarity index 78% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java index 116497fe2301c..4947244a6c8bd 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java @@ -16,13 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest.TermBoost; +import org.elasticsearch.client.graph.GraphExploreRequest.TermBoost; import java.io.IOException; import java.util.HashMap; @@ -52,57 +50,6 @@ public VertexRequest() { } - void readFrom(StreamInput in) throws IOException { - fieldName = in.readString(); - size = in.readVInt(); - minDocCount = in.readVInt(); - shardMinDocCount = in.readVInt(); - - int numIncludes = in.readVInt(); - if (numIncludes > 0) { - includes = new HashMap<>(); - for (int i = 0; i < numIncludes; i++) { - TermBoost tb = new TermBoost(); - tb.readFrom(in); - includes.put(tb.term, tb); - } - } - - int numExcludes = in.readVInt(); - if (numExcludes > 0) { - excludes = new HashSet<>(); - for (int i = 0; i < numExcludes; i++) { - excludes.add(in.readString()); - } - } - - } - - void writeTo(StreamOutput out) throws IOException { - out.writeString(fieldName); - out.writeVInt(size); - out.writeVInt(minDocCount); - out.writeVInt(shardMinDocCount); - - if (includes != null) { - out.writeVInt(includes.size()); - for (TermBoost tb : includes.values()) { - tb.writeTo(out); - } - } else { - out.writeVInt(0); - } - - if (excludes != null) { - out.writeVInt(excludes.size()); - for (String term : excludes) { - out.writeString(term); - } - } else { - out.writeVInt(0); - } - } - public String fieldName() { return fieldName; } @@ -224,7 +171,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (shardMinDocCount != DEFAULT_SHARD_MIN_DOC_COUNT) { builder.field("shard_min_doc_count", shardMinDocCount); } - if(includes!=null) { + if (includes != null) { builder.startArray("include"); for (TermBoost tb : includes.values()) { builder.startObject(); @@ -234,7 +181,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } builder.endArray(); } - if(excludes!=null) { + if (excludes != null) { builder.startArray("exclude"); for (String value : excludes) { builder.value(value); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/package-info.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/package-info.java similarity index 94% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/package-info.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/graph/package-info.java index f4f666074a118..27cfb29d381d9 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/graph/package-info.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/package-info.java @@ -21,4 +21,4 @@ * Request and Response objects for the default distribution's Graph * APIs. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequest.java similarity index 65% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequest.java index ae26bc4de8d0e..fb37a449435f4 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequest.java @@ -16,21 +16,17 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.migration; +package org.elasticsearch.client.migration; -import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.action.support.master.MasterNodeReadRequest; +import org.elasticsearch.client.TimedRequest; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import java.io.IOException; import java.util.Arrays; import java.util.Objects; -public class IndexUpgradeInfoRequest extends MasterNodeReadRequest implements IndicesRequest.Replaceable { +public class IndexUpgradeInfoRequest extends TimedRequest implements IndicesRequest.Replaceable { private String[] indices = Strings.EMPTY_ARRAY; private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, true, true, true); @@ -39,19 +35,6 @@ public IndexUpgradeInfoRequest(String... indices) { indices(indices); } - public IndexUpgradeInfoRequest(StreamInput in) throws IOException { - super(in); - indices = in.readStringArray(); - indicesOptions = IndicesOptions.readIndicesOptions(in); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeStringArray(indices); - indicesOptions.writeIndicesOptions(out); - } - @Override public String[] indices() { return indices; @@ -72,16 +55,6 @@ public void indicesOptions(IndicesOptions indicesOptions) { this.indicesOptions = indicesOptions; } - @Override - public ActionRequestValidationException validate() { - return null; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponse.java similarity index 68% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponse.java index 4c1208f960ebd..a9af1e36cc258 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponse.java @@ -16,25 +16,19 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.migration; +package org.elasticsearch.client.migration; -import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; 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 java.io.IOException; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; -public class IndexUpgradeInfoResponse extends ActionResponse implements ToXContentObject { +public class IndexUpgradeInfoResponse { private static final ParseField INDICES = new ParseField("indices"); private static final ParseField ACTION_REQUIRED = new ParseField("action_required"); @@ -70,50 +64,16 @@ public class IndexUpgradeInfoResponse extends ActionResponse implements ToXConte } - private Map actions; - - public IndexUpgradeInfoResponse() { - - } + private final Map actions; public IndexUpgradeInfoResponse(Map actions) { this.actions = actions; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - actions = in.readMap(StreamInput::readString, UpgradeActionRequired::readFromStream); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeMap(actions, StreamOutput::writeString, (out1, value) -> value.writeTo(out1)); - } - public Map getActions() { return actions; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - { - builder.startObject(INDICES.getPreferredName()); - for (Map.Entry entry : actions.entrySet()) { - builder.startObject(entry.getKey()); - { - builder.field(ACTION_REQUIRED.getPreferredName(), entry.getValue().toString()); - } - builder.endObject(); - } - builder.endObject(); - } - builder.endObject(); - return builder; - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/UpgradeActionRequired.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java similarity index 97% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/UpgradeActionRequired.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java index c87e37be7a55a..e743d10529e1a 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/UpgradeActionRequired.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.migration; +package org.elasticsearch.client.migration; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/package-info.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/package-info.java similarity index 94% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/package-info.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/migration/package-info.java index 12dc8eebc1773..dcb29a3776e3c 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/migration/package-info.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/package-info.java @@ -21,4 +21,4 @@ * Request and Response objects for the default distribution's Migration * APIs. */ -package org.elasticsearch.protocol.xpack.migration; +package org.elasticsearch.client.migration; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java index 1381544744dae..8c36caf9dbd5b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java @@ -21,7 +21,6 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import java.util.Locale; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java index 7f2849ff39c0c..e242ea4f20ba9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java @@ -20,7 +20,6 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import java.util.Objects; diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java similarity index 56% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java index 1ec83a8c05ae7..6009b70ffcb55 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java @@ -16,21 +16,18 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; -import org.elasticsearch.action.ActionRequest; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.ValidateActions; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.client.Validatable; +import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.lucene.uid.Versions; -import java.io.IOException; +import java.util.Optional; /** * A delete watch request to delete an watch by name (id) */ -public class DeleteWatchRequest extends ActionRequest { +public class DeleteWatchRequest implements Validatable { private String id; private long version = Versions.MATCH_ANY; @@ -58,28 +55,14 @@ public void setId(String id) { } @Override - public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = null; - if (id == null){ - validationException = ValidateActions.addValidationError("watch id is missing", validationException); + public Optional validate() { + ValidationException exception = new ValidationException(); + if (id == null) { + exception.addValidationError("watch id is missing"); } else if (PutWatchRequest.isValidId(id) == false) { - validationException = ValidateActions.addValidationError("watch id contains whitespace", validationException); + exception.addValidationError("watch id contains whitespace"); } - return validationException; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - id = in.readString(); - version = in.readLong(); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeString(id); - out.writeLong(version); + return exception.validationErrors().isEmpty() ? Optional.empty() : Optional.of(exception); } @Override diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java similarity index 81% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java index b644a6a854cfa..4e946ad459cf8 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java @@ -16,12 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; -import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -30,7 +27,7 @@ import java.io.IOException; import java.util.Objects; -public class DeleteWatchResponse extends ActionResponse implements ToXContentObject { +public class DeleteWatchResponse implements ToXContentObject { private static final ObjectParser PARSER = new ObjectParser<>("x_pack_delete_watch_response", DeleteWatchResponse::new); @@ -92,22 +89,6 @@ public int hashCode() { return Objects.hash(id, version, found); } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - id = in.readString(); - version = in.readVLong(); - found = in.readBoolean(); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeString(id); - out.writeVLong(version); - out.writeBoolean(found); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java similarity index 64% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java index 0bfa7dc7d343f..0601d457503a4 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java @@ -16,26 +16,23 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; -import org.elasticsearch.action.ActionRequest; -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.ValidateActions; +import org.elasticsearch.client.Validatable; +import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.xcontent.XContentType; -import java.io.IOException; +import java.util.Optional; import java.util.regex.Pattern; /** * This request class contains the data needed to create a watch along with the name of the watch. * The name of the watch will become the ID of the indexed document. */ -public final class PutWatchRequest extends ActionRequest { +public final class PutWatchRequest implements Validatable { private static final Pattern NO_WS_PATTERN = Pattern.compile("\\S+"); @@ -47,36 +44,12 @@ public final class PutWatchRequest extends ActionRequest { public PutWatchRequest() {} - public PutWatchRequest(StreamInput in) throws IOException { - readFrom(in); - } - public PutWatchRequest(String id, BytesReference source, XContentType xContentType) { this.id = id; this.source = source; this.xContentType = xContentType; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - id = in.readString(); - source = in.readBytesReference(); - active = in.readBoolean(); - xContentType = in.readEnum(XContentType.class); - version = in.readZLong(); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeString(id); - out.writeBytesReference(source); - out.writeBoolean(active); - out.writeEnum(xContentType); - out.writeZLong(version); - } - /** * @return The name that will be the ID of the indexed document */ @@ -136,20 +109,20 @@ public void setVersion(long version) { } @Override - public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = null; + public Optional validate() { + ValidationException exception = new ValidationException(); if (id == null) { - validationException = ValidateActions.addValidationError("watch id is missing", validationException); + exception.addValidationError("watch id is missing"); } else if (isValidId(id) == false) { - validationException = ValidateActions.addValidationError("watch id contains whitespace", validationException); + exception.addValidationError("watch id contains whitespace"); } if (source == null) { - validationException = ValidateActions.addValidationError("watch source is missing", validationException); + exception.addValidationError("watch source is missing"); } if (xContentType == null) { - validationException = ValidateActions.addValidationError("request body is missing", validationException); + exception.addValidationError("request body is missing"); } - return validationException; + return exception.validationErrors().isEmpty() ? Optional.empty() : Optional.of(exception); } public static boolean isValidId(String id) { diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java similarity index 74% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java index 98467f32dd860..5c8d7bde9b158 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java @@ -16,12 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; -import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -30,14 +27,15 @@ import java.io.IOException; import java.util.Objects; -public class PutWatchResponse extends ActionResponse implements ToXContentObject { +public class PutWatchResponse implements ToXContentObject { private static final ObjectParser PARSER = new ObjectParser<>("x_pack_put_watch_response", PutWatchResponse::new); + static { - PARSER.declareString(PutWatchResponse::setId, new ParseField("_id")); - PARSER.declareLong(PutWatchResponse::setVersion, new ParseField("_version")); - PARSER.declareBoolean(PutWatchResponse::setCreated, new ParseField("created")); + PARSER.declareString(PutWatchResponse::setId, new ParseField("_id")); + PARSER.declareLong(PutWatchResponse::setVersion, new ParseField("_version")); + PARSER.declareBoolean(PutWatchResponse::setCreated, new ParseField("created")); } private String id; @@ -92,22 +90,6 @@ public int hashCode() { return Objects.hash(id, version, created); } - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeString(id); - out.writeVLong(version); - out.writeBoolean(created); - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - id = in.readString(); - version = in.readVLong(); - created = in.readBoolean(); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/package-info.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/package-info.java similarity index 94% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/package-info.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/package-info.java index d34fd598ab170..cadd7d7558fd8 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/watcher/package-info.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/package-info.java @@ -21,4 +21,4 @@ * Request and Response objects for the default distribution's Watcher * APIs. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GrapRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GrapRequestConvertersTests.java index 6598800d76edb..965f1f627af7d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GrapRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GrapRequestConvertersTests.java @@ -24,17 +24,16 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; -import org.elasticsearch.protocol.xpack.graph.Hop; +import org.elasticsearch.client.graph.GraphExploreRequest; +import org.elasticsearch.client.graph.Hop; import org.elasticsearch.test.ESTestCase; -import org.junit.Assert; import java.util.HashMap; import java.util.Map; import static org.hamcrest.Matchers.is; -public class GrapRequestConvertersTests extends ESTestCase{ +public class GrapRequestConvertersTests extends ESTestCase { public void testGraphExplore() throws Exception { Map expectedParams = new HashMap<>(); @@ -43,14 +42,14 @@ public void testGraphExplore() throws Exception { graphExploreRequest.sampleDiversityField("diversity"); graphExploreRequest.indices("index1", "index2"); graphExploreRequest.types("type1", "type2"); - int timeout = ESTestCase.randomIntBetween(10000, 20000); + int timeout = randomIntBetween(10000, 20000); graphExploreRequest.timeout(TimeValue.timeValueMillis(timeout)); - graphExploreRequest.useSignificance(ESTestCase.randomBoolean()); - int numHops = ESTestCase.randomIntBetween(1, 5); + graphExploreRequest.useSignificance(randomBoolean()); + int numHops = randomIntBetween(1, 5); for (int i = 0; i < numHops; i++) { int hopNumber = i + 1; QueryBuilder guidingQuery = null; - if (ESTestCase.randomBoolean()) { + if (randomBoolean()) { guidingQuery = new TermQueryBuilder("field" + hopNumber, "value" + hopNumber); } Hop hop = graphExploreRequest.createNextHop(guidingQuery); @@ -58,10 +57,10 @@ public void testGraphExplore() throws Exception { hop.getVertexRequest(0).addInclude("value" + hopNumber, hopNumber); } Request request = GraphRequestConverters.explore(graphExploreRequest); - Assert.assertEquals(HttpGet.METHOD_NAME, request.getMethod()); - Assert.assertEquals("/index1,index2/type1,type2/_xpack/graph/_explore", request.getEndpoint()); - Assert.assertEquals(expectedParams, request.getParameters()); - Assert.assertThat(request.getEntity().getContentType().getValue(), is(XContentType.JSON.mediaTypeWithoutParameters())); + assertEquals(HttpGet.METHOD_NAME, request.getMethod()); + assertEquals("/index1,index2/type1,type2/_xpack/graph/_explore", request.getEndpoint()); + assertEquals(expectedParams, request.getParameters()); + assertThat(request.getEntity().getContentType().getValue(), is(XContentType.JSON.mediaTypeWithoutParameters())); RequestConvertersTests.assertToXContentBody(graphExploreRequest, request.getEntity()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java index 4376b47d737b4..3673afa13896d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java @@ -23,11 +23,11 @@ import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; -import org.elasticsearch.protocol.xpack.graph.GraphExploreResponse; -import org.elasticsearch.protocol.xpack.graph.Hop; -import org.elasticsearch.protocol.xpack.graph.Vertex; -import org.elasticsearch.protocol.xpack.graph.VertexRequest; +import org.elasticsearch.client.graph.GraphExploreRequest; +import org.elasticsearch.client.graph.GraphExploreResponse; +import org.elasticsearch.client.graph.Hop; +import org.elasticsearch.client.graph.Vertex; +import org.elasticsearch.client.graph.VertexRequest; import org.hamcrest.Matchers; import org.junit.Before; @@ -136,4 +136,4 @@ public void testBadExplore() throws Exception { } -} \ No newline at end of file +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java index 03614537bfe78..f83986829d529 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java @@ -20,8 +20,8 @@ package org.elasticsearch.client; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoResponse; +import org.elasticsearch.client.migration.IndexUpgradeInfoRequest; +import org.elasticsearch.client.migration.IndexUpgradeInfoResponse; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationRequestConvertersTests.java index 97a2cc16a7ef9..e3adefcb262a3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationRequestConvertersTests.java @@ -20,7 +20,7 @@ package org.elasticsearch.client; import org.apache.http.client.methods.HttpGet; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; +import org.elasticsearch.client.migration.IndexUpgradeInfoRequest; import org.elasticsearch.test.ESTestCase; import java.util.HashMap; @@ -28,7 +28,7 @@ public class MigrationRequestConvertersTests extends ESTestCase { - public static void testGetMigrationAssistance() { + public void testGetMigrationAssistance() { IndexUpgradeInfoRequest upgradeInfoRequest = new IndexUpgradeInfoRequest(); String expectedEndpoint = "/_xpack/migration/assistance"; if (randomBoolean()) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java index 4ea462efb02a0..984ad92a7a01d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java @@ -28,10 +28,10 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchResponse; +import org.elasticsearch.client.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.PutWatchResponse; import org.elasticsearch.rest.RestStatus; import static org.hamcrest.Matchers.is; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java index 72065150989e2..43d0769ba0906 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java @@ -25,8 +25,8 @@ import org.elasticsearch.client.watcher.AckWatchRequest; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.PutWatchRequest; import org.elasticsearch.test.ESTestCase; import java.io.ByteArrayOutputStream; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java index 8631e18b8739b..bf507242bc89e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java @@ -26,12 +26,12 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.index.query.TermQueryBuilder; -import org.elasticsearch.protocol.xpack.graph.Connection; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; -import org.elasticsearch.protocol.xpack.graph.GraphExploreResponse; -import org.elasticsearch.protocol.xpack.graph.Hop; -import org.elasticsearch.protocol.xpack.graph.Vertex; -import org.elasticsearch.protocol.xpack.graph.VertexRequest; +import org.elasticsearch.client.graph.Connection; +import org.elasticsearch.client.graph.GraphExploreRequest; +import org.elasticsearch.client.graph.GraphExploreResponse; +import org.elasticsearch.client.graph.Hop; +import org.elasticsearch.client.graph.Vertex; +import org.elasticsearch.client.graph.VertexRequest; import org.junit.Before; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationClientDocumentationIT.java index c8310be8053b2..57f8a8314fa97 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationClientDocumentationIT.java @@ -24,9 +24,9 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoResponse; -import org.elasticsearch.protocol.xpack.migration.UpgradeActionRequired; +import org.elasticsearch.client.migration.IndexUpgradeInfoRequest; +import org.elasticsearch.client.migration.IndexUpgradeInfoResponse; +import org.elasticsearch.client.migration.UpgradeActionRequired; import java.io.IOException; import java.util.Map; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java index 4f41b723943ad..b0aed4bc5cd63 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java @@ -35,10 +35,10 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchResponse; +import org.elasticsearch.client.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.PutWatchResponse; import org.elasticsearch.rest.RestStatus; import java.util.concurrent.CountDownLatch; diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java similarity index 98% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java index 2e6e325c4a009..b161eb2513baa 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.graph; +package org.elasticsearch.client.graph; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ShardOperationFailedException; @@ -35,7 +35,7 @@ import static org.hamcrest.Matchers.equalTo; -public class GraphExploreResponseTests extends AbstractXContentTestCase< GraphExploreResponse> { +public class GraphExploreResponseTests extends AbstractXContentTestCase { @Override protected GraphExploreResponse createTestInstance() { diff --git a/x-pack/protocol/build.gradle b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java similarity index 63% rename from x-pack/protocol/build.gradle rename to client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java index 7ca81c05e3947..2375077220116 100644 --- a/x-pack/protocol/build.gradle +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java @@ -17,13 +17,16 @@ * under the License. */ -apply plugin: 'elasticsearch.build' +package org.elasticsearch.client.migration; -description = 'Request and Response objects for x-pack that are used by the' + - ' high level rest client and x-pack itself' +import org.elasticsearch.test.ESTestCase; -dependencies { - compileOnly "org.elasticsearch:elasticsearch:${version}" +public class IndexUpgradeInfoRequestTests extends ESTestCase { - testCompile "org.elasticsearch.test:framework:${version}" + // TODO: add to cross XPack-HLRC serialization test + + public void testNullIndices() { + expectThrows(NullPointerException.class, () -> new IndexUpgradeInfoRequest((String[])null)); + expectThrows(NullPointerException.class, () -> new IndexUpgradeInfoRequest().indices((String[])null)); + } } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/package-info.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java similarity index 79% rename from x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/package-info.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java index fab18ccc637e8..8106043c08b3a 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/package-info.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java @@ -17,7 +17,10 @@ * under the License. */ -/** - * Request and Response objects for miscellaneous X-Pack APIs. - */ -package org.elasticsearch.protocol.xpack; +package org.elasticsearch.client.migration; + +import org.elasticsearch.test.ESTestCase; + +public class IndexUpgradeInfoResponseTests extends ESTestCase { + // TODO: add to cross XPack-HLRC serialization test +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java similarity index 96% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java index 1dbc4cec32128..3017b188292c0 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java similarity index 96% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java index d0aadef161175..e82ccd11cb2cb 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java index d75e36f7a3664..adcb530581fc2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java @@ -19,19 +19,15 @@ package org.elasticsearch.client.watcher; -import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.test.ESTestCase; import java.util.Optional; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; public class WatchRequestValidationTests extends ESTestCase { @@ -61,38 +57,42 @@ public void testAcknowledgeWatchNullActionId() { } public void testDeleteWatchInvalidWatchId() { - ActionRequestValidationException e = new DeleteWatchRequest("id with whitespaces").validate(); - assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), hasItem("watch id contains whitespace")); + final Optional validationException = new DeleteWatchRequest("id with whitespaces").validate(); + assertThat(validationException.isPresent(), is(true)); + assertThat(validationException.get().validationErrors(), hasItem("watch id contains whitespace")); } public void testDeleteWatchNullId() { - ActionRequestValidationException e = new DeleteWatchRequest(null).validate(); - assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), hasItem("watch id is missing")); + final Optional validationException = new DeleteWatchRequest(null).validate(); + assertThat(validationException.isPresent(), is(true)); + assertThat(validationException.get().validationErrors(), hasItem("watch id is missing")); } public void testPutWatchInvalidWatchId() { - ActionRequestValidationException e = new PutWatchRequest("id with whitespaces", BytesArray.EMPTY, XContentType.JSON).validate(); - assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), hasItem("watch id contains whitespace")); + final Optional validationException = + new PutWatchRequest("id with whitespaces", BytesArray.EMPTY, XContentType.JSON).validate(); + assertThat(validationException.isPresent(), is(true)); + assertThat(validationException.get().validationErrors(), hasItem("watch id contains whitespace")); } public void testPutWatchNullId() { - ActionRequestValidationException e = new PutWatchRequest(null, BytesArray.EMPTY, XContentType.JSON).validate(); - assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), hasItem("watch id is missing")); + final Optional validationException = + new PutWatchRequest(null, BytesArray.EMPTY, XContentType.JSON).validate(); + assertThat(validationException.isPresent(), is(true)); + assertThat(validationException.get().validationErrors(), hasItem("watch id is missing")); } public void testPutWatchSourceNull() { - ActionRequestValidationException e = new PutWatchRequest("foo", null, XContentType.JSON).validate(); - assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), hasItem("watch source is missing")); + final Optional validationException = + new PutWatchRequest("foo", null, XContentType.JSON).validate(); + assertThat(validationException.isPresent(), is(true)); + assertThat(validationException.get().validationErrors(), hasItem("watch source is missing")); } public void testPutWatchContentNull() { - ActionRequestValidationException e = new PutWatchRequest("foo", BytesArray.EMPTY, null).validate(); - assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), hasItem("request body is missing")); + final Optional validationException = + new PutWatchRequest("foo", BytesArray.EMPTY, null).validate(); + assertThat(validationException.isPresent(), is(true)); + assertThat(validationException.get().validationErrors(), hasItem("request body is missing")); } } diff --git a/x-pack/build.gradle b/x-pack/build.gradle index d2a19be2136fb..0b1406519685b 100644 --- a/x-pack/build.gradle +++ b/x-pack/build.gradle @@ -16,19 +16,17 @@ subprojects { project.esplugin.licenseFile = rootProject.file('licenses/ELASTIC-LICENSE.txt') project.esplugin.noticeFile = xpackRootProject.file('NOTICE.txt') } + + tasks.withType(LicenseHeadersTask.class) { + approvedLicenses = ['Elastic License', 'Generated'] + additionalLicense 'ELAST', 'Elastic License', 'Licensed under the Elastic License' + } - if (project.name != 'protocol') { - tasks.withType(LicenseHeadersTask.class) { - approvedLicenses = ['Elastic License', 'Generated'] - additionalLicense 'ELAST', 'Elastic License', 'Licensed under the Elastic License' - } - - ext.licenseName = 'Elastic License' - ext.licenseUrl = ext.elasticLicenseUrl + ext.licenseName = 'Elastic License' + ext.licenseUrl = ext.elasticLicenseUrl - project.ext.licenseFile = rootProject.file('licenses/ELASTIC-LICENSE.txt') - project.ext.noticeFile = xpackRootProject.file('NOTICE.txt') - } + project.ext.licenseFile = rootProject.file('licenses/ELASTIC-LICENSE.txt') + project.ext.noticeFile = xpackRootProject.file('NOTICE.txt') } subprojects { diff --git a/x-pack/protocol/LICENSE.txt b/x-pack/protocol/LICENSE.txt deleted file mode 100644 index d645695673349..0000000000000 --- a/x-pack/protocol/LICENSE.txt +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequestTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequestTests.java deleted file mode 100644 index a4daa39566462..0000000000000 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoRequestTests.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.protocol.xpack.migration; - -import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.test.AbstractWireSerializingTestCase; - -public class IndexUpgradeInfoRequestTests extends AbstractWireSerializingTestCase { - @Override - protected IndexUpgradeInfoRequest createTestInstance() { - int indexCount = randomInt(4); - String[] indices = new String[indexCount]; - for (int i = 0; i < indexCount; i++) { - indices[i] = randomAlphaOfLength(10); - } - IndexUpgradeInfoRequest request = new IndexUpgradeInfoRequest(indices); - if (randomBoolean()) { - request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); - } - return request; - } - - @Override - protected Writeable.Reader instanceReader() { - return IndexUpgradeInfoRequest::new; - } - - public void testNullIndices() { - expectThrows(NullPointerException.class, () -> new IndexUpgradeInfoRequest((String[])null)); - expectThrows(NullPointerException.class, () -> new IndexUpgradeInfoRequest().indices((String[])null)); - } -} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java deleted file mode 100644 index 42de1ae60908a..0000000000000 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.protocol.xpack.migration; - -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -public class IndexUpgradeInfoResponseTests extends AbstractStreamableXContentTestCase { - @Override - protected IndexUpgradeInfoResponse doParseInstance(XContentParser parser) { - return IndexUpgradeInfoResponse.fromXContent(parser); - } - - @Override - protected IndexUpgradeInfoResponse createBlankInstance() { - return new IndexUpgradeInfoResponse(); - } - - @Override - protected IndexUpgradeInfoResponse createTestInstance() { - return randomIndexUpgradeInfoResponse(randomIntBetween(0, 10)); - } - - private static IndexUpgradeInfoResponse randomIndexUpgradeInfoResponse(int numIndices) { - Map actions = new HashMap<>(); - for (int i = 0; i < numIndices; i++) { - actions.put(randomAlphaOfLength(5), randomFrom(UpgradeActionRequired.values())); - } - return new IndexUpgradeInfoResponse(actions); - } - - @Override - protected IndexUpgradeInfoResponse mutateInstance(IndexUpgradeInfoResponse instance) { - if (instance.getActions().size() == 0) { - return randomIndexUpgradeInfoResponse(1); - } - Map actions = new HashMap<>(instance.getActions()); - if (randomBoolean()) { - Iterator> iterator = actions.entrySet().iterator(); - iterator.next(); - iterator.remove(); - } else { - actions.put(randomAlphaOfLength(5), randomFrom(UpgradeActionRequired.values())); - } - return new IndexUpgradeInfoResponse(actions); - } -} From 87e7a66cf886219da52e2cbb3e4d424e8df772aa Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Fri, 19 Oct 2018 17:56:50 +0200 Subject: [PATCH 11/26] initial extension of XPack tests to support HLRC requests/responses --- .../org/elasticsearch/client/GraphClient.java | 4 +- .../client/graph/GraphExploreResponse.java | 66 ++------ .../elasticsearch/client/graph/Vertex.java | 8 + .../migration/UpgradeActionRequired.java | 16 +- .../client/watcher/DeleteWatchResponse.java | 13 +- .../client/watcher/PutWatchResponse.java | 13 +- .../client/xpack/XPackInfoResponse.java | 50 +----- .../graph/GraphExploreResponseTests.java | 160 ------------------ .../IndexUpgradeInfoRequestTests.java | 2 - .../IndexUpgradeInfoResponseTests.java | 26 --- .../watcher/DeleteWatchResponseTests.java | 45 ----- .../client/watcher/PutWatchResponseTests.java | 45 ----- x-pack/plugin/core/build.gradle | 1 + .../xpack/graph/GraphExploreResponse.java | 2 +- ...bstractHLRCStreamableXContentTestCase.java | 21 +-- .../AbstractHLRCXContentTestCase.java | 30 ++++ .../xpack/XPackInfoResponseTests.java | 34 +++- .../graph/GraphExploreResponseTests.java | 50 +++++- .../xpack/license/LicenseStatusTests.java | 16 ++ .../license/PutLicenseResponseTests.java | 17 +- .../IndexUpgradeInfoResponseTests.java | 22 ++- .../watcher/DeleteWatchResponseTests.java | 15 +- .../xpack/watcher/PutWatchResponseTests.java | 15 +- x-pack/response-tests/build.gradle | 44 ----- .../xpack/license/LicenseStatusTests.java | 26 --- .../license/PutLicenseResponseTests.java | 123 -------------- 26 files changed, 225 insertions(+), 639 deletions(-) delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java rename x-pack/{response-tests/src/test/java/org/elasticsearch/protocol/xpack/license => plugin/core/src/test/java/org/elasticsearch/protocol}/AbstractHLRCStreamableXContentTestCase.java (59%) create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java delete mode 100644 x-pack/response-tests/build.gradle delete mode 100644 x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java delete mode 100644 x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java index 3d5365fedde5a..70912b094d023 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java @@ -44,7 +44,7 @@ public class GraphClient { public final GraphExploreResponse explore(GraphExploreRequest graphExploreRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity(graphExploreRequest, GraphRequestConverters::explore, - options, GraphExploreResponse::fromXContext, emptySet()); + options, GraphExploreResponse::fromXContent, emptySet()); } /** @@ -57,7 +57,7 @@ public final void exploreAsync(GraphExploreRequest graphExploreRequest, RequestOptions options, ActionListener listener) { restHighLevelClient.performRequestAsyncAndParseEntity(graphExploreRequest, GraphRequestConverters::explore, - options, GraphExploreResponse::fromXContext, listener, emptySet()); + options, GraphExploreResponse::fromXContent, listener, emptySet()); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java index 2b5d1c7ecf4be..11beef778f7d7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java @@ -18,15 +18,11 @@ */ package org.elasticsearch.client.graph; -import com.carrotsearch.hppc.ObjectIntHashMap; - import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.ParseField; 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.client.graph.Connection.ConnectionId; import org.elasticsearch.client.graph.Connection.UnresolvedConnection; @@ -47,7 +43,7 @@ * * @see GraphExploreRequest */ -public class GraphExploreResponse implements ToXContentObject { +public class GraphExploreResponse { private long tookInMillis; private boolean timedOut = false; @@ -94,62 +90,36 @@ public Collection getConnections() { return connections.values(); } + public Collection getConnectionIds() { + return connections.keySet(); + } + + public Connection getConnection(ConnectionId connectionId) { + return connections.get(connectionId); + } + public Collection getVertices() { return vertices.values(); } + + public Collection getVertexIds() { + return vertices.keySet(); + } public Vertex getVertex(VertexId id) { return vertices.get(id); } + public boolean isReturnDetailedInfo() { + return returnDetailedInfo; + } + private static final ParseField TOOK = new ParseField("took"); private static final ParseField TIMED_OUT = new ParseField("timed_out"); private static final ParseField VERTICES = new ParseField("vertices"); private static final ParseField CONNECTIONS = new ParseField("connections"); private static final ParseField FAILURES = new ParseField("failures"); - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(TOOK.getPreferredName(), tookInMillis); - builder.field(TIMED_OUT.getPreferredName(), timedOut); - - builder.startArray(FAILURES.getPreferredName()); - if (shardFailures != null) { - for (ShardOperationFailedException shardFailure : shardFailures) { - builder.startObject(); - shardFailure.toXContent(builder, params); - builder.endObject(); - } - } - builder.endArray(); - - ObjectIntHashMap vertexNumbers = new ObjectIntHashMap<>(vertices.size()); - - Map extraParams = new HashMap<>(); - extraParams.put(RETURN_DETAILED_INFO_PARAM, Boolean.toString(returnDetailedInfo)); - Params extendedParams = new DelegatingMapParams(extraParams, params); - - builder.startArray(VERTICES.getPreferredName()); - for (Vertex vertex : vertices.values()) { - builder.startObject(); - vertexNumbers.put(vertex, vertexNumbers.size()); - vertex.toXContent(builder, extendedParams); - builder.endObject(); - } - builder.endArray(); - - builder.startArray(CONNECTIONS.getPreferredName()); - for (Connection connection : connections.values()) { - builder.startObject(); - connection.toXContent(builder, extendedParams, vertexNumbers); - builder.endObject(); - } - builder.endArray(); - builder.endObject(); - return builder; - } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "GraphExploreResponsenParser", true, args -> { @@ -190,7 +160,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ShardSearchFailure.fromXContent(p), FAILURES); } - public static GraphExploreResponse fromXContext(XContentParser parser) throws IOException { + public static GraphExploreResponse fromXContent(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java index 852372209da9c..54b0b5223277d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java @@ -220,6 +220,14 @@ public VertexId(String field, String term) { this.term = term; } + public String getField() { + return field; + } + + public String getTerm() { + return term; + } + @Override public boolean equals(Object o) { if (this == o) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java index e743d10529e1a..26b7b1e815d7f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/UpgradeActionRequired.java @@ -18,17 +18,12 @@ */ package org.elasticsearch.client.migration; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Writeable; - -import java.io.IOException; import java.util.Locale; /** * Indicates the type of the upgrade required for the index */ -public enum UpgradeActionRequired implements Writeable { +public enum UpgradeActionRequired { NOT_APPLICABLE, // Indicates that the check is not applicable to this index type, the next check will be performed UP_TO_DATE, // Indicates that the check finds this index to be up to date - no additional checks are required REINDEX, // The index should be reindex @@ -38,15 +33,6 @@ public static UpgradeActionRequired fromString(String value) { return UpgradeActionRequired.valueOf(value.toUpperCase(Locale.ROOT)); } - public static UpgradeActionRequired readFromStream(StreamInput in) throws IOException { - return in.readEnum(UpgradeActionRequired.class); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeEnum(this); - } - @Override public String toString() { return name().toLowerCase(Locale.ROOT); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java index 4e946ad459cf8..2847c08de3f1b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java @@ -20,14 +20,12 @@ import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; -public class DeleteWatchResponse implements ToXContentObject { +public class DeleteWatchResponse { private static final ObjectParser PARSER = new ObjectParser<>("x_pack_delete_watch_response", DeleteWatchResponse::new); @@ -89,15 +87,6 @@ public int hashCode() { return Objects.hash(id, version, found); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("_id", id) - .field("_version", version) - .field("found", found) - .endObject(); - } - public static DeleteWatchResponse fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java index 5c8d7bde9b158..052eb4d56976e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java @@ -20,14 +20,12 @@ import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; -public class PutWatchResponse implements ToXContentObject { +public class PutWatchResponse { private static final ObjectParser PARSER = new ObjectParser<>("x_pack_put_watch_response", PutWatchResponse::new); @@ -90,15 +88,6 @@ public int hashCode() { return Objects.hash(id, version, created); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("_id", id) - .field("_version", version) - .field("created", created) - .endObject(); - } - public static PutWatchResponse fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java index 2b7d205447f69..f9a92d2fbbe02 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java @@ -21,9 +21,6 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; 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.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.ToXContentObject; @@ -252,7 +249,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } - public static class BuildInfo implements ToXContentObject, Writeable { + public static class BuildInfo implements ToXContentObject { private final String hash; private final String timestamp; @@ -261,16 +258,6 @@ public BuildInfo(String hash, String timestamp) { this.timestamp = timestamp; } - public BuildInfo(StreamInput input) throws IOException { - this(input.readString(), input.readString()); - } - - @Override - public void writeTo(StreamOutput output) throws IOException { - output.writeString(hash); - output.writeString(timestamp); - } - public String getHash() { return hash; } @@ -309,7 +296,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } - public static class FeatureSetsInfo implements ToXContentObject, Writeable { + public static class FeatureSetsInfo implements ToXContentObject { private final Map featureSets; public FeatureSetsInfo(Set featureSets) { @@ -320,24 +307,6 @@ public FeatureSetsInfo(Set featureSets) { this.featureSets = Collections.unmodifiableMap(map); } - public FeatureSetsInfo(StreamInput in) throws IOException { - int size = in.readVInt(); - Map featureSets = new HashMap<>(size); - for (int i = 0; i < size; i++) { - FeatureSet featureSet = new FeatureSet(in); - featureSets.put(featureSet.name, featureSet); - } - this.featureSets = Collections.unmodifiableMap(featureSets); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(featureSets.size()); - for (FeatureSet featureSet : featureSets.values()) { - featureSet.writeTo(out); - } - } - public Map getFeatureSets() { return featureSets; } @@ -365,7 +334,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } - public static class FeatureSet implements ToXContentObject, Writeable { + public static class FeatureSet implements ToXContentObject { private final String name; @Nullable private final String description; private final boolean available; @@ -381,19 +350,6 @@ public FeatureSet(String name, @Nullable String description, boolean available, this.nativeCodeInfo = nativeCodeInfo; } - public FeatureSet(StreamInput in) throws IOException { - this(in.readString(), in.readOptionalString(), in.readBoolean(), in.readBoolean(), in.readMap()); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(name); - out.writeOptionalString(description); - out.writeBoolean(available); - out.writeBoolean(enabled); - out.writeMap(nativeCodeInfo); - } - public String name() { return name; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java deleted file mode 100644 index b161eb2513baa..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.client.graph; - -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.action.ShardOperationFailedException; -import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import static org.hamcrest.Matchers.equalTo; - -public class GraphExploreResponseTests extends AbstractXContentTestCase { - - @Override - protected GraphExploreResponse createTestInstance() { - return createInstance(0); - } - private static GraphExploreResponse createInstance(int numFailures) { - int numItems = randomIntBetween(4, 128); - boolean timedOut = randomBoolean(); - boolean showDetails = randomBoolean(); - long overallTookInMillis = randomNonNegativeLong(); - Map vertices = new HashMap<>(); - Map connections = new HashMap<>(); - ShardOperationFailedException [] failures = new ShardOperationFailedException [numFailures]; - for (int i = 0; i < failures.length; i++) { - failures[i] = new ShardSearchFailure(new ElasticsearchException("an error")); - } - - //Create random set of vertices - for (int i = 0; i < numItems; i++) { - Vertex v = new Vertex("field1", randomAlphaOfLength(5), randomDouble(), 0, - showDetails?randomIntBetween(100, 200):0, - showDetails?randomIntBetween(1, 100):0); - vertices.put(v.getId(), v); - } - - //Wire up half the vertices randomly - Vertex[] vs = vertices.values().toArray(new Vertex[vertices.size()]); - for (int i = 0; i < numItems/2; i++) { - Vertex v1 = vs[randomIntBetween(0, vs.length-1)]; - Vertex v2 = vs[randomIntBetween(0, vs.length-1)]; - if(v1 != v2) { - Connection conn = new Connection(v1, v2, randomDouble(), randomLongBetween(1, 10)); - connections.put(conn.getId(), conn); - } - } - return new GraphExploreResponse(overallTookInMillis, timedOut, failures, vertices, connections, showDetails); - } - - - private static GraphExploreResponse createTestInstanceWithFailures() { - return createInstance(randomIntBetween(1, 128)); - } - - @Override - protected GraphExploreResponse doParseInstance(XContentParser parser) throws IOException { - return GraphExploreResponse.fromXContext(parser); - } - - @Override - protected boolean supportsUnknownFields() { - return true; - } - - @Override - protected boolean assertToXContentEquivalence() { - return false; - } - - @Override - protected String[] getShuffleFieldsExceptions() { - return new String[]{"vertices", "connections"}; - } - - protected Predicate getRandomFieldsExcludeFilterWhenResultHasErrors() { - return field -> field.startsWith("responses"); - } - - @Override - protected void assertEqualInstances( GraphExploreResponse expectedInstance, GraphExploreResponse newInstance) { - assertThat(newInstance.getTook(), equalTo(expectedInstance.getTook())); - assertThat(newInstance.isTimedOut(), equalTo(expectedInstance.isTimedOut())); - - Comparator connComparator = new Comparator() { - @Override - public int compare(Connection o1, Connection o2) { - return o1.getId().toString().compareTo(o2.getId().toString()); - } - }; - Connection[] newConns = newInstance.getConnections().toArray(new Connection[0]); - Connection[] expectedConns = expectedInstance.getConnections().toArray(new Connection[0]); - Arrays.sort(newConns, connComparator); - Arrays.sort(expectedConns, connComparator); - assertArrayEquals(expectedConns, newConns); - - //Sort the vertices lists before equality test (map insertion sequences can cause order differences) - Comparator comparator = new Comparator() { - @Override - public int compare(Vertex o1, Vertex o2) { - return o1.getId().toString().compareTo(o2.getId().toString()); - } - }; - Vertex[] newVertices = newInstance.getVertices().toArray(new Vertex[0]); - Vertex[] expectedVertices = expectedInstance.getVertices().toArray(new Vertex[0]); - Arrays.sort(newVertices, comparator); - Arrays.sort(expectedVertices, comparator); - assertArrayEquals(expectedVertices, newVertices); - - ShardOperationFailedException[] newFailures = newInstance.getShardFailures(); - ShardOperationFailedException[] expectedFailures = expectedInstance.getShardFailures(); - assertEquals(expectedFailures.length, newFailures.length); - - } - - /** - * Test parsing {@link GraphExploreResponse} with inner failures as they don't support asserting on xcontent equivalence, given - * exceptions are not parsed back as the same original class. We run the usual {@link AbstractXContentTestCase#testFromXContent()} - * without failures, and this other test with failures where we disable asserting on xcontent equivalence at the end. - */ - public void testFromXContentWithFailures() throws IOException { - Supplier< GraphExploreResponse> instanceSupplier = GraphExploreResponseTests::createTestInstanceWithFailures; - //with random fields insertion in the inner exceptions, some random stuff may be parsed back as metadata, - //but that does not bother our assertions, as we only want to test that we don't break. - boolean supportsUnknownFields = true; - //exceptions are not of the same type whenever parsed back - boolean assertToXContentEquivalence = false; - AbstractXContentTestCase.testFromXContent( - NUMBER_OF_TEST_RUNS, instanceSupplier, supportsUnknownFields, getShuffleFieldsExceptions(), - getRandomFieldsExcludeFilterWhenResultHasErrors(), this::createParser, this::doParseInstance, - this::assertEqualInstances, assertToXContentEquivalence, ToXContent.EMPTY_PARAMS); - } - -} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java index 2375077220116..86250fdaec274 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoRequestTests.java @@ -23,8 +23,6 @@ public class IndexUpgradeInfoRequestTests extends ESTestCase { - // TODO: add to cross XPack-HLRC serialization test - public void testNullIndices() { expectThrows(NullPointerException.class, () -> new IndexUpgradeInfoRequest((String[])null)); expectThrows(NullPointerException.class, () -> new IndexUpgradeInfoRequest().indices((String[])null)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java deleted file mode 100644 index 8106043c08b3a..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.client.migration; - -import org.elasticsearch.test.ESTestCase; - -public class IndexUpgradeInfoResponseTests extends ESTestCase { - // TODO: add to cross XPack-HLRC serialization test -} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java deleted file mode 100644 index 3017b188292c0..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.client.watcher; - -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; - -public class DeleteWatchResponseTests extends AbstractXContentTestCase { - - @Override - protected DeleteWatchResponse createTestInstance() { - String id = randomAlphaOfLength(10); - long version = randomLongBetween(1, 10); - boolean found = randomBoolean(); - return new DeleteWatchResponse(id, version, found); - } - - @Override - protected DeleteWatchResponse doParseInstance(XContentParser parser) throws IOException { - return DeleteWatchResponse.fromXContent(parser); - } - - @Override - protected boolean supportsUnknownFields() { - return false; - } -} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java deleted file mode 100644 index e82ccd11cb2cb..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.client.watcher; - -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; - -public class PutWatchResponseTests extends AbstractXContentTestCase { - - @Override - protected PutWatchResponse createTestInstance() { - String id = randomAlphaOfLength(10); - long version = randomLongBetween(1, 10); - boolean created = randomBoolean(); - return new PutWatchResponse(id, version, created); - } - - @Override - protected PutWatchResponse doParseInstance(XContentParser parser) throws IOException { - return PutWatchResponse.fromXContent(parser); - } - - @Override - protected boolean supportsUnknownFields() { - return false; - } -} diff --git a/x-pack/plugin/core/build.gradle b/x-pack/plugin/core/build.gradle index 01e8179fb62ea..647ebd53f1cd8 100644 --- a/x-pack/plugin/core/build.gradle +++ b/x-pack/plugin/core/build.gradle @@ -49,6 +49,7 @@ dependencies { testCompile project(path: ':modules:reindex', configuration: 'runtime') testCompile project(path: ':modules:parent-join', configuration: 'runtime') testCompile project(path: ':modules:analysis-common', configuration: 'runtime') + testCompile ("org.elasticsearch.client:elasticsearch-rest-high-level-client:${version}") } ext.expansions = [ diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java index 12eb20617ff0f..b44e192f407ac 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java @@ -241,7 +241,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ShardSearchFailure.fromXContent(p), FAILURES); } - public static GraphExploreResponse fromXContext(XContentParser parser) throws IOException { + public static GraphExploreResponse fromXContent(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java similarity index 59% rename from x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java rename to x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java index a1effb0ccc9bc..fcae15af45462 100644 --- a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/AbstractHLRCStreamableXContentTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java @@ -1,22 +1,9 @@ /* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.protocol.xpack.license; +package org.elasticsearch.protocol; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.xcontent.ToXContent; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java new file mode 100644 index 0000000000000..56b14d2249ec3 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol; + +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +public abstract class AbstractHLRCXContentTestCase extends AbstractXContentTestCase { + + /** + * Generic test that creates new instance of HLRC request/response from the test instance and checks + * both for equality and asserts equality on the two queries. + */ + public final void testHLRCFromXContent() throws IOException { + AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, this::createTestInstance, supportsUnknownFields(), + getShuffleFieldsExceptions(), getRandomFieldsExcludeFilter(), this::createParser, + p -> convert(doHLRCParseInstance(p)), + this::assertEqualInstances, true, getToXContentParams()); + } + + public abstract H doHLRCParseInstance(XContentParser parser) throws IOException; + + public abstract T convert(H instance); +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java index fac99959c536a..1871ed4c837f6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java @@ -7,12 +7,12 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; import org.elasticsearch.protocol.xpack.license.LicenseStatus; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; import java.util.HashMap; import java.util.HashSet; @@ -21,8 +21,10 @@ import java.util.function.Function; import java.util.function.Predicate; import java.io.IOException; +import java.util.stream.Collectors; -public class XPackInfoResponseTests extends AbstractStreamableXContentTestCase { +public class XPackInfoResponseTests extends AbstractHLRCStreamableXContentTestCase { @Override protected XPackInfoResponse doParseInstance(XContentParser parser) throws IOException { return XPackInfoResponse.fromXContent(parser); @@ -33,6 +35,34 @@ protected XPackInfoResponse createBlankInstance() { return new XPackInfoResponse(); } + @Override + public org.elasticsearch.client.xpack.XPackInfoResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.xpack.XPackInfoResponse.fromXContent(parser); + } + + @Override + public XPackInfoResponse convert(org.elasticsearch.client.xpack.XPackInfoResponse instance) { + final org.elasticsearch.client.xpack.XPackInfoResponse.BuildInfo buildInfo = instance.getBuildInfo(); + final XPackInfoResponse.BuildInfo buildInfo1 = + buildInfo != null ? new BuildInfo(buildInfo.getHash(), buildInfo.getTimestamp()) : null; + final org.elasticsearch.client.xpack.XPackInfoResponse.LicenseInfo licenseInfo = instance.getLicenseInfo(); + final XPackInfoResponse.LicenseInfo licenseInfo1 = + licenseInfo != null + ? new LicenseInfo(licenseInfo.getUid(), licenseInfo.getType(), licenseInfo.getMode(), + licenseInfo.getStatus() != null ? LicenseStatus.valueOf(licenseInfo.getStatus().name()) : null, + licenseInfo.getExpiryDate()) + : null; + final org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo featureSetsInfo = instance.getFeatureSetsInfo(); + final XPackInfoResponse.FeatureSetsInfo featureSetsInfo1 = + featureSetsInfo != null + ? new FeatureSetsInfo(featureSetsInfo.getFeatureSets().values().stream() + .map(fs -> new XPackInfoResponse.FeatureSetsInfo.FeatureSet(fs.name(), fs.description(), fs.available(), fs.enabled(), + fs.nativeCodeInfo())) + .collect(Collectors.toSet())) + : null; + return new XPackInfoResponse(buildInfo1, licenseInfo1, featureSetsInfo1); + } + @Override protected Predicate getRandomFieldsExcludeFilter() { return path -> path.equals("features") diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java index 72f84e5815c97..a4bdcc8b941cb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java @@ -10,24 +10,66 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.protocol.AbstractHLRCXContentTestCase; import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Arrays; +import java.util.Collection; import java.util.Comparator; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import static org.hamcrest.Matchers.equalTo; -public class GraphExploreResponseTests extends AbstractXContentTestCase< GraphExploreResponse> { +public class GraphExploreResponseTests extends AbstractHLRCXContentTestCase { + + @Override + public org.elasticsearch.client.graph.GraphExploreResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.graph.GraphExploreResponse.fromXContent(parser); + } + + @Override + public GraphExploreResponse convert(org.elasticsearch.client.graph.GraphExploreResponse instance) { + final Collection vertexIds = instance.getVertexIds(); + final Map vertexMap = new LinkedHashMap<>(vertexIds.size()); + + final Function vertexIdFn = + vId -> new Vertex.VertexId(vId.getField(), vId.getTerm()); + final Function vertexFn = + v -> new Vertex(v.getField(), v.getTerm(), v.getWeight(), v.getHopDepth(), v.getBg(), v.getFg()); + + for (org.elasticsearch.client.graph.Vertex.VertexId vertexId : vertexIds) { + final org.elasticsearch.client.graph.Vertex vertex = instance.getVertex(vertexId); + + vertexMap.put(vertexIdFn.apply(vertexId), vertexFn.apply(vertex)); + } + + final Collection connectionIds = instance.getConnectionIds(); + final Map connectionMap = new LinkedHashMap<>(connectionIds.size()); + for (org.elasticsearch.client.graph.Connection.ConnectionId connectionId : connectionIds) { + final org.elasticsearch.client.graph.Connection connection = instance.getConnection(connectionId); + final Connection.ConnectionId connectionId1 = + new Connection.ConnectionId(vertexIdFn.apply(connectionId.getSource()), vertexIdFn.apply(connectionId.getTarget())); + final Connection connection1 = new Connection(vertexFn.apply(connection.getFrom()), vertexFn.apply(connection.getTo()), + connection.getWeight(), connection.getDocCount()); + connectionMap.put(connectionId1, connection1); + } + + return new GraphExploreResponse(instance.getTookInMillis(), instance.isTimedOut(), + instance.getShardFailures(), vertexMap, connectionMap, instance.isReturnDetailedInfo()); + } @Override protected GraphExploreResponse createTestInstance() { return createInstance(0); } + private static GraphExploreResponse createInstance(int numFailures) { int numItems = randomIntBetween(4, 128); boolean timedOut = randomBoolean(); @@ -62,13 +104,13 @@ private static GraphExploreResponse createInstance(int numFailures) { } - private static GraphExploreResponse createTestInstanceWithFailures() { + private static GraphExploreResponse createTestInstanceWithFailures() { return createInstance(randomIntBetween(1, 128)); } @Override protected GraphExploreResponse doParseInstance(XContentParser parser) throws IOException { - return GraphExploreResponse.fromXContext(parser); + return GraphExploreResponse.fromXContent(parser); } @Override @@ -79,7 +121,7 @@ protected boolean supportsUnknownFields() { @Override protected boolean assertToXContentEquivalence() { return false; - } + } @Override protected String[] getShuffleFieldsExceptions() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java index 7149477d00765..0b73850c5e637 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java @@ -9,9 +9,25 @@ import org.elasticsearch.test.ESTestCase; +import static org.hamcrest.CoreMatchers.equalTo; + public class LicenseStatusTests extends ESTestCase { public void testSerialization() throws IOException { LicenseStatus status = randomFrom(LicenseStatus.values()); assertSame(status, copyWriteable(status, writableRegistry(), LicenseStatus::readFrom)); } + + public void testCompatibility() { + final LicenseStatus[] values = LicenseStatus.values(); + final org.elasticsearch.client.license.LicenseStatus[] hlrcValues = + org.elasticsearch.client.license.LicenseStatus.values(); + + assertThat(values.length, equalTo(hlrcValues.length)); + + for (LicenseStatus value : values) { + final org.elasticsearch.client.license.LicenseStatus licenseStatus = + org.elasticsearch.client.license.LicenseStatus.fromString(value.label()); + assertThat(licenseStatus.label(), equalTo(value.label())); + } + } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java index a09fd6fb99b45..00617e4636bab 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -6,7 +6,7 @@ package org.elasticsearch.protocol.xpack.license; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; import java.io.IOException; import java.util.Collections; @@ -15,7 +15,20 @@ import java.util.function.Function; import java.util.function.Predicate; -public class PutLicenseResponseTests extends AbstractStreamableXContentTestCase { +public class PutLicenseResponseTests extends AbstractHLRCStreamableXContentTestCase { + + @Override + public org.elasticsearch.client.license.PutLicenseResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); + } + + @Override + public PutLicenseResponse convert(org.elasticsearch.client.license.PutLicenseResponse instance) { + return new PutLicenseResponse(instance.isAcknowledged(), LicensesStatus.valueOf(instance.status().name()), + instance.acknowledgeHeader(), instance.acknowledgeMessages()); + } + @Override protected boolean supportsUnknownFields() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java index 57f01a4454e02..31cd300656ad3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java @@ -6,18 +6,36 @@ package org.elasticsearch.protocol.xpack.migration; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; +import java.io.IOException; +import java.util.AbstractMap; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.stream.Collectors; + +public class IndexUpgradeInfoResponseTests extends AbstractHLRCStreamableXContentTestCase { -public class IndexUpgradeInfoResponseTests extends AbstractStreamableXContentTestCase { @Override protected IndexUpgradeInfoResponse doParseInstance(XContentParser parser) { return IndexUpgradeInfoResponse.fromXContent(parser); } + @Override + public org.elasticsearch.client.migration.IndexUpgradeInfoResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.migration.IndexUpgradeInfoResponse.fromXContent(parser); + } + + @Override + public IndexUpgradeInfoResponse convert(org.elasticsearch.client.migration.IndexUpgradeInfoResponse instance) { + final Map actions = instance.getActions(); + return new IndexUpgradeInfoResponse(actions.entrySet().stream().map( + e -> new AbstractMap.SimpleEntry<>(e.getKey(), UpgradeActionRequired.valueOf(e.getValue().name())) + ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + } + @Override protected IndexUpgradeInfoResponse createBlankInstance() { return new IndexUpgradeInfoResponse(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java index 209bc790a8c54..a65fb67fa56e8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java @@ -6,11 +6,12 @@ package org.elasticsearch.protocol.xpack.watcher; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.protocol.AbstractHLRCXContentTestCase; import java.io.IOException; -public class DeleteWatchResponseTests extends AbstractXContentTestCase { +public class DeleteWatchResponseTests extends AbstractHLRCXContentTestCase { @Override protected DeleteWatchResponse createTestInstance() { @@ -25,6 +26,16 @@ protected DeleteWatchResponse doParseInstance(XContentParser parser) throws IOEx return DeleteWatchResponse.fromXContent(parser); } + @Override + public org.elasticsearch.client.watcher.DeleteWatchResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.watcher.DeleteWatchResponse.fromXContent(parser); + } + + @Override + public DeleteWatchResponse convert(org.elasticsearch.client.watcher.DeleteWatchResponse instance) { + return new DeleteWatchResponse(instance.getId(), instance.getVersion(), instance.isFound()); + } + @Override protected boolean supportsUnknownFields() { return false; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java index 1fc2f61b684c7..b3b069d0ec221 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java @@ -6,11 +6,12 @@ package org.elasticsearch.protocol.xpack.watcher; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.protocol.AbstractHLRCXContentTestCase; import java.io.IOException; -public class PutWatchResponseTests extends AbstractXContentTestCase { +public class PutWatchResponseTests extends AbstractHLRCXContentTestCase { @Override protected PutWatchResponse createTestInstance() { @@ -25,6 +26,16 @@ protected PutWatchResponse doParseInstance(XContentParser parser) throws IOExcep return PutWatchResponse.fromXContent(parser); } + @Override + public org.elasticsearch.client.watcher.PutWatchResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.watcher.PutWatchResponse.fromXContent(parser); + } + + @Override + public PutWatchResponse convert(org.elasticsearch.client.watcher.PutWatchResponse instance) { + return new PutWatchResponse(instance.getId(), instance.getVersion(), instance.isCreated()); + } + @Override protected boolean supportsUnknownFields() { return false; diff --git a/x-pack/response-tests/build.gradle b/x-pack/response-tests/build.gradle deleted file mode 100644 index 5677ca9a25e60..0000000000000 --- a/x-pack/response-tests/build.gradle +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -import org.elasticsearch.gradle.test.RestIntegTestTask -import org.gradle.api.internal.provider.Providers - -apply plugin: 'elasticsearch.build' - -group = 'org.elasticsearch.client' -archivesBaseName = 'response-tests' - -dependencies { - testCompile "org.elasticsearch.plugin:x-pack-core:${version}" - testCompile "org.elasticsearch.test:framework:${version}" - testCompile ("org.elasticsearch.client:elasticsearch-rest-high-level-client:${version}") { - exclude group: 'org.elasticsearch.plugin', module: 'protocol' - } - testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" - testCompile "junit:junit:${versions.junit}" - testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" -} - -// due to clash of xpack and protocol classes -jarHell.enabled = false -// jarHell is there - run tests manually before clean up is done -test.enabled = false - -// Not published so no need to assemble -assemble.enabled = true \ No newline at end of file diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java deleted file mode 100644 index a424a30383d13..0000000000000 --- a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol.xpack.license; - -import org.elasticsearch.test.ESTestCase; - -import static org.hamcrest.CoreMatchers.equalTo; - -public class LicenseStatusTests extends ESTestCase { - public void testCompatibility() { - final LicenseStatus[] values = LicenseStatus.values(); - final org.elasticsearch.client.license.LicenseStatus[] hlrcValues = - org.elasticsearch.client.license.LicenseStatus.values(); - - assertThat(values.length, equalTo(hlrcValues.length)); - - for (LicenseStatus value : values) { - final org.elasticsearch.client.license.LicenseStatus licenseStatus = - org.elasticsearch.client.license.LicenseStatus.fromString(value.label()); - assertThat(licenseStatus.label(), equalTo(value.label())); - } - } -} diff --git a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java deleted file mode 100644 index 5eb3f1d18c298..0000000000000 --- a/x-pack/response-tests/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol.xpack.license; - -import org.elasticsearch.common.xcontent.XContentParser; - -import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.function.Predicate; - -public class PutLicenseResponseTests extends AbstractHLRCStreamableXContentTestCase { - - @Override - protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { - return PutLicenseResponse.fromXContent(parser); - } - - @Override - public org.elasticsearch.client.license.PutLicenseResponse doHLRCParseInstance(XContentParser parser) throws IOException { - return org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); - } - - @Override - public PutLicenseResponse convert(org.elasticsearch.client.license.PutLicenseResponse instance) { - return new PutLicenseResponse(instance.isAcknowledged(), LicensesStatus.valueOf(instance.status().name()), - instance.acknowledgeHeader(), instance.acknowledgeMessages()); - } - - @Override - protected boolean supportsUnknownFields() { - return true; - } - - @Override - protected Predicate getRandomFieldsExcludeFilter() { - // The structure of the response is such that unknown fields inside acknowledge cannot be supported since they - // are treated as messages from new services - return p -> p.startsWith("acknowledge"); - } - - @Override - protected PutLicenseResponse createTestInstance() { - boolean acknowledged = randomBoolean(); - LicensesStatus status = randomFrom(LicensesStatus.VALID, LicensesStatus.INVALID, LicensesStatus.EXPIRED); - String messageHeader; - Map ackMessages; - if (randomBoolean()) { - messageHeader = randomAlphaOfLength(10); - ackMessages = randomAckMessages(); - } else { - messageHeader = null; - ackMessages = Collections.emptyMap(); - } - - return new PutLicenseResponse(acknowledged, status, messageHeader, ackMessages); - } - - private static Map randomAckMessages() { - int nFeatures = randomIntBetween(1, 5); - - Map ackMessages = new HashMap<>(); - - for (int i = 0; i < nFeatures; i++) { - String feature = randomAlphaOfLengthBetween(9, 15); - int nMessages = randomIntBetween(1, 5); - String[] messages = new String[nMessages]; - for (int j = 0; j < nMessages; j++) { - messages[j] = randomAlphaOfLengthBetween(10, 30); - } - ackMessages.put(feature, messages); - } - - return ackMessages; - } - - @Override - protected PutLicenseResponse createBlankInstance() { - return new PutLicenseResponse(); - } - - @Override - protected PutLicenseResponse mutateInstance(PutLicenseResponse response) { - @SuppressWarnings("unchecked") - Function mutator = randomFrom( - r -> new PutLicenseResponse( - r.isAcknowledged() == false, - r.status(), - r.acknowledgeHeader(), - r.acknowledgeMessages()), - r -> new PutLicenseResponse( - r.isAcknowledged(), - mutateStatus(r.status()), - r.acknowledgeHeader(), - r.acknowledgeMessages()), - r -> { - if (r.acknowledgeMessages().isEmpty()) { - return new PutLicenseResponse( - r.isAcknowledged(), - r.status(), - randomAlphaOfLength(10), - randomAckMessages() - ); - } else { - return new PutLicenseResponse(r.isAcknowledged(), r.status()); - } - } - - ); - return mutator.apply(response); - } - - private LicensesStatus mutateStatus(LicensesStatus status) { - return randomValueOtherThan(status, () -> randomFrom(LicensesStatus.values())); - } - -} From 26f426031a7b0145e1ec58b7f8dd208acdc67bf1 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Fri, 19 Oct 2018 21:33:50 +0200 Subject: [PATCH 12/26] Merge branch 'origin/master' into hlrc_xprotocol_cleanup_2 --- .../src/main/java/org/elasticsearch/client/WatcherClient.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java index 94e308220aeb6..e377d41bb09e9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java @@ -26,10 +26,6 @@ import org.elasticsearch.client.watcher.AckWatchResponse; import org.elasticsearch.client.watcher.StartWatchServiceRequest; import org.elasticsearch.client.watcher.StopWatchServiceRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.client.watcher.DeleteWatchRequest; import org.elasticsearch.client.watcher.DeleteWatchResponse; import org.elasticsearch.client.watcher.PutWatchRequest; From 0d62e6abe716507cb734daf5cda884926648091f Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Sat, 20 Oct 2018 11:46:13 +0200 Subject: [PATCH 13/26] revert missed StartWatchServiceRequest --- .../watcher/StartWatchServiceRequest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/StartWatchServiceRequest.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/StartWatchServiceRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/StartWatchServiceRequest.java new file mode 100644 index 0000000000000..c3f34456ecfd7 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/StartWatchServiceRequest.java @@ -0,0 +1,26 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.watcher; + +import org.elasticsearch.client.Validatable; + +public class StartWatchServiceRequest implements Validatable { + +} From 6afde116b2cbdbf578e3ccea7ac645cb5886299a Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Sat, 20 Oct 2018 16:55:52 +0200 Subject: [PATCH 14/26] added StartBasicResponse xpack-hlrc test --- .../client/license/StartBasicResponse.java | 9 +- .../license/StartBasicResponseTests.java | 103 ---------------- .../license/PostStartBasicResponse.java | 116 ++++++++++++++++-- .../license/PutLicenseResponseTests.java | 1 - .../license/StartBasicResponseTests.java | 99 +++++++++++++++ 5 files changed, 211 insertions(+), 117 deletions(-) delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java index f6ab026402462..c2596f3e38a4e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java @@ -84,14 +84,13 @@ public class StartBasicResponse { } } return new Tuple<>(message, acknowledgeMessages); - }, - new ParseField("acknowledge")); + }, new ParseField("acknowledge")); } private Map acknowledgeMessages; private String acknowledgeMessage; - enum Status { + public enum Status { GENERATED_BASIC(true, null, RestStatus.OK), ALREADY_USING_BASIC(false, "Operation failed: Current license is basic.", RestStatus.FORBIDDEN), NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.", RestStatus.OK); @@ -141,6 +140,10 @@ public StartBasicResponse() { this.acknowledgeMessage = acknowledgeMessage; } + public Status getStatus() { + return status; + } + public boolean isAcknowledged() { return status != StartBasicResponse.Status.NEED_ACKNOWLEDGEMENT; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java deleted file mode 100644 index 8370a6ba9afed..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.client.license; - -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.client.common.ProtocolUtils; -import org.elasticsearch.test.ESTestCase; - -import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.equalTo; - -public class StartBasicResponseTests extends ESTestCase { - - public void testFromXContent() throws Exception { - StartBasicResponse.Status status = randomFrom(StartBasicResponse.Status.values()); - - boolean acknowledged = status != StartBasicResponse.Status.NEED_ACKNOWLEDGEMENT; - String acknowledgeMessage = null; - Map ackMessages = Collections.emptyMap(); - if (status != StartBasicResponse.Status.GENERATED_BASIC) { - acknowledgeMessage = randomAlphaOfLength(10); - ackMessages = randomAckMessages(); - } - - final StartBasicResponse startBasicResponse = new StartBasicResponse(status, ackMessages, acknowledgeMessage); - - XContentType xContentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(xContentType); - - toXContent(startBasicResponse, builder); - - final StartBasicResponse response = StartBasicResponse.fromXContent(createParser(builder)); - assertThat(response.isAcknowledged(), equalTo(acknowledged)); - assertThat(response.isBasicStarted(), equalTo(status.isBasicStarted())); - assertThat(response.getAcknowledgeMessage(), equalTo(acknowledgeMessage)); - assertThat(ProtocolUtils.equals(response.getAcknowledgeMessages(), ackMessages), equalTo(true)); - } - - private static void toXContent(StartBasicResponse response, XContentBuilder builder) throws IOException { - builder.startObject(); - builder.field("acknowledged", response.isAcknowledged()); - if (response.isBasicStarted()) { - builder.field("basic_was_started", true); - } else { - builder.field("basic_was_started", false); - builder.field("error_message", response.getErrorMessage()); - } - if (response.getAcknowledgeMessages().isEmpty() == false) { - builder.startObject("acknowledge"); - builder.field("message", response.getAcknowledgeMessage()); - for (Map.Entry entry : response.getAcknowledgeMessages().entrySet()) { - builder.startArray(entry.getKey()); - for (String message : entry.getValue()) { - builder.value(message); - } - builder.endArray(); - } - builder.endObject(); - } - builder.endObject(); - } - - private static Map randomAckMessages() { - int nFeatures = randomIntBetween(1, 5); - - Map ackMessages = new HashMap<>(); - - for (int i = 0; i < nFeatures; i++) { - String feature = randomAlphaOfLengthBetween(9, 15); - int nMessages = randomIntBetween(1, 5); - String[] messages = new String[nMessages]; - for (int j = 0; j < nMessages; j++) { - messages[j] = randomAlphaOfLengthBetween(10, 30); - } - ackMessages.put(feature, messages); - } - - return ackMessages; - } - -} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java index 66fffbe28d397..3d69c4ad6c4ff 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java @@ -6,24 +6,88 @@ package org.elasticsearch.license; import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParseException; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.protocol.xpack.common.ProtocolUtils; import org.elasticsearch.rest.RestStatus; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; -import static org.elasticsearch.license.PostStartBasicResponse.Status.NEED_ACKNOWLEDGEMENT; +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; -class PostStartBasicResponse extends AcknowledgedResponse { +public class PostStartBasicResponse extends AcknowledgedResponse { + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "start_basic_response", true, (a, v) -> { + boolean basicWasStarted = (Boolean) a[0]; + String errorMessage = (String) a[1]; + + if (basicWasStarted) { + return new PostStartBasicResponse(Status.GENERATED_BASIC); + } + Status status = Status.fromErrorMessage(errorMessage); + @SuppressWarnings("unchecked") Tuple> acknowledgements = (Tuple>) a[2]; + return new PostStartBasicResponse(status, acknowledgements.v2(), acknowledgements.v1()); + }); + + private static final ParseField BASIC_WAS_STARTED_FIELD = new ParseField("basic_was_started"); + private static final ParseField ERROR_MESSAGE_FIELD = new ParseField("error_message"); + private static final ParseField ACKNOWLEDGE_FIELD = new ParseField("acknowledge"); + private static final ParseField MESSAGE_FIELD = new ParseField("message"); + + static { + PARSER.declareBoolean(constructorArg(), BASIC_WAS_STARTED_FIELD); + PARSER.declareString(optionalConstructorArg(), ERROR_MESSAGE_FIELD); + PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { + Map acknowledgeMessages = new HashMap<>(); + String message = null; + XContentParser.Token token; + String currentFieldName = null; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else { + if (currentFieldName == null) { + throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); + } + if (MESSAGE_FIELD.getPreferredName().equals(currentFieldName)) { + ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation); + message = parser.text(); + } else { + if (token != XContentParser.Token.START_ARRAY) { + throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); + } + List acknowledgeMessagesList = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation); + acknowledgeMessagesList.add(parser.text()); + } + acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); + } + } + } + return new Tuple<>(message, acknowledgeMessages); + }, ACKNOWLEDGE_FIELD); + } private Map acknowledgeMessages; private String acknowledgeMessage; - enum Status { + public enum Status { GENERATED_BASIC(true, null, RestStatus.OK), ALREADY_USING_BASIC(false, "Operation failed: Current license is basic.", RestStatus.FORBIDDEN), NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.", RestStatus.OK); @@ -49,19 +113,29 @@ String getErrorMessage() { RestStatus getRestStatus() { return restStatus; } + + static Status fromErrorMessage(final String errorMessage) { + final Status[] values = Status.values(); + for (Status status : values) { + if (Objects.equals(status.errorMessage, errorMessage)) { + return status; + } + } + throw new IllegalArgumentException("No status for error message ['" + errorMessage + "']"); + } } private Status status; - PostStartBasicResponse() { + public PostStartBasicResponse() { } PostStartBasicResponse(Status status) { this(status, Collections.emptyMap(), null); } - PostStartBasicResponse(Status status, Map acknowledgeMessages, String acknowledgeMessage) { - super(status != NEED_ACKNOWLEDGEMENT); + public PostStartBasicResponse(Status status, Map acknowledgeMessages, String acknowledgeMessage) { + super(status != Status.NEED_ACKNOWLEDGEMENT); this.status = status; this.acknowledgeMessages = acknowledgeMessages; this.acknowledgeMessage = acknowledgeMessage; @@ -108,14 +182,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { if (status.isBasicStarted()) { - builder.field("basic_was_started", true); + builder.field(BASIC_WAS_STARTED_FIELD.getPreferredName(), true); } else { - builder.field("basic_was_started", false); - builder.field("error_message", status.getErrorMessage()); + builder.field(BASIC_WAS_STARTED_FIELD.getPreferredName(), false); + builder.field(ERROR_MESSAGE_FIELD.getPreferredName(), status.getErrorMessage()); } if (acknowledgeMessages.isEmpty() == false) { builder.startObject("acknowledge"); - builder.field("message", acknowledgeMessage); + builder.field(MESSAGE_FIELD.getPreferredName(), acknowledgeMessage); for (Map.Entry entry : acknowledgeMessages.entrySet()) { builder.startArray(entry.getKey()); for (String message : entry.getValue()) { @@ -126,4 +200,26 @@ protected void addCustomFields(XContentBuilder builder, Params params) throws IO builder.endObject(); } } + + public static PostStartBasicResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + PostStartBasicResponse that = (PostStartBasicResponse) o; + + return status == that.status && + ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) && + Objects.equals(acknowledgeMessage, that.acknowledgeMessage); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), status, ProtocolUtils.hashCode(acknowledgeMessages), acknowledgeMessage); + } + } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java index 00617e4636bab..813975274c9ee 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -29,7 +29,6 @@ public PutLicenseResponse convert(org.elasticsearch.client.license.PutLicenseRes instance.acknowledgeHeader(), instance.acknowledgeMessages()); } - @Override protected boolean supportsUnknownFields() { return true; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java new file mode 100644 index 0000000000000..bb2aabbc22a24 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java @@ -0,0 +1,99 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.protocol.xpack.license; + +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.license.PostStartBasicResponse; +import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Predicate; + +public class StartBasicResponseTests extends AbstractHLRCStreamableXContentTestCase { + + @Override + public org.elasticsearch.client.license.StartBasicResponse doHLRCParseInstance(XContentParser parser) throws IOException { + return org.elasticsearch.client.license.StartBasicResponse.fromXContent(parser); + } + + @Override + public PostStartBasicResponse convert(org.elasticsearch.client.license.StartBasicResponse instance) { + return new PostStartBasicResponse(PostStartBasicResponse.Status.valueOf(instance.getStatus().name()), + instance.getAcknowledgeMessages(), instance.getAcknowledgeMessage()); + } + + @Override + protected PostStartBasicResponse doParseInstance(XContentParser parser) throws IOException { + return PostStartBasicResponse.fromXContent(parser); + } + + @Override + protected PostStartBasicResponse createBlankInstance() { + return new PostStartBasicResponse(); + } + + @Override + protected boolean supportsUnknownFields() { + return true; + } + + @Override + protected Predicate getRandomFieldsExcludeFilter() { + // The structure of the response is such that unknown fields inside acknowledge cannot be supported since they + // are treated as messages from new services + return p -> p.startsWith("acknowledge"); + } + + @Override + protected PostStartBasicResponse createTestInstance() { + PostStartBasicResponse.Status status = randomFrom(PostStartBasicResponse.Status.values()); + String acknowledgeMessage = null; + Map ackMessages = Collections.emptyMap(); + if (status != PostStartBasicResponse.Status.GENERATED_BASIC) { + acknowledgeMessage = randomAlphaOfLength(10); + ackMessages = randomAckMessages(); + } + final PostStartBasicResponse postStartBasicResponse = new PostStartBasicResponse(status, ackMessages, acknowledgeMessage); + logger.info("{}", Strings.toString(postStartBasicResponse)); + return postStartBasicResponse; + } + + private static Map randomAckMessages() { + int nFeatures = randomIntBetween(1, 5); + + Map ackMessages = new HashMap<>(); + + for (int i = 0; i < nFeatures; i++) { + String feature = randomAlphaOfLengthBetween(9, 15); + int nMessages = randomIntBetween(1, 5); + String[] messages = new String[nMessages]; + for (int j = 0; j < nMessages; j++) { + messages[j] = randomAlphaOfLengthBetween(10, 30); + } + ackMessages.put(feature, messages); + } + + return ackMessages; + } +} From 1b28d1fd5a3a3f739ec6b7140920e8228018bbb5 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Sat, 20 Oct 2018 18:36:44 +0200 Subject: [PATCH 15/26] license --- .../license/StartBasicResponseTests.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java index bb2aabbc22a24..2550466002d6d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java @@ -1,20 +1,7 @@ /* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. */ package org.elasticsearch.protocol.xpack.license; From 61be8a31a6fadaa8701d7a5ca6b20be5e5d01c69 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Sat, 20 Oct 2018 21:09:12 +0200 Subject: [PATCH 16/26] XPackInfoResponse clean up --- .../client/xpack/XPackInfoResponse.java | 173 +----------------- .../client/xpack/XPackUsageResponse.java | 2 +- 2 files changed, 6 insertions(+), 169 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java index f9a92d2fbbe02..a89c778347d8f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java @@ -20,31 +20,24 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.client.license.LicenseStatus; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class XPackInfoResponse implements ToXContentObject { +public class XPackInfoResponse { /** * Value of the license's expiration time if it should never expire. */ @@ -85,26 +78,6 @@ public FeatureSetsInfo getFeatureSetsInfo() { return featureSetsInfo; } - @Override - public boolean equals(Object other) { - if (other == null || other.getClass() != getClass()) return false; - if (this == other) return true; - XPackInfoResponse rhs = (XPackInfoResponse) other; - return Objects.equals(buildInfo, rhs.buildInfo) - && Objects.equals(licenseInfo, rhs.licenseInfo) - && Objects.equals(featureSetsInfo, rhs.featureSetsInfo); - } - - @Override - public int hashCode() { - return Objects.hash(buildInfo, licenseInfo, featureSetsInfo); - } - - @Override - public String toString() { - return Strings.toString(this, true, false); - } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "xpack_info_response", true, (a, v) -> { BuildInfo buildInfo = (BuildInfo) a[0]; @@ -135,37 +108,7 @@ public static XPackInfoResponse fromXContent(XContentParser parser) throws IOExc return PARSER.parse(parser, null); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - - if (buildInfo != null) { - builder.field("build", buildInfo, params); - } - - EnumSet categories = XPackInfoRequest.Category - .toSet(Strings.splitStringByCommaToArray(params.param("categories", "_all"))); - if (licenseInfo != null) { - builder.field("license", licenseInfo, params); - } else if (categories.contains(XPackInfoRequest.Category.LICENSE)) { - // if the user requested the license info, and there is no license, we should send - // back an explicit null value (indicating there is no license). This is different - // than not adding the license info at all - builder.nullField("license"); - } - - if (featureSetsInfo != null) { - builder.field("features", featureSetsInfo, params); - } - - if (params.paramAsBoolean("human", true)) { - builder.field("tagline", "You know, for X"); - } - - return builder.endObject(); - } - - public static class LicenseInfo implements ToXContentObject { + public static class LicenseInfo { private final String uid; private final String type; private final String mode; @@ -200,23 +143,6 @@ public LicenseStatus getStatus() { return status; } - @Override - public boolean equals(Object other) { - if (other == null || other.getClass() != getClass()) return false; - if (this == other) return true; - LicenseInfo rhs = (LicenseInfo) other; - return Objects.equals(uid, rhs.uid) - && Objects.equals(type, rhs.type) - && Objects.equals(mode, rhs.mode) - && Objects.equals(status, rhs.status) - && expiryDate == rhs.expiryDate; - } - - @Override - public int hashCode() { - return Objects.hash(uid, type, mode, status, expiryDate); - } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "license_info", true, (a, v) -> { String uid = (String) a[0]; @@ -234,22 +160,9 @@ public int hashCode() { PARSER.declareString(constructorArg(), new ParseField("status")); PARSER.declareLong(optionalConstructorArg(), new ParseField("expiry_date_in_millis")); } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .field("uid", uid) - .field("type", type) - .field("mode", mode) - .field("status", status.label()); - if (expiryDate != BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) { - builder.timeField("expiry_date_in_millis", "expiry_date", expiryDate); - } - return builder.endObject(); - } } - public static class BuildInfo implements ToXContentObject { + public static class BuildInfo { private final String hash; private final String timestamp; @@ -266,37 +179,15 @@ public String getTimestamp() { return timestamp; } - @Override - public boolean equals(Object other) { - if (other == null || other.getClass() != getClass()) return false; - if (this == other) return true; - BuildInfo rhs = (BuildInfo) other; - return Objects.equals(hash, rhs.hash) - && Objects.equals(timestamp, rhs.timestamp); - } - - @Override - public int hashCode() { - return Objects.hash(hash, timestamp); - } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "build_info", true, (a, v) -> new BuildInfo((String) a[0], (String) a[1])); static { PARSER.declareString(constructorArg(), new ParseField("hash")); PARSER.declareString(constructorArg(), new ParseField("date")); } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("hash", hash) - .field("date", timestamp) - .endObject(); - } } - public static class FeatureSetsInfo implements ToXContentObject { + public static class FeatureSetsInfo { private final Map featureSets; public FeatureSetsInfo(Set featureSets) { @@ -311,30 +202,7 @@ public Map getFeatureSets() { return featureSets; } - @Override - public boolean equals(Object other) { - if (other == null || other.getClass() != getClass()) return false; - if (this == other) return true; - FeatureSetsInfo rhs = (FeatureSetsInfo) other; - return Objects.equals(featureSets, rhs.featureSets); - } - - @Override - public int hashCode() { - return Objects.hash(featureSets); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - List names = new ArrayList<>(this.featureSets.keySet()).stream().sorted().collect(Collectors.toList()); - for (String name : names) { - builder.field(name, featureSets.get(name), params); - } - return builder.endObject(); - } - - public static class FeatureSet implements ToXContentObject { + public static class FeatureSet { private final String name; @Nullable private final String description; private final boolean available; @@ -372,23 +240,6 @@ public Map nativeCodeInfo() { return nativeCodeInfo; } - @Override - public boolean equals(Object other) { - if (other == null || other.getClass() != getClass()) return false; - if (this == other) return true; - FeatureSet rhs = (FeatureSet) other; - return Objects.equals(name, rhs.name) - && Objects.equals(description, rhs.description) - && available == rhs.available - && enabled == rhs.enabled - && Objects.equals(nativeCodeInfo, rhs.nativeCodeInfo); - } - - @Override - public int hashCode() { - return Objects.hash(name, description, available, enabled, nativeCodeInfo); - } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "feature_set", true, (a, name) -> { String description = (String) a[0]; @@ -404,20 +255,6 @@ public int hashCode() { PARSER.declareBoolean(constructorArg(), new ParseField("enabled")); PARSER.declareObject(optionalConstructorArg(), (p, name) -> p.map(), new ParseField("native_code_info")); } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - if (description != null) { - builder.field("description", description); - } - builder.field("available", available); - builder.field("enabled", enabled); - if (nativeCodeInfo != null) { - builder.field("native_code_info", nativeCodeInfo); - } - return builder.endObject(); - } } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java index b51a2d7de9fbd..2f9c99cc65e09 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java @@ -34,7 +34,7 @@ public class XPackUsageResponse { private final Map> usages; - private XPackUsageResponse(Map> usages) throws IOException { + private XPackUsageResponse(Map> usages) { this.usages = usages; } From b5372c4b24e3d6b0cd1e6ee7105ae1b38e503ac7 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Wed, 24 Oct 2018 21:27:17 +0200 Subject: [PATCH 17/26] code style enhancements --- ...bstractHlrcStreamableXContentTestCase.java | 46 +++++++++++++++++++ .../AbstractHlrcXContentTestCase.java | 41 +++++++++++++++++ .../xpack/XPackInfoResponseTests.java | 41 +++++++++-------- .../graph/GraphExploreResponseTests.java | 40 +++++++++------- .../license/PutLicenseResponseTests.java | 10 ++-- .../license/StartBasicResponseTests.java | 10 ++-- .../IndexUpgradeInfoResponseTests.java | 10 ++-- .../watcher/DeleteWatchResponseTests.java | 10 ++-- .../xpack/watcher/PutWatchResponseTests.java | 10 ++-- 9 files changed, 159 insertions(+), 59 deletions(-) create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java new file mode 100644 index 0000000000000..315ce395aaf9e --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol; + +import org.elasticsearch.common.io.stream.Streamable; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; + +import java.io.IOException; + +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; + +public abstract class AbstractHlrcStreamableXContentTestCase + extends AbstractStreamableXContentTestCase { + + /** + * Generic test that creates new instance of HLRC request/response from the test instance and checks + * both for equality and asserts equality on the two queries. + */ + public final void testHlrcFromXContent() throws IOException { + xContentTester(this::createParser, this::createTestInstance, getToXContentParams(), + p -> convertHlrcToInternal(doHlrcParseInstance(p))) + .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + .supportsUnknownFields(supportsUnknownFields()) + .shuffleFieldsExceptions(getShuffleFieldsExceptions()) + .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) + .assertEqualsConsumer(this::assertEqualInstances) + .assertToXContentEquivalence(true) + .test(); + } + + /** + * Parses to a new HLRC instance using the provided {@link XContentParser} + */ + public abstract H doHlrcParseInstance(XContentParser parser) throws IOException; + + /** + * Converts a HLRC instance to a XPack instance + */ + public abstract T convertHlrcToInternal(H instance); + +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java new file mode 100644 index 0000000000000..0b72fbed4b77b --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol; + +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +public abstract class AbstractHlrcXContentTestCase extends AbstractXContentTestCase { + + /** + * Generic test that creates new instance of HLRC request/response from the test instance and checks + * both for equality and asserts equality on the two queries. + */ + public final void testHlrcFromXContent() throws IOException { + xContentTester(this::createParser, this::createTestInstance, getToXContentParams(), + p -> convertHlrcToInternal(doHlrcParseInstance(p))) + .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + .supportsUnknownFields(supportsUnknownFields()) + .shuffleFieldsExceptions(getShuffleFieldsExceptions()) + .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) + .assertEqualsConsumer(this::assertEqualInstances) + .assertToXContentEquivalence(true) + .test(); + } + + /** + * Parses to a new HLRC instance using the provided {@link XContentParser} + */ + public abstract H doHlrcParseInstance(XContentParser parser) throws IOException; + + /** + * Converts a HLRC instance to a XPack instance + */ + public abstract T convertHlrcToInternal(H instance); +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java index 1871ed4c837f6..1e77d6a83f2ed 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoResponseTests.java @@ -7,7 +7,7 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase; import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; @@ -23,8 +23,9 @@ import java.io.IOException; import java.util.stream.Collectors; -public class XPackInfoResponseTests extends AbstractHLRCStreamableXContentTestCase { +public class XPackInfoResponseTests extends + AbstractHlrcStreamableXContentTestCase { + @Override protected XPackInfoResponse doParseInstance(XContentParser parser) throws IOException { return XPackInfoResponse.fromXContent(parser); @@ -36,31 +37,35 @@ protected XPackInfoResponse createBlankInstance() { } @Override - public org.elasticsearch.client.xpack.XPackInfoResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.xpack.XPackInfoResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.xpack.XPackInfoResponse.fromXContent(parser); } @Override - public XPackInfoResponse convert(org.elasticsearch.client.xpack.XPackInfoResponse instance) { - final org.elasticsearch.client.xpack.XPackInfoResponse.BuildInfo buildInfo = instance.getBuildInfo(); - final XPackInfoResponse.BuildInfo buildInfo1 = - buildInfo != null ? new BuildInfo(buildInfo.getHash(), buildInfo.getTimestamp()) : null; - final org.elasticsearch.client.xpack.XPackInfoResponse.LicenseInfo licenseInfo = instance.getLicenseInfo(); - final XPackInfoResponse.LicenseInfo licenseInfo1 = - licenseInfo != null + public XPackInfoResponse convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfoResponse instance) { + return new XPackInfoResponse(convertHlrcToInternal(instance.getBuildInfo()), + convertHlrcToInternal(instance.getLicenseInfo()), convertHlrcToInternal(instance.getFeatureSetsInfo())); + } + + private BuildInfo convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfoResponse.BuildInfo buildInfo) { + return buildInfo != null ? new BuildInfo(buildInfo.getHash(), buildInfo.getTimestamp()) : null; + } + + private LicenseInfo convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfoResponse.LicenseInfo licenseInfo) { + return licenseInfo != null ? new LicenseInfo(licenseInfo.getUid(), licenseInfo.getType(), licenseInfo.getMode(), licenseInfo.getStatus() != null ? LicenseStatus.valueOf(licenseInfo.getStatus().name()) : null, licenseInfo.getExpiryDate()) : null; - final org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo featureSetsInfo = instance.getFeatureSetsInfo(); - final XPackInfoResponse.FeatureSetsInfo featureSetsInfo1 = - featureSetsInfo != null + } + + private FeatureSetsInfo convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo featureSetsInfo) { + return featureSetsInfo != null ? new FeatureSetsInfo(featureSetsInfo.getFeatureSets().values().stream() - .map(fs -> new XPackInfoResponse.FeatureSetsInfo.FeatureSet(fs.name(), fs.description(), fs.available(), fs.enabled(), - fs.nativeCodeInfo())) - .collect(Collectors.toSet())) + .map(fs -> new FeatureSet(fs.name(), fs.description(), fs.available(), fs.enabled(), + fs.nativeCodeInfo())) + .collect(Collectors.toSet())) : null; - return new XPackInfoResponse(buildInfo1, licenseInfo1, featureSetsInfo1); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java index a4bdcc8b941cb..3235f62b3e481 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponseTests.java @@ -10,7 +10,7 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.AbstractHLRCXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcXContentTestCase; import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -26,43 +26,51 @@ import static org.hamcrest.Matchers.equalTo; -public class GraphExploreResponseTests extends AbstractHLRCXContentTestCase { +public class GraphExploreResponseTests extends + AbstractHlrcXContentTestCase { + + static final Function VERTEX_ID_FUNCTION = + vId -> new Vertex.VertexId(vId.getField(), vId.getTerm()); + static final Function VERTEX_FUNCTION = + v -> new Vertex(v.getField(), v.getTerm(), v.getWeight(), v.getHopDepth(), v.getBg(), v.getFg()); @Override - public org.elasticsearch.client.graph.GraphExploreResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.graph.GraphExploreResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.graph.GraphExploreResponse.fromXContent(parser); } @Override - public GraphExploreResponse convert(org.elasticsearch.client.graph.GraphExploreResponse instance) { + public GraphExploreResponse convertHlrcToInternal(org.elasticsearch.client.graph.GraphExploreResponse instance) { + return new GraphExploreResponse(instance.getTookInMillis(), instance.isTimedOut(), + instance.getShardFailures(), convertVertices(instance), convertConnections(instance), instance.isReturnDetailedInfo()); + } + + public Map convertVertices(org.elasticsearch.client.graph.GraphExploreResponse instance) { final Collection vertexIds = instance.getVertexIds(); final Map vertexMap = new LinkedHashMap<>(vertexIds.size()); - final Function vertexIdFn = - vId -> new Vertex.VertexId(vId.getField(), vId.getTerm()); - final Function vertexFn = - v -> new Vertex(v.getField(), v.getTerm(), v.getWeight(), v.getHopDepth(), v.getBg(), v.getFg()); - for (org.elasticsearch.client.graph.Vertex.VertexId vertexId : vertexIds) { final org.elasticsearch.client.graph.Vertex vertex = instance.getVertex(vertexId); - vertexMap.put(vertexIdFn.apply(vertexId), vertexFn.apply(vertex)); + vertexMap.put(VERTEX_ID_FUNCTION.apply(vertexId), VERTEX_FUNCTION.apply(vertex)); } + return vertexMap; + } + public Map convertConnections(org.elasticsearch.client.graph.GraphExploreResponse instance) { final Collection connectionIds = instance.getConnectionIds(); final Map connectionMap = new LinkedHashMap<>(connectionIds.size()); for (org.elasticsearch.client.graph.Connection.ConnectionId connectionId : connectionIds) { final org.elasticsearch.client.graph.Connection connection = instance.getConnection(connectionId); final Connection.ConnectionId connectionId1 = - new Connection.ConnectionId(vertexIdFn.apply(connectionId.getSource()), vertexIdFn.apply(connectionId.getTarget())); - final Connection connection1 = new Connection(vertexFn.apply(connection.getFrom()), vertexFn.apply(connection.getTo()), + new Connection.ConnectionId(VERTEX_ID_FUNCTION.apply(connectionId.getSource()), + VERTEX_ID_FUNCTION.apply(connectionId.getTarget())); + final Connection connection1 = new Connection(VERTEX_FUNCTION.apply(connection.getFrom()), + VERTEX_FUNCTION.apply(connection.getTo()), connection.getWeight(), connection.getDocCount()); connectionMap.put(connectionId1, connection1); } - - return new GraphExploreResponse(instance.getTookInMillis(), instance.isTimedOut(), - instance.getShardFailures(), vertexMap, connectionMap, instance.isReturnDetailedInfo()); + return connectionMap; } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java index 813975274c9ee..87ba4324ec1e3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -6,7 +6,7 @@ package org.elasticsearch.protocol.xpack.license; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase; import java.io.IOException; import java.util.Collections; @@ -15,16 +15,16 @@ import java.util.function.Function; import java.util.function.Predicate; -public class PutLicenseResponseTests extends AbstractHLRCStreamableXContentTestCase { +public class PutLicenseResponseTests extends + AbstractHlrcStreamableXContentTestCase { @Override - public org.elasticsearch.client.license.PutLicenseResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.license.PutLicenseResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); } @Override - public PutLicenseResponse convert(org.elasticsearch.client.license.PutLicenseResponse instance) { + public PutLicenseResponse convertHlrcToInternal(org.elasticsearch.client.license.PutLicenseResponse instance) { return new PutLicenseResponse(instance.isAcknowledged(), LicensesStatus.valueOf(instance.status().name()), instance.acknowledgeHeader(), instance.acknowledgeMessages()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java index 2550466002d6d..78e1c75483aa6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java @@ -8,7 +8,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.license.PostStartBasicResponse; -import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase; import java.io.IOException; import java.util.Collections; @@ -16,16 +16,16 @@ import java.util.Map; import java.util.function.Predicate; -public class StartBasicResponseTests extends AbstractHLRCStreamableXContentTestCase { +public class StartBasicResponseTests extends + AbstractHlrcStreamableXContentTestCase { @Override - public org.elasticsearch.client.license.StartBasicResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.license.StartBasicResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.license.StartBasicResponse.fromXContent(parser); } @Override - public PostStartBasicResponse convert(org.elasticsearch.client.license.StartBasicResponse instance) { + public PostStartBasicResponse convertHlrcToInternal(org.elasticsearch.client.license.StartBasicResponse instance) { return new PostStartBasicResponse(PostStartBasicResponse.Status.valueOf(instance.getStatus().name()), instance.getAcknowledgeMessages(), instance.getAcknowledgeMessage()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java index 31cd300656ad3..76f00ebb24309 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/migration/IndexUpgradeInfoResponseTests.java @@ -6,7 +6,7 @@ package org.elasticsearch.protocol.xpack.migration; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.AbstractHLRCStreamableXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase; import java.io.IOException; import java.util.AbstractMap; @@ -15,8 +15,8 @@ import java.util.Map; import java.util.stream.Collectors; -public class IndexUpgradeInfoResponseTests extends AbstractHLRCStreamableXContentTestCase { +public class IndexUpgradeInfoResponseTests extends + AbstractHlrcStreamableXContentTestCase { @Override protected IndexUpgradeInfoResponse doParseInstance(XContentParser parser) { @@ -24,12 +24,12 @@ protected IndexUpgradeInfoResponse doParseInstance(XContentParser parser) { } @Override - public org.elasticsearch.client.migration.IndexUpgradeInfoResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.migration.IndexUpgradeInfoResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.migration.IndexUpgradeInfoResponse.fromXContent(parser); } @Override - public IndexUpgradeInfoResponse convert(org.elasticsearch.client.migration.IndexUpgradeInfoResponse instance) { + public IndexUpgradeInfoResponse convertHlrcToInternal(org.elasticsearch.client.migration.IndexUpgradeInfoResponse instance) { final Map actions = instance.getActions(); return new IndexUpgradeInfoResponse(actions.entrySet().stream().map( e -> new AbstractMap.SimpleEntry<>(e.getKey(), UpgradeActionRequired.valueOf(e.getValue().name())) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java index a65fb67fa56e8..7486252f53859 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponseTests.java @@ -6,12 +6,12 @@ package org.elasticsearch.protocol.xpack.watcher; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.AbstractHLRCXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcXContentTestCase; import java.io.IOException; -public class DeleteWatchResponseTests extends AbstractHLRCXContentTestCase { +public class DeleteWatchResponseTests extends + AbstractHlrcXContentTestCase { @Override protected DeleteWatchResponse createTestInstance() { @@ -27,12 +27,12 @@ protected DeleteWatchResponse doParseInstance(XContentParser parser) throws IOEx } @Override - public org.elasticsearch.client.watcher.DeleteWatchResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.watcher.DeleteWatchResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.watcher.DeleteWatchResponse.fromXContent(parser); } @Override - public DeleteWatchResponse convert(org.elasticsearch.client.watcher.DeleteWatchResponse instance) { + public DeleteWatchResponse convertHlrcToInternal(org.elasticsearch.client.watcher.DeleteWatchResponse instance) { return new DeleteWatchResponse(instance.getId(), instance.getVersion(), instance.isFound()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java index b3b069d0ec221..8ea4a84daed95 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponseTests.java @@ -6,12 +6,12 @@ package org.elasticsearch.protocol.xpack.watcher; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.AbstractHLRCXContentTestCase; +import org.elasticsearch.protocol.AbstractHlrcXContentTestCase; import java.io.IOException; -public class PutWatchResponseTests extends AbstractHLRCXContentTestCase { +public class PutWatchResponseTests extends + AbstractHlrcXContentTestCase { @Override protected PutWatchResponse createTestInstance() { @@ -27,12 +27,12 @@ protected PutWatchResponse doParseInstance(XContentParser parser) throws IOExcep } @Override - public org.elasticsearch.client.watcher.PutWatchResponse doHLRCParseInstance(XContentParser parser) throws IOException { + public org.elasticsearch.client.watcher.PutWatchResponse doHlrcParseInstance(XContentParser parser) throws IOException { return org.elasticsearch.client.watcher.PutWatchResponse.fromXContent(parser); } @Override - public PutWatchResponse convert(org.elasticsearch.client.watcher.PutWatchResponse instance) { + public PutWatchResponse convertHlrcToInternal(org.elasticsearch.client.watcher.PutWatchResponse instance) { return new PutWatchResponse(instance.getId(), instance.getVersion(), instance.isCreated()); } From 6c3b19c3c9833779eab79bb3a45dd6f3c092b934 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Thu, 25 Oct 2018 11:20:56 +0200 Subject: [PATCH 18/26] drop FS case sensitive mess --- ...bstractHLRCStreamableXContentTestCase.java | 34 -------------- .../AbstractHLRCXContentTestCase.java | 30 ------------ ...bstractHlrcStreamableXContentTestCase.java | 46 ------------------- .../AbstractHlrcXContentTestCase.java | 41 ----------------- 4 files changed, 151 deletions(-) delete mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java delete mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java delete mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java delete mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java deleted file mode 100644 index fcae15af45462..0000000000000 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCStreamableXContentTestCase.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol; - -import org.elasticsearch.common.io.stream.Streamable; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; - -public abstract class AbstractHLRCStreamableXContentTestCase - extends AbstractStreamableXContentTestCase { - - /** - * Generic test that creates new instance of HLRC request/response from the test instance and checks - * both for equality and asserts equality on the two queries. - */ - public final void testHLRCFromXContent() throws IOException { - AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, this::createTestInstance, supportsUnknownFields(), - getShuffleFieldsExceptions(), getRandomFieldsExcludeFilter(), this::createParser, - p -> convert(doHLRCParseInstance(p)), - this::assertEqualInstances, true, getToXContentParams()); - } - - public abstract H doHLRCParseInstance(XContentParser parser) throws IOException; - - public abstract T convert(H instance); - -} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java deleted file mode 100644 index 56b14d2249ec3..0000000000000 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHLRCXContentTestCase.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol; - -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; - -public abstract class AbstractHLRCXContentTestCase extends AbstractXContentTestCase { - - /** - * Generic test that creates new instance of HLRC request/response from the test instance and checks - * both for equality and asserts equality on the two queries. - */ - public final void testHLRCFromXContent() throws IOException { - AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, this::createTestInstance, supportsUnknownFields(), - getShuffleFieldsExceptions(), getRandomFieldsExcludeFilter(), this::createParser, - p -> convert(doHLRCParseInstance(p)), - this::assertEqualInstances, true, getToXContentParams()); - } - - public abstract H doHLRCParseInstance(XContentParser parser) throws IOException; - - public abstract T convert(H instance); -} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java deleted file mode 100644 index 315ce395aaf9e..0000000000000 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol; - -import org.elasticsearch.common.io.stream.Streamable; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractStreamableXContentTestCase; - -import java.io.IOException; - -import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; - -public abstract class AbstractHlrcStreamableXContentTestCase - extends AbstractStreamableXContentTestCase { - - /** - * Generic test that creates new instance of HLRC request/response from the test instance and checks - * both for equality and asserts equality on the two queries. - */ - public final void testHlrcFromXContent() throws IOException { - xContentTester(this::createParser, this::createTestInstance, getToXContentParams(), - p -> convertHlrcToInternal(doHlrcParseInstance(p))) - .numberOfTestRuns(NUMBER_OF_TEST_RUNS) - .supportsUnknownFields(supportsUnknownFields()) - .shuffleFieldsExceptions(getShuffleFieldsExceptions()) - .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) - .assertEqualsConsumer(this::assertEqualInstances) - .assertToXContentEquivalence(true) - .test(); - } - - /** - * Parses to a new HLRC instance using the provided {@link XContentParser} - */ - public abstract H doHlrcParseInstance(XContentParser parser) throws IOException; - - /** - * Converts a HLRC instance to a XPack instance - */ - public abstract T convertHlrcToInternal(H instance); - -} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java deleted file mode 100644 index 0b72fbed4b77b..0000000000000 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.protocol; - -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; - -public abstract class AbstractHlrcXContentTestCase extends AbstractXContentTestCase { - - /** - * Generic test that creates new instance of HLRC request/response from the test instance and checks - * both for equality and asserts equality on the two queries. - */ - public final void testHlrcFromXContent() throws IOException { - xContentTester(this::createParser, this::createTestInstance, getToXContentParams(), - p -> convertHlrcToInternal(doHlrcParseInstance(p))) - .numberOfTestRuns(NUMBER_OF_TEST_RUNS) - .supportsUnknownFields(supportsUnknownFields()) - .shuffleFieldsExceptions(getShuffleFieldsExceptions()) - .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) - .assertEqualsConsumer(this::assertEqualInstances) - .assertToXContentEquivalence(true) - .test(); - } - - /** - * Parses to a new HLRC instance using the provided {@link XContentParser} - */ - public abstract H doHlrcParseInstance(XContentParser parser) throws IOException; - - /** - * Converts a HLRC instance to a XPack instance - */ - public abstract T convertHlrcToInternal(H instance); -} From 775ef01bbfa1629369ef19323c02103e8cd3bb23 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Thu, 25 Oct 2018 11:30:28 +0200 Subject: [PATCH 19/26] fix FS case sensitive mess --- ...bstractHlrcStreamableXContentTestCase.java | 46 +++++++++++++++++++ .../AbstractHlrcXContentTestCase.java | 36 +++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java new file mode 100644 index 0000000000000..315ce395aaf9e --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol; + +import org.elasticsearch.common.io.stream.Streamable; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; + +import java.io.IOException; + +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; + +public abstract class AbstractHlrcStreamableXContentTestCase + extends AbstractStreamableXContentTestCase { + + /** + * Generic test that creates new instance of HLRC request/response from the test instance and checks + * both for equality and asserts equality on the two queries. + */ + public final void testHlrcFromXContent() throws IOException { + xContentTester(this::createParser, this::createTestInstance, getToXContentParams(), + p -> convertHlrcToInternal(doHlrcParseInstance(p))) + .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + .supportsUnknownFields(supportsUnknownFields()) + .shuffleFieldsExceptions(getShuffleFieldsExceptions()) + .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) + .assertEqualsConsumer(this::assertEqualInstances) + .assertToXContentEquivalence(true) + .test(); + } + + /** + * Parses to a new HLRC instance using the provided {@link XContentParser} + */ + public abstract H doHlrcParseInstance(XContentParser parser) throws IOException; + + /** + * Converts a HLRC instance to a XPack instance + */ + public abstract T convertHlrcToInternal(H instance); + +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java new file mode 100644 index 0000000000000..d6d8f9afe3659 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcXContentTestCase.java @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol; + +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +public abstract class AbstractHlrcXContentTestCase extends AbstractXContentTestCase { + + /** + * Generic test that creates new instance of HLRC request/response from the test instance and checks + * both for equality and asserts equality on the two queries. + */ + public final void testHlrcFromXContent() throws IOException { + AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, this::createTestInstance, supportsUnknownFields(), + getShuffleFieldsExceptions(), getRandomFieldsExcludeFilter(), this::createParser, + p -> convertHlrcToInternal(doHlrcParseInstance(p)), + this::assertEqualInstances, true, getToXContentParams()); + } + + /** + * Parses to a new HLRC instance using the provided {@link XContentParser} + */ + public abstract H doHlrcParseInstance(XContentParser parser) throws IOException; + + /** + * Converts a HLRC instance to a XPack instance + */ + public abstract T convertHlrcToInternal(H instance); +} From 7a7ca56b7c816306738fb9f31639401e7aca34b6 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Fri, 26 Oct 2018 08:21:04 +0200 Subject: [PATCH 20/26] enforce validation in ctor --- .../client/watcher/DeleteWatchRequest.java | 32 +++--------- .../client/watcher/PutWatchRequest.java | 49 ++++--------------- .../client/WatcherRequestConvertersTests.java | 7 +-- 3 files changed, 18 insertions(+), 70 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java index 6009b70ffcb55..76783a70050ab 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java @@ -19,24 +19,22 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.lucene.uid.Versions; -import java.util.Optional; +import java.util.Objects; /** * A delete watch request to delete an watch by name (id) */ public class DeleteWatchRequest implements Validatable { - private String id; - private long version = Versions.MATCH_ANY; - - public DeleteWatchRequest() { - this(null); - } + private final String id; public DeleteWatchRequest(String id) { + Objects.requireNonNull(id, "watch id is missing"); + if (PutWatchRequest.isValidId(id) == false) { + throw new IllegalArgumentException("watch id contains whitespace"); + } this.id = id; } @@ -47,24 +45,6 @@ public String getId() { return id; } - /** - * Sets the name of the watch to be deleted - */ - public void setId(String id) { - this.id = id; - } - - @Override - public Optional validate() { - ValidationException exception = new ValidationException(); - if (id == null) { - exception.addValidationError("watch id is missing"); - } else if (PutWatchRequest.isValidId(id) == false) { - exception.addValidationError("watch id contains whitespace"); - } - return exception.validationErrors().isEmpty() ? Optional.empty() : Optional.of(exception); - } - @Override public String toString() { return "delete [" + id + "]"; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java index 0601d457503a4..88f47aeaeee7a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java @@ -19,13 +19,12 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.xcontent.XContentType; -import java.util.Optional; +import java.util.Objects; import java.util.regex.Pattern; /** @@ -36,15 +35,19 @@ public final class PutWatchRequest implements Validatable { private static final Pattern NO_WS_PATTERN = Pattern.compile("\\S+"); - private String id; - private BytesReference source; - private XContentType xContentType = XContentType.JSON; + private final String id; + private final BytesReference source; + private final XContentType xContentType; private boolean active = true; private long version = Versions.MATCH_ANY; - public PutWatchRequest() {} - public PutWatchRequest(String id, BytesReference source, XContentType xContentType) { + Objects.requireNonNull(id, "watch id is missing"); + if (isValidId(id) == false) { + throw new IllegalArgumentException("watch id contains whitespace"); + } + Objects.requireNonNull(source, "watch source is missing"); + Objects.requireNonNull(xContentType, "request body is missing"); this.id = id; this.source = source; this.xContentType = xContentType; @@ -57,13 +60,6 @@ public String getId() { return id; } - /** - * Set the watch name - */ - public void setId(String id) { - this.id = id; - } - /** * @return The source of the watch */ @@ -71,14 +67,6 @@ public BytesReference getSource() { return source; } - /** - * Set the source of the watch - */ - public void setSource(BytesReference source, XContentType xContentType) { - this.source = source; - this.xContentType = xContentType; - } - /** * @return The initial active state of the watch (defaults to {@code true}, e.g. "active") */ @@ -108,23 +96,6 @@ public void setVersion(long version) { this.version = version; } - @Override - public Optional validate() { - ValidationException exception = new ValidationException(); - if (id == null) { - exception.addValidationError("watch id is missing"); - } else if (isValidId(id) == false) { - exception.addValidationError("watch id contains whitespace"); - } - if (source == null) { - exception.addValidationError("watch source is missing"); - } - if (xContentType == null) { - exception.addValidationError("request body is missing"); - } - return exception.validationErrors().isEmpty() ? Optional.empty() : Optional.of(exception); - } - public static boolean isValidId(String id) { return Strings.isEmpty(id) == false && NO_WS_PATTERN.matcher(id).matches(); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java index e06220f804299..79cf543e83366 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java @@ -55,11 +55,9 @@ public void testStopWatchService() { } public void testPutWatch() throws Exception { - PutWatchRequest putWatchRequest = new PutWatchRequest(); String watchId = randomAlphaOfLength(10); - putWatchRequest.setId(watchId); String body = randomAlphaOfLength(20); - putWatchRequest.setSource(new BytesArray(body), XContentType.JSON); + PutWatchRequest putWatchRequest = new PutWatchRequest(watchId, new BytesArray(body), XContentType.JSON); Map expectedParams = new HashMap<>(); if (randomBoolean()) { @@ -84,9 +82,8 @@ public void testPutWatch() throws Exception { } public void testDeleteWatch() { - DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(); String watchId = randomAlphaOfLength(10); - deleteWatchRequest.setId(watchId); + DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(watchId); Request request = WatcherRequestConverters.deleteWatch(deleteWatchRequest); assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); From 7a776f3595b8ee603373231cb8ed3cc74e0d0a65 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 29 Oct 2018 08:35:26 +0100 Subject: [PATCH 21/26] fix wrong import --- .../elasticsearch/client/watcher/DeactivateWatchRequest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index b20a56c361f8f..ccbd64358e154 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -19,7 +19,7 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.PutWatchRequest; import java.util.Objects; @@ -27,7 +27,6 @@ public class DeactivateWatchRequest implements Validatable { private final String watchId; public DeactivateWatchRequest(String watchId) { - Objects.requireNonNull(watchId, "watch id is missing"); if (PutWatchRequest.isValidId(watchId) == false) { throw new IllegalArgumentException("watch id contains whitespace"); From 4669e4ae73937d439c06347dd68002617351bdf3 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 29 Oct 2018 09:04:46 +0100 Subject: [PATCH 22/26] fix wrong import --- .../org/elasticsearch/client/watcher/DeleteWatchRequest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java index 76783a70050ab..3abee6fb47e3f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchRequest.java @@ -19,7 +19,6 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.common.lucene.uid.Versions; import java.util.Objects; From ce3bd665a952607cc588a3b201cfbdf31af9e4b9 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 29 Oct 2018 10:25:08 +0100 Subject: [PATCH 23/26] fixed test due to enforce ctor checks in DeleteWatchRequest/PutWatchRequest --- .../watcher/WatchRequestValidationTests.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java index adcb530581fc2..1fea3bccb62a7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java @@ -57,42 +57,38 @@ public void testAcknowledgeWatchNullActionId() { } public void testDeleteWatchInvalidWatchId() { - final Optional validationException = new DeleteWatchRequest("id with whitespaces").validate(); - assertThat(validationException.isPresent(), is(true)); - assertThat(validationException.get().validationErrors(), hasItem("watch id contains whitespace")); + final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, + () -> new DeleteWatchRequest("id with whitespaces")); + assertThat(exception.getMessage(), is("watch id contains whitespace")); } public void testDeleteWatchNullId() { - final Optional validationException = new DeleteWatchRequest(null).validate(); - assertThat(validationException.isPresent(), is(true)); - assertThat(validationException.get().validationErrors(), hasItem("watch id is missing")); + final NullPointerException exception = expectThrows(NullPointerException.class, + () -> new DeleteWatchRequest(null)); + assertThat(exception.getMessage(), is("watch id is missing")); } public void testPutWatchInvalidWatchId() { - final Optional validationException = - new PutWatchRequest("id with whitespaces", BytesArray.EMPTY, XContentType.JSON).validate(); - assertThat(validationException.isPresent(), is(true)); - assertThat(validationException.get().validationErrors(), hasItem("watch id contains whitespace")); + final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, + () -> new PutWatchRequest("id with whitespaces", BytesArray.EMPTY, XContentType.JSON)); + assertThat(exception.getMessage(), is("watch id contains whitespace")); } public void testPutWatchNullId() { - final Optional validationException = - new PutWatchRequest(null, BytesArray.EMPTY, XContentType.JSON).validate(); - assertThat(validationException.isPresent(), is(true)); - assertThat(validationException.get().validationErrors(), hasItem("watch id is missing")); + final NullPointerException exception = expectThrows(NullPointerException.class, + () -> new PutWatchRequest(null, BytesArray.EMPTY, XContentType.JSON)); + assertThat(exception.getMessage(), is("watch id is missing")); } public void testPutWatchSourceNull() { - final Optional validationException = - new PutWatchRequest("foo", null, XContentType.JSON).validate(); - assertThat(validationException.isPresent(), is(true)); - assertThat(validationException.get().validationErrors(), hasItem("watch source is missing")); + final NullPointerException exception = expectThrows(NullPointerException.class, + () -> new PutWatchRequest("foo", null, XContentType.JSON)); + assertThat(exception.getMessage(), is("watch source is missing")); } public void testPutWatchContentNull() { - final Optional validationException = - new PutWatchRequest("foo", BytesArray.EMPTY, null).validate(); - assertThat(validationException.isPresent(), is(true)); - assertThat(validationException.get().validationErrors(), hasItem("request body is missing")); + final NullPointerException exception = expectThrows(NullPointerException.class, + () -> new PutWatchRequest("foo", BytesArray.EMPTY, null)); + assertThat(exception.getMessage(), is("request body is missing")); } } From 36ffbd86f1fd76a55b9f680e5cb6d266c086ca95 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 29 Oct 2018 16:46:16 +0100 Subject: [PATCH 24/26] drop empty file --- .../client/migration/IndexUpgradeInfoResponseTests.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/IndexUpgradeInfoResponseTests.java deleted file mode 100644 index e69de29bb2d1d..0000000000000 From c99b27bc0dda43651989fb44ab2b93a975d6aedb Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 30 Oct 2018 17:27:03 +0100 Subject: [PATCH 25/26] make XPackInfoResponse ToXContentObject-able again --- .../client/xpack/XPackInfoResponse.java | 173 +++++++++++++++++- .../client/xpack/XPackInfoResponseTests.java | 116 ++++++++++++ 2 files changed, 284 insertions(+), 5 deletions(-) create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/xpack/XPackInfoResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java index a89c778347d8f..f9a92d2fbbe02 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java @@ -20,24 +20,31 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.client.license.LicenseStatus; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class XPackInfoResponse { +public class XPackInfoResponse implements ToXContentObject { /** * Value of the license's expiration time if it should never expire. */ @@ -78,6 +85,26 @@ public FeatureSetsInfo getFeatureSetsInfo() { return featureSetsInfo; } + @Override + public boolean equals(Object other) { + if (other == null || other.getClass() != getClass()) return false; + if (this == other) return true; + XPackInfoResponse rhs = (XPackInfoResponse) other; + return Objects.equals(buildInfo, rhs.buildInfo) + && Objects.equals(licenseInfo, rhs.licenseInfo) + && Objects.equals(featureSetsInfo, rhs.featureSetsInfo); + } + + @Override + public int hashCode() { + return Objects.hash(buildInfo, licenseInfo, featureSetsInfo); + } + + @Override + public String toString() { + return Strings.toString(this, true, false); + } + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "xpack_info_response", true, (a, v) -> { BuildInfo buildInfo = (BuildInfo) a[0]; @@ -108,7 +135,37 @@ public static XPackInfoResponse fromXContent(XContentParser parser) throws IOExc return PARSER.parse(parser, null); } - public static class LicenseInfo { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + + if (buildInfo != null) { + builder.field("build", buildInfo, params); + } + + EnumSet categories = XPackInfoRequest.Category + .toSet(Strings.splitStringByCommaToArray(params.param("categories", "_all"))); + if (licenseInfo != null) { + builder.field("license", licenseInfo, params); + } else if (categories.contains(XPackInfoRequest.Category.LICENSE)) { + // if the user requested the license info, and there is no license, we should send + // back an explicit null value (indicating there is no license). This is different + // than not adding the license info at all + builder.nullField("license"); + } + + if (featureSetsInfo != null) { + builder.field("features", featureSetsInfo, params); + } + + if (params.paramAsBoolean("human", true)) { + builder.field("tagline", "You know, for X"); + } + + return builder.endObject(); + } + + public static class LicenseInfo implements ToXContentObject { private final String uid; private final String type; private final String mode; @@ -143,6 +200,23 @@ public LicenseStatus getStatus() { return status; } + @Override + public boolean equals(Object other) { + if (other == null || other.getClass() != getClass()) return false; + if (this == other) return true; + LicenseInfo rhs = (LicenseInfo) other; + return Objects.equals(uid, rhs.uid) + && Objects.equals(type, rhs.type) + && Objects.equals(mode, rhs.mode) + && Objects.equals(status, rhs.status) + && expiryDate == rhs.expiryDate; + } + + @Override + public int hashCode() { + return Objects.hash(uid, type, mode, status, expiryDate); + } + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "license_info", true, (a, v) -> { String uid = (String) a[0]; @@ -160,9 +234,22 @@ public LicenseStatus getStatus() { PARSER.declareString(constructorArg(), new ParseField("status")); PARSER.declareLong(optionalConstructorArg(), new ParseField("expiry_date_in_millis")); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject() + .field("uid", uid) + .field("type", type) + .field("mode", mode) + .field("status", status.label()); + if (expiryDate != BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) { + builder.timeField("expiry_date_in_millis", "expiry_date", expiryDate); + } + return builder.endObject(); + } } - public static class BuildInfo { + public static class BuildInfo implements ToXContentObject { private final String hash; private final String timestamp; @@ -179,15 +266,37 @@ public String getTimestamp() { return timestamp; } + @Override + public boolean equals(Object other) { + if (other == null || other.getClass() != getClass()) return false; + if (this == other) return true; + BuildInfo rhs = (BuildInfo) other; + return Objects.equals(hash, rhs.hash) + && Objects.equals(timestamp, rhs.timestamp); + } + + @Override + public int hashCode() { + return Objects.hash(hash, timestamp); + } + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "build_info", true, (a, v) -> new BuildInfo((String) a[0], (String) a[1])); static { PARSER.declareString(constructorArg(), new ParseField("hash")); PARSER.declareString(constructorArg(), new ParseField("date")); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return builder.startObject() + .field("hash", hash) + .field("date", timestamp) + .endObject(); + } } - public static class FeatureSetsInfo { + public static class FeatureSetsInfo implements ToXContentObject { private final Map featureSets; public FeatureSetsInfo(Set featureSets) { @@ -202,7 +311,30 @@ public Map getFeatureSets() { return featureSets; } - public static class FeatureSet { + @Override + public boolean equals(Object other) { + if (other == null || other.getClass() != getClass()) return false; + if (this == other) return true; + FeatureSetsInfo rhs = (FeatureSetsInfo) other; + return Objects.equals(featureSets, rhs.featureSets); + } + + @Override + public int hashCode() { + return Objects.hash(featureSets); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + List names = new ArrayList<>(this.featureSets.keySet()).stream().sorted().collect(Collectors.toList()); + for (String name : names) { + builder.field(name, featureSets.get(name), params); + } + return builder.endObject(); + } + + public static class FeatureSet implements ToXContentObject { private final String name; @Nullable private final String description; private final boolean available; @@ -240,6 +372,23 @@ public Map nativeCodeInfo() { return nativeCodeInfo; } + @Override + public boolean equals(Object other) { + if (other == null || other.getClass() != getClass()) return false; + if (this == other) return true; + FeatureSet rhs = (FeatureSet) other; + return Objects.equals(name, rhs.name) + && Objects.equals(description, rhs.description) + && available == rhs.available + && enabled == rhs.enabled + && Objects.equals(nativeCodeInfo, rhs.nativeCodeInfo); + } + + @Override + public int hashCode() { + return Objects.hash(name, description, available, enabled, nativeCodeInfo); + } + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "feature_set", true, (a, name) -> { String description = (String) a[0]; @@ -255,6 +404,20 @@ public Map nativeCodeInfo() { PARSER.declareBoolean(constructorArg(), new ParseField("enabled")); PARSER.declareObject(optionalConstructorArg(), (p, name) -> p.map(), new ParseField("native_code_info")); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + if (description != null) { + builder.field("description", description); + } + builder.field("available", available); + builder.field("enabled", enabled); + if (nativeCodeInfo != null) { + builder.field("native_code_info", nativeCodeInfo); + } + return builder.endObject(); + } } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/xpack/XPackInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/xpack/XPackInfoResponseTests.java new file mode 100644 index 0000000000000..702c4bef64bd2 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/xpack/XPackInfoResponseTests.java @@ -0,0 +1,116 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.xpack; + +import org.elasticsearch.client.license.LicenseStatus; +import org.elasticsearch.client.xpack.XPackInfoResponse.BuildInfo; +import org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo; +import org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; +import org.elasticsearch.client.xpack.XPackInfoResponse.LicenseInfo; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.function.Predicate; + +public class XPackInfoResponseTests extends AbstractXContentTestCase { + + @Override + protected boolean supportsUnknownFields() { + return true; + } + + protected XPackInfoResponse doParseInstance(XContentParser parser) throws IOException { + return XPackInfoResponse.fromXContent(parser); + } + + protected Predicate getRandomFieldsExcludeFilter() { + return path -> path.equals("features") + || (path.startsWith("features") && path.endsWith("native_code_info")); + } + + protected ToXContent.Params getToXContentParams() { + Map params = new HashMap<>(); + if (randomBoolean()) { + params.put("human", randomBoolean() ? "true" : "false"); + } + if (randomBoolean()) { + params.put("categories", "_none"); + } + return new ToXContent.MapParams(params); + } + + protected XPackInfoResponse createTestInstance() { + return new XPackInfoResponse( + randomBoolean() ? null : randomBuildInfo(), + randomBoolean() ? null : randomLicenseInfo(), + randomBoolean() ? null : randomFeatureSetsInfo()); + } + + private BuildInfo randomBuildInfo() { + return new BuildInfo( + randomAlphaOfLength(10), + randomAlphaOfLength(15)); + } + + private LicenseInfo randomLicenseInfo() { + return new LicenseInfo( + randomAlphaOfLength(10), + randomAlphaOfLength(4), + randomAlphaOfLength(5), + randomFrom(LicenseStatus.values()), + randomLong()); + } + + private FeatureSetsInfo randomFeatureSetsInfo() { + int size = between(0, 10); + Set featureSets = new HashSet<>(size); + while (featureSets.size() < size) { + featureSets.add(randomFeatureSet()); + } + return new FeatureSetsInfo(featureSets); + } + + private FeatureSet randomFeatureSet() { + return new FeatureSet( + randomAlphaOfLength(5), + randomBoolean() ? null : randomAlphaOfLength(20), + randomBoolean(), + randomBoolean(), + randomNativeCodeInfo()); + } + + private Map randomNativeCodeInfo() { + if (randomBoolean()) { + return null; + } + int size = between(0, 10); + Map nativeCodeInfo = new HashMap<>(size); + while (nativeCodeInfo.size() < size) { + nativeCodeInfo.put(randomAlphaOfLength(5), randomAlphaOfLength(5)); + } + return nativeCodeInfo; + } +} From 793baccb36de63f52b79fefba85d56e08b8d4703 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 30 Oct 2018 17:57:56 +0100 Subject: [PATCH 26/26] indent! --- .../protocol/AbstractHlrcStreamableXContentTestCase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java index 315ce395aaf9e..d81fd5aed79ba 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java @@ -15,7 +15,7 @@ import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; public abstract class AbstractHlrcStreamableXContentTestCase - extends AbstractStreamableXContentTestCase { + extends AbstractStreamableXContentTestCase { /** * Generic test that creates new instance of HLRC request/response from the test instance and checks