Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate some *ResponseTests to AbstractStreamableXContentTestCase #28749

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to double check this, I'm not sure if using super might violate the equals() contract.

Copy link
Member

@cbuescher cbuescher Feb 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its all good, because AcknowledgedResponse is abstract. If it would be possible to create instances of it, I think there could have been cases where the symmetry property of equals() would be broken, but this cannot happen with non-instantiable superclasses.

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