Skip to content

Commit 34c36e9

Browse files
authored
Added unit test for FollowParameters class (#38500) (#38694)
A unit test that tests FollowParameters directly was missing.
1 parent f388c79 commit 34c36e9

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowInfoResponseTests.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
package org.elasticsearch.xpack.ccr.action;
77

88
import org.elasticsearch.common.io.stream.Writeable;
9-
import org.elasticsearch.common.unit.ByteSizeValue;
10-
import org.elasticsearch.common.unit.TimeValue;
119
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
12-
import org.elasticsearch.common.xcontent.ObjectParser;
1310
import org.elasticsearch.common.xcontent.XContentParser;
1411
import org.elasticsearch.test.AbstractSerializingTestCase;
1512
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction;
@@ -26,7 +23,6 @@
2623

2724
public class FollowInfoResponseTests extends AbstractSerializingTestCase<FollowInfoAction.Response> {
2825

29-
static final ObjectParser<FollowParameters, Void> PARAMETERS_PARSER = new ObjectParser<>("parameters_parser", FollowParameters::new);
3026
static final ConstructingObjectParser<FollowerInfo, Void> INFO_PARSER = new ConstructingObjectParser<>(
3127
"info_parser",
3228
args -> {
@@ -40,13 +36,12 @@ public class FollowInfoResponseTests extends AbstractSerializingTestCase<FollowI
4036
});
4137

4238
static {
43-
FollowParameters.initParser(PARAMETERS_PARSER);
44-
4539
INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.FOLLOWER_INDEX_FIELD);
4640
INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.REMOTE_CLUSTER_FIELD);
4741
INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.LEADER_INDEX_FIELD);
4842
INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.STATUS_FIELD);
49-
INFO_PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), PARAMETERS_PARSER, FollowerInfo.PARAMETERS_FIELD);
43+
INFO_PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), FollowParametersTests.PARSER,
44+
FollowerInfo.PARAMETERS_FIELD);
5045
}
5146

5247
@SuppressWarnings("unchecked")
@@ -79,17 +74,7 @@ protected FollowInfoAction.Response createTestInstance() {
7974
for (int i = 0; i < numInfos; i++) {
8075
FollowParameters followParameters = null;
8176
if (randomBoolean()) {
82-
followParameters = new FollowParameters();
83-
followParameters.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
84-
followParameters.setMaxOutstandingWriteRequests(randomIntBetween(0, Integer.MAX_VALUE));
85-
followParameters.setMaxReadRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
86-
followParameters.setMaxWriteRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
87-
followParameters.setMaxReadRequestSize(new ByteSizeValue(randomNonNegativeLong()));
88-
followParameters.setMaxWriteRequestSize(new ByteSizeValue(randomNonNegativeLong()));
89-
followParameters.setMaxWriteBufferCount(randomIntBetween(0, Integer.MAX_VALUE));
90-
followParameters.setMaxWriteBufferSize(new ByteSizeValue(randomNonNegativeLong()));
91-
followParameters.setMaxRetryDelay(new TimeValue(randomNonNegativeLong()));
92-
followParameters.setReadPollTimeout(new TimeValue(randomNonNegativeLong()));
77+
followParameters = FollowParametersTests.randomInstance();
9378
}
9479

9580
infos.add(new FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.ccr.action;
7+
8+
import org.elasticsearch.common.io.stream.Writeable;
9+
import org.elasticsearch.common.unit.ByteSizeValue;
10+
import org.elasticsearch.common.unit.TimeValue;
11+
import org.elasticsearch.common.xcontent.ObjectParser;
12+
import org.elasticsearch.common.xcontent.XContentParser;
13+
import org.elasticsearch.test.AbstractSerializingTestCase;
14+
import org.elasticsearch.xpack.core.ccr.action.FollowParameters;
15+
16+
import java.io.IOException;
17+
18+
public class FollowParametersTests extends AbstractSerializingTestCase<FollowParameters> {
19+
20+
static final ObjectParser<FollowParameters, Void> PARSER = new ObjectParser<>("test_parser", FollowParameters::new);
21+
static {
22+
FollowParameters.initParser(PARSER);
23+
}
24+
25+
@Override
26+
protected FollowParameters doParseInstance(XContentParser parser) throws IOException {
27+
return PARSER.apply(parser, null);
28+
}
29+
30+
@Override
31+
protected FollowParameters createTestInstance() {
32+
return randomInstance();
33+
}
34+
35+
@Override
36+
protected Writeable.Reader<FollowParameters> instanceReader() {
37+
return FollowParameters::new;
38+
}
39+
40+
static FollowParameters randomInstance() {
41+
FollowParameters followParameters = new FollowParameters();
42+
followParameters.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
43+
followParameters.setMaxOutstandingWriteRequests(randomIntBetween(0, Integer.MAX_VALUE));
44+
followParameters.setMaxReadRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
45+
followParameters.setMaxWriteRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
46+
followParameters.setMaxReadRequestSize(new ByteSizeValue(randomNonNegativeLong()));
47+
followParameters.setMaxWriteRequestSize(new ByteSizeValue(randomNonNegativeLong()));
48+
followParameters.setMaxWriteBufferCount(randomIntBetween(0, Integer.MAX_VALUE));
49+
followParameters.setMaxWriteBufferSize(new ByteSizeValue(randomNonNegativeLong()));
50+
followParameters.setMaxRetryDelay(new TimeValue(randomNonNegativeLong()));
51+
followParameters.setReadPollTimeout(new TimeValue(randomNonNegativeLong()));
52+
return followParameters;
53+
}
54+
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowInfoAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public FollowParameters getParameters() {
191191
remoteCluster = in.readString();
192192
leaderIndex = in.readString();
193193
status = Status.fromString(in.readString());
194-
parameters = in.readOptionalWriteable(innerIn -> new FollowParameters(in));
194+
parameters = in.readOptionalWriteable(FollowParameters::new);
195195
}
196196

197197
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowParameters.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
import org.elasticsearch.common.unit.TimeValue;
1515
import org.elasticsearch.common.xcontent.AbstractObjectParser;
1616
import org.elasticsearch.common.xcontent.ObjectParser;
17+
import org.elasticsearch.common.xcontent.ToXContentObject;
1718
import org.elasticsearch.common.xcontent.XContentBuilder;
1819

1920
import java.io.IOException;
2021
import java.util.Objects;
2122

2223
import static org.elasticsearch.action.ValidateActions.addValidationError;
2324

24-
public class FollowParameters implements Writeable {
25+
public class FollowParameters implements Writeable, ToXContentObject {
2526

2627
private static final TimeValue RETRY_DELAY_MAX = TimeValue.timeValueMinutes(5);
2728

@@ -184,7 +185,7 @@ public ActionRequestValidationException validate() {
184185
return e;
185186
}
186187

187-
FollowParameters(StreamInput in) throws IOException {
188+
public FollowParameters(StreamInput in) throws IOException {
188189
fromStreamInput(in);
189190
}
190191

@@ -215,6 +216,14 @@ void fromStreamInput(StreamInput in) throws IOException {
215216
readPollTimeout = in.readOptionalTimeValue();
216217
}
217218

219+
@Override
220+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
221+
builder.startObject();
222+
toXContentFragment(builder);
223+
builder.endObject();
224+
return builder;
225+
}
226+
218227
XContentBuilder toXContentFragment(final XContentBuilder builder) throws IOException {
219228
if (maxReadRequestOperationCount != null) {
220229
builder.field(MAX_READ_REQUEST_OPERATION_COUNT.getPreferredName(), maxReadRequestOperationCount);

0 commit comments

Comments
 (0)