From 8b4a298874cb6ff5f19c2dba749d7447e44169ee Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Wed, 21 Feb 2018 20:04:12 +0100 Subject: [PATCH] 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. --- .../ClusterUpdateSettingsResponse.java | 18 ++- .../indices/alias/IndicesAliasesResponse.java | 2 +- .../indices/close/CloseIndexResponse.java | 2 +- .../indices/create/CreateIndexResponse.java | 15 +++ .../mapping/put/PutMappingResponse.java | 2 +- .../indices/rollover/RolloverResponse.java | 23 ++-- .../support/master/AcknowledgedResponse.java | 18 +++ .../master/ShardsAcknowledgedResponse.java | 16 +++ .../ClusterUpdateSettingsResponseTests.java | 85 +++++++++------ .../alias/IndicesAliasesResponseTests.java | 47 ++++++++ .../close/CloseIndexResponseTests.java | 53 ++++----- .../create/CreateIndexResponseTests.java | 103 ++++++------------ .../delete/DeleteIndexResponseTests.java | 61 +++-------- .../mapping/put/PutMappingResponseTests.java | 61 +++-------- .../indices/open/OpenIndexResponseTests.java | 62 +++++------ .../indices/shrink/ResizeResponseTests.java | 80 ++++++-------- .../action/main/MainResponseTests.java | 75 ++++++------- ...AbstractDiffableSerializationTestCase.java | 3 +- ...ractDiffableWireSerializationTestCase.java | 3 +- .../test/AbstractSerializingTestCase.java | 2 +- .../test/AbstractStreamableTestCase.java | 4 +- .../AbstractStreamableXContentTestCase.java | 2 +- .../test/AbstractWireSerializingTestCase.java | 4 +- 23 files changed, 366 insertions(+), 375 deletions(-) create mode 100644 server/src/test/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponseTests.java diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java index 7ef0e76330d..4fef5fa7a2d 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java @@ -30,6 +30,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; +import java.util.Objects; /** * A response for a cluster update settings action. @@ -103,7 +104,22 @@ public class ClusterUpdateSettingsResponse extends AcknowledgedResponse implemen return builder; } - public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) throws IOException { + public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } + + @Override + public boolean equals(Object o) { + if (super.equals(o)) { + ClusterUpdateSettingsResponse that = (ClusterUpdateSettingsResponse) o; + return Objects.equals(transientSettings, that.transientSettings) && + Objects.equals(persistentSettings, that.persistentSettings); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), transientSettings, persistentSettings); + } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java index c3cc0b5ebd4..cd0802619e4 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java @@ -67,7 +67,7 @@ public class IndicesAliasesResponse extends AcknowledgedResponse implements ToXC return builder; } - public static IndicesAliasesResponse fromXContent(XContentParser parser) throws IOException { + public static IndicesAliasesResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } } \ No newline at end of file diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java index 4607586d9fa..4f639400b01 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java @@ -67,7 +67,7 @@ public class CloseIndexResponse extends AcknowledgedResponse implements ToXConte return builder; } - public static CloseIndexResponse fromXContent(XContentParser parser) throws IOException { + public static CloseIndexResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java index e5b7d323b5a..38e7cf0c969 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; +import java.util.Objects; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; @@ -100,4 +101,18 @@ public class CreateIndexResponse extends ShardsAcknowledgedResponse implements T public static CreateIndexResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } + + @Override + public boolean equals(Object o) { + if (super.equals(o)) { + CreateIndexResponse that = (CreateIndexResponse) o; + return Objects.equals(index, that.index); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), index); + } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java index f427a316c2e..0cddba3b73a 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java @@ -69,7 +69,7 @@ public class PutMappingResponse extends AcknowledgedResponse implements ToXConte return builder; } - public static PutMappingResponse fromXContent(XContentParser parser) throws IOException { + public static PutMappingResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java index 58c84385f49..2aabc569730 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java @@ -168,24 +168,19 @@ public final class RolloverResponse extends ShardsAcknowledgedResponse implement @Override public boolean equals(Object o) { - if (this == o) { - return true; + if (super.equals(o)) { + RolloverResponse that = (RolloverResponse) o; + return dryRun == that.dryRun && + rolledOver == that.rolledOver && + Objects.equals(oldIndex, that.oldIndex) && + Objects.equals(newIndex, that.newIndex) && + Objects.equals(conditionStatus, that.conditionStatus); } - if (o == null || getClass() != o.getClass()) { - return false; - } - RolloverResponse that = (RolloverResponse) o; - return isAcknowledged() == that.isAcknowledged() && - isShardsAcknowledged() == that.isShardsAcknowledged() && - dryRun == that.dryRun && - rolledOver == that.rolledOver && - Objects.equals(oldIndex, that.oldIndex) && - Objects.equals(newIndex, that.newIndex) && - Objects.equals(conditionStatus, that.conditionStatus); + return false; } @Override public int hashCode() { - return Objects.hash(isAcknowledged(), isShardsAcknowledged(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver); + return Objects.hash(super.hashCode(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver); } } diff --git a/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java b/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java index 343a76c8157..0d2b26aecca 100755 --- a/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java +++ b/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; +import java.util.Objects; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; @@ -78,4 +79,21 @@ public abstract class AcknowledgedResponse extends ActionResponse { protected void addAcknowledgedField(XContentBuilder builder) throws IOException { builder.field(ACKNOWLEDGED.getPreferredName(), isAcknowledged()); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AcknowledgedResponse that = (AcknowledgedResponse) o; + return isAcknowledged() == that.isAcknowledged(); + } + + @Override + public int hashCode() { + return Objects.hash(isAcknowledged()); + } } diff --git a/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java b/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java index c8672456acb..36a492445fd 100644 --- a/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java +++ b/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; +import java.util.Objects; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; @@ -73,4 +74,19 @@ public abstract class ShardsAcknowledgedResponse extends AcknowledgedResponse { protected void addShardsAcknowledgedField(XContentBuilder builder) throws IOException { builder.field(SHARDS_ACKNOWLEDGED.getPreferredName(), isShardsAcknowledged()); } + + @Override + public boolean equals(Object o) { + if (super.equals(o)) { + ShardsAcknowledgedResponse that = (ShardsAcknowledgedResponse) o; + return shardsAcknowledged == that.shardsAcknowledged; + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), shardsAcknowledged); + } + } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponseTests.java index 77ccae5a96d..efbf33c9683 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponseTests.java @@ -19,52 +19,65 @@ package org.elasticsearch.action.admin.cluster.settings; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings.Builder; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; -import java.io.IOException; +import java.util.List; +import java.util.Set; +import java.util.function.Predicate; -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; -import static org.hamcrest.CoreMatchers.equalTo; +public class ClusterUpdateSettingsResponseTests extends AbstractStreamableXContentTestCase { -public class ClusterUpdateSettingsResponseTests extends ESTestCase { - - public void testFromXContent() throws IOException { - doFromXContentTestWithRandomFields(false); + @Override + protected ClusterUpdateSettingsResponse doParseInstance(XContentParser parser) { + return ClusterUpdateSettingsResponse.fromXContent(parser); } - public void testFromXContentWithRandomFields() throws IOException { - doFromXContentTestWithRandomFields(true); + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> { + int i = randomIntBetween(0, 2); + switch(i) { + case 0: + return new ClusterUpdateSettingsResponse(response.isAcknowledged() == false, + response.transientSettings, response.persistentSettings); + case 1: + return new ClusterUpdateSettingsResponse(response.isAcknowledged(), mutateSettings(response.transientSettings), + response.persistentSettings); + case 2: + return new ClusterUpdateSettingsResponse(response.isAcknowledged(), response.transientSettings, + mutateSettings(response.persistentSettings)); + default: + throw new UnsupportedOperationException(); + } + }; } - private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { - final ClusterUpdateSettingsResponse response = createTestItem(); - boolean humanReadable = randomBoolean(); - final XContentType xContentType = XContentType.JSON; - - BytesReference originalBytes = toShuffledXContent(response, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - BytesReference mutated; - if (addRandomFields) { - mutated = insertRandomFields(xContentType, originalBytes, p -> p.startsWith("transient") || p.startsWith("persistent"), - random()); - } else { - mutated = originalBytes; + private static Settings mutateSettings(Settings settings) { + if (settings.isEmpty()) { + return randomClusterSettings(1, 3); } + Set allKeys = settings.keySet(); + List keysToBeModified = randomSubsetOf(randomIntBetween(1, allKeys.size()), allKeys); + Builder builder = Settings.builder(); + for (String key : allKeys) { + String value = settings.get(key); + if (keysToBeModified.contains(key)) { + value += randomAlphaOfLengthBetween(2, 5); + } + builder.put(key, value); + } + return builder.build(); + } - XContentParser parser = createParser(xContentType.xContent(), mutated); - ClusterUpdateSettingsResponse parsedResponse = ClusterUpdateSettingsResponse.fromXContent(parser); - - assertNull(parser.nextToken()); - assertThat(parsedResponse.isAcknowledged(), equalTo(response.isAcknowledged())); - assertThat(response.transientSettings, equalTo(response.transientSettings)); - assertThat(response.persistentSettings, equalTo(response.persistentSettings)); + @Override + protected Predicate getRandomFieldsExcludeFilter() { + return p -> p.startsWith("transient") || p.startsWith("persistent"); } public static Settings randomClusterSettings(int min, int max) { @@ -77,7 +90,13 @@ public class ClusterUpdateSettingsResponseTests extends ESTestCase { return builder.build(); } - private static ClusterUpdateSettingsResponse createTestItem() { + @Override + protected ClusterUpdateSettingsResponse createTestInstance() { return new ClusterUpdateSettingsResponse(randomBoolean(), randomClusterSettings(0, 2), randomClusterSettings(0, 2)); } + + @Override + protected ClusterUpdateSettingsResponse createBlankInstance() { + return new ClusterUpdateSettingsResponse(); + } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponseTests.java new file mode 100644 index 00000000000..b4f48805154 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponseTests.java @@ -0,0 +1,47 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.admin.indices.alias; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; + +public class IndicesAliasesResponseTests extends AbstractStreamableXContentTestCase { + + @Override + protected IndicesAliasesResponse doParseInstance(XContentParser parser) { + return IndicesAliasesResponse.fromXContent(parser); + } + + @Override + protected IndicesAliasesResponse createTestInstance() { + return new IndicesAliasesResponse(randomBoolean()); + } + + @Override + protected IndicesAliasesResponse createBlankInstance() { + return new IndicesAliasesResponse(); + } + + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> new IndicesAliasesResponse(response.isAcknowledged() == false); + } +} diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java index e616e038311..6701270d11a 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java @@ -19,42 +19,29 @@ package org.elasticsearch.action.admin.indices.close; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; -import java.io.IOException; +public class CloseIndexResponseTests extends AbstractStreamableXContentTestCase { -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; -import static org.hamcrest.CoreMatchers.equalTo; - -public class CloseIndexResponseTests extends ESTestCase { - - public void testFromToXContent() throws IOException { - final CloseIndexResponse closeIndexResponse = createTestItem(); - - boolean humanReadable = randomBoolean(); - final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(closeIndexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - BytesReference mutated; - if (randomBoolean()) { - mutated = insertRandomFields(xContentType, originalBytes, null, random()); - } else { - mutated = originalBytes; - } - - CloseIndexResponse parsedCloseIndexResponse; - try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { - parsedCloseIndexResponse = CloseIndexResponse.fromXContent(parser); - assertNull(parser.nextToken()); - } - assertThat(parsedCloseIndexResponse.isAcknowledged(), equalTo(closeIndexResponse.isAcknowledged())); + @Override + protected CloseIndexResponse doParseInstance(XContentParser parser) { + return CloseIndexResponse.fromXContent(parser); } - - private static CloseIndexResponse createTestItem() { - boolean acknowledged = randomBoolean(); - return new CloseIndexResponse(acknowledged); + + @Override + protected CloseIndexResponse createTestInstance() { + return new CloseIndexResponse(randomBoolean()); + } + + @Override + protected CloseIndexResponse createBlankInstance() { + return new CloseIndexResponse(); + } + + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> new CloseIndexResponse(response.isAcknowledged() == false); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java index 358073db751..21bf3a13a96 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java @@ -21,35 +21,53 @@ package org.elasticsearch.action.admin.indices.create; import org.elasticsearch.Version; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; +public class CreateIndexResponseTests extends AbstractStreamableXContentTestCase { -public class CreateIndexResponseTests extends ESTestCase { + @Override + protected CreateIndexResponse createTestInstance() { + boolean acknowledged = randomBoolean(); + boolean shardsAcknowledged = acknowledged && randomBoolean(); + String index = randomAlphaOfLength(5); + return new CreateIndexResponse(acknowledged, shardsAcknowledged, index); + } - public void testSerialization() throws IOException { - CreateIndexResponse response = new CreateIndexResponse(true, true, "foo"); + @Override + protected CreateIndexResponse createBlankInstance() { + return new CreateIndexResponse(); + } - try (BytesStreamOutput output = new BytesStreamOutput()) { - response.writeTo(output); - - try (StreamInput in = output.bytes().streamInput()) { - CreateIndexResponse serialized = new CreateIndexResponse(); - serialized.readFrom(in); - assertEquals(response.isShardsAcknowledged(), serialized.isShardsAcknowledged()); - assertEquals(response.isAcknowledged(), serialized.isAcknowledged()); - assertEquals(response.index(), serialized.index()); + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> { + if (randomBoolean()) { + if (randomBoolean()) { + boolean acknowledged = response.isAcknowledged() == false; + boolean shardsAcknowledged = acknowledged && response.isShardsAcknowledged(); + return new CreateIndexResponse(acknowledged, shardsAcknowledged, response.index()); + } else { + boolean shardsAcknowledged = response.isShardsAcknowledged() == false; + boolean acknowledged = shardsAcknowledged || response.isAcknowledged(); + return new CreateIndexResponse(acknowledged, shardsAcknowledged, response.index()); + } + } else { + return new CreateIndexResponse(response.isAcknowledged(), response.isShardsAcknowledged(), + response.index() + randomAlphaOfLengthBetween(2, 5)); } - } + }; + } + + @Override + protected CreateIndexResponse doParseInstance(XContentParser parser) { + return CreateIndexResponse.fromXContent(parser); } public void testSerializationWithOldVersion() throws IOException { @@ -88,53 +106,4 @@ public class CreateIndexResponseTests extends ESTestCase { assertFalse(parsedResponse.isShardsAcknowledged()); } } - - public void testToAndFromXContent() throws IOException { - doFromXContentTestWithRandomFields(false); - } - - /** - * This test adds random fields and objects to the xContent rendered out to - * ensure we can parse it back to be forward compatible with additions to - * the xContent - */ - public void testFromXContentWithRandomFields() throws IOException { - doFromXContentTestWithRandomFields(true); - } - - private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { - - final CreateIndexResponse createIndexResponse = createTestItem(); - - boolean humanReadable = randomBoolean(); - final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(createIndexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - - BytesReference mutated; - if (addRandomFields) { - mutated = insertRandomFields(xContentType, originalBytes, null, random()); - } else { - mutated = originalBytes; - } - CreateIndexResponse parsedCreateIndexResponse; - try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { - parsedCreateIndexResponse = CreateIndexResponse.fromXContent(parser); - assertNull(parser.nextToken()); - } - - assertEquals(createIndexResponse.index(), parsedCreateIndexResponse.index()); - assertEquals(createIndexResponse.isShardsAcknowledged(), parsedCreateIndexResponse.isShardsAcknowledged()); - assertEquals(createIndexResponse.isAcknowledged(), parsedCreateIndexResponse.isAcknowledged()); - } - - /** - * Returns a random {@link CreateIndexResponse}. - */ - private static CreateIndexResponse createTestItem() { - boolean acknowledged = randomBoolean(); - boolean shardsAcknowledged = acknowledged && randomBoolean(); - String index = randomAlphaOfLength(5); - - return new CreateIndexResponse(acknowledged, shardsAcknowledged, index); - } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponseTests.java index 4e036319ad9..9325dbdfa2b 100755 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponseTests.java @@ -20,17 +20,11 @@ package org.elasticsearch.action.admin.indices.delete; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; -import java.io.IOException; - -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; - -public class DeleteIndexResponseTests extends ESTestCase { +public class DeleteIndexResponseTests extends AbstractStreamableXContentTestCase { public void testToXContent() { DeleteIndexResponse response = new DeleteIndexResponse(true); @@ -38,48 +32,23 @@ public class DeleteIndexResponseTests extends ESTestCase { assertEquals("{\"acknowledged\":true}", output); } - public void testToAndFromXContent() throws IOException { - doFromXContentTestWithRandomFields(false); + @Override + protected DeleteIndexResponse doParseInstance(XContentParser parser) { + return DeleteIndexResponse.fromXContent(parser); } - /** - * This test adds random fields and objects to the xContent rendered out to - * ensure we can parse it back to be forward compatible with additions to - * the xContent - */ - public void testFromXContentWithRandomFields() throws IOException { - doFromXContentTestWithRandomFields(true); + @Override + protected DeleteIndexResponse createTestInstance() { + return new DeleteIndexResponse(randomBoolean()); } - private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { - - final DeleteIndexResponse deleteIndexResponse = createTestItem(); - - boolean humanReadable = randomBoolean(); - final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(deleteIndexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - - BytesReference mutated; - if (addRandomFields) { - mutated = insertRandomFields(xContentType, originalBytes, null, random()); - } else { - mutated = originalBytes; - } - DeleteIndexResponse parsedDeleteIndexResponse; - try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { - parsedDeleteIndexResponse = DeleteIndexResponse.fromXContent(parser); - assertNull(parser.nextToken()); - } - - assertEquals(deleteIndexResponse.isAcknowledged(), parsedDeleteIndexResponse.isAcknowledged()); + @Override + protected DeleteIndexResponse createBlankInstance() { + return new DeleteIndexResponse(); } - /** - * Returns a random {@link DeleteIndexResponse}. - */ - private static DeleteIndexResponse createTestItem() throws IOException { - boolean acknowledged = randomBoolean(); - - return new DeleteIndexResponse(acknowledged); + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> new DeleteIndexResponse(response.isAcknowledged() == false); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponseTests.java index a52969c6281..7d42d707605 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponseTests.java @@ -20,17 +20,11 @@ package org.elasticsearch.action.admin.indices.mapping.put; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; -import java.io.IOException; - -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; - -public class PutMappingResponseTests extends ESTestCase { +public class PutMappingResponseTests extends AbstractStreamableXContentTestCase { public void testToXContent() { PutMappingResponse response = new PutMappingResponse(true); @@ -38,48 +32,23 @@ public class PutMappingResponseTests extends ESTestCase { assertEquals("{\"acknowledged\":true}", output); } - public void testToAndFromXContent() throws IOException { - doFromXContentTestWithRandomFields(false); + @Override + protected PutMappingResponse doParseInstance(XContentParser parser) { + return PutMappingResponse.fromXContent(parser); } - /** - * This test adds random fields and objects to the xContent rendered out to - * ensure we can parse it back to be forward compatible with additions to - * the xContent - */ - public void testFromXContentWithRandomFields() throws IOException { - doFromXContentTestWithRandomFields(true); + @Override + protected PutMappingResponse createTestInstance() { + return new PutMappingResponse(randomBoolean()); } - private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { - - final PutMappingResponse putMappingResponse = createTestItem(); - - boolean humanReadable = randomBoolean(); - final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(putMappingResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - - BytesReference mutated; - if (addRandomFields) { - mutated = insertRandomFields(xContentType, originalBytes, null, random()); - } else { - mutated = originalBytes; - } - PutMappingResponse parsedPutMappingResponse; - try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { - parsedPutMappingResponse = PutMappingResponse.fromXContent(parser); - assertNull(parser.nextToken()); - } - - assertEquals(putMappingResponse.isAcknowledged(), parsedPutMappingResponse.isAcknowledged()); + @Override + protected PutMappingResponse createBlankInstance() { + return new PutMappingResponse(); } - /** - * Returns a random {@link PutMappingResponse}. - */ - private static PutMappingResponse createTestItem() throws IOException { - boolean acknowledged = randomBoolean(); - - return new PutMappingResponse(acknowledged); + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> new PutMappingResponse(response.isAcknowledged() == false); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java index df49de0c1ee..5eca6254edf 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponseTests.java @@ -19,45 +19,41 @@ package org.elasticsearch.action.admin.indices.open; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; -import java.io.IOException; +public class OpenIndexResponseTests extends AbstractStreamableXContentTestCase { -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; -import static org.hamcrest.CoreMatchers.equalTo; - -public class OpenIndexResponseTests extends ESTestCase { - - public void testFromToXContent() throws IOException { - final OpenIndexResponse openIndexResponse = createTestItem(); - - boolean humanReadable = randomBoolean(); - final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(openIndexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - BytesReference mutated; - if (randomBoolean()) { - mutated = insertRandomFields(xContentType, originalBytes, null, random()); - } else { - mutated = originalBytes; - } - - OpenIndexResponse parsedOpenIndexResponse; - try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { - parsedOpenIndexResponse = OpenIndexResponse.fromXContent(parser); - assertNull(parser.nextToken()); - } - - assertThat(parsedOpenIndexResponse.isShardsAcknowledged(), equalTo(openIndexResponse.isShardsAcknowledged())); - assertThat(parsedOpenIndexResponse.isAcknowledged(), equalTo(openIndexResponse.isAcknowledged())); + @Override + protected OpenIndexResponse doParseInstance(XContentParser parser){ + return OpenIndexResponse.fromXContent(parser); } - - private static OpenIndexResponse createTestItem() { + + @Override + protected OpenIndexResponse createTestInstance() { boolean acknowledged = randomBoolean(); boolean shardsAcknowledged = acknowledged && randomBoolean(); return new OpenIndexResponse(acknowledged, shardsAcknowledged); } + + @Override + protected OpenIndexResponse createBlankInstance() { + return new OpenIndexResponse(); + } + + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> { + if (randomBoolean()) { + boolean acknowledged = response.isAcknowledged() == false; + boolean shardsAcknowledged = acknowledged && response.isShardsAcknowledged(); + return new OpenIndexResponse(acknowledged, shardsAcknowledged); + } else { + boolean shardsAcknowledged = response.isShardsAcknowledged() == false; + boolean acknowledged = shardsAcknowledged || response.isAcknowledged(); + return new OpenIndexResponse(acknowledged, shardsAcknowledged); + } + }; + } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java index 6ed3d01e1b7..7e03e980304 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java @@ -20,17 +20,11 @@ package org.elasticsearch.action.admin.indices.shrink; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; -import java.io.IOException; - -import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; - -public class ResizeResponseTests extends ESTestCase { +public class ResizeResponseTests extends AbstractStreamableXContentTestCase { public void testToXContent() { ResizeResponse response = new ResizeResponse(true, false, "index_name"); @@ -38,49 +32,41 @@ public class ResizeResponseTests extends ESTestCase { assertEquals("{\"acknowledged\":true,\"shards_acknowledged\":false,\"index\":\"index_name\"}", output); } - public void testToAndFromXContent() throws IOException { - doFromXContentTestWithRandomFields(false); + @Override + protected ResizeResponse doParseInstance(XContentParser parser) { + return ResizeResponse.fromXContent(parser); } - /** - * This test adds random fields and objects to the xContent rendered out to - * ensure we can parse it back to be forward compatible with additions to - * the xContent - */ - public void testFromXContentWithRandomFields() throws IOException { - doFromXContentTestWithRandomFields(true); - } - - private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { - - final ResizeResponse resizeResponse = createTestItem(); - - boolean humanReadable = randomBoolean(); - final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(resizeResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); - - BytesReference mutated; - if (addRandomFields) { - mutated = insertRandomFields(xContentType, originalBytes, null, random()); - } else { - mutated = originalBytes; - } - ResizeResponse parsedResizeResponse; - try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { - parsedResizeResponse = ResizeResponse.fromXContent(parser); - assertNull(parser.nextToken()); - } - - assertEquals(resizeResponse.index(), parsedResizeResponse.index()); - assertEquals(resizeResponse.isShardsAcknowledged(), parsedResizeResponse.isShardsAcknowledged()); - assertEquals(resizeResponse.isAcknowledged(), parsedResizeResponse.isAcknowledged()); - } - - private static ResizeResponse createTestItem() { + @Override + protected ResizeResponse createTestInstance() { boolean acknowledged = randomBoolean(); boolean shardsAcknowledged = acknowledged && randomBoolean(); String index = randomAlphaOfLength(5); - return new ResizeResponse(acknowledged, shardsAcknowledged, index); } + + @Override + protected ResizeResponse createBlankInstance() { + return new ResizeResponse(); + } + + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return response -> { + if (randomBoolean()) { + if (randomBoolean()) { + boolean acknowledged = response.isAcknowledged() == false; + boolean shardsAcknowledged = acknowledged && response.isShardsAcknowledged(); + return new ResizeResponse(acknowledged, shardsAcknowledged, response.index()); + } else { + boolean shardsAcknowledged = response.isShardsAcknowledged() == false; + boolean acknowledged = shardsAcknowledged || response.isAcknowledged(); + return new ResizeResponse(acknowledged, shardsAcknowledged, response.index()); + } + } else { + return new ResizeResponse(response.isAcknowledged(), response.isShardsAcknowledged(), + response.index() + randomAlphaOfLengthBetween(2, 5)); + } + }; + } } diff --git a/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java b/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java index 2eac6d9369a..a7e18c29ce7 100644 --- a/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java @@ -27,13 +27,12 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractStreamableXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.test.VersionUtils; import java.io.IOException; import java.util.Date; -import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode; - public class MainResponseTests extends AbstractStreamableXContentTestCase { @Override @@ -87,45 +86,37 @@ public class MainResponseTests extends AbstractStreamableXContentTestCase VersionUtils.randomVersion(random())); - break; - case 5: - clusterName = new ClusterName(clusterName + randomAlphaOfLength(5)); - break; - } - return new MainResponse(nodeName, version, clusterName, clusterUuid, build, available); + @Override + protected EqualsHashCodeTestUtils.MutateFunction getMutateFunction() { + return o -> { + String clusterUuid = o.getClusterUuid(); + boolean available = o.isAvailable(); + Build build = o.getBuild(); + Version version = o.getVersion(); + String nodeName = o.getNodeName(); + ClusterName clusterName = o.getClusterName(); + switch (randomIntBetween(0, 5)) { + case 0: + clusterUuid = clusterUuid + randomAlphaOfLength(5); + break; + case 1: + nodeName = nodeName + randomAlphaOfLength(5); + break; + case 2: + available = !available; + break; + case 3: + // toggle the snapshot flag of the original Build parameter + build = new Build(build.shortHash(), build.date(), !build.isSnapshot()); + break; + case 4: + version = randomValueOtherThan(version, () -> VersionUtils.randomVersion(random())); + break; + case 5: + clusterName = new ClusterName(clusterName + randomAlphaOfLength(5)); + break; + } + return new MainResponse(nodeName, version, clusterName, clusterUuid, build, available); + }; } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableSerializationTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableSerializationTestCase.java index feabca586b8..13fbbf9517a 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableSerializationTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableSerializationTestCase.java @@ -41,9 +41,8 @@ public abstract class AbstractDiffableSerializationTestCase> diffReader(); - public void testDiffableSerialization() throws IOException { + public final void testDiffableSerialization() throws IOException { DiffableTestUtils.testDiffableSerialization(this::createTestInstance, this::makeTestChanges, getNamedWriteableRegistry(), instanceReader(), diffReader()); } - } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableWireSerializationTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableWireSerializationTestCase.java index aa801308825..06f8919da58 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableWireSerializationTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractDiffableWireSerializationTestCase.java @@ -39,9 +39,8 @@ public abstract class AbstractDiffableWireSerializationTestCase> diffReader(); - public void testDiffableSerialization() throws IOException { + public final void testDiffableSerialization() throws IOException { DiffableTestUtils.testDiffableSerialization(this::createTestInstance, this::makeTestChanges, getNamedWriteableRegistry(), instanceReader(), diffReader()); } - } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractSerializingTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractSerializingTestCase.java index 9eea29b4f46..cca2f5c6702 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractSerializingTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractSerializingTestCase.java @@ -34,7 +34,7 @@ public abstract class AbstractSerializingTestCase extends E * Tests that the equals and hashcode methods are consistent and copied * versions of the instance have are equal. */ - public void testEqualsAndHashcode() { + public final void testEqualsAndHashcode() { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), getCopyFunction(), getMutateFunction()); } @@ -80,7 +80,7 @@ public abstract class AbstractStreamableTestCase extends E /** * Test serialization and deserialization of the test instance. */ - public void testSerialization() throws IOException { + public final void testSerialization() throws IOException { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { T testInstance = createTestInstance(); assertSerialization(testInstance); diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractStreamableXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractStreamableXContentTestCase.java index d36c58aa6f6..657bc6d0e25 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractStreamableXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractStreamableXContentTestCase.java @@ -37,7 +37,7 @@ public abstract class AbstractStreamableXContentTestCase exten * Tests that the equals and hashcode methods are consistent and copied * versions of the instance have are equal. */ - public void testEqualsAndHashcode() throws IOException { + public final void testEqualsAndHashcode() { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), this::copyInstance, this::mutateInstance); } @@ -67,7 +67,7 @@ public abstract class AbstractWireSerializingTestCase exten /** * Test serialization and deserialization of the test instance. */ - public void testSerialization() throws IOException { + public final void testSerialization() throws IOException { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { T testInstance = createTestInstance(); assertSerialization(testInstance);