diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractHlrcWriteableXContentTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractHlrcWriteableXContentTestCase.java deleted file mode 100644 index 432c2db99643b..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractHlrcWriteableXContentTestCase.java +++ /dev/null @@ -1,70 +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; - -import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; - -import java.io.IOException; - -import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; - -/** - * @deprecated Use {@link AbstractResponseTestCase} instead of this class. - */ -// TODO: Remove and change subclasses to use AbstractResponseTestCase instead -@Deprecated -public abstract class AbstractHlrcWriteableXContentTestCase - extends AbstractSerializingTestCase { - - /** - * 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); - - //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? Then the additional testHlrcFromXContent is also no longer needed. - @Override - protected T doParseInstance(XContentParser parser) throws IOException { - return convertHlrcToInternal(doHlrcParseInstance(parser)); - } -} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java index 2422403659199..b00ef2c968ca9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java @@ -18,48 +18,23 @@ */ package org.elasticsearch.client.license; -import org.elasticsearch.client.AbstractHlrcWriteableXContentTestCase; -import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.license.LicensesStatus; +import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; +import java.util.Arrays; 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 AbstractHlrcWriteableXContentTestCase< - org.elasticsearch.protocol.xpack.license.PutLicenseResponse, PutLicenseResponse> { - - @Override - public org.elasticsearch.client.license.PutLicenseResponse doHlrcParseInstance(XContentParser parser) throws IOException { - return org.elasticsearch.client.license.PutLicenseResponse.fromXContent(parser); - } - - @Override - public org.elasticsearch.protocol.xpack.license.PutLicenseResponse convertHlrcToInternal( - org.elasticsearch.client.license.PutLicenseResponse instance) { - return new org.elasticsearch.protocol.xpack.license.PutLicenseResponse(instance.isAcknowledged(), - org.elasticsearch.protocol.xpack.license.LicensesStatus.valueOf(instance.status().name()), - instance.acknowledgeHeader(), instance.acknowledgeMessages()); - } - - @Override - protected boolean supportsUnknownFields() { - return true; - } +import static org.hamcrest.Matchers.equalTo; - @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"); - } +public class PutLicenseResponseTests extends AbstractResponseTestCase< + org.elasticsearch.protocol.xpack.license.PutLicenseResponse, PutLicenseResponse> { @Override - protected org.elasticsearch.protocol.xpack.license.PutLicenseResponse createTestInstance() { + protected org.elasticsearch.protocol.xpack.license.PutLicenseResponse createServerTestInstance(XContentType xContentType) { boolean acknowledged = randomBoolean(); org.elasticsearch.protocol.xpack.license.LicensesStatus status = randomFrom(org.elasticsearch.protocol.xpack.license.LicensesStatus.VALID, @@ -97,45 +72,18 @@ private static Map randomAckMessages() { } @Override - protected Writeable.Reader instanceReader() { - return org.elasticsearch.protocol.xpack.license.PutLicenseResponse::new; + protected PutLicenseResponse doParseToClientInstance(XContentParser parser) throws IOException { + return PutLicenseResponse.fromXContent(parser); } @Override - protected org.elasticsearch.protocol.xpack.license.PutLicenseResponse mutateInstance( - org.elasticsearch.protocol.xpack.license.PutLicenseResponse response) { - @SuppressWarnings("unchecked") - Function mutator = randomFrom( - r -> new org.elasticsearch.protocol.xpack.license.PutLicenseResponse( - r.isAcknowledged() == false, - r.status(), - r.acknowledgeHeader(), - r.acknowledgeMessages()), - r -> new org.elasticsearch.protocol.xpack.license.PutLicenseResponse( - r.isAcknowledged(), - mutateStatus(r.status()), - r.acknowledgeHeader(), - r.acknowledgeMessages()), - r -> { - if (r.acknowledgeMessages().isEmpty()) { - return new org.elasticsearch.protocol.xpack.license.PutLicenseResponse( - r.isAcknowledged(), - r.status(), - randomAlphaOfLength(10), - randomAckMessages() - ); - } else { - return new org.elasticsearch.protocol.xpack.license.PutLicenseResponse(r.isAcknowledged(), r.status()); - } - } - - ); - return mutator.apply(response); - } - - private org.elasticsearch.protocol.xpack.license.LicensesStatus mutateStatus( - org.elasticsearch.protocol.xpack.license.LicensesStatus status) { - return randomValueOtherThan(status, () -> randomFrom(LicensesStatus.values())); + protected void assertInstances(org.elasticsearch.protocol.xpack.license.PutLicenseResponse serverTestInstance, + PutLicenseResponse clientInstance) { + assertThat(serverTestInstance.status().name(), equalTo(clientInstance.status().name())); + assertThat(serverTestInstance.acknowledgeHeader(), equalTo(clientInstance.acknowledgeHeader())); + assertThat(serverTestInstance.acknowledgeMessages().keySet(), equalTo(clientInstance.acknowledgeMessages().keySet())); + for(Map.Entry entry: serverTestInstance.acknowledgeMessages().entrySet()) { + assertTrue(Arrays.equals(entry.getValue(), clientInstance.acknowledgeMessages().get(entry.getKey()))); + } } } 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 a22b40d7e6e63..af253957aebd5 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 @@ -18,51 +18,24 @@ */ package org.elasticsearch.client.license; -import org.elasticsearch.client.AbstractHlrcWriteableXContentTestCase; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.license.PostStartBasicResponse; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.function.Predicate; -public class StartBasicResponseTests extends AbstractHlrcWriteableXContentTestCase< - PostStartBasicResponse, StartBasicResponse> { - - @Override - public org.elasticsearch.client.license.StartBasicResponse doHlrcParseInstance(XContentParser parser) throws IOException { - return org.elasticsearch.client.license.StartBasicResponse.fromXContent(parser); - } - - @Override - public PostStartBasicResponse convertHlrcToInternal(org.elasticsearch.client.license.StartBasicResponse instance) { - return new PostStartBasicResponse(PostStartBasicResponse.Status.valueOf(instance.getStatus().name()), - instance.getAcknowledgeMessages(), instance.getAcknowledgeMessage()); - } - - @Override - protected Writeable.Reader instanceReader() { - return PostStartBasicResponse::new; - } +import static org.hamcrest.Matchers.equalTo; - @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"); - } +public class StartBasicResponseTests extends AbstractResponseTestCase< + PostStartBasicResponse, StartBasicResponse> { @Override - protected PostStartBasicResponse createTestInstance() { + protected PostStartBasicResponse createServerTestInstance(XContentType xContentType) { PostStartBasicResponse.Status status = randomFrom(PostStartBasicResponse.Status.values()); String acknowledgeMessage = null; Map ackMessages = Collections.emptyMap(); @@ -71,7 +44,6 @@ protected PostStartBasicResponse createTestInstance() { ackMessages = randomAckMessages(); } final PostStartBasicResponse postStartBasicResponse = new PostStartBasicResponse(status, ackMessages, acknowledgeMessage); - logger.info("{}", Strings.toString(postStartBasicResponse)); return postStartBasicResponse; } @@ -92,4 +64,22 @@ private static Map randomAckMessages() { return ackMessages; } + + @Override + protected StartBasicResponse doParseToClientInstance(XContentParser parser) throws IOException { + return StartBasicResponse.fromXContent(parser); + } + + @Override + protected void assertInstances(PostStartBasicResponse serverTestInstance, StartBasicResponse clientInstance) { + assertThat(serverTestInstance.getStatus().name(), equalTo(clientInstance.getStatus().name())); + assertThat(serverTestInstance.getStatus().isBasicStarted(), equalTo(clientInstance.isBasicStarted())); + assertThat(serverTestInstance.isAcknowledged(), equalTo(clientInstance.isAcknowledged())); + assertThat(serverTestInstance.getStatus().getErrorMessage(), equalTo(clientInstance.getErrorMessage())); + assertThat(serverTestInstance.getAcknowledgeMessage(), equalTo(clientInstance.getAcknowledgeMessage())); + assertThat(serverTestInstance.getAcknowledgeMessages().keySet(), equalTo(clientInstance.getAcknowledgeMessages().keySet())); + for(Map.Entry entry: serverTestInstance.getAcknowledgeMessages().entrySet()) { + assertTrue(Arrays.equals(entry.getValue(), clientInstance.getAcknowledgeMessages().get(entry.getKey()))); + } + } } 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 84cbcdef102e5..d37cbe32bf156 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 @@ -44,11 +44,11 @@ public enum Status { this.restStatus = restStatus; } - boolean isBasicStarted() { + public boolean isBasicStarted() { return isBasicStarted; } - String getErrorMessage() { + public String getErrorMessage() { return errorMessage; } @@ -134,6 +134,14 @@ public RestStatus status() { return status.restStatus; } + public String getAcknowledgeMessage() { + return acknowledgeMessage; + } + + public Map getAcknowledgeMessages() { + return acknowledgeMessages; + } + @Override public boolean equals(Object o) { if (this == o) return true;