Skip to content

Commit b408e74

Browse files
committed
Migrate some *ResponseTests to AbstractStreamableXContentTestCase (#28749)
This allows us to save a bit of code, but also adds more coverage as it tests serialization which was missing in some of the existing tests. Also it requires implementing equals/hashcode and we get the corresponding tests for them for free from the base test class.
1 parent 526ee89 commit b408e74

23 files changed

+364
-373
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.common.xcontent.XContentParser;
3131

3232
import java.io.IOException;
33+
import java.util.Objects;
3334

3435
/**
3536
* A response for a cluster update settings action.
@@ -103,7 +104,22 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
103104
return builder;
104105
}
105106

106-
public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) throws IOException {
107+
public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) {
107108
return PARSER.apply(parser, null);
108109
}
110+
111+
@Override
112+
public boolean equals(Object o) {
113+
if (super.equals(o)) {
114+
ClusterUpdateSettingsResponse that = (ClusterUpdateSettingsResponse) o;
115+
return Objects.equals(transientSettings, that.transientSettings) &&
116+
Objects.equals(persistentSettings, that.persistentSettings);
117+
}
118+
return false;
119+
}
120+
121+
@Override
122+
public int hashCode() {
123+
return Objects.hash(super.hashCode(), transientSettings, persistentSettings);
124+
}
109125
}

server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6767
return builder;
6868
}
6969

70-
public static IndicesAliasesResponse fromXContent(XContentParser parser) throws IOException {
70+
public static IndicesAliasesResponse fromXContent(XContentParser parser) {
7171
return PARSER.apply(parser, null);
7272
}
7373
}

server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6767
return builder;
6868
}
6969

70-
public static CloseIndexResponse fromXContent(XContentParser parser) throws IOException {
70+
public static CloseIndexResponse fromXContent(XContentParser parser) {
7171
return PARSER.apply(parser, null);
7272
}
7373
}

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.xcontent.XContentParser;
3232

3333
import java.io.IOException;
34+
import java.util.Objects;
3435

3536
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3637

@@ -112,4 +113,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
112113
public static CreateIndexResponse fromXContent(XContentParser parser) {
113114
return PARSER.apply(parser, null);
114115
}
116+
117+
@Override
118+
public boolean equals(Object o) {
119+
if (super.equals(o)) {
120+
CreateIndexResponse that = (CreateIndexResponse) o;
121+
return Objects.equals(index, that.index);
122+
}
123+
return false;
124+
}
125+
126+
@Override
127+
public int hashCode() {
128+
return Objects.hash(super.hashCode(), index);
129+
}
115130
}

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6969
return builder;
7070
}
7171

72-
public static PutMappingResponse fromXContent(XContentParser parser) throws IOException {
72+
public static PutMappingResponse fromXContent(XContentParser parser) {
7373
return PARSER.apply(parser, null);
7474
}
7575
}

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,19 @@ public static RolloverResponse fromXContent(XContentParser parser) {
180180

181181
@Override
182182
public boolean equals(Object o) {
183-
if (this == o) {
184-
return true;
183+
if (super.equals(o)) {
184+
RolloverResponse that = (RolloverResponse) o;
185+
return dryRun == that.dryRun &&
186+
rolledOver == that.rolledOver &&
187+
Objects.equals(oldIndex, that.oldIndex) &&
188+
Objects.equals(newIndex, that.newIndex) &&
189+
Objects.equals(conditionStatus, that.conditionStatus);
185190
}
186-
if (o == null || getClass() != o.getClass()) {
187-
return false;
188-
}
189-
RolloverResponse that = (RolloverResponse) o;
190-
return isAcknowledged() == that.isAcknowledged() &&
191-
isShardsAcknowledged() == that.isShardsAcknowledged() &&
192-
dryRun == that.dryRun &&
193-
rolledOver == that.rolledOver &&
194-
Objects.equals(oldIndex, that.oldIndex) &&
195-
Objects.equals(newIndex, that.newIndex) &&
196-
Objects.equals(conditionStatus, that.conditionStatus);
191+
return false;
197192
}
198193

199194
@Override
200195
public int hashCode() {
201-
return Objects.hash(isAcknowledged(), isShardsAcknowledged(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver);
196+
return Objects.hash(super.hashCode(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver);
202197
}
203198
}

server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
2828

2929
import java.io.IOException;
30+
import java.util.Objects;
3031

3132
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3233

@@ -78,4 +79,21 @@ protected void writeAcknowledged(StreamOutput out) throws IOException {
7879
protected void addAcknowledgedField(XContentBuilder builder) throws IOException {
7980
builder.field(ACKNOWLEDGED.getPreferredName(), isAcknowledged());
8081
}
82+
83+
@Override
84+
public boolean equals(Object o) {
85+
if (this == o) {
86+
return true;
87+
}
88+
if (o == null || getClass() != o.getClass()) {
89+
return false;
90+
}
91+
AcknowledgedResponse that = (AcknowledgedResponse) o;
92+
return isAcknowledged() == that.isAcknowledged();
93+
}
94+
95+
@Override
96+
public int hashCode() {
97+
return Objects.hash(isAcknowledged());
98+
}
8199
}

server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
2828

2929
import java.io.IOException;
30+
import java.util.Objects;
3031

3132
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3233

@@ -73,4 +74,19 @@ protected void writeShardsAcknowledged(StreamOutput out) throws IOException {
7374
protected void addShardsAcknowledgedField(XContentBuilder builder) throws IOException {
7475
builder.field(SHARDS_ACKNOWLEDGED.getPreferredName(), isShardsAcknowledged());
7576
}
77+
78+
@Override
79+
public boolean equals(Object o) {
80+
if (super.equals(o)) {
81+
ShardsAcknowledgedResponse that = (ShardsAcknowledgedResponse) o;
82+
return shardsAcknowledged == that.shardsAcknowledged;
83+
}
84+
return false;
85+
}
86+
87+
@Override
88+
public int hashCode() {
89+
return Objects.hash(super.hashCode(), shardsAcknowledged);
90+
}
91+
7692
}

server/src/test/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponseTests.java

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

2020
package org.elasticsearch.action.admin.cluster.settings;
2121

22-
import org.elasticsearch.common.bytes.BytesReference;
2322
import org.elasticsearch.common.settings.ClusterSettings;
2423
import org.elasticsearch.common.settings.Setting;
2524
import org.elasticsearch.common.settings.Settings;
2625
import org.elasticsearch.common.settings.Settings.Builder;
27-
import org.elasticsearch.common.xcontent.ToXContent;
2826
import org.elasticsearch.common.xcontent.XContentParser;
29-
import org.elasticsearch.common.xcontent.XContentType;
30-
import org.elasticsearch.test.ESTestCase;
27+
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
28+
import org.elasticsearch.test.EqualsHashCodeTestUtils;
3129

32-
import java.io.IOException;
30+
import java.util.List;
31+
import java.util.Set;
32+
import java.util.function.Predicate;
3333

34-
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
35-
import static org.hamcrest.CoreMatchers.equalTo;
34+
public class ClusterUpdateSettingsResponseTests extends AbstractStreamableXContentTestCase<ClusterUpdateSettingsResponse> {
3635

37-
public class ClusterUpdateSettingsResponseTests extends ESTestCase {
38-
39-
public void testFromXContent() throws IOException {
40-
doFromXContentTestWithRandomFields(false);
36+
@Override
37+
protected ClusterUpdateSettingsResponse doParseInstance(XContentParser parser) {
38+
return ClusterUpdateSettingsResponse.fromXContent(parser);
4139
}
4240

43-
public void testFromXContentWithRandomFields() throws IOException {
44-
doFromXContentTestWithRandomFields(true);
41+
@Override
42+
protected EqualsHashCodeTestUtils.MutateFunction<ClusterUpdateSettingsResponse> getMutateFunction() {
43+
return response -> {
44+
int i = randomIntBetween(0, 2);
45+
switch(i) {
46+
case 0:
47+
return new ClusterUpdateSettingsResponse(response.isAcknowledged() == false,
48+
response.transientSettings, response.persistentSettings);
49+
case 1:
50+
return new ClusterUpdateSettingsResponse(response.isAcknowledged(), mutateSettings(response.transientSettings),
51+
response.persistentSettings);
52+
case 2:
53+
return new ClusterUpdateSettingsResponse(response.isAcknowledged(), response.transientSettings,
54+
mutateSettings(response.persistentSettings));
55+
default:
56+
throw new UnsupportedOperationException();
57+
}
58+
};
4559
}
4660

47-
private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
48-
final ClusterUpdateSettingsResponse response = createTestItem();
49-
boolean humanReadable = randomBoolean();
50-
final XContentType xContentType = XContentType.JSON;
51-
52-
BytesReference originalBytes = toShuffledXContent(response, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
53-
BytesReference mutated;
54-
if (addRandomFields) {
55-
mutated = insertRandomFields(xContentType, originalBytes, p -> p.startsWith("transient") || p.startsWith("persistent"),
56-
random());
57-
} else {
58-
mutated = originalBytes;
61+
private static Settings mutateSettings(Settings settings) {
62+
if (settings.isEmpty()) {
63+
return randomClusterSettings(1, 3);
5964
}
65+
Set<String> allKeys = settings.keySet();
66+
List<String> keysToBeModified = randomSubsetOf(randomIntBetween(1, allKeys.size()), allKeys);
67+
Builder builder = Settings.builder();
68+
for (String key : allKeys) {
69+
String value = settings.get(key);
70+
if (keysToBeModified.contains(key)) {
71+
value += randomAlphaOfLengthBetween(2, 5);
72+
}
73+
builder.put(key, value);
74+
}
75+
return builder.build();
76+
}
6077

61-
XContentParser parser = createParser(xContentType.xContent(), mutated);
62-
ClusterUpdateSettingsResponse parsedResponse = ClusterUpdateSettingsResponse.fromXContent(parser);
63-
64-
assertNull(parser.nextToken());
65-
assertThat(parsedResponse.isAcknowledged(), equalTo(response.isAcknowledged()));
66-
assertThat(response.transientSettings, equalTo(response.transientSettings));
67-
assertThat(response.persistentSettings, equalTo(response.persistentSettings));
78+
@Override
79+
protected Predicate<String> getRandomFieldsExcludeFilter() {
80+
return p -> p.startsWith("transient") || p.startsWith("persistent");
6881
}
6982

7083
public static Settings randomClusterSettings(int min, int max) {
@@ -77,7 +90,13 @@ public static Settings randomClusterSettings(int min, int max) {
7790
return builder.build();
7891
}
7992

80-
private static ClusterUpdateSettingsResponse createTestItem() {
93+
@Override
94+
protected ClusterUpdateSettingsResponse createTestInstance() {
8195
return new ClusterUpdateSettingsResponse(randomBoolean(), randomClusterSettings(0, 2), randomClusterSettings(0, 2));
8296
}
97+
98+
@Override
99+
protected ClusterUpdateSettingsResponse createBlankInstance() {
100+
return new ClusterUpdateSettingsResponse();
101+
}
83102
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
20+
package org.elasticsearch.action.admin.indices.alias;
21+
22+
import org.elasticsearch.common.xcontent.XContentParser;
23+
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
24+
import org.elasticsearch.test.EqualsHashCodeTestUtils;
25+
26+
public class IndicesAliasesResponseTests extends AbstractStreamableXContentTestCase<IndicesAliasesResponse> {
27+
28+
@Override
29+
protected IndicesAliasesResponse doParseInstance(XContentParser parser) {
30+
return IndicesAliasesResponse.fromXContent(parser);
31+
}
32+
33+
@Override
34+
protected IndicesAliasesResponse createTestInstance() {
35+
return new IndicesAliasesResponse(randomBoolean());
36+
}
37+
38+
@Override
39+
protected IndicesAliasesResponse createBlankInstance() {
40+
return new IndicesAliasesResponse();
41+
}
42+
43+
@Override
44+
protected EqualsHashCodeTestUtils.MutateFunction<IndicesAliasesResponse> getMutateFunction() {
45+
return response -> new IndicesAliasesResponse(response.isAcknowledged() == false);
46+
}
47+
}

0 commit comments

Comments
 (0)