From c639009295ff263f1d5fa6ee09a55d8e0d21d26e Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 22 Mar 2019 14:21:19 +0100 Subject: [PATCH 1/4] Add realistic hlrc request serialization test base class and change hlrc ccr request tests to use AbstractRequestTestCase base class. This way the request classes are tested in a more realistic setting. Note this change also adds a test dependency on xpack core module. Similar to #39844 but then for hlrc request serialization tests. Relates to #39745 --- client/rest-high-level/build.gradle | 3 + .../client/AbstractRequestTestCase.java | 70 +++++++++++++++ .../ccr/PutAutoFollowPatternRequestTests.java | 77 +++++------------ .../client/ccr/PutFollowRequestTests.java | 85 +++++++------------ .../client/ccr/ResumeFollowRequestTests.java | 66 ++++---------- 5 files changed, 145 insertions(+), 156 deletions(-) create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java diff --git a/client/rest-high-level/build.gradle b/client/rest-high-level/build.gradle index 44262f09346de..644e2c0fb4e8e 100644 --- a/client/rest-high-level/build.gradle +++ b/client/rest-high-level/build.gradle @@ -66,6 +66,9 @@ dependencies { testCompile "org.elasticsearch:rest-api-spec:${version}" restSpec "org.elasticsearch:rest-api-spec:${version}" + // Needed for serialization tests: + // (In order to serialize a server side class to a client side class or the other way around) + testCompile "org.elasticsearch.plugin:x-pack-core:${version}" } //we need to copy the yaml spec so we can check naming (see RestHighlevelClientTests#testApiNamingConventions) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java new file mode 100644 index 0000000000000..80a8680e97ca3 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java @@ -0,0 +1,70 @@ +/* + * 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.cluster.ClusterModule; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContent; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +/** + * Base class for HLRC request parsing tests. + * + * This case class facilitates generating client side request test instances and + * verifies that they are correctly parsed into server side request instances. + * + * @param The class representing the request on the client side. + * @param The class representing the request on the server side. + */ +public abstract class AbstractRequestTestCase extends ESTestCase { + + private static final int NUMBER_OF_TEST_RUNS = 20; + + public final void testFromXContent() throws IOException { + for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { + final C clientTestInstance = createClientTestInstance(); + + final XContentType xContentType = randomFrom(XContentType.values()); + final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); + + final XContent xContent = XContentFactory.xContent(xContentType); + final XContentParser parser = xContent.createParser( + new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), + LoggingDeprecationHandler.INSTANCE, + bytes.streamInput()); + final S serverInstance = doParseToServerInstance(parser); + assertInstances(serverInstance, clientTestInstance); + } + } + + protected abstract C createClientTestInstance(); + + protected abstract S doParseToServerInstance(XContentParser parser) throws IOException; + + protected abstract void assertInstances(S serverInstance, C clientTestInstance); + +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java index 429cc4a9f901b..b9ee34c8bda37 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java @@ -19,71 +19,24 @@ package org.elasticsearch.client.ccr; +import org.elasticsearch.client.AbstractRequestTestCase; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; import java.io.IOException; import java.util.Arrays; -import java.util.List; -public class PutAutoFollowPatternRequestTests extends AbstractXContentTestCase { +import static org.elasticsearch.client.ccr.PutFollowRequestTests.assertFollowConfig; +import static org.hamcrest.Matchers.equalTo; - @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("test_parser", - true, (args) -> new PutAutoFollowPatternRequest("name", (String) args[0], (List) args[1])); - - static { - PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD); - PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), PutAutoFollowPatternRequest.LEADER_PATTERNS_FIELD); - PARSER.declareString(PutAutoFollowPatternRequest::setFollowIndexNamePattern, PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD); - PARSER.declareInt(PutAutoFollowPatternRequest::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT); - PARSER.declareField( - PutAutoFollowPatternRequest::setMaxReadRequestSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()), - PutFollowRequest.MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareInt(PutAutoFollowPatternRequest::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS); - PARSER.declareInt(PutAutoFollowPatternRequest::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT); - PARSER.declareField( - PutAutoFollowPatternRequest::setMaxWriteRequestSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()), - PutFollowRequest.MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareInt(PutAutoFollowPatternRequest::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS); - PARSER.declareInt(PutAutoFollowPatternRequest::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT); - PARSER.declareField( - PutAutoFollowPatternRequest::setMaxWriteBufferSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()), - PutFollowRequest.MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareField( - PutAutoFollowPatternRequest::setMaxRetryDelay, - (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()), - PutFollowRequest.MAX_RETRY_DELAY_FIELD, - ObjectParser.ValueType.STRING); - PARSER.declareField( - PutAutoFollowPatternRequest::setReadPollTimeout, - (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()), - PutFollowRequest.READ_POLL_TIMEOUT, - ObjectParser.ValueType.STRING); - } - - @Override - protected PutAutoFollowPatternRequest doParseInstance(XContentParser parser) throws IOException { - return PARSER.apply(parser, null); - } - - @Override - protected boolean supportsUnknownFields() { - return true; - } +public class PutAutoFollowPatternRequestTests extends AbstractRequestTestCase< + PutAutoFollowPatternRequest, + PutAutoFollowPatternAction.Request> { @Override - protected PutAutoFollowPatternRequest createTestInstance() { + protected PutAutoFollowPatternRequest createClientTestInstance() { // Name isn't serialized, because it specified in url path, so no need to randomly generate it here. PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest("name", randomAlphaOfLength(4), Arrays.asList(generateRandomStringArray(4, 4, false))); @@ -123,4 +76,18 @@ protected PutAutoFollowPatternRequest createTestInstance() { return putAutoFollowPatternRequest; } + @Override + protected PutAutoFollowPatternAction.Request doParseToServerInstance(XContentParser parser) throws IOException { + return PutAutoFollowPatternAction.Request.fromXContent(parser, "name"); + } + + @Override + protected void assertInstances(PutAutoFollowPatternAction.Request serverInstance, PutAutoFollowPatternRequest clientTestInstance) { + assertThat(serverInstance.getName(), equalTo(clientTestInstance.getName())); + assertThat(serverInstance.getRemoteCluster(), equalTo(clientTestInstance.getRemoteCluster())); + assertThat(serverInstance.getLeaderIndexPatterns(), equalTo(clientTestInstance.getLeaderIndexPatterns())); + assertThat(serverInstance.getFollowIndexNamePattern(), equalTo(clientTestInstance.getFollowIndexNamePattern())); + assertFollowConfig(serverInstance.getParameters(), clientTestInstance); + } + } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java index 1f6a3d9f0ac28..cee28f6b15ed6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java @@ -19,67 +19,22 @@ package org.elasticsearch.client.ccr; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.AbstractRequestTestCase; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xpack.core.ccr.action.FollowParameters; +import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; import java.io.IOException; -public class PutFollowRequestTests extends AbstractXContentTestCase { +import static org.hamcrest.Matchers.equalTo; - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("test_parser", - true, (args) -> new PutFollowRequest((String) args[0], (String) args[1], "followerIndex")); - - static { - PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD); - PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.LEADER_INDEX_FIELD); - PARSER.declareInt(PutFollowRequest::setMaxReadRequestOperationCount, PutFollowRequest.MAX_READ_REQUEST_OPERATION_COUNT); - PARSER.declareField( - PutFollowRequest::setMaxReadRequestSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), PutFollowRequest.MAX_READ_REQUEST_SIZE.getPreferredName()), - PutFollowRequest.MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareInt(PutFollowRequest::setMaxOutstandingReadRequests, PutFollowRequest.MAX_OUTSTANDING_READ_REQUESTS); - PARSER.declareInt(PutFollowRequest::setMaxWriteRequestOperationCount, PutFollowRequest.MAX_WRITE_REQUEST_OPERATION_COUNT); - PARSER.declareField( - PutFollowRequest::setMaxWriteRequestSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), PutFollowRequest.MAX_WRITE_REQUEST_SIZE.getPreferredName()), - PutFollowRequest.MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareInt(PutFollowRequest::setMaxOutstandingWriteRequests, PutFollowRequest.MAX_OUTSTANDING_WRITE_REQUESTS); - PARSER.declareInt(PutFollowRequest::setMaxWriteBufferCount, PutFollowRequest.MAX_WRITE_BUFFER_COUNT); - PARSER.declareField( - PutFollowRequest::setMaxWriteBufferSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), PutFollowRequest.MAX_WRITE_BUFFER_SIZE.getPreferredName()), - PutFollowRequest.MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareField( - PutFollowRequest::setMaxRetryDelay, - (p, c) -> TimeValue.parseTimeValue(p.text(), PutFollowRequest.MAX_RETRY_DELAY_FIELD.getPreferredName()), - PutFollowRequest.MAX_RETRY_DELAY_FIELD, - ObjectParser.ValueType.STRING); - PARSER.declareField( - PutFollowRequest::setReadPollTimeout, - (p, c) -> TimeValue.parseTimeValue(p.text(), PutFollowRequest.READ_POLL_TIMEOUT.getPreferredName()), - PutFollowRequest.READ_POLL_TIMEOUT, - ObjectParser.ValueType.STRING); - } - - @Override - protected PutFollowRequest doParseInstance(XContentParser parser) throws IOException { - return PARSER.apply(parser, null); - } - - @Override - protected boolean supportsUnknownFields() { - return false; - } +public class PutFollowRequestTests extends AbstractRequestTestCase { @Override - protected PutFollowRequest createTestInstance() { + protected PutFollowRequest createClientTestInstance() { PutFollowRequest putFollowRequest = new PutFollowRequest(randomAlphaOfLength(4), randomAlphaOfLength(4), "followerIndex"); if (randomBoolean()) { @@ -115,4 +70,30 @@ protected PutFollowRequest createTestInstance() { return putFollowRequest; } + @Override + protected PutFollowAction.Request doParseToServerInstance(XContentParser parser) throws IOException { + return PutFollowAction.Request.fromXContent(parser, "followerIndex", ActiveShardCount.DEFAULT); + } + + @Override + protected void assertInstances(PutFollowAction.Request serverInstance, PutFollowRequest clientTestInstance) { + assertThat(serverInstance.getRemoteCluster(), equalTo(clientTestInstance.getRemoteCluster())); + assertThat(serverInstance.getLeaderIndex(), equalTo(clientTestInstance.getLeaderIndex())); + assertThat(serverInstance.getFollowerIndex(), equalTo(clientTestInstance.getFollowerIndex())); + assertFollowConfig(serverInstance.getParameters(), clientTestInstance); + } + + static void assertFollowConfig(FollowParameters serverParameters, FollowConfig clientConfig) { + assertThat(serverParameters.getMaxReadRequestOperationCount(), equalTo(clientConfig.getMaxReadRequestOperationCount())); + assertThat(serverParameters.getMaxWriteRequestOperationCount(), equalTo(clientConfig.getMaxWriteRequestOperationCount())); + assertThat(serverParameters.getMaxOutstandingReadRequests(), equalTo(clientConfig.getMaxOutstandingReadRequests())); + assertThat(serverParameters.getMaxOutstandingWriteRequests(), equalTo(clientConfig.getMaxOutstandingWriteRequests())); + assertThat(serverParameters.getMaxReadRequestSize(), equalTo(clientConfig.getMaxReadRequestSize())); + assertThat(serverParameters.getMaxWriteRequestSize(), equalTo(clientConfig.getMaxWriteRequestSize())); + assertThat(serverParameters.getMaxWriteBufferCount(), equalTo(clientConfig.getMaxWriteBufferCount())); + assertThat(serverParameters.getMaxWriteBufferSize(), equalTo(clientConfig.getMaxWriteBufferSize())); + assertThat(serverParameters.getMaxRetryDelay(), equalTo(clientConfig.getMaxRetryDelay())); + assertThat(serverParameters.getReadPollTimeout(), equalTo(clientConfig.getReadPollTimeout())); + } + } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/ResumeFollowRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/ResumeFollowRequestTests.java index d5d2b7e25539f..2ae9f56465c3c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/ResumeFollowRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/ResumeFollowRequestTests.java @@ -19,64 +19,21 @@ package org.elasticsearch.client.ccr; +import org.elasticsearch.client.AbstractRequestTestCase; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction; import java.io.IOException; -public class ResumeFollowRequestTests extends AbstractXContentTestCase { +import static org.elasticsearch.client.ccr.PutFollowRequestTests.assertFollowConfig; +import static org.hamcrest.Matchers.equalTo; - private static final ObjectParser PARSER = new ObjectParser<>("test_parser", - true, () -> new ResumeFollowRequest("followerIndex")); - - static { - PARSER.declareInt(ResumeFollowRequest::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT); - PARSER.declareField( - ResumeFollowRequest::setMaxReadRequestSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()), - PutFollowRequest.MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareInt(ResumeFollowRequest::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS); - PARSER.declareInt(ResumeFollowRequest::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT); - PARSER.declareField( - ResumeFollowRequest::setMaxWriteRequestSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()), - PutFollowRequest.MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareInt(ResumeFollowRequest::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS); - PARSER.declareInt(ResumeFollowRequest::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT); - PARSER.declareField( - ResumeFollowRequest::setMaxWriteBufferSize, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()), - PutFollowRequest.MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareField( - ResumeFollowRequest::setMaxRetryDelay, - (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()), - PutFollowRequest.MAX_RETRY_DELAY_FIELD, - ObjectParser.ValueType.STRING); - PARSER.declareField( - ResumeFollowRequest::setReadPollTimeout, - (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()), - PutFollowRequest.READ_POLL_TIMEOUT, - ObjectParser.ValueType.STRING); - } - - @Override - protected ResumeFollowRequest doParseInstance(XContentParser parser) throws IOException { - return PARSER.apply(parser, null); - } - - @Override - protected boolean supportsUnknownFields() { - return true; - } +public class ResumeFollowRequestTests extends AbstractRequestTestCase { @Override - protected ResumeFollowRequest createTestInstance() { + protected ResumeFollowRequest createClientTestInstance() { ResumeFollowRequest resumeFollowRequest = new ResumeFollowRequest("followerIndex"); if (randomBoolean()) { resumeFollowRequest.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE)); @@ -111,4 +68,15 @@ protected ResumeFollowRequest createTestInstance() { return resumeFollowRequest; } + @Override + protected ResumeFollowAction.Request doParseToServerInstance(XContentParser parser) throws IOException { + return ResumeFollowAction.Request.fromXContent(parser, "followerIndex"); + } + + @Override + protected void assertInstances(ResumeFollowAction.Request serverInstance, ResumeFollowRequest clientTestInstance) { + assertThat(serverInstance.getFollowerIndex(), equalTo(clientTestInstance.getFollowerIndex())); + assertFollowConfig(serverInstance.getParameters(), clientTestInstance); + } + } From 2b464b6e57dc6127c8f54c389e1c9960f3364898 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 5 Apr 2019 20:49:21 +0200 Subject: [PATCH 2/4] use empty named xcontent registry --- .../java/org/elasticsearch/client/AbstractRequestTestCase.java | 3 +-- .../org/elasticsearch/client/AbstractResponseTestCase.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java index 80a8680e97ca3..385ec5d395eb1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.client; -import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -53,7 +52,7 @@ public final void testFromXContent() throws IOException { final XContent xContent = XContentFactory.xContent(xContentType); final XContentParser parser = xContent.createParser( - new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), + NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytes.streamInput()); final S serverInstance = doParseToServerInstance(parser); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java index ebdbfc05035c6..635f1ed28c699 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.client; -import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -53,7 +52,7 @@ public final void testFromXContent() throws IOException { final XContent xContent = XContentFactory.xContent(xContentType); final XContentParser parser = xContent.createParser( - new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), + NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytes.streamInput()); final C clientInstance = doParseToClientInstance(parser); From e5b1ca778702f9f068dc2819d311412b6c4accf8 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 8 Apr 2019 07:38:40 +0200 Subject: [PATCH 3/4] removed iterations --- .../client/AbstractRequestTestCase.java | 24 ++++++++----------- .../client/AbstractResponseTestCase.java | 24 ++++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java index 385ec5d395eb1..8a10831c47749 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java @@ -41,23 +41,19 @@ */ public abstract class AbstractRequestTestCase extends ESTestCase { - private static final int NUMBER_OF_TEST_RUNS = 20; - public final void testFromXContent() throws IOException { - for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { - final C clientTestInstance = createClientTestInstance(); + final C clientTestInstance = createClientTestInstance(); - final XContentType xContentType = randomFrom(XContentType.values()); - final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); + final XContentType xContentType = randomFrom(XContentType.values()); + final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); - final XContent xContent = XContentFactory.xContent(xContentType); - final XContentParser parser = xContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - bytes.streamInput()); - final S serverInstance = doParseToServerInstance(parser); - assertInstances(serverInstance, clientTestInstance); - } + final XContent xContent = XContentFactory.xContent(xContentType); + final XContentParser parser = xContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + bytes.streamInput()); + final S serverInstance = doParseToServerInstance(parser); + assertInstances(serverInstance, clientTestInstance); } protected abstract C createClientTestInstance(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java index 635f1ed28c699..8565ca14a908a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java @@ -41,23 +41,19 @@ */ public abstract class AbstractResponseTestCase extends ESTestCase { - private static final int NUMBER_OF_TEST_RUNS = 20; - public final void testFromXContent() throws IOException { - for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { - final S serverTestInstance = createServerTestInstance(); + final S serverTestInstance = createServerTestInstance(); - final XContentType xContentType = randomFrom(XContentType.values()); - final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); + final XContentType xContentType = randomFrom(XContentType.values()); + final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); - final XContent xContent = XContentFactory.xContent(xContentType); - final XContentParser parser = xContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - bytes.streamInput()); - final C clientInstance = doParseToClientInstance(parser); - assertInstances(serverTestInstance, clientInstance); - } + final XContent xContent = XContentFactory.xContent(xContentType); + final XContentParser parser = xContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + bytes.streamInput()); + final C clientInstance = doParseToClientInstance(parser); + assertInstances(serverTestInstance, clientInstance); } protected abstract S createServerTestInstance(); From a1b579b7cb4dcaf64b378c4d81cbb26771981473 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 8 Apr 2019 07:43:01 +0200 Subject: [PATCH 4/4] removed redundant test dependency --- client/rest-high-level/build.gradle | 3 --- 1 file changed, 3 deletions(-) diff --git a/client/rest-high-level/build.gradle b/client/rest-high-level/build.gradle index 38a0b8ea1dea0..420bd6d7414f4 100644 --- a/client/rest-high-level/build.gradle +++ b/client/rest-high-level/build.gradle @@ -69,9 +69,6 @@ dependencies { testCompile "org.elasticsearch.plugin:x-pack-core:${version}" restSpec "org.elasticsearch:rest-api-spec:${version}" - // Needed for serialization tests: - // (In order to serialize a server side class to a client side class or the other way around) - testCompile "org.elasticsearch.plugin:x-pack-core:${version}" } //we need to copy the yaml spec so we can check naming (see RestHighlevelClientTests#testApiNamingConventions)