Skip to content

Commit 46b0fda

Browse files
committed
Add realistic hlrc request serialization test base class and (#40362)
changed 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. Removed iterators from hlrc parsing tests. Use empty xcontent registries. Relates to #39745
1 parent 0157ebf commit 46b0fda

File tree

5 files changed

+147
-171
lines changed

5 files changed

+147
-171
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.common.bytes.BytesReference;
22+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
23+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
24+
import org.elasticsearch.common.xcontent.ToXContent;
25+
import org.elasticsearch.common.xcontent.XContent;
26+
import org.elasticsearch.common.xcontent.XContentFactory;
27+
import org.elasticsearch.common.xcontent.XContentParser;
28+
import org.elasticsearch.common.xcontent.XContentType;
29+
import org.elasticsearch.test.ESTestCase;
30+
31+
import java.io.IOException;
32+
33+
/**
34+
* Base class for HLRC request parsing tests.
35+
*
36+
* This case class facilitates generating client side request test instances and
37+
* verifies that they are correctly parsed into server side request instances.
38+
*
39+
* @param <C> The class representing the request on the client side.
40+
* @param <S> The class representing the request on the server side.
41+
*/
42+
public abstract class AbstractRequestTestCase<C extends ToXContent, S> extends ESTestCase {
43+
44+
public final void testFromXContent() throws IOException {
45+
final C clientTestInstance = createClientTestInstance();
46+
47+
final XContentType xContentType = randomFrom(XContentType.values());
48+
final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
49+
50+
final XContent xContent = XContentFactory.xContent(xContentType);
51+
final XContentParser parser = xContent.createParser(
52+
NamedXContentRegistry.EMPTY,
53+
LoggingDeprecationHandler.INSTANCE,
54+
bytes.streamInput());
55+
final S serverInstance = doParseToServerInstance(parser);
56+
assertInstances(serverInstance, clientTestInstance);
57+
}
58+
59+
protected abstract C createClientTestInstance();
60+
61+
protected abstract S doParseToServerInstance(XContentParser parser) throws IOException;
62+
63+
protected abstract void assertInstances(S serverInstance, C clientTestInstance);
64+
65+
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.elasticsearch.client;
2020

21-
import org.elasticsearch.cluster.ClusterModule;
2221
import org.elasticsearch.common.bytes.BytesReference;
2322
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2423
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -42,23 +41,19 @@
4241
*/
4342
public abstract class AbstractResponseTestCase<S extends ToXContent, C> extends ESTestCase {
4443

45-
private static final int NUMBER_OF_TEST_RUNS = 20;
46-
4744
public final void testFromXContent() throws IOException {
48-
for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) {
49-
final S serverTestInstance = createServerTestInstance();
45+
final S serverTestInstance = createServerTestInstance();
5046

51-
final XContentType xContentType = randomFrom(XContentType.values());
52-
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
47+
final XContentType xContentType = randomFrom(XContentType.values());
48+
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
5349

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 C clientInstance = doParseToClientInstance(parser);
60-
assertInstances(serverTestInstance, clientInstance);
61-
}
50+
final XContent xContent = XContentFactory.xContent(xContentType);
51+
final XContentParser parser = xContent.createParser(
52+
NamedXContentRegistry.EMPTY,
53+
LoggingDeprecationHandler.INSTANCE,
54+
bytes.streamInput());
55+
final C clientInstance = doParseToClientInstance(parser);
56+
assertInstances(serverTestInstance, clientInstance);
6257
}
6358

6459
protected abstract S createServerTestInstance();

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
}

0 commit comments

Comments
 (0)