Skip to content

Commit 3cb4a6c

Browse files
committed
[TEST] share code between streamable/writeable/xcontent base test classes (#28785)
Today we have two test base classes that have a lot in common when it comes to testing wire and xcontent serialization: `AbstractSerializingTestCase` and `AbstractXContentStreamableTestCase`. There are subtle differences though between the two, in the way they work, what can be overridden and features that they support (e.g. insertion of random fields). This commit introduces a new base class called `AbstractWireTestCase` which holds all of the serialization test code in common between `Streamable` and `Writeable`. It has two minimal subclasses called `AbstractWireSerializingTestCase` and `AbstractStreamableTestCase` which are specialized for `Writeable` and `Streamable`. This commit also introduces a new test class called `AbstractXContentTestCase` for all of the xContent testing, which holds a testFromXContent method for parsing and rendering to xContent. This one can be delegated to from the existing `AbstractStreamableXContentTestCase` and `AbstractSerializingTestCase` so that we avoid code duplicate as much as possible and all these base classes offer the same functionalities in the same way. Having this last base class decoupled from the serialization testing may also help with the REST high-level client testing, as there are some classes where it's hard to implement equals/hashcode and this makes it possible to override `assertEqualInstances` for custom equality comparisons (also this base class doesn't require implementing equals/hashcode as it doesn't test such methods.
1 parent 098db06 commit 3cb4a6c

27 files changed

+523
-488
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.common;
21+
22+
/**
23+
* A {@link java.util.function.BiFunction}-like interface which allows throwing checked exceptions.
24+
*/
25+
@FunctionalInterface
26+
public interface CheckedBiFunction<T, U, R, E extends Exception> {
27+
R apply(T t, U u) throws E;
28+
}

server/src/main/java/org/elasticsearch/persistent/PersistentTasksCustomMetaData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ public EnumSet<MetaData.XContentContext> context() {
205205
return ALL_CONTEXTS;
206206
}
207207

208-
public static PersistentTasksCustomMetaData fromXContent(XContentParser parser) throws IOException {
209-
return PERSISTENT_TASKS_PARSER.parse(parser, null).build();
208+
public static PersistentTasksCustomMetaData fromXContent(XContentParser parser) {
209+
return PERSISTENT_TASKS_PARSER.apply(parser, null).build();
210210
}
211211

212212
@SuppressWarnings("unchecked")

server/src/main/java/org/elasticsearch/script/StoredScriptSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
318318
* Note that the "source" parameter can also handle template parsing including from
319319
* a complex JSON object.
320320
*/
321-
public static StoredScriptSource fromXContent(XContentParser parser) throws IOException {
321+
public static StoredScriptSource fromXContent(XContentParser parser) {
322322
return PARSER.apply(parser, null).build();
323323
}
324324

server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ public void writeTo(StreamOutput out) throws IOException {
121121
}
122122
}
123123

124-
public static CollapseBuilder fromXContent(XContentParser parser) throws IOException {
125-
CollapseBuilder builder = PARSER.parse(parser, new CollapseBuilder(), null);
126-
return builder;
124+
public static CollapseBuilder fromXContent(XContentParser parser) {
125+
return PARSER.apply(parser, null);
127126
}
128127

129128
// for object parser only

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.settings.Settings.Builder;
2626
import org.elasticsearch.common.xcontent.XContentParser;
2727
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
28-
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2928

3029
import java.util.List;
3130
import java.util.Set;
@@ -39,23 +38,21 @@ protected ClusterUpdateSettingsResponse doParseInstance(XContentParser parser) {
3938
}
4039

4140
@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-
};
41+
protected ClusterUpdateSettingsResponse mutateInstance(ClusterUpdateSettingsResponse response) {
42+
int i = randomIntBetween(0, 2);
43+
switch(i) {
44+
case 0:
45+
return new ClusterUpdateSettingsResponse(response.isAcknowledged() == false,
46+
response.transientSettings, response.persistentSettings);
47+
case 1:
48+
return new ClusterUpdateSettingsResponse(response.isAcknowledged(), mutateSettings(response.transientSettings),
49+
response.persistentSettings);
50+
case 2:
51+
return new ClusterUpdateSettingsResponse(response.isAcknowledged(), response.transientSettings,
52+
mutateSettings(response.persistentSettings));
53+
default:
54+
throw new UnsupportedOperationException();
55+
}
5956
}
6057

6158
private static Settings mutateSettings(Settings settings) {

server/src/test/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponseTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.common.xcontent.XContentParser;
2323
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
24-
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2524

2625
public class IndicesAliasesResponseTests extends AbstractStreamableXContentTestCase<IndicesAliasesResponse> {
2726

@@ -41,7 +40,7 @@ protected IndicesAliasesResponse createBlankInstance() {
4140
}
4241

4342
@Override
44-
protected EqualsHashCodeTestUtils.MutateFunction<IndicesAliasesResponse> getMutateFunction() {
45-
return response -> new IndicesAliasesResponse(response.isAcknowledged() == false);
43+
protected IndicesAliasesResponse mutateInstance(IndicesAliasesResponse response) {
44+
return new IndicesAliasesResponse(response.isAcknowledged() == false);
4645
}
4746
}

server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.common.xcontent.XContentParser;
2323
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
24-
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2524

2625
public class CloseIndexResponseTests extends AbstractStreamableXContentTestCase<CloseIndexResponse> {
2726

@@ -41,7 +40,7 @@ protected CloseIndexResponse createBlankInstance() {
4140
}
4241

4342
@Override
44-
protected EqualsHashCodeTestUtils.MutateFunction<CloseIndexResponse> getMutateFunction() {
45-
return response -> new CloseIndexResponse(response.isAcknowledged() == false);
43+
protected CloseIndexResponse mutateInstance(CloseIndexResponse response) {
44+
return new CloseIndexResponse(response.isAcknowledged() == false);
4645
}
4746
}

server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.common.xcontent.XContentParser;
2727
import org.elasticsearch.common.xcontent.json.JsonXContent;
2828
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
29-
import org.elasticsearch.test.EqualsHashCodeTestUtils;
3029

3130
import java.io.IOException;
3231

@@ -46,23 +45,21 @@ protected CreateIndexResponse createBlankInstance() {
4645
}
4746

4847
@Override
49-
protected EqualsHashCodeTestUtils.MutateFunction<CreateIndexResponse> getMutateFunction() {
50-
return response -> {
48+
protected CreateIndexResponse mutateInstance(CreateIndexResponse response) {
49+
if (randomBoolean()) {
5150
if (randomBoolean()) {
52-
if (randomBoolean()) {
53-
boolean acknowledged = response.isAcknowledged() == false;
54-
boolean shardsAcknowledged = acknowledged && response.isShardsAcknowledged();
55-
return new CreateIndexResponse(acknowledged, shardsAcknowledged, response.index());
56-
} else {
57-
boolean shardsAcknowledged = response.isShardsAcknowledged() == false;
58-
boolean acknowledged = shardsAcknowledged || response.isAcknowledged();
59-
return new CreateIndexResponse(acknowledged, shardsAcknowledged, response.index());
60-
}
51+
boolean acknowledged = response.isAcknowledged() == false;
52+
boolean shardsAcknowledged = acknowledged && response.isShardsAcknowledged();
53+
return new CreateIndexResponse(acknowledged, shardsAcknowledged, response.index());
6154
} else {
62-
return new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcknowledged(),
63-
response.index() + randomAlphaOfLengthBetween(2, 5));
55+
boolean shardsAcknowledged = response.isShardsAcknowledged() == false;
56+
boolean acknowledged = shardsAcknowledged || response.isAcknowledged();
57+
return new CreateIndexResponse(acknowledged, shardsAcknowledged, response.index());
6458
}
65-
};
59+
} else {
60+
return new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcknowledged(),
61+
response.index() + randomAlphaOfLengthBetween(2, 5));
62+
}
6663
}
6764

6865
@Override

server/src/test/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponseTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.common.Strings;
2323
import org.elasticsearch.common.xcontent.XContentParser;
2424
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
25-
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2625

2726
public class DeleteIndexResponseTests extends AbstractStreamableXContentTestCase<DeleteIndexResponse> {
2827

@@ -48,7 +47,7 @@ protected DeleteIndexResponse createBlankInstance() {
4847
}
4948

5049
@Override
51-
protected EqualsHashCodeTestUtils.MutateFunction<DeleteIndexResponse> getMutateFunction() {
52-
return response -> new DeleteIndexResponse(response.isAcknowledged() == false);
50+
protected DeleteIndexResponse mutateInstance(DeleteIndexResponse response) {
51+
return new DeleteIndexResponse(response.isAcknowledged() == false);
5352
}
5453
}

server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponseTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.common.Strings;
2323
import org.elasticsearch.common.xcontent.XContentParser;
2424
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
25-
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2625

2726
public class PutMappingResponseTests extends AbstractStreamableXContentTestCase<PutMappingResponse> {
2827

@@ -48,7 +47,7 @@ protected PutMappingResponse createBlankInstance() {
4847
}
4948

5049
@Override
51-
protected EqualsHashCodeTestUtils.MutateFunction<PutMappingResponse> getMutateFunction() {
52-
return response -> new PutMappingResponse(response.isAcknowledged() == false);
50+
protected PutMappingResponse mutateInstance(PutMappingResponse response) {
51+
return new PutMappingResponse(response.isAcknowledged() == false);
5352
}
5453
}

0 commit comments

Comments
 (0)