Added unit test for Match and UpdateResult + Javadoc fixes
Change-Id: I8dae6c9568d33d580d60a72fdcc1be45b7308727
diff --git a/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java b/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java
index 127a567..f1ef356 100644
--- a/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java
+++ b/core/api/src/main/java/org/onosproject/store/service/ConsistentMap.java
@@ -24,7 +24,7 @@
import java.util.function.Predicate;
/**
- * A distributed, strongly consistent map.
+ * A distributed, strongly consistent key-value map.
* <p>
* This map offers strong read-after-update (where update == create/update/delete)
* consistency. All operations to the map are serialized and applied in a consistent
@@ -55,7 +55,7 @@
/**
* Returns true if the map is empty.
*
- * @return true if map has no entries, false otherwise.
+ * @return true if map has no entries, false otherwise
*/
boolean isEmpty();
@@ -63,7 +63,7 @@
* Returns true if this map contains a mapping for the specified key.
*
* @param key key
- * @return true if map contains key, false otherwise.
+ * @return true if map contains key, false otherwise
*/
boolean containsKey(K key);
diff --git a/core/api/src/main/java/org/onosproject/store/service/Serializer.java b/core/api/src/main/java/org/onosproject/store/service/Serializer.java
index a3e2b0c..6245175 100644
--- a/core/api/src/main/java/org/onosproject/store/service/Serializer.java
+++ b/core/api/src/main/java/org/onosproject/store/service/Serializer.java
@@ -24,7 +24,7 @@
import com.google.common.collect.Lists;
/**
- * Interface for serialization for store artifacts.
+ * Interface for serialization of store artifacts.
*/
public interface Serializer {
/**
diff --git a/core/api/src/main/java/org/onosproject/store/service/TransactionException.java b/core/api/src/main/java/org/onosproject/store/service/TransactionException.java
index 6135394..a3723ee 100644
--- a/core/api/src/main/java/org/onosproject/store/service/TransactionException.java
+++ b/core/api/src/main/java/org/onosproject/store/service/TransactionException.java
@@ -20,7 +20,7 @@
* Top level exception for Transaction failures.
*/
@SuppressWarnings("serial")
-public class TransactionException extends RuntimeException {
+public class TransactionException extends StorageException {
public TransactionException() {
}
diff --git a/core/api/src/main/java/org/onosproject/store/service/Versioned.java b/core/api/src/main/java/org/onosproject/store/service/Versioned.java
index f0e70bd..ac7109c 100644
--- a/core/api/src/main/java/org/onosproject/store/service/Versioned.java
+++ b/core/api/src/main/java/org/onosproject/store/service/Versioned.java
@@ -21,6 +21,7 @@
import org.joda.time.DateTime;
import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
/**
* Versioned value.
@@ -90,7 +91,7 @@
/**
* Maps this instance into another after transforming its
* value while retaining the same version and creationTime.
- * @param transformer function to mapping the value
+ * @param transformer function for mapping the value
* @param <U> value type of the returned instance
* @return mapped instance
*/
@@ -99,6 +100,22 @@
}
@Override
+ public int hashCode() {
+ return Objects.hashCode(value, version, creationTime);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof Versioned)) {
+ return false;
+ }
+ Versioned<V> that = (Versioned) other;
+ return Objects.equal(this.value, that.value) &&
+ Objects.equal(this.version, that.version) &&
+ Objects.equal(this.creationTime, that.creationTime);
+ }
+
+ @Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("value", value)
diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java
index 093e02c..804514c 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java
@@ -104,6 +104,21 @@
}
@Override
+ public int hashCode() {
+ return Objects.hash(matchAny, value);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof Match)) {
+ return false;
+ }
+ Match<T> that = (Match) other;
+ return Objects.equals(this.matchAny, that.matchAny) &&
+ Objects.equals(this.value, that.value);
+ }
+
+ @Override
public String toString() {
return toStringHelper(this)
.add("matchAny", matchAny)
diff --git a/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/MatchTest.java b/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/MatchTest.java
new file mode 100644
index 0000000..7ff94c8
--- /dev/null
+++ b/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/MatchTest.java
@@ -0,0 +1,52 @@
+package org.onosproject.store.consistent.impl;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertTrue;
+
+import org.junit.Test;
+
+import com.google.common.base.Objects;
+
+/**
+ * Unit tests for Match.
+ */
+public class MatchTest {
+
+ @Test
+ public void testMatches() {
+ Match<String> m1 = Match.any();
+ assertTrue(m1.matches(null));
+ assertTrue(m1.matches("foo"));
+ assertTrue(m1.matches("bar"));
+
+ Match<String> m2 = Match.ifNull();
+ assertTrue(m2.matches(null));
+ assertFalse(m2.matches("foo"));
+
+ Match<String> m3 = Match.ifValue("foo");
+ assertFalse(m3.matches(null));
+ assertFalse(m3.matches("bar"));
+ assertTrue(m3.matches("foo"));
+ }
+
+ @Test
+ public void testEquals() {
+ Match<String> m1 = Match.any();
+ Match<String> m2 = Match.any();
+ Match<String> m3 = Match.ifNull();
+ Match<String> m4 = Match.ifValue("bar");
+ assertEquals(m1, m2);
+ assertFalse(Objects.equal(m1, m3));
+ assertFalse(Objects.equal(m3, m4));
+ }
+
+ @Test
+ public void testMap() {
+ Match<String> m1 = Match.ifNull();
+ assertEquals(m1.map(s -> "bar"), Match.ifNull());
+ Match<String> m2 = Match.ifValue("foo");
+ Match<String> m3 = m2.map(s -> "bar");
+ assertTrue(m3.matches("bar"));
+ }
+}
diff --git a/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/UpdateResultTest.java b/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/UpdateResultTest.java
new file mode 100644
index 0000000..84dc915
--- /dev/null
+++ b/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/UpdateResultTest.java
@@ -0,0 +1,84 @@
+package org.onosproject.store.consistent.impl;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertNull;
+import static junit.framework.TestCase.assertTrue;
+
+import org.junit.Test;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.Versioned;
+
+/**
+ * Unit tests for UpdateResult.
+ */
+public class UpdateResultTest {
+
+ @Test
+ public void testGetters() {
+ Versioned<String> oldValue = new Versioned<String>("a", 1);
+ Versioned<String> newValue = new Versioned<String>("b", 2);
+ UpdateResult<String, String> ur =
+ new UpdateResult<>(true, "foo", "k", oldValue, newValue);
+
+ assertTrue(ur.updated());
+ assertEquals("foo", ur.mapName());
+ assertEquals("k", ur.key());
+ assertEquals(oldValue, ur.oldValue());
+ assertEquals(newValue, ur.newValue());
+ }
+
+ @Test
+ public void testToMapEvent() {
+ Versioned<String> oldValue = new Versioned<String>("a", 1);
+ Versioned<String> newValue = new Versioned<String>("b", 2);
+ UpdateResult<String, String> ur1 =
+ new UpdateResult<>(true, "foo", "k", oldValue, newValue);
+ MapEvent<String, String> event1 = ur1.toMapEvent();
+ assertEquals(MapEvent.Type.UPDATE, event1.type());
+ assertEquals("k", event1.key());
+ assertEquals(newValue, event1.value());
+
+ UpdateResult<String, String> ur2 =
+ new UpdateResult<>(true, "foo", "k", null, newValue);
+ MapEvent<String, String> event2 = ur2.toMapEvent();
+ assertEquals(MapEvent.Type.INSERT, event2.type());
+ assertEquals("k", event2.key());
+ assertEquals(newValue, event2.value());
+
+ UpdateResult<String, String> ur3 =
+ new UpdateResult<>(true, "foo", "k", oldValue, null);
+ MapEvent<String, String> event3 = ur3.toMapEvent();
+ assertEquals(MapEvent.Type.REMOVE, event3.type());
+ assertEquals("k", event3.key());
+ assertEquals(oldValue, event3.value());
+
+ UpdateResult<String, String> ur4 =
+ new UpdateResult<>(false, "foo", "k", oldValue, oldValue);
+ assertNull(ur4.toMapEvent());
+ }
+
+ @Test
+ public void testMap() {
+ Versioned<String> oldValue = new Versioned<String>("a", 1);
+ Versioned<String> newValue = new Versioned<String>("b", 2);
+ UpdateResult<String, String> ur1 =
+ new UpdateResult<>(true, "foo", "k", oldValue, newValue);
+ UpdateResult<Integer, Integer> ur2 = ur1.map(s -> s.length(), s -> s.length());
+
+ assertEquals(ur2.updated(), ur1.updated());
+ assertEquals(ur1.mapName(), ur2.mapName());
+ assertEquals(new Integer(1), ur2.key());
+ assertEquals(oldValue.map(s -> s.length()), ur2.oldValue());
+ assertEquals(newValue.map(s -> s.length()), ur2.newValue());
+
+ UpdateResult<String, String> ur3 =
+ new UpdateResult<>(true, "foo", "k", null, newValue);
+ UpdateResult<Integer, Integer> ur4 = ur3.map(s -> s.length(), s -> s.length());
+
+ assertEquals(ur3.updated(), ur4.updated());
+ assertEquals(ur3.mapName(), ur4.mapName());
+ assertEquals(new Integer(1), ur4.key());
+ assertNull(ur4.oldValue());
+ assertEquals(newValue.map(s -> s.length()), ur4.newValue());
+ }
+}