Add unit tests for FlowId class, and update pom.xml to use guava-testlib library.
- Changed toString() method to return the value in HEX string.
- Added the guava-testlib library to pom.xml.
- Added unit tests for FlowId class.
Change-Id: I29c4d11a1737c9fac6a62e5541c5a98add46c295
diff --git a/pom.xml b/pom.xml
index a0bca46..24216b8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -620,6 +620,12 @@
<version>17.0</version>
</dependency>
<dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava-testlib</artifactId>
+ <version>17.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java b/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java
index b6bb9f0..3e94c7b 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java
@@ -2,20 +2,25 @@
import java.util.Objects;
+import javax.annotation.concurrent.Immutable;
+
import net.onrc.onos.api.batchoperation.BatchOperationTarget;
/**
* Represents ID for Flow objects.
+ * <p>
+ * This class is immutable.
*/
-public class FlowId implements BatchOperationTarget {
- private final long value;
+@Immutable
+public final class FlowId implements BatchOperationTarget {
+ private final long id;
/**
* Default constructor for Kryo deserialization.
*/
@Deprecated
protected FlowId() {
- value = 0;
+ id = 0;
}
/**
@@ -26,24 +31,24 @@
* @param id String representation of the ID.
*/
public FlowId(long id) {
- value = id;
+ this.id = id;
}
@Override
public String toString() {
- return Long.toString(value);
+ return "0x" + Long.toHexString(id);
}
@Override
public int hashCode() {
- return Objects.hashCode(value);
+ return Objects.hashCode(id);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FlowId) {
FlowId that = (FlowId) obj;
- return Objects.equals(this.value, that.value);
+ return Objects.equals(this.id, that.id);
}
return false;
}
diff --git a/src/test/java/net/onrc/onos/api/flowmanager/FlowIdTest.java b/src/test/java/net/onrc/onos/api/flowmanager/FlowIdTest.java
index 49981b0..20ffcc0 100644
--- a/src/test/java/net/onrc/onos/api/flowmanager/FlowIdTest.java
+++ b/src/test/java/net/onrc/onos/api/flowmanager/FlowIdTest.java
@@ -1,40 +1,68 @@
package net.onrc.onos.api.flowmanager;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static net.onrc.onos.core.util.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.junit.Assert.assertEquals;
+import net.onrc.onos.core.util.TestUtils;
+import org.junit.Before;
import org.junit.Test;
+import com.google.common.testing.EqualsTester;
+
+/**
+ * Unit tests for {@link FlowId} class.
+ */
public class FlowIdTest {
+ private FlowId flowId1;
+ private FlowId flowId2;
+ private FlowId flowId3;
+ private FlowId flowId4;
+ private FlowId flowId5;
- /**
- * Tests FlowId's equals method.
- */
- @Test
- public void testEquals() {
- FlowId flow1 = new FlowId(0L);
- FlowId flow2 = new FlowId(1L);
- FlowId flow3 = new FlowId(2L);
- FlowId flow4 = new FlowId(1L);
-
- assertTrue(flow1.equals(flow1));
- assertTrue(flow2.equals(flow2));
- assertTrue(flow3.equals(flow3));
- assertTrue(flow4.equals(flow4));
-
- assertFalse(flow1.equals(flow2));
- assertFalse(flow1.equals(flow3));
- assertFalse(flow1.equals(flow4));
- assertFalse(flow2.equals(flow1));
- assertFalse(flow2.equals(flow3));
- assertFalse(flow3.equals(flow1));
- assertFalse(flow3.equals(flow2));
- assertFalse(flow3.equals(flow4));
- assertFalse(flow4.equals(flow1));
- assertFalse(flow4.equals(flow3));
-
- assertTrue(flow2.equals(flow4));
- assertTrue(flow4.equals(flow2));
+ @Before
+ public void setUp() {
+ flowId1 = new FlowId(0L);
+ flowId2 = new FlowId(1L);
+ flowId3 = new FlowId(2L);
+ flowId4 = new FlowId(1L);
+ flowId5 = new FlowId(0xABCDEFL);
}
+ /**
+ * Tests {@link FlowId#FlowId(long)} constructor.
+ */
+ @Test
+ public void testConstructor() {
+ assertEquals(0xABCDEFL, TestUtils.getField(flowId5, "id"));
+ }
+
+ /**
+ * Tests the equality of {@link FlowId} objects.
+ */
+ @Test
+ public void testEqualsAndHashCode() {
+ new EqualsTester()
+ .addEqualityGroup(flowId1)
+ .addEqualityGroup(flowId2, flowId4)
+ .addEqualityGroup(flowId3)
+ .addEqualityGroup(flowId5)
+ .testEquals();
+ }
+
+ /**
+ * Tests {@link FlowId#toString()} method.
+ */
+ @Test
+ public void testToString() {
+ assertEquals("0x0", flowId1.toString());
+ assertEquals("0xabcdef", flowId5.toString());
+ }
+
+ /**
+ * Tests if {@link FlowId} is immutable.
+ */
+ @Test
+ public void testImmutable() {
+ assertThatClassIsImmutable(FlowId.class);
+ }
}