Browse Source

Add XContent Serialization Test to SnapshotShardFailure (#72811)

Adding missing test from #72801
Armin Braun 4 years ago
parent
commit
b5bbd4d591

+ 8 - 7
server/src/main/java/org/elasticsearch/snapshots/SnapshotShardFailure.java

@@ -31,8 +31,8 @@ import java.util.Objects;
 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();
@@ -78,6 +78,10 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
         return nodeId;
     }
 
+    public ShardId getShardId() {
+        return shardId;
+    }
+
     @Override
     public void writeTo(StreamOutput out) throws IOException {
         out.writeOptionalString(nodeId);
@@ -167,9 +171,7 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
         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();
@@ -177,7 +179,6 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
 
     @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());
     }
 }

+ 98 - 0
server/src/test/java/org/elasticsearch/snapshots/SnapshotShardFailureSerializationTests.java

@@ -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;
+    }
+}