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.
This commit is contained in:
Luca Cavanna 2018-02-21 20:04:12 +01:00 committed by GitHub
parent 45470945d2
commit 8b4a298874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 366 additions and 375 deletions

View File

@ -30,6 +30,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
/** /**
* A response for a cluster update settings action. * A response for a cluster update settings action.
@ -103,7 +104,22 @@ public class ClusterUpdateSettingsResponse extends AcknowledgedResponse implemen
return builder; return builder;
} }
public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) throws IOException { public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); 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);
}
} }

View File

@ -67,7 +67,7 @@ public class IndicesAliasesResponse extends AcknowledgedResponse implements ToXC
return builder; return builder;
} }
public static IndicesAliasesResponse fromXContent(XContentParser parser) throws IOException { public static IndicesAliasesResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return PARSER.apply(parser, null);
} }
} }

View File

@ -67,7 +67,7 @@ public class CloseIndexResponse extends AcknowledgedResponse implements ToXConte
return builder; return builder;
} }
public static CloseIndexResponse fromXContent(XContentParser parser) throws IOException { public static CloseIndexResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return PARSER.apply(parser, null);
} }
} }

View File

@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; 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) { public static CreateIndexResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); 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);
}
} }

View File

@ -69,7 +69,7 @@ public class PutMappingResponse extends AcknowledgedResponse implements ToXConte
return builder; return builder;
} }
public static PutMappingResponse fromXContent(XContentParser parser) throws IOException { public static PutMappingResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return PARSER.apply(parser, null);
} }
} }

View File

@ -168,24 +168,19 @@ public final class RolloverResponse extends ShardsAcknowledgedResponse implement
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (super.equals(o)) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RolloverResponse that = (RolloverResponse) o; RolloverResponse that = (RolloverResponse) o;
return isAcknowledged() == that.isAcknowledged() && return dryRun == that.dryRun &&
isShardsAcknowledged() == that.isShardsAcknowledged() &&
dryRun == that.dryRun &&
rolledOver == that.rolledOver && rolledOver == that.rolledOver &&
Objects.equals(oldIndex, that.oldIndex) && Objects.equals(oldIndex, that.oldIndex) &&
Objects.equals(newIndex, that.newIndex) && Objects.equals(newIndex, that.newIndex) &&
Objects.equals(conditionStatus, that.conditionStatus); Objects.equals(conditionStatus, that.conditionStatus);
} }
return false;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(isAcknowledged(), isShardsAcknowledged(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver); return Objects.hash(super.hashCode(), oldIndex, newIndex, conditionStatus, dryRun, rolledOver);
} }
} }

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; 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 { protected void addAcknowledgedField(XContentBuilder builder) throws IOException {
builder.field(ACKNOWLEDGED.getPreferredName(), isAcknowledged()); 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());
}
} }

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; 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 { protected void addShardsAcknowledgedField(XContentBuilder builder) throws IOException {
builder.field(SHARDS_ACKNOWLEDGED.getPreferredName(), isShardsAcknowledged()); 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);
}
} }

View File

@ -19,52 +19,65 @@
package org.elasticsearch.action.admin.cluster.settings; package org.elasticsearch.action.admin.cluster.settings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder; import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.ESTestCase; 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; public class ClusterUpdateSettingsResponseTests extends AbstractStreamableXContentTestCase<ClusterUpdateSettingsResponse> {
import static org.hamcrest.CoreMatchers.equalTo;
public class ClusterUpdateSettingsResponseTests extends ESTestCase { @Override
protected ClusterUpdateSettingsResponse doParseInstance(XContentParser parser) {
public void testFromXContent() throws IOException { return ClusterUpdateSettingsResponse.fromXContent(parser);
doFromXContentTestWithRandomFields(false);
} }
public void testFromXContentWithRandomFields() throws IOException { @Override
doFromXContentTestWithRandomFields(true); protected EqualsHashCodeTestUtils.MutateFunction<ClusterUpdateSettingsResponse> 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 { private static Settings mutateSettings(Settings settings) {
final ClusterUpdateSettingsResponse response = createTestItem(); if (settings.isEmpty()) {
boolean humanReadable = randomBoolean(); return randomClusterSettings(1, 3);
final XContentType xContentType = XContentType.JSON; }
Set<String> allKeys = settings.keySet();
BytesReference originalBytes = toShuffledXContent(response, xContentType, ToXContent.EMPTY_PARAMS, humanReadable); List<String> keysToBeModified = randomSubsetOf(randomIntBetween(1, allKeys.size()), allKeys);
BytesReference mutated; Builder builder = Settings.builder();
if (addRandomFields) { for (String key : allKeys) {
mutated = insertRandomFields(xContentType, originalBytes, p -> p.startsWith("transient") || p.startsWith("persistent"), String value = settings.get(key);
random()); if (keysToBeModified.contains(key)) {
} else { value += randomAlphaOfLengthBetween(2, 5);
mutated = originalBytes; }
builder.put(key, value);
}
return builder.build();
} }
XContentParser parser = createParser(xContentType.xContent(), mutated); @Override
ClusterUpdateSettingsResponse parsedResponse = ClusterUpdateSettingsResponse.fromXContent(parser); protected Predicate<String> getRandomFieldsExcludeFilter() {
return p -> p.startsWith("transient") || p.startsWith("persistent");
assertNull(parser.nextToken());
assertThat(parsedResponse.isAcknowledged(), equalTo(response.isAcknowledged()));
assertThat(response.transientSettings, equalTo(response.transientSettings));
assertThat(response.persistentSettings, equalTo(response.persistentSettings));
} }
public static Settings randomClusterSettings(int min, int max) { public static Settings randomClusterSettings(int min, int max) {
@ -77,7 +90,13 @@ public class ClusterUpdateSettingsResponseTests extends ESTestCase {
return builder.build(); return builder.build();
} }
private static ClusterUpdateSettingsResponse createTestItem() { @Override
protected ClusterUpdateSettingsResponse createTestInstance() {
return new ClusterUpdateSettingsResponse(randomBoolean(), randomClusterSettings(0, 2), randomClusterSettings(0, 2)); return new ClusterUpdateSettingsResponse(randomBoolean(), randomClusterSettings(0, 2), randomClusterSettings(0, 2));
} }
@Override
protected ClusterUpdateSettingsResponse createBlankInstance() {
return new ClusterUpdateSettingsResponse();
}
} }

View File

@ -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<IndicesAliasesResponse> {
@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<IndicesAliasesResponse> getMutateFunction() {
return response -> new IndicesAliasesResponse(response.isAcknowledged() == false);
}
}

View File

@ -19,42 +19,29 @@
package org.elasticsearch.action.admin.indices.close; 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.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.io.IOException; public class CloseIndexResponseTests extends AbstractStreamableXContentTestCase<CloseIndexResponse> {
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; @Override
import static org.hamcrest.CoreMatchers.equalTo; protected CloseIndexResponse doParseInstance(XContentParser parser) {
return CloseIndexResponse.fromXContent(parser);
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; @Override
try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { protected CloseIndexResponse createTestInstance() {
parsedCloseIndexResponse = CloseIndexResponse.fromXContent(parser); return new CloseIndexResponse(randomBoolean());
assertNull(parser.nextToken());
}
assertThat(parsedCloseIndexResponse.isAcknowledged(), equalTo(closeIndexResponse.isAcknowledged()));
} }
private static CloseIndexResponse createTestItem() { @Override
boolean acknowledged = randomBoolean(); protected CloseIndexResponse createBlankInstance() {
return new CloseIndexResponse(acknowledged); return new CloseIndexResponse();
}
@Override
protected EqualsHashCodeTestUtils.MutateFunction<CloseIndexResponse> getMutateFunction() {
return response -> new CloseIndexResponse(response.isAcknowledged() == false);
} }
} }

View File

@ -21,35 +21,53 @@ package org.elasticsearch.action.admin.indices.create;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent; 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 java.io.IOException;
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; public class CreateIndexResponseTests extends AbstractStreamableXContentTestCase<CreateIndexResponse> {
public class CreateIndexResponseTests extends ESTestCase { @Override
protected CreateIndexResponse createTestInstance() {
public void testSerialization() throws IOException { boolean acknowledged = randomBoolean();
CreateIndexResponse response = new CreateIndexResponse(true, true, "foo"); boolean shardsAcknowledged = acknowledged && randomBoolean();
String index = randomAlphaOfLength(5);
try (BytesStreamOutput output = new BytesStreamOutput()) { return new CreateIndexResponse(acknowledged, shardsAcknowledged, index);
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 CreateIndexResponse createBlankInstance() {
return new CreateIndexResponse();
} }
@Override
protected EqualsHashCodeTestUtils.MutateFunction<CreateIndexResponse> 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 { public void testSerializationWithOldVersion() throws IOException {
@ -88,53 +106,4 @@ public class CreateIndexResponseTests extends ESTestCase {
assertFalse(parsedResponse.isShardsAcknowledged()); 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);
}
} }

View File

@ -20,17 +20,11 @@
package org.elasticsearch.action.admin.indices.delete; package org.elasticsearch.action.admin.indices.delete;
import org.elasticsearch.common.Strings; 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.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.io.IOException; public class DeleteIndexResponseTests extends AbstractStreamableXContentTestCase<DeleteIndexResponse> {
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
public class DeleteIndexResponseTests extends ESTestCase {
public void testToXContent() { public void testToXContent() {
DeleteIndexResponse response = new DeleteIndexResponse(true); DeleteIndexResponse response = new DeleteIndexResponse(true);
@ -38,48 +32,23 @@ public class DeleteIndexResponseTests extends ESTestCase {
assertEquals("{\"acknowledged\":true}", output); assertEquals("{\"acknowledged\":true}", output);
} }
public void testToAndFromXContent() throws IOException { @Override
doFromXContentTestWithRandomFields(false); protected DeleteIndexResponse doParseInstance(XContentParser parser) {
return DeleteIndexResponse.fromXContent(parser);
} }
/** @Override
* This test adds random fields and objects to the xContent rendered out to protected DeleteIndexResponse createTestInstance() {
* ensure we can parse it back to be forward compatible with additions to return new DeleteIndexResponse(randomBoolean());
* the xContent
*/
public void testFromXContentWithRandomFields() throws IOException {
doFromXContentTestWithRandomFields(true);
} }
private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { @Override
protected DeleteIndexResponse createBlankInstance() {
final DeleteIndexResponse deleteIndexResponse = createTestItem(); return new DeleteIndexResponse();
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 EqualsHashCodeTestUtils.MutateFunction<DeleteIndexResponse> getMutateFunction() {
return response -> new DeleteIndexResponse(response.isAcknowledged() == false);
/**
* Returns a random {@link DeleteIndexResponse}.
*/
private static DeleteIndexResponse createTestItem() throws IOException {
boolean acknowledged = randomBoolean();
return new DeleteIndexResponse(acknowledged);
} }
} }

View File

@ -20,17 +20,11 @@
package org.elasticsearch.action.admin.indices.mapping.put; package org.elasticsearch.action.admin.indices.mapping.put;
import org.elasticsearch.common.Strings; 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.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.io.IOException; public class PutMappingResponseTests extends AbstractStreamableXContentTestCase<PutMappingResponse> {
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
public class PutMappingResponseTests extends ESTestCase {
public void testToXContent() { public void testToXContent() {
PutMappingResponse response = new PutMappingResponse(true); PutMappingResponse response = new PutMappingResponse(true);
@ -38,48 +32,23 @@ public class PutMappingResponseTests extends ESTestCase {
assertEquals("{\"acknowledged\":true}", output); assertEquals("{\"acknowledged\":true}", output);
} }
public void testToAndFromXContent() throws IOException { @Override
doFromXContentTestWithRandomFields(false); protected PutMappingResponse doParseInstance(XContentParser parser) {
return PutMappingResponse.fromXContent(parser);
} }
/** @Override
* This test adds random fields and objects to the xContent rendered out to protected PutMappingResponse createTestInstance() {
* ensure we can parse it back to be forward compatible with additions to return new PutMappingResponse(randomBoolean());
* the xContent
*/
public void testFromXContentWithRandomFields() throws IOException {
doFromXContentTestWithRandomFields(true);
} }
private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException { @Override
protected PutMappingResponse createBlankInstance() {
final PutMappingResponse putMappingResponse = createTestItem(); return new PutMappingResponse();
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 EqualsHashCodeTestUtils.MutateFunction<PutMappingResponse> getMutateFunction() {
return response -> new PutMappingResponse(response.isAcknowledged() == false);
/**
* Returns a random {@link PutMappingResponse}.
*/
private static PutMappingResponse createTestItem() throws IOException {
boolean acknowledged = randomBoolean();
return new PutMappingResponse(acknowledged);
} }
} }

View File

@ -19,45 +19,41 @@
package org.elasticsearch.action.admin.indices.open; 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.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.io.IOException; public class OpenIndexResponseTests extends AbstractStreamableXContentTestCase<OpenIndexResponse> {
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; @Override
import static org.hamcrest.CoreMatchers.equalTo; protected OpenIndexResponse doParseInstance(XContentParser parser){
return OpenIndexResponse.fromXContent(parser);
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; @Override
try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { protected OpenIndexResponse createTestInstance() {
parsedOpenIndexResponse = OpenIndexResponse.fromXContent(parser);
assertNull(parser.nextToken());
}
assertThat(parsedOpenIndexResponse.isShardsAcknowledged(), equalTo(openIndexResponse.isShardsAcknowledged()));
assertThat(parsedOpenIndexResponse.isAcknowledged(), equalTo(openIndexResponse.isAcknowledged()));
}
private static OpenIndexResponse createTestItem() {
boolean acknowledged = randomBoolean(); boolean acknowledged = randomBoolean();
boolean shardsAcknowledged = acknowledged && randomBoolean(); boolean shardsAcknowledged = acknowledged && randomBoolean();
return new OpenIndexResponse(acknowledged, shardsAcknowledged); return new OpenIndexResponse(acknowledged, shardsAcknowledged);
} }
@Override
protected OpenIndexResponse createBlankInstance() {
return new OpenIndexResponse();
}
@Override
protected EqualsHashCodeTestUtils.MutateFunction<OpenIndexResponse> 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);
}
};
}
} }

View File

@ -20,17 +20,11 @@
package org.elasticsearch.action.admin.indices.shrink; package org.elasticsearch.action.admin.indices.shrink;
import org.elasticsearch.common.Strings; 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.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.io.IOException; public class ResizeResponseTests extends AbstractStreamableXContentTestCase<ResizeResponse> {
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
public class ResizeResponseTests extends ESTestCase {
public void testToXContent() { public void testToXContent() {
ResizeResponse response = new ResizeResponse(true, false, "index_name"); 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); assertEquals("{\"acknowledged\":true,\"shards_acknowledged\":false,\"index\":\"index_name\"}", output);
} }
public void testToAndFromXContent() throws IOException { @Override
doFromXContentTestWithRandomFields(false); protected ResizeResponse doParseInstance(XContentParser parser) {
return ResizeResponse.fromXContent(parser);
} }
/** @Override
* This test adds random fields and objects to the xContent rendered out to protected ResizeResponse createTestInstance() {
* 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() {
boolean acknowledged = randomBoolean(); boolean acknowledged = randomBoolean();
boolean shardsAcknowledged = acknowledged && randomBoolean(); boolean shardsAcknowledged = acknowledged && randomBoolean();
String index = randomAlphaOfLength(5); String index = randomAlphaOfLength(5);
return new ResizeResponse(acknowledged, shardsAcknowledged, index); return new ResizeResponse(acknowledged, shardsAcknowledged, index);
} }
@Override
protected ResizeResponse createBlankInstance() {
return new ResizeResponse();
}
@Override
protected EqualsHashCodeTestUtils.MutateFunction<ResizeResponse> 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));
}
};
}
} }

View File

@ -27,13 +27,12 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractStreamableXContentTestCase; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.test.VersionUtils; import org.elasticsearch.test.VersionUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
public class MainResponseTests extends AbstractStreamableXContentTestCase<MainResponse> { public class MainResponseTests extends AbstractStreamableXContentTestCase<MainResponse> {
@Override @Override
@ -87,18 +86,9 @@ public class MainResponseTests extends AbstractStreamableXContentTestCase<MainRe
+ "}", builder.string()); + "}", builder.string());
} }
//TODO this should be removed and the metehod from AbstractStreamableTestCase should be @Override
//used instead once https://github.com/elastic/elasticsearch/pull/25910 goes in protected EqualsHashCodeTestUtils.MutateFunction<MainResponse> getMutateFunction() {
public void testEqualsAndHashcode() { return o -> {
MainResponse original = createTestInstance();
checkEqualsAndHashCode(original, MainResponseTests::copy, MainResponseTests::mutate);
}
private static MainResponse copy(MainResponse o) {
return new MainResponse(o.getNodeName(), o.getVersion(), o.getClusterName(), o.getClusterUuid(), o.getBuild(), o.isAvailable());
}
private static MainResponse mutate(MainResponse o) {
String clusterUuid = o.getClusterUuid(); String clusterUuid = o.getClusterUuid();
boolean available = o.isAvailable(); boolean available = o.isAvailable();
Build build = o.getBuild(); Build build = o.getBuild();
@ -127,5 +117,6 @@ public class MainResponseTests extends AbstractStreamableXContentTestCase<MainRe
break; break;
} }
return new MainResponse(nodeName, version, clusterName, clusterUuid, build, available); return new MainResponse(nodeName, version, clusterName, clusterUuid, build, available);
};
} }
} }

View File

@ -41,9 +41,8 @@ public abstract class AbstractDiffableSerializationTestCase<T extends Diffable<T
protected abstract Reader<Diff<T>> diffReader(); protected abstract Reader<Diff<T>> diffReader();
public void testDiffableSerialization() throws IOException { public final void testDiffableSerialization() throws IOException {
DiffableTestUtils.testDiffableSerialization(this::createTestInstance, this::makeTestChanges, getNamedWriteableRegistry(), DiffableTestUtils.testDiffableSerialization(this::createTestInstance, this::makeTestChanges, getNamedWriteableRegistry(),
instanceReader(), diffReader()); instanceReader(), diffReader());
} }
} }

View File

@ -39,9 +39,8 @@ public abstract class AbstractDiffableWireSerializationTestCase<T extends Diffab
protected abstract Reader<Diff<T>> diffReader(); protected abstract Reader<Diff<T>> diffReader();
public void testDiffableSerialization() throws IOException { public final void testDiffableSerialization() throws IOException {
DiffableTestUtils.testDiffableSerialization(this::createTestInstance, this::makeTestChanges, getNamedWriteableRegistry(), DiffableTestUtils.testDiffableSerialization(this::createTestInstance, this::makeTestChanges, getNamedWriteableRegistry(),
instanceReader(), diffReader()); instanceReader(), diffReader());
} }
} }

View File

@ -34,7 +34,7 @@ public abstract class AbstractSerializingTestCase<T extends ToXContent & Writeab
* Generic test that creates new instance from the test instance and checks * Generic test that creates new instance from the test instance and checks
* both for equality and asserts equality on the two instances. * both for equality and asserts equality on the two instances.
*/ */
public void testFromXContent() throws IOException { public final void testFromXContent() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance(); T testInstance = createTestInstance();
XContentType xContentType = randomFrom(XContentType.values()); XContentType xContentType = randomFrom(XContentType.values());

View File

@ -71,7 +71,7 @@ public abstract class AbstractStreamableTestCase<T extends Streamable> extends E
* Tests that the equals and hashcode methods are consistent and copied * Tests that the equals and hashcode methods are consistent and copied
* versions of the instance have are equal. * versions of the instance have are equal.
*/ */
public void testEqualsAndHashcode() { public final void testEqualsAndHashcode() {
for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), getCopyFunction(), getMutateFunction()); EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), getCopyFunction(), getMutateFunction());
} }
@ -80,7 +80,7 @@ public abstract class AbstractStreamableTestCase<T extends Streamable> extends E
/** /**
* Test serialization and deserialization of the test instance. * 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++) { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance(); T testInstance = createTestInstance();
assertSerialization(testInstance); assertSerialization(testInstance);

View File

@ -37,7 +37,7 @@ public abstract class AbstractStreamableXContentTestCase<T extends ToXContent &
* Generic test that creates new instance from the test instance and checks * Generic test that creates new instance from the test instance and checks
* both for equality and asserts equality on the two queries. * both for equality and asserts equality on the two queries.
*/ */
public void testFromXContent() throws IOException { public final void testFromXContent() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance(); T testInstance = createTestInstance();
XContentType xContentType = randomFrom(XContentType.values()); XContentType xContentType = randomFrom(XContentType.values());

View File

@ -58,7 +58,7 @@ public abstract class AbstractWireSerializingTestCase<T extends Writeable> exten
* Tests that the equals and hashcode methods are consistent and copied * Tests that the equals and hashcode methods are consistent and copied
* versions of the instance have are equal. * 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++) { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), this::copyInstance, this::mutateInstance); EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), this::copyInstance, this::mutateInstance);
} }
@ -67,7 +67,7 @@ public abstract class AbstractWireSerializingTestCase<T extends Writeable> exten
/** /**
* Test serialization and deserialization of the test instance. * 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++) { for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance(); T testInstance = createTestInstance();
assertSerialization(testInstance); assertSerialization(testInstance);