Skip to content

Commit

Permalink
Migrate some *ResponseTests to AbstractStreamableXContentTestCase
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
javanna committed Feb 20, 2018
1 parent 8bbb3c9 commit ad697dd
Show file tree
Hide file tree
Showing 23 changed files with 364 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

/**
* A response for a cluster update settings action.
Expand Down Expand Up @@ -103,7 +104,22 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static IndicesAliasesResponse fromXContent(XContentParser parser) throws IOException {
public static IndicesAliasesResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static CloseIndexResponse fromXContent(XContentParser parser) throws IOException {
public static CloseIndexResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

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

Expand Down Expand Up @@ -100,4 +101,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static PutMappingResponse fromXContent(XContentParser parser) throws IOException {
public static PutMappingResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,19 @@ public static RolloverResponse fromXContent(XContentParser parser) {

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

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

Expand Down Expand Up @@ -78,4 +79,21 @@ protected void writeAcknowledged(StreamOutput out) throws IOException {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

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

Expand Down Expand Up @@ -73,4 +74,19 @@ protected void writeShardsAcknowledged(StreamOutput out) throws IOException {
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClusterUpdateSettingsResponse> {

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<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 {
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<String> allKeys = settings.keySet();
List<String> 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<String> getRandomFieldsExcludeFilter() {
return p -> p.startsWith("transient") || p.startsWith("persistent");
}

public static Settings randomClusterSettings(int min, int max) {
Expand All @@ -77,7 +90,13 @@ public static Settings randomClusterSettings(int min, int max) {
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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit ad697dd

Please sign in to comment.