Add unit tests for ClusterMetadataEvent
- add toString, equals, and hashCode to ClusterMetadataEvent
Change-Id: Idb1031c01cc6333be76851bb1b2ee196a8732d76
diff --git a/core/api/src/main/java/org/onosproject/cluster/ClusterMetadataEvent.java b/core/api/src/main/java/org/onosproject/cluster/ClusterMetadataEvent.java
index 665030e..75b488c 100644
--- a/core/api/src/main/java/org/onosproject/cluster/ClusterMetadataEvent.java
+++ b/core/api/src/main/java/org/onosproject/cluster/ClusterMetadataEvent.java
@@ -15,8 +15,11 @@
*/
package org.onosproject.cluster;
+import com.google.common.base.MoreObjects;
import org.onosproject.event.AbstractEvent;
+import java.util.Objects;
+
/**
* Describes a cluster metadata event.
*/
@@ -53,4 +56,32 @@
public ClusterMetadataEvent(Type type, ClusterMetadata metadata, long time) {
super(type, metadata, time);
}
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(type(), subject(), time());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof ClusterMetadataEvent) {
+ final ClusterMetadataEvent other = (ClusterMetadataEvent) obj;
+ return Objects.equals(this.type(), other.type()) &&
+ Objects.equals(this.subject(), other.subject()) &&
+ Objects.equals(this.time(), other.time());
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this.getClass())
+ .add("type", type())
+ .add("subject", subject())
+ .add("time", time())
+ .toString();
+ }
}
diff --git a/core/api/src/test/java/org/onosproject/cluster/ClusterMetadataEventTest.java b/core/api/src/test/java/org/onosproject/cluster/ClusterMetadataEventTest.java
new file mode 100644
index 0000000..3ea802d
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/cluster/ClusterMetadataEventTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed 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.onosproject.cluster;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for the Cluster Metadata event.
+ */
+public class ClusterMetadataEventTest {
+ private final long time = System.currentTimeMillis();
+ private final PartitionId pid1 = PartitionId.from(1);
+ private final PartitionId pid2 = PartitionId.from(2);
+ private final NodeId nid1 = NodeId.nodeId("10.0.0.1");
+ private final NodeId nid2 = NodeId.nodeId("10.0.0.2");
+ private final ControllerNode n1 =
+ new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
+ private final ControllerNode n2 =
+ new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876);
+ private final Partition p1 = new DefaultPartition(pid1, ImmutableSet.of(nid1));
+ private final Partition p2 = new DefaultPartition(pid2, ImmutableSet.of(nid1, nid2));
+ private final Partition p3 = new DefaultPartition(pid2, ImmutableSet.of(nid2));
+ private final ClusterMetadata metadata1 =
+ new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1));
+ private final ClusterMetadata metadata2 =
+ new ClusterMetadata("bar", ImmutableSet.of(n1, n2), ImmutableSet.of(p1, p2));
+ private final ClusterMetadata metadata3 =
+ new ClusterMetadata("baz", ImmutableSet.of(n2), ImmutableSet.of(p3));
+
+ private final ClusterMetadataEvent event1 =
+ new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1);
+ private final ClusterMetadataEvent sameAsEvent1 =
+ new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1);
+ private final ClusterMetadataEvent event2 =
+ new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
+ private final ClusterMetadataEvent sameAsEvent2 =
+ new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
+ private final ClusterMetadataEvent event3 =
+ new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata3);
+
+ /**
+ * Tests for proper operation of equals(), hashCode() and toString() methods.
+ */
+ @Test
+ public void checkEquals() {
+ new EqualsTester()
+ .addEqualityGroup(event1, sameAsEvent1)
+ .addEqualityGroup(event2, sameAsEvent2)
+ .addEqualityGroup(event3)
+ .testEquals();
+ }
+
+ /**
+ * Tests that objects are created properly.
+ */
+ @Test
+ public void checkConstruction() {
+ assertThat(event1.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
+ assertThat(event1.subject(), is(metadata1));
+
+ assertThat(event2.time(), is(time));
+ assertThat(event2.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
+ assertThat(event2.subject(), is(metadata2));
+ }
+
+}