Skip to content

Commit c639009

Browse files
committed
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 elastic#39844 but then for hlrc request serialization tests. Relates to elastic#39745
1 parent 4b45c1f commit c639009

File tree

5 files changed

+145
-156
lines changed

5 files changed

+145
-156
lines changed

client/rest-high-level/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ dependencies {
6666
testCompile "org.elasticsearch:rest-api-spec:${version}"
6767

6868
restSpec "org.elasticsearch:rest-api-spec:${version}"
69+
// Needed for serialization tests:
70+
// (In order to serialize a server side class to a client side class or the other way around)
71+
testCompile "org.elasticsearch.plugin:x-pack-core:${version}"
6972
}
7073

7174
//we need to copy the yaml spec so we can check naming (see RestHighlevelClientTests#testApiNamingConventions)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client;
20+
21+
import org.elasticsearch.cluster.ClusterModule;
22+
import org.elasticsearch.common.bytes.BytesReference;
23+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
24+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
25+
import org.elasticsearch.common.xcontent.ToXContent;
26+
import org.elasticsearch.common.xcontent.XContent;
27+
import org.elasticsearch.common.xcontent.XContentFactory;
28+
import org.elasticsearch.common.xcontent.XContentParser;
29+
import org.elasticsearch.common.xcontent.XContentType;
30+
import org.elasticsearch.test.ESTestCase;
31+
32+
import java.io.IOException;
33+
34+
/**
35+
* Base class for HLRC request parsing tests.
36+
*
37+
* This case class facilitates generating client side request test instances and
38+
* verifies that they are correctly parsed into server side request instances.
39+
*
40+
* @param <C> The class representing the request on the client side.
41+
* @param <S> The class representing the request on the server side.
42+
*/
43+
public abstract class AbstractRequestTestCase<C extends ToXContent, S> extends ESTestCase {
44+
45+
private static final int NUMBER_OF_TEST_RUNS = 20;
46+
47+
public final void testFromXContent() throws IOException {
48+
for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) {
49+
final C clientTestInstance = createClientTestInstance();
50+
51+
final XContentType xContentType = randomFrom(XContentType.values());
52+
final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
53+
54+
final XContent xContent = XContentFactory.xContent(xContentType);
55+
final XContentParser parser = xContent.createParser(
56+
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
57+
LoggingDeprecationHandler.INSTANCE,
58+
bytes.streamInput());
59+
final S serverInstance = doParseToServerInstance(parser);
60+
assertInstances(serverInstance, clientTestInstance);
61+
}
62+
}
63+
64+
protected abstract C createClientTestInstance();
65+
66+
protected abstract S doParseToServerInstance(XContentParser parser) throws IOException;
67+
68+
protected abstract void assertInstances(S serverInstance, C clientTestInstance);
69+
70+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,71 +19,24 @@
1919

2020
package org.elasticsearch.client.ccr;
2121

22+
import org.elasticsearch.client.AbstractRequestTestCase;
2223
import org.elasticsearch.common.unit.ByteSizeValue;
2324
import org.elasticsearch.common.unit.TimeValue;
24-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
25-
import org.elasticsearch.common.xcontent.ObjectParser;
2625
import org.elasticsearch.common.xcontent.XContentParser;
27-
import org.elasticsearch.test.AbstractXContentTestCase;
26+
import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction;
2827

2928
import java.io.IOException;
3029
import java.util.Arrays;
31-
import java.util.List;
3230

33-
public class PutAutoFollowPatternRequestTests extends AbstractXContentTestCase<PutAutoFollowPatternRequest> {
31+
import static org.elasticsearch.client.ccr.PutFollowRequestTests.assertFollowConfig;
32+
import static org.hamcrest.Matchers.equalTo;
3433

35-
@SuppressWarnings("unchecked")
36-
private static final ConstructingObjectParser<PutAutoFollowPatternRequest, Void> PARSER = new ConstructingObjectParser<>("test_parser",
37-
true, (args) -> new PutAutoFollowPatternRequest("name", (String) args[0], (List<String>) args[1]));
38-
39-
static {
40-
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD);
41-
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), PutAutoFollowPatternRequest.LEADER_PATTERNS_FIELD);
42-
PARSER.declareString(PutAutoFollowPatternRequest::setFollowIndexNamePattern, PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD);
43-
PARSER.declareInt(PutAutoFollowPatternRequest::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT);
44-
PARSER.declareField(
45-
PutAutoFollowPatternRequest::setMaxReadRequestSize,
46-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()),
47-
PutFollowRequest.MAX_READ_REQUEST_SIZE,
48-
ObjectParser.ValueType.STRING);
49-
PARSER.declareInt(PutAutoFollowPatternRequest::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS);
50-
PARSER.declareInt(PutAutoFollowPatternRequest::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT);
51-
PARSER.declareField(
52-
PutAutoFollowPatternRequest::setMaxWriteRequestSize,
53-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()),
54-
PutFollowRequest.MAX_WRITE_REQUEST_SIZE,
55-
ObjectParser.ValueType.STRING);
56-
PARSER.declareInt(PutAutoFollowPatternRequest::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS);
57-
PARSER.declareInt(PutAutoFollowPatternRequest::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT);
58-
PARSER.declareField(
59-
PutAutoFollowPatternRequest::setMaxWriteBufferSize,
60-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()),
61-
PutFollowRequest.MAX_WRITE_BUFFER_SIZE,
62-
ObjectParser.ValueType.STRING);
63-
PARSER.declareField(
64-
PutAutoFollowPatternRequest::setMaxRetryDelay,
65-
(p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()),
66-
PutFollowRequest.MAX_RETRY_DELAY_FIELD,
67-
ObjectParser.ValueType.STRING);
68-
PARSER.declareField(
69-
PutAutoFollowPatternRequest::setReadPollTimeout,
70-
(p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()),
71-
PutFollowRequest.READ_POLL_TIMEOUT,
72-
ObjectParser.ValueType.STRING);
73-
}
74-
75-
@Override
76-
protected PutAutoFollowPatternRequest doParseInstance(XContentParser parser) throws IOException {
77-
return PARSER.apply(parser, null);
78-
}
79-
80-
@Override
81-
protected boolean supportsUnknownFields() {
82-
return true;
83-
}
34+
public class PutAutoFollowPatternRequestTests extends AbstractRequestTestCase<
35+
PutAutoFollowPatternRequest,
36+
PutAutoFollowPatternAction.Request> {
8437

8538
@Override
86-
protected PutAutoFollowPatternRequest createTestInstance() {
39+
protected PutAutoFollowPatternRequest createClientTestInstance() {
8740
// Name isn't serialized, because it specified in url path, so no need to randomly generate it here.
8841
PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest("name",
8942
randomAlphaOfLength(4), Arrays.asList(generateRandomStringArray(4, 4, false)));
@@ -123,4 +76,18 @@ protected PutAutoFollowPatternRequest createTestInstance() {
12376
return putAutoFollowPatternRequest;
12477
}
12578

79+
@Override
80+
protected PutAutoFollowPatternAction.Request doParseToServerInstance(XContentParser parser) throws IOException {
81+
return PutAutoFollowPatternAction.Request.fromXContent(parser, "name");
82+
}
83+
84+
@Override
85+
protected void assertInstances(PutAutoFollowPatternAction.Request serverInstance, PutAutoFollowPatternRequest clientTestInstance) {
86+
assertThat(serverInstance.getName(), equalTo(clientTestInstance.getName()));
87+
assertThat(serverInstance.getRemoteCluster(), equalTo(clientTestInstance.getRemoteCluster()));
88+
assertThat(serverInstance.getLeaderIndexPatterns(), equalTo(clientTestInstance.getLeaderIndexPatterns()));
89+
assertThat(serverInstance.getFollowIndexNamePattern(), equalTo(clientTestInstance.getFollowIndexNamePattern()));
90+
assertFollowConfig(serverInstance.getParameters(), clientTestInstance);
91+
}
92+
12693
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,22 @@
1919

2020
package org.elasticsearch.client.ccr;
2121

22+
import org.elasticsearch.action.support.ActiveShardCount;
23+
import org.elasticsearch.client.AbstractRequestTestCase;
2224
import org.elasticsearch.common.unit.ByteSizeValue;
2325
import org.elasticsearch.common.unit.TimeValue;
24-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
25-
import org.elasticsearch.common.xcontent.ObjectParser;
2626
import org.elasticsearch.common.xcontent.XContentParser;
27-
import org.elasticsearch.test.AbstractXContentTestCase;
27+
import org.elasticsearch.xpack.core.ccr.action.FollowParameters;
28+
import org.elasticsearch.xpack.core.ccr.action.PutFollowAction;
2829

2930
import java.io.IOException;
3031

31-
public class PutFollowRequestTests extends AbstractXContentTestCase<PutFollowRequest> {
32+
import static org.hamcrest.Matchers.equalTo;
3233

33-
private static final ConstructingObjectParser<PutFollowRequest, Void> PARSER = new ConstructingObjectParser<>("test_parser",
34-
true, (args) -> new PutFollowRequest((String) args[0], (String) args[1], "followerIndex"));
35-
36-
static {
37-
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD);
38-
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.LEADER_INDEX_FIELD);
39-
PARSER.declareInt(PutFollowRequest::setMaxReadRequestOperationCount, PutFollowRequest.MAX_READ_REQUEST_OPERATION_COUNT);
40-
PARSER.declareField(
41-
PutFollowRequest::setMaxReadRequestSize,
42-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), PutFollowRequest.MAX_READ_REQUEST_SIZE.getPreferredName()),
43-
PutFollowRequest.MAX_READ_REQUEST_SIZE,
44-
ObjectParser.ValueType.STRING);
45-
PARSER.declareInt(PutFollowRequest::setMaxOutstandingReadRequests, PutFollowRequest.MAX_OUTSTANDING_READ_REQUESTS);
46-
PARSER.declareInt(PutFollowRequest::setMaxWriteRequestOperationCount, PutFollowRequest.MAX_WRITE_REQUEST_OPERATION_COUNT);
47-
PARSER.declareField(
48-
PutFollowRequest::setMaxWriteRequestSize,
49-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), PutFollowRequest.MAX_WRITE_REQUEST_SIZE.getPreferredName()),
50-
PutFollowRequest.MAX_WRITE_REQUEST_SIZE,
51-
ObjectParser.ValueType.STRING);
52-
PARSER.declareInt(PutFollowRequest::setMaxOutstandingWriteRequests, PutFollowRequest.MAX_OUTSTANDING_WRITE_REQUESTS);
53-
PARSER.declareInt(PutFollowRequest::setMaxWriteBufferCount, PutFollowRequest.MAX_WRITE_BUFFER_COUNT);
54-
PARSER.declareField(
55-
PutFollowRequest::setMaxWriteBufferSize,
56-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), PutFollowRequest.MAX_WRITE_BUFFER_SIZE.getPreferredName()),
57-
PutFollowRequest.MAX_WRITE_BUFFER_SIZE,
58-
ObjectParser.ValueType.STRING);
59-
PARSER.declareField(
60-
PutFollowRequest::setMaxRetryDelay,
61-
(p, c) -> TimeValue.parseTimeValue(p.text(), PutFollowRequest.MAX_RETRY_DELAY_FIELD.getPreferredName()),
62-
PutFollowRequest.MAX_RETRY_DELAY_FIELD,
63-
ObjectParser.ValueType.STRING);
64-
PARSER.declareField(
65-
PutFollowRequest::setReadPollTimeout,
66-
(p, c) -> TimeValue.parseTimeValue(p.text(), PutFollowRequest.READ_POLL_TIMEOUT.getPreferredName()),
67-
PutFollowRequest.READ_POLL_TIMEOUT,
68-
ObjectParser.ValueType.STRING);
69-
}
70-
71-
@Override
72-
protected PutFollowRequest doParseInstance(XContentParser parser) throws IOException {
73-
return PARSER.apply(parser, null);
74-
}
75-
76-
@Override
77-
protected boolean supportsUnknownFields() {
78-
return false;
79-
}
34+
public class PutFollowRequestTests extends AbstractRequestTestCase<PutFollowRequest, PutFollowAction.Request> {
8035

8136
@Override
82-
protected PutFollowRequest createTestInstance() {
37+
protected PutFollowRequest createClientTestInstance() {
8338
PutFollowRequest putFollowRequest =
8439
new PutFollowRequest(randomAlphaOfLength(4), randomAlphaOfLength(4), "followerIndex");
8540
if (randomBoolean()) {
@@ -115,4 +70,30 @@ protected PutFollowRequest createTestInstance() {
11570
return putFollowRequest;
11671
}
11772

73+
@Override
74+
protected PutFollowAction.Request doParseToServerInstance(XContentParser parser) throws IOException {
75+
return PutFollowAction.Request.fromXContent(parser, "followerIndex", ActiveShardCount.DEFAULT);
76+
}
77+
78+
@Override
79+
protected void assertInstances(PutFollowAction.Request serverInstance, PutFollowRequest clientTestInstance) {
80+
assertThat(serverInstance.getRemoteCluster(), equalTo(clientTestInstance.getRemoteCluster()));
81+
assertThat(serverInstance.getLeaderIndex(), equalTo(clientTestInstance.getLeaderIndex()));
82+
assertThat(serverInstance.getFollowerIndex(), equalTo(clientTestInstance.getFollowerIndex()));
83+
assertFollowConfig(serverInstance.getParameters(), clientTestInstance);
84+
}
85+
86+
static void assertFollowConfig(FollowParameters serverParameters, FollowConfig clientConfig) {
87+
assertThat(serverParameters.getMaxReadRequestOperationCount(), equalTo(clientConfig.getMaxReadRequestOperationCount()));
88+
assertThat(serverParameters.getMaxWriteRequestOperationCount(), equalTo(clientConfig.getMaxWriteRequestOperationCount()));
89+
assertThat(serverParameters.getMaxOutstandingReadRequests(), equalTo(clientConfig.getMaxOutstandingReadRequests()));
90+
assertThat(serverParameters.getMaxOutstandingWriteRequests(), equalTo(clientConfig.getMaxOutstandingWriteRequests()));
91+
assertThat(serverParameters.getMaxReadRequestSize(), equalTo(clientConfig.getMaxReadRequestSize()));
92+
assertThat(serverParameters.getMaxWriteRequestSize(), equalTo(clientConfig.getMaxWriteRequestSize()));
93+
assertThat(serverParameters.getMaxWriteBufferCount(), equalTo(clientConfig.getMaxWriteBufferCount()));
94+
assertThat(serverParameters.getMaxWriteBufferSize(), equalTo(clientConfig.getMaxWriteBufferSize()));
95+
assertThat(serverParameters.getMaxRetryDelay(), equalTo(clientConfig.getMaxRetryDelay()));
96+
assertThat(serverParameters.getReadPollTimeout(), equalTo(clientConfig.getReadPollTimeout()));
97+
}
98+
11899
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/ResumeFollowRequestTests.java

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,21 @@
1919

2020
package org.elasticsearch.client.ccr;
2121

22+
import org.elasticsearch.client.AbstractRequestTestCase;
2223
import org.elasticsearch.common.unit.ByteSizeValue;
2324
import org.elasticsearch.common.unit.TimeValue;
24-
import org.elasticsearch.common.xcontent.ObjectParser;
2525
import org.elasticsearch.common.xcontent.XContentParser;
26-
import org.elasticsearch.test.AbstractXContentTestCase;
26+
import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction;
2727

2828
import java.io.IOException;
2929

30-
public class ResumeFollowRequestTests extends AbstractXContentTestCase<ResumeFollowRequest> {
30+
import static org.elasticsearch.client.ccr.PutFollowRequestTests.assertFollowConfig;
31+
import static org.hamcrest.Matchers.equalTo;
3132

32-
private static final ObjectParser<ResumeFollowRequest, Void> PARSER = new ObjectParser<>("test_parser",
33-
true, () -> new ResumeFollowRequest("followerIndex"));
34-
35-
static {
36-
PARSER.declareInt(ResumeFollowRequest::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT);
37-
PARSER.declareField(
38-
ResumeFollowRequest::setMaxReadRequestSize,
39-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()),
40-
PutFollowRequest.MAX_READ_REQUEST_SIZE,
41-
ObjectParser.ValueType.STRING);
42-
PARSER.declareInt(ResumeFollowRequest::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS);
43-
PARSER.declareInt(ResumeFollowRequest::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT);
44-
PARSER.declareField(
45-
ResumeFollowRequest::setMaxWriteRequestSize,
46-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()),
47-
PutFollowRequest.MAX_WRITE_REQUEST_SIZE,
48-
ObjectParser.ValueType.STRING);
49-
PARSER.declareInt(ResumeFollowRequest::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS);
50-
PARSER.declareInt(ResumeFollowRequest::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT);
51-
PARSER.declareField(
52-
ResumeFollowRequest::setMaxWriteBufferSize,
53-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()),
54-
PutFollowRequest.MAX_WRITE_BUFFER_SIZE,
55-
ObjectParser.ValueType.STRING);
56-
PARSER.declareField(
57-
ResumeFollowRequest::setMaxRetryDelay,
58-
(p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()),
59-
PutFollowRequest.MAX_RETRY_DELAY_FIELD,
60-
ObjectParser.ValueType.STRING);
61-
PARSER.declareField(
62-
ResumeFollowRequest::setReadPollTimeout,
63-
(p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()),
64-
PutFollowRequest.READ_POLL_TIMEOUT,
65-
ObjectParser.ValueType.STRING);
66-
}
67-
68-
@Override
69-
protected ResumeFollowRequest doParseInstance(XContentParser parser) throws IOException {
70-
return PARSER.apply(parser, null);
71-
}
72-
73-
@Override
74-
protected boolean supportsUnknownFields() {
75-
return true;
76-
}
33+
public class ResumeFollowRequestTests extends AbstractRequestTestCase<ResumeFollowRequest, ResumeFollowAction.Request> {
7734

7835
@Override
79-
protected ResumeFollowRequest createTestInstance() {
36+
protected ResumeFollowRequest createClientTestInstance() {
8037
ResumeFollowRequest resumeFollowRequest = new ResumeFollowRequest("followerIndex");
8138
if (randomBoolean()) {
8239
resumeFollowRequest.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
@@ -111,4 +68,15 @@ protected ResumeFollowRequest createTestInstance() {
11168
return resumeFollowRequest;
11269
}
11370

71+
@Override
72+
protected ResumeFollowAction.Request doParseToServerInstance(XContentParser parser) throws IOException {
73+
return ResumeFollowAction.Request.fromXContent(parser, "followerIndex");
74+
}
75+
76+
@Override
77+
protected void assertInstances(ResumeFollowAction.Request serverInstance, ResumeFollowRequest clientTestInstance) {
78+
assertThat(serverInstance.getFollowerIndex(), equalTo(clientTestInstance.getFollowerIndex()));
79+
assertFollowConfig(serverInstance.getParameters(), clientTestInstance);
80+
}
81+
11482
}

0 commit comments

Comments
 (0)