Browse Source

HLRC: add support for get license basic/trial status API (#33176)

Relates to #29827
Luca Cavanna 7 years ago
parent
commit
0b7d18d733
19 changed files with 448 additions and 47 deletions
  1. 24 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java
  2. 8 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java
  3. 3 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java
  4. 79 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java
  5. 80 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java
  6. 12 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java
  7. 16 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java
  8. 8 7
      client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java
  9. 28 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java
  10. 23 0
      docs/java-rest/high-level/licensing/get-basic-status.asciidoc
  11. 23 0
      docs/java-rest/high-level/licensing/get-trial-status.asciidoc
  12. 4 0
      docs/java-rest/high-level/supported-apis.asciidoc
  13. 30 2
      x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusResponse.java
  14. 30 2
      x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusResponse.java
  15. 3 18
      x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java
  16. 3 18
      x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java
  17. 6 0
      x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java
  18. 34 0
      x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetBasicStatusResponseTests.java
  19. 34 0
      x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetTrialStatusResponseTests.java

+ 24 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java

@@ -26,6 +26,8 @@ import org.elasticsearch.client.license.StartTrialRequest;
 import org.elasticsearch.client.license.StartTrialResponse;
 import org.elasticsearch.client.license.StartBasicRequest;
 import org.elasticsearch.client.license.StartBasicResponse;
+import org.elasticsearch.client.license.GetBasicStatusResponse;
+import org.elasticsearch.client.license.GetTrialStatusResponse;
 import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.Streams;
 import org.elasticsearch.common.xcontent.DeprecationHandler;
@@ -172,6 +174,28 @@ public final class LicenseClient {
             StartBasicResponse::fromXContent, listener, emptySet());
     }
 
+    /**
+     * Retrieve the license trial status
+     * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
+     * @return the response
+     * @throws IOException in case there is a problem sending the request or parsing back the response
+     */
+    public GetTrialStatusResponse getTrialStatus(RequestOptions options) throws IOException {
+        return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY,
+            request -> LicenseRequestConverters.getLicenseTrialStatus(), options, GetTrialStatusResponse::fromXContent, emptySet());
+    }
+
+    /**
+     * Retrieve the license basic status
+     * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
+     * @return the response
+     * @throws IOException in case there is a problem sending the request or parsing back the response
+     */
+    public GetBasicStatusResponse getBasicStatus(RequestOptions options) throws IOException {
+        return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY,
+            request -> LicenseRequestConverters.getLicenseBasicStatus(), options, GetBasicStatusResponse::fromXContent, emptySet());
+    }
+
     /**
      * Converts an entire response into a json string
      *

+ 8 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java

@@ -88,4 +88,12 @@ final class LicenseRequestConverters {
         }
         return request;
     }
+
+    static Request getLicenseTrialStatus() {
+        return new Request(HttpGet.METHOD_NAME, "/_xpack/license/trial_status");
+    }
+
+    static Request getLicenseBasicStatus() {
+        return new Request(HttpGet.METHOD_NAME, "/_xpack/license/basic_status");
+    }
 }

+ 3 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java

@@ -24,6 +24,9 @@ import java.util.Optional;
  * Defines a validation layer for Requests.
  */
 public interface Validatable {
+
+    Validatable EMPTY = new Validatable() {};
+
     /**
      * Perform validation. This method does not have to be overridden in the event that no validation needs to be done,
      * or the validation was done during object construction time. A {@link ValidationException} that is not null is

+ 79 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java

@@ -0,0 +1,79 @@
+/*
+ * 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.ParseField;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
+import org.elasticsearch.common.xcontent.XContentParser;
+
+import java.util.Objects;
+
+import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
+
+/**
+ * Response class for license get basic status API
+ */
+public class GetBasicStatusResponse {
+
+    private static final ParseField ELIGIBLE_TO_START_BASIC = new ParseField("eligible_to_start_basic");
+
+    private static final ConstructingObjectParser<GetBasicStatusResponse, Void> PARSER = new ConstructingObjectParser<>(
+        "get_basic_status_response", true, a -> new GetBasicStatusResponse((boolean) a[0]));
+
+    static {
+        PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_BASIC,
+            ObjectParser.ValueType.BOOLEAN);
+    }
+
+    private final boolean eligibleToStartBasic;
+
+    GetBasicStatusResponse(boolean eligibleToStartBasic) {
+        this.eligibleToStartBasic = eligibleToStartBasic;
+    }
+
+    /**
+     * Returns whether the license is eligible to start basic or not
+     */
+    public boolean isEligibleToStartBasic() {
+        return eligibleToStartBasic;
+    }
+
+    public static GetBasicStatusResponse fromXContent(XContentParser parser) {
+        return PARSER.apply(parser, null);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        GetBasicStatusResponse that = (GetBasicStatusResponse) o;
+        return eligibleToStartBasic == that.eligibleToStartBasic;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(eligibleToStartBasic);
+    }
+}

+ 80 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java

@@ -0,0 +1,80 @@
+/*
+ * 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.ParseField;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
+import org.elasticsearch.common.xcontent.XContentParser;
+
+import java.util.Objects;
+
+import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
+
+/**
+ * Response class for license get trial status API
+ */
+public class GetTrialStatusResponse {
+
+    private static final ParseField ELIGIBLE_TO_START_TRIAL = new ParseField("eligible_to_start_trial");
+
+    private static final ConstructingObjectParser<GetTrialStatusResponse, Void> PARSER = new ConstructingObjectParser<>(
+        "get_trial_status_response", true, a -> new GetTrialStatusResponse((boolean) a[0]));
+
+    static {
+        PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_TRIAL,
+            ObjectParser.ValueType.BOOLEAN);
+    }
+
+    private final boolean eligibleToStartTrial;
+
+    GetTrialStatusResponse(boolean eligibleToStartTrial) {
+        this.eligibleToStartTrial = eligibleToStartTrial;
+    }
+
+    /**
+     * Returns whether the license is eligible to start trial or not
+     */
+    public boolean isEligibleToStartTrial() {
+        return eligibleToStartTrial;
+    }
+
+    public static GetTrialStatusResponse fromXContent(XContentParser parser) {
+        return PARSER.apply(parser, null);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        GetTrialStatusResponse that = (GetTrialStatusResponse) o;
+        return eligibleToStartTrial == that.eligibleToStartTrial;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(eligibleToStartTrial);
+    }
+
+}

+ 12 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java

@@ -22,8 +22,10 @@ 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.GetBasicStatusResponse;
 import org.elasticsearch.client.license.GetLicenseRequest;
 import org.elasticsearch.client.license.GetLicenseResponse;
+import org.elasticsearch.client.license.GetTrialStatusResponse;
 import org.elasticsearch.client.license.LicensesStatus;
 import org.elasticsearch.client.license.PutLicenseRequest;
 import org.elasticsearch.client.license.PutLicenseResponse;
@@ -198,4 +200,14 @@ public class LicenseIT extends ESRestHighLevelClientTestCase {
         final AcknowledgedResponse response = highLevelClient().license().deleteLicense(request, RequestOptions.DEFAULT);
         assertThat(response.isAcknowledged(), equalTo(true));
     }
+
+    public void testGetTrialStatus() throws IOException {
+        GetTrialStatusResponse trialStatus = highLevelClient().license().getTrialStatus(RequestOptions.DEFAULT);
+        assertFalse(trialStatus.isEligibleToStartTrial());
+    }
+
+    public void testGetBasicStatus() throws IOException {
+        GetBasicStatusResponse basicStatus = highLevelClient().license().getBasicStatus(RequestOptions.DEFAULT);
+        assertTrue(basicStatus.isEligibleToStartBasic());
+    }
 }

+ 16 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java

@@ -128,4 +128,20 @@ public class LicenseRequestConvertersTests extends ESTestCase {
         assertThat(request.getParameters(), equalTo(expectedParams));
         assertThat(request.getEntity(), is(nullValue()));
     }
+
+    public void testGetLicenseTrialStatus() {
+        Request request = LicenseRequestConverters.getLicenseTrialStatus();
+        assertEquals(HttpGet.METHOD_NAME, request.getMethod());
+        assertEquals("/_xpack/license/trial_status", request.getEndpoint());
+        assertEquals(request.getParameters().size(), 0);
+        assertNull(request.getEntity());
+    }
+
+    public void testGetLicenseBasicStatus() {
+        Request request = LicenseRequestConverters.getLicenseBasicStatus();
+        assertEquals(HttpGet.METHOD_NAME, request.getMethod());
+        assertEquals("/_xpack/license/basic_status", request.getEndpoint());
+        assertEquals(request.getParameters().size(), 0);
+        assertNull(request.getEntity());
+    }
 }

+ 8 - 7
client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

@@ -736,7 +736,6 @@ public class RestHighLevelClientTests extends ESTestCase {
                 assertSubmitTaskMethod(methods, method, apiName, restSpec);
             } else {
                 assertSyncMethod(method, apiName);
-
                 boolean remove = apiSpec.remove(apiName);
                 if (remove == false) {
                     if (deprecatedMethods.contains(apiName)) {
@@ -771,7 +770,7 @@ public class RestHighLevelClientTests extends ESTestCase {
         assertThat("Some API are not supported but they should be: " + apiSpec, apiSpec.size(), equalTo(0));
     }
 
-    private void assertSyncMethod(Method method, String apiName) {
+    private static void assertSyncMethod(Method method, String apiName) {
         //A few methods return a boolean rather than a response object
         if (apiName.equals("ping") || apiName.contains("exist")) {
             assertThat("the return type for method [" + method + "] is incorrect",
@@ -787,7 +786,8 @@ public class RestHighLevelClientTests extends ESTestCase {
         assertEquals("incorrect number of exceptions for method [" + method + "]", 1, method.getExceptionTypes().length);
         //a few methods don't accept a request object as argument
         if (apiName.equals("ping") || apiName.equals("info") || apiName.equals("security.get_ssl_certificates")
-            || apiName.equals("security.authenticate")) {
+            || apiName.equals("security.authenticate") || apiName.equals("license.get_trial_status")
+            || apiName.equals("license.get_basic_status")) {
             assertEquals("incorrect number of arguments for method [" + method + "]", 1, method.getParameterTypes().length);
             assertThat("the parameter to method [" + method + "] is the wrong type",
                 method.getParameterTypes()[0], equalTo(RequestOptions.class));
@@ -800,7 +800,7 @@ public class RestHighLevelClientTests extends ESTestCase {
         }
     }
 
-    private void assertAsyncMethod(Map<String, Method> methods, Method method, String apiName) {
+    private static void assertAsyncMethod(Map<String, Method> methods, Method method, String apiName) {
         assertTrue("async method [" + method.getName() + "] doesn't have corresponding sync method",
                 methods.containsKey(apiName.substring(0, apiName.length() - 6)));
         assertThat("async method [" + method + "] should return void", method.getReturnType(), equalTo(Void.TYPE));
@@ -820,7 +820,8 @@ public class RestHighLevelClientTests extends ESTestCase {
         }
     }
 
-    private void assertSubmitTaskMethod(Map<String, Method> methods, Method method, String apiName, ClientYamlSuiteRestSpec restSpec) {
+    private static void assertSubmitTaskMethod(Map<String, Method> methods, Method method, String apiName,
+                                               ClientYamlSuiteRestSpec restSpec) {
         String methodName = extractMethodName(apiName);
         assertTrue("submit task method [" + method.getName() + "] doesn't have corresponding sync method",
             methods.containsKey(methodName));
@@ -834,11 +835,11 @@ public class RestHighLevelClientTests extends ESTestCase {
             restSpec.getApi(methodName).getParams(), Matchers.hasKey("wait_for_completion"));
     }
 
-    private String extractMethodName(String apiName) {
+    private static String extractMethodName(String apiName) {
         return apiName.substring(SUBMIT_TASK_PREFIX.length(), apiName.length() - SUBMIT_TASK_SUFFIX.length());
     }
 
-    private boolean isSubmitTaskMethod(String apiName) {
+    private static boolean isSubmitTaskMethod(String apiName) {
         return apiName.startsWith(SUBMIT_TASK_PREFIX) && apiName.endsWith(SUBMIT_TASK_SUFFIX);
     }
 

+ 28 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java

@@ -30,6 +30,8 @@ import org.elasticsearch.client.license.StartTrialRequest;
 import org.elasticsearch.client.license.StartTrialResponse;
 import org.elasticsearch.client.license.StartBasicRequest;
 import org.elasticsearch.client.license.StartBasicResponse;
+import org.elasticsearch.client.license.GetBasicStatusResponse;
+import org.elasticsearch.client.license.GetTrialStatusResponse;
 import org.elasticsearch.common.Booleans;
 import org.junit.After;
 import org.junit.BeforeClass;
@@ -336,4 +338,30 @@ public class LicensingDocumentationIT extends ESRestHighLevelClientTestCase {
             assertTrue(latch.await(30L, TimeUnit.SECONDS));
         }
     }
+
+    public void testGetTrialStatus() throws IOException {
+        RestHighLevelClient client = highLevelClient();
+        {
+            //tag::get-trial-status-execute
+            GetTrialStatusResponse response = client.license().getTrialStatus(RequestOptions.DEFAULT);
+            //end::get-trial-status-execute
+
+            //tag::get-trial-status-response
+            boolean eligibleToStartTrial = response.isEligibleToStartTrial(); // <1>
+            //end::get-trial-status-response
+        }
+    }
+
+    public void testGetBasicStatus() throws IOException {
+        RestHighLevelClient client = highLevelClient();
+        {
+            //tag::get-basic-status-execute
+            GetBasicStatusResponse response = client.license().getBasicStatus(RequestOptions.DEFAULT);
+            //end::get-basic-status-execute
+
+            //tag::get-basic-status-response
+            boolean eligibleToStartbasic = response.isEligibleToStartBasic(); // <1>
+            //end::get-basic-status-response
+        }
+    }
 }

+ 23 - 0
docs/java-rest/high-level/licensing/get-basic-status.asciidoc

@@ -0,0 +1,23 @@
+[[java-rest-high-get-basic-status]]
+=== Get Basic Status
+
+[[java-rest-high-get-basic-status-execution]]
+==== Execution
+
+The basic status of the license can be retrieved as follows:
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-basic-status-execute]
+--------------------------------------------------
+
+[[java-rest-high-get-basic-status-response]]
+==== Response
+
+The returned `GetTrialStatusResponse` holds only a `boolean` flag:
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-basic-status-response]
+--------------------------------------------------
+<1> Whether the license is eligible to start basic or not

+ 23 - 0
docs/java-rest/high-level/licensing/get-trial-status.asciidoc

@@ -0,0 +1,23 @@
+[[java-rest-high-get-trial-status]]
+=== Get Trial Status
+
+[[java-rest-high-get-trial-status-execution]]
+==== Execution
+
+The trial status of the license can be retrieved as follows:
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-trial-status-execute]
+--------------------------------------------------
+
+[[java-rest-high-get-trial-status-response]]
+==== Response
+
+The returned `GetTrialStatusResponse` holds only a `boolean` flag:
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/LicensingDocumentationIT.java[get-trial-status-response]
+--------------------------------------------------
+<1> Whether the license is eligible to start trial or not

+ 4 - 0
docs/java-rest/high-level/supported-apis.asciidoc

@@ -220,12 +220,16 @@ The Java High Level REST Client supports the following Licensing APIs:
 * <<java-rest-high-delete-license>>
 * <<java-rest-high-start-trial>>
 * <<java-rest-high-start-basic>>
+* <<java-rest-high-get-trial-status>>
+* <<java-rest-high-get-basic-status>>
 
 include::licensing/put-license.asciidoc[]
 include::licensing/get-license.asciidoc[]
 include::licensing/delete-license.asciidoc[]
 include::licensing/start-trial.asciidoc[]
 include::licensing/start-basic.asciidoc[]
+include::licensing/get-trial-status.asciidoc[]
+include::licensing/get-basic-status.asciidoc[]
 
 == Machine Learning APIs
 :upid: {mainid}-x-pack-ml

+ 30 - 2
x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusResponse.java

@@ -8,17 +8,20 @@ package org.elasticsearch.license;
 import org.elasticsearch.action.ActionResponse;
 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 java.io.IOException;
+import java.util.Objects;
 
-class GetBasicStatusResponse extends ActionResponse {
+public class GetBasicStatusResponse extends ActionResponse implements ToXContentObject {
 
     private boolean eligibleToStartBasic;
 
     GetBasicStatusResponse() {
     }
 
-    GetBasicStatusResponse(boolean eligibleToStartBasic) {
+    public GetBasicStatusResponse(boolean eligibleToStartBasic) {
         this.eligibleToStartBasic = eligibleToStartBasic;
     }
 
@@ -35,4 +38,29 @@ class GetBasicStatusResponse extends ActionResponse {
     public void writeTo(StreamOutput out) throws IOException {
         out.writeBoolean(eligibleToStartBasic);
     }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field("eligible_to_start_basic", eligibleToStartBasic);
+        builder.endObject();
+        return builder;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        GetBasicStatusResponse that = (GetBasicStatusResponse) o;
+        return eligibleToStartBasic == that.eligibleToStartBasic;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(eligibleToStartBasic);
+    }
 }

+ 30 - 2
x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusResponse.java

@@ -8,17 +8,20 @@ package org.elasticsearch.license;
 import org.elasticsearch.action.ActionResponse;
 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 java.io.IOException;
+import java.util.Objects;
 
-class GetTrialStatusResponse extends ActionResponse {
+public class GetTrialStatusResponse extends ActionResponse implements ToXContentObject {
 
     private boolean eligibleToStartTrial;
 
     GetTrialStatusResponse() {
     }
 
-    GetTrialStatusResponse(boolean eligibleToStartTrial) {
+    public GetTrialStatusResponse(boolean eligibleToStartTrial) {
         this.eligibleToStartTrial = eligibleToStartTrial;
     }
 
@@ -35,4 +38,29 @@ class GetTrialStatusResponse extends ActionResponse {
     public void writeTo(StreamOutput out) throws IOException {
         out.writeBoolean(eligibleToStartTrial);
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        GetTrialStatusResponse that = (GetTrialStatusResponse) o;
+        return eligibleToStartTrial == that.eligibleToStartTrial;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(eligibleToStartTrial);
+    }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field("eligible_to_start_trial", eligibleToStartTrial);
+        builder.endObject();
+        return builder;
+    }
 }

+ 3 - 18
x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java

@@ -6,18 +6,12 @@
 package org.elasticsearch.license;
 
 import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.rest.BytesRestResponse;
 import org.elasticsearch.rest.RestController;
 import org.elasticsearch.rest.RestRequest;
-import org.elasticsearch.rest.RestResponse;
-import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.rest.action.RestBuilderListener;
+import org.elasticsearch.rest.action.RestToXContentListener;
 import org.elasticsearch.xpack.core.XPackClient;
 import org.elasticsearch.xpack.core.rest.XPackRestHandler;
 
-import java.io.IOException;
-
 import static org.elasticsearch.rest.RestRequest.Method.GET;
 
 public class RestGetBasicStatus extends XPackRestHandler {
@@ -28,17 +22,8 @@ public class RestGetBasicStatus extends XPackRestHandler {
     }
 
     @Override
-    protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException {
-        return channel -> client.licensing().prepareGetStartBasic().execute(
-                new RestBuilderListener<GetBasicStatusResponse>(channel) {
-                    @Override
-                    public RestResponse buildResponse(GetBasicStatusResponse response, XContentBuilder builder) throws Exception {
-                        builder.startObject();
-                        builder.field("eligible_to_start_basic", response.isEligibleToStartBasic());
-                        builder.endObject();
-                        return new BytesRestResponse(RestStatus.OK, builder);
-                    }
-                });
+    protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) {
+        return channel -> client.licensing().prepareGetStartBasic().execute(new RestToXContentListener<>(channel));
     }
 
     @Override

+ 3 - 18
x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java

@@ -6,18 +6,12 @@
 package org.elasticsearch.license;
 
 import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.rest.BytesRestResponse;
 import org.elasticsearch.rest.RestController;
 import org.elasticsearch.rest.RestRequest;
-import org.elasticsearch.rest.RestResponse;
-import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.rest.action.RestBuilderListener;
+import org.elasticsearch.rest.action.RestToXContentListener;
 import org.elasticsearch.xpack.core.XPackClient;
 import org.elasticsearch.xpack.core.rest.XPackRestHandler;
 
-import java.io.IOException;
-
 import static org.elasticsearch.rest.RestRequest.Method.GET;
 
 public class RestGetTrialStatus extends XPackRestHandler {
@@ -28,17 +22,8 @@ public class RestGetTrialStatus extends XPackRestHandler {
     }
 
     @Override
-    protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException {
-        return channel -> client.licensing().prepareGetStartTrial().execute(
-                new RestBuilderListener<GetTrialStatusResponse>(channel) {
-                    @Override
-                    public RestResponse buildResponse(GetTrialStatusResponse response, XContentBuilder builder) throws Exception {
-                        builder.startObject();
-                        builder.field("eligible_to_start_trial", response.isEligibleToStartTrial());
-                        builder.endObject();
-                        return new BytesRestResponse(RestStatus.OK, builder);
-                    }
-                });
+    protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) {
+        return channel -> client.licensing().prepareGetStartTrial().execute(new RestToXContentListener<>(channel));
     }
 
     @Override

+ 6 - 0
x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/AbstractHlrcStreamableXContentTestCase.java

@@ -43,4 +43,10 @@ public abstract class AbstractHlrcStreamableXContentTestCase<T extends ToXConten
      */
     public abstract T convertHlrcToInternal(H instance);
 
+    //TODO this would be final ideally: why do both responses need to parse from xcontent, only one (H) should? I think that T#fromXContent
+    //are only there for testing and could go away?
+    @Override
+    protected T doParseInstance(XContentParser parser) throws IOException {
+        return convertHlrcToInternal(doHlrcParseInstance(parser));
+    }
 }

+ 34 - 0
x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetBasicStatusResponseTests.java

@@ -0,0 +1,34 @@
+/*
+ * 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.client.license.GetBasicStatusResponse;
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase;
+
+public class GetBasicStatusResponseTests
+    extends AbstractHlrcStreamableXContentTestCase<org.elasticsearch.license.GetBasicStatusResponse, GetBasicStatusResponse> {
+    @Override
+    public GetBasicStatusResponse doHlrcParseInstance(XContentParser parser) {
+        return GetBasicStatusResponse.fromXContent(parser);
+    }
+
+    @Override
+    public org.elasticsearch.license.GetBasicStatusResponse convertHlrcToInternal(GetBasicStatusResponse instance) {
+        return new org.elasticsearch.license.GetBasicStatusResponse(instance.isEligibleToStartBasic());
+    }
+
+    @Override
+    protected org.elasticsearch.license.GetBasicStatusResponse createBlankInstance() {
+        return new org.elasticsearch.license.GetBasicStatusResponse(false);
+    }
+
+    @Override
+    protected org.elasticsearch.license.GetBasicStatusResponse createTestInstance() {
+        return new org.elasticsearch.license.GetBasicStatusResponse(randomBoolean());
+    }
+}

+ 34 - 0
x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/GetTrialStatusResponseTests.java

@@ -0,0 +1,34 @@
+/*
+ * 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.client.license.GetTrialStatusResponse;
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase;
+
+public class GetTrialStatusResponseTests extends
+    AbstractHlrcStreamableXContentTestCase<org.elasticsearch.license.GetTrialStatusResponse, GetTrialStatusResponse> {
+
+    @Override
+    public GetTrialStatusResponse doHlrcParseInstance(XContentParser parser) {
+        return GetTrialStatusResponse.fromXContent(parser);
+    }
+
+    @Override
+    public org.elasticsearch.license.GetTrialStatusResponse convertHlrcToInternal(GetTrialStatusResponse instance) {
+        return new org.elasticsearch.license.GetTrialStatusResponse(instance.isEligibleToStartTrial());
+    }
+
+    @Override
+    protected org.elasticsearch.license.GetTrialStatusResponse createBlankInstance() {
+        return new org.elasticsearch.license.GetTrialStatusResponse(false);
+    }
+
+    @Override
+    protected org.elasticsearch.license.GetTrialStatusResponse createTestInstance() {
+        return new org.elasticsearch.license.GetTrialStatusResponse(randomBoolean());
+    }
+}