Skip to content

Commit

Permalink
Add XContent Serialization Test to SnapshotShardFailure (#72811)
Browse files Browse the repository at this point in the history
Adding missing test from #72801
  • Loading branch information
original-brownbear committed May 11, 2021
1 parent 29e3282 commit b5bbd4d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
public class SnapshotShardFailure extends ShardOperationFailedException {

@Nullable
private String nodeId;
private ShardId shardId;
private final String nodeId;
private final ShardId shardId;

SnapshotShardFailure(StreamInput in) throws IOException {
nodeId = in.readOptionalString();
Expand Down Expand Up @@ -78,6 +78,10 @@ public String nodeId() {
return nodeId;
}

public ShardId getShardId() {
return shardId;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(nodeId);
Expand Down Expand Up @@ -167,17 +171,14 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SnapshotShardFailure that = (SnapshotShardFailure) o;
// customized to account for discrepancies in shardId/Index toXContent/fromXContent related to uuid
return shardId.id() == that.shardId.id() &&
shardId.getIndexName().equals(shardId.getIndexName()) &&
return shardId.equals(that.shardId) &&
Objects.equals(reason, that.reason) &&
Objects.equals(nodeId, that.nodeId) &&
status.getStatus() == that.status.getStatus();
}

@Override
public int hashCode() {
// customized to account for discrepancies in shardId/Index toXContent/fromXContent related to uuid
return Objects.hash(shardId.id(), shardId.getIndexName(), reason, nodeId, status.getStatus());
return Objects.hash(shardId, reason, nodeId, status.getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.snapshots;

import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.AbstractXContentTestCase;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;

import java.io.IOException;

public class SnapshotShardFailureSerializationTests extends AbstractXContentTestCase<SnapshotShardFailure> {

public void testEqualsAndHashCode() {
final SnapshotShardFailure instance = createTestInstance();
EqualsHashCodeTestUtils.checkEqualsAndHashCode(
instance,
snapshotShardFailure -> new SnapshotShardFailure(
snapshotShardFailure.nodeId(),
snapshotShardFailure.getShardId(),
snapshotShardFailure.reason()
),
snapshotShardFailure -> {
if (randomBoolean()) {
return new SnapshotShardFailure(
randomValueOtherThan(snapshotShardFailure.nodeId(), () -> randomAlphaOfLengthBetween(5, 10)),
snapshotShardFailure.getShardId(),
snapshotShardFailure.reason()
);
} else if (randomBoolean()) {
return new SnapshotShardFailure(
snapshotShardFailure.nodeId(),
snapshotShardFailure.getShardId(),
randomValueOtherThan(snapshotShardFailure.reason(), () -> randomAlphaOfLengthBetween(1, 100))
);
} else {
final ShardId originalShardId = snapshotShardFailure.getShardId();
final ShardId mutatedShardId;
if (randomBoolean()) {
mutatedShardId = new ShardId(
originalShardId.getIndex(),
randomValueOtherThan((byte) originalShardId.getId(), ESTestCase::randomNonNegativeByte)
);
} else if (randomBoolean()) {
mutatedShardId = new ShardId(
new Index(
originalShardId.getIndexName(),
randomValueOtherThan(
originalShardId.getIndex().getUUID(),
() -> UUIDs.randomBase64UUID(random())
)
),
originalShardId.id()
);
} else {
mutatedShardId = randomValueOtherThan(originalShardId, SnapshotShardFailureSerializationTests::randomShardId);
}
return new SnapshotShardFailure(snapshotShardFailure.nodeId(), mutatedShardId, snapshotShardFailure.reason());
}
}
);
}

@Override
protected SnapshotShardFailure createTestInstance() {
return new SnapshotShardFailure(
UUIDs.randomBase64UUID(random()),
randomShardId(),
randomAlphaOfLengthBetween(1, 100)
);
}

private static ShardId randomShardId() {
return new ShardId(
randomAlphaOfLengthBetween(5, 10),
UUIDs.randomBase64UUID(random()),
randomNonNegativeByte()
);
}

@Override
protected SnapshotShardFailure doParseInstance(XContentParser parser) throws IOException {
return SnapshotShardFailure.fromXContent(parser);
}

@Override
protected boolean supportsUnknownFields() {
return false;
}
}

0 comments on commit b5bbd4d

Please sign in to comment.