Add unit tests for PartitionEvent
- add hashCode and equals for PartitionEvent
Change-Id: I9f0b2ed7dbadae56ed2045360f56f9be24399183
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/PartitionEvent.java b/core/api/src/main/java/org/onosproject/store/primitives/PartitionEvent.java
index 1a80d59..d398034 100644
--- a/core/api/src/main/java/org/onosproject/store/primitives/PartitionEvent.java
+++ b/core/api/src/main/java/org/onosproject/store/primitives/PartitionEvent.java
@@ -18,6 +18,8 @@
import org.onosproject.cluster.Partition;
import org.onosproject.event.AbstractEvent;
+import java.util.Objects;
+
/**
* Describes partition-related event.
*/
@@ -64,4 +66,23 @@
protected PartitionEvent(Type type, Partition subject, long time) {
super(type, subject, 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 PartitionEvent) {
+ final PartitionEvent other = (PartitionEvent) obj;
+ return Objects.equals(this.type(), other.type()) &&
+ Objects.equals(this.subject(), other.subject()) &&
+ Objects.equals(this.time(), other.time());
+ }
+ return false;
+ }
}
\ No newline at end of file
diff --git a/core/api/src/test/java/org/onosproject/store/primitives/PartitionEventTest.java b/core/api/src/test/java/org/onosproject/store/primitives/PartitionEventTest.java
new file mode 100644
index 0000000..ef11c46
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/primitives/PartitionEventTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.store.primitives;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onosproject.cluster.DefaultPartition;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.cluster.Partition;
+import org.onosproject.cluster.PartitionId;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for the mastership event.
+ */
+public class PartitionEventTest {
+ private final long time = System.currentTimeMillis();
+ private final long time0 = 0;
+
+ 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 Partition p1 =
+ new DefaultPartition(pid1, ImmutableSet.of(nid1));
+ private final Partition p2 =
+ new DefaultPartition(pid2, ImmutableSet.of(nid1, nid2));
+
+ private final PartitionEvent event1 =
+ new PartitionEvent(PartitionEvent.Type.UPDATED, p1, time);
+ private final PartitionEvent sameAsEvent1 =
+ new PartitionEvent(PartitionEvent.Type.UPDATED, p1, time);
+ private final PartitionEvent event2 =
+ new PartitionEvent(PartitionEvent.Type.OPENED, p1, time);
+ private final PartitionEvent event3 =
+ new PartitionEvent(PartitionEvent.Type.CLOSED, p1, time);
+ private final PartitionEvent event4 =
+ new PartitionEvent(PartitionEvent.Type.AVAILABLE, p1, time);
+ private final PartitionEvent event5 =
+ new PartitionEvent(PartitionEvent.Type.UNAVAILABLE, p1, time);
+ private final PartitionEvent event6 =
+ new PartitionEvent(PartitionEvent.Type.UPDATED, p2, time);
+ private final PartitionEvent event7 =
+ new PartitionEvent(PartitionEvent.Type.UPDATED, p1, time0);
+
+ /**
+ * Tests for proper operation of equals(), hashCode() and toString() methods.
+ */
+ @Test
+ public void checkEquals() {
+ new EqualsTester()
+ .addEqualityGroup(event1, sameAsEvent1)
+ .addEqualityGroup(event2)
+ .addEqualityGroup(event3)
+ .addEqualityGroup(event4)
+ .addEqualityGroup(event5)
+ .addEqualityGroup(event6)
+ .addEqualityGroup(event7)
+ .testEquals();
+ }
+
+ /**
+ * Tests that objects are created properly.
+ */
+ @Test
+ public void checkConstruction() {
+ assertThat(event1.type(), is(PartitionEvent.Type.UPDATED));
+ assertThat(event1.subject(), is(p1));
+ assertThat(event1.time(), is(time));
+ }
+
+}