Added unit test for Match and UpdateResult + Javadoc fixes
Change-Id: I8dae6c9568d33d580d60a72fdcc1be45b7308727
Showing
7 changed files
with
174 additions
and
6 deletions
... | @@ -24,7 +24,7 @@ import java.util.function.Function; | ... | @@ -24,7 +24,7 @@ import java.util.function.Function; |
24 | import java.util.function.Predicate; | 24 | import java.util.function.Predicate; |
25 | 25 | ||
26 | /** | 26 | /** |
27 | - * A distributed, strongly consistent map. | 27 | + * A distributed, strongly consistent key-value map. |
28 | * <p> | 28 | * <p> |
29 | * This map offers strong read-after-update (where update == create/update/delete) | 29 | * This map offers strong read-after-update (where update == create/update/delete) |
30 | * consistency. All operations to the map are serialized and applied in a consistent | 30 | * consistency. All operations to the map are serialized and applied in a consistent |
... | @@ -55,7 +55,7 @@ public interface ConsistentMap<K, V> { | ... | @@ -55,7 +55,7 @@ public interface ConsistentMap<K, V> { |
55 | /** | 55 | /** |
56 | * Returns true if the map is empty. | 56 | * Returns true if the map is empty. |
57 | * | 57 | * |
58 | - * @return true if map has no entries, false otherwise. | 58 | + * @return true if map has no entries, false otherwise |
59 | */ | 59 | */ |
60 | boolean isEmpty(); | 60 | boolean isEmpty(); |
61 | 61 | ||
... | @@ -63,7 +63,7 @@ public interface ConsistentMap<K, V> { | ... | @@ -63,7 +63,7 @@ public interface ConsistentMap<K, V> { |
63 | * Returns true if this map contains a mapping for the specified key. | 63 | * Returns true if this map contains a mapping for the specified key. |
64 | * | 64 | * |
65 | * @param key key | 65 | * @param key key |
66 | - * @return true if map contains key, false otherwise. | 66 | + * @return true if map contains key, false otherwise |
67 | */ | 67 | */ |
68 | boolean containsKey(K key); | 68 | boolean containsKey(K key); |
69 | 69 | ... | ... |
... | @@ -24,7 +24,7 @@ import org.onlab.util.KryoNamespace; | ... | @@ -24,7 +24,7 @@ import org.onlab.util.KryoNamespace; |
24 | import com.google.common.collect.Lists; | 24 | import com.google.common.collect.Lists; |
25 | 25 | ||
26 | /** | 26 | /** |
27 | - * Interface for serialization for store artifacts. | 27 | + * Interface for serialization of store artifacts. |
28 | */ | 28 | */ |
29 | public interface Serializer { | 29 | public interface Serializer { |
30 | /** | 30 | /** | ... | ... |
... | @@ -20,7 +20,7 @@ package org.onosproject.store.service; | ... | @@ -20,7 +20,7 @@ package org.onosproject.store.service; |
20 | * Top level exception for Transaction failures. | 20 | * Top level exception for Transaction failures. |
21 | */ | 21 | */ |
22 | @SuppressWarnings("serial") | 22 | @SuppressWarnings("serial") |
23 | -public class TransactionException extends RuntimeException { | 23 | +public class TransactionException extends StorageException { |
24 | public TransactionException() { | 24 | public TransactionException() { |
25 | } | 25 | } |
26 | 26 | ... | ... |
... | @@ -21,6 +21,7 @@ import java.util.function.Function; | ... | @@ -21,6 +21,7 @@ import java.util.function.Function; |
21 | import org.joda.time.DateTime; | 21 | import org.joda.time.DateTime; |
22 | 22 | ||
23 | import com.google.common.base.MoreObjects; | 23 | import com.google.common.base.MoreObjects; |
24 | +import com.google.common.base.Objects; | ||
24 | 25 | ||
25 | /** | 26 | /** |
26 | * Versioned value. | 27 | * Versioned value. |
... | @@ -90,7 +91,7 @@ public class Versioned<V> { | ... | @@ -90,7 +91,7 @@ public class Versioned<V> { |
90 | /** | 91 | /** |
91 | * Maps this instance into another after transforming its | 92 | * Maps this instance into another after transforming its |
92 | * value while retaining the same version and creationTime. | 93 | * value while retaining the same version and creationTime. |
93 | - * @param transformer function to mapping the value | 94 | + * @param transformer function for mapping the value |
94 | * @param <U> value type of the returned instance | 95 | * @param <U> value type of the returned instance |
95 | * @return mapped instance | 96 | * @return mapped instance |
96 | */ | 97 | */ |
... | @@ -99,6 +100,22 @@ public class Versioned<V> { | ... | @@ -99,6 +100,22 @@ public class Versioned<V> { |
99 | } | 100 | } |
100 | 101 | ||
101 | @Override | 102 | @Override |
103 | + public int hashCode() { | ||
104 | + return Objects.hashCode(value, version, creationTime); | ||
105 | + } | ||
106 | + | ||
107 | + @Override | ||
108 | + public boolean equals(Object other) { | ||
109 | + if (!(other instanceof Versioned)) { | ||
110 | + return false; | ||
111 | + } | ||
112 | + Versioned<V> that = (Versioned) other; | ||
113 | + return Objects.equal(this.value, that.value) && | ||
114 | + Objects.equal(this.version, that.version) && | ||
115 | + Objects.equal(this.creationTime, that.creationTime); | ||
116 | + } | ||
117 | + | ||
118 | + @Override | ||
102 | public String toString() { | 119 | public String toString() { |
103 | return MoreObjects.toStringHelper(this) | 120 | return MoreObjects.toStringHelper(this) |
104 | .add("value", value) | 121 | .add("value", value) | ... | ... |
... | @@ -104,6 +104,21 @@ public final class Match<T> { | ... | @@ -104,6 +104,21 @@ public final class Match<T> { |
104 | } | 104 | } |
105 | 105 | ||
106 | @Override | 106 | @Override |
107 | + public int hashCode() { | ||
108 | + return Objects.hash(matchAny, value); | ||
109 | + } | ||
110 | + | ||
111 | + @Override | ||
112 | + public boolean equals(Object other) { | ||
113 | + if (!(other instanceof Match)) { | ||
114 | + return false; | ||
115 | + } | ||
116 | + Match<T> that = (Match) other; | ||
117 | + return Objects.equals(this.matchAny, that.matchAny) && | ||
118 | + Objects.equals(this.value, that.value); | ||
119 | + } | ||
120 | + | ||
121 | + @Override | ||
107 | public String toString() { | 122 | public String toString() { |
108 | return toStringHelper(this) | 123 | return toStringHelper(this) |
109 | .add("matchAny", matchAny) | 124 | .add("matchAny", matchAny) | ... | ... |
1 | +package org.onosproject.store.consistent.impl; | ||
2 | + | ||
3 | +import static junit.framework.TestCase.assertEquals; | ||
4 | +import static junit.framework.TestCase.assertFalse; | ||
5 | +import static junit.framework.TestCase.assertTrue; | ||
6 | + | ||
7 | +import org.junit.Test; | ||
8 | + | ||
9 | +import com.google.common.base.Objects; | ||
10 | + | ||
11 | +/** | ||
12 | + * Unit tests for Match. | ||
13 | + */ | ||
14 | +public class MatchTest { | ||
15 | + | ||
16 | + @Test | ||
17 | + public void testMatches() { | ||
18 | + Match<String> m1 = Match.any(); | ||
19 | + assertTrue(m1.matches(null)); | ||
20 | + assertTrue(m1.matches("foo")); | ||
21 | + assertTrue(m1.matches("bar")); | ||
22 | + | ||
23 | + Match<String> m2 = Match.ifNull(); | ||
24 | + assertTrue(m2.matches(null)); | ||
25 | + assertFalse(m2.matches("foo")); | ||
26 | + | ||
27 | + Match<String> m3 = Match.ifValue("foo"); | ||
28 | + assertFalse(m3.matches(null)); | ||
29 | + assertFalse(m3.matches("bar")); | ||
30 | + assertTrue(m3.matches("foo")); | ||
31 | + } | ||
32 | + | ||
33 | + @Test | ||
34 | + public void testEquals() { | ||
35 | + Match<String> m1 = Match.any(); | ||
36 | + Match<String> m2 = Match.any(); | ||
37 | + Match<String> m3 = Match.ifNull(); | ||
38 | + Match<String> m4 = Match.ifValue("bar"); | ||
39 | + assertEquals(m1, m2); | ||
40 | + assertFalse(Objects.equal(m1, m3)); | ||
41 | + assertFalse(Objects.equal(m3, m4)); | ||
42 | + } | ||
43 | + | ||
44 | + @Test | ||
45 | + public void testMap() { | ||
46 | + Match<String> m1 = Match.ifNull(); | ||
47 | + assertEquals(m1.map(s -> "bar"), Match.ifNull()); | ||
48 | + Match<String> m2 = Match.ifValue("foo"); | ||
49 | + Match<String> m3 = m2.map(s -> "bar"); | ||
50 | + assertTrue(m3.matches("bar")); | ||
51 | + } | ||
52 | +} |
core/store/dist/src/test/java/org/onosproject/store/consistent/impl/UpdateResultTest.java
0 → 100644
1 | +package org.onosproject.store.consistent.impl; | ||
2 | + | ||
3 | +import static junit.framework.TestCase.assertEquals; | ||
4 | +import static junit.framework.TestCase.assertNull; | ||
5 | +import static junit.framework.TestCase.assertTrue; | ||
6 | + | ||
7 | +import org.junit.Test; | ||
8 | +import org.onosproject.store.service.MapEvent; | ||
9 | +import org.onosproject.store.service.Versioned; | ||
10 | + | ||
11 | +/** | ||
12 | + * Unit tests for UpdateResult. | ||
13 | + */ | ||
14 | +public class UpdateResultTest { | ||
15 | + | ||
16 | + @Test | ||
17 | + public void testGetters() { | ||
18 | + Versioned<String> oldValue = new Versioned<String>("a", 1); | ||
19 | + Versioned<String> newValue = new Versioned<String>("b", 2); | ||
20 | + UpdateResult<String, String> ur = | ||
21 | + new UpdateResult<>(true, "foo", "k", oldValue, newValue); | ||
22 | + | ||
23 | + assertTrue(ur.updated()); | ||
24 | + assertEquals("foo", ur.mapName()); | ||
25 | + assertEquals("k", ur.key()); | ||
26 | + assertEquals(oldValue, ur.oldValue()); | ||
27 | + assertEquals(newValue, ur.newValue()); | ||
28 | + } | ||
29 | + | ||
30 | + @Test | ||
31 | + public void testToMapEvent() { | ||
32 | + Versioned<String> oldValue = new Versioned<String>("a", 1); | ||
33 | + Versioned<String> newValue = new Versioned<String>("b", 2); | ||
34 | + UpdateResult<String, String> ur1 = | ||
35 | + new UpdateResult<>(true, "foo", "k", oldValue, newValue); | ||
36 | + MapEvent<String, String> event1 = ur1.toMapEvent(); | ||
37 | + assertEquals(MapEvent.Type.UPDATE, event1.type()); | ||
38 | + assertEquals("k", event1.key()); | ||
39 | + assertEquals(newValue, event1.value()); | ||
40 | + | ||
41 | + UpdateResult<String, String> ur2 = | ||
42 | + new UpdateResult<>(true, "foo", "k", null, newValue); | ||
43 | + MapEvent<String, String> event2 = ur2.toMapEvent(); | ||
44 | + assertEquals(MapEvent.Type.INSERT, event2.type()); | ||
45 | + assertEquals("k", event2.key()); | ||
46 | + assertEquals(newValue, event2.value()); | ||
47 | + | ||
48 | + UpdateResult<String, String> ur3 = | ||
49 | + new UpdateResult<>(true, "foo", "k", oldValue, null); | ||
50 | + MapEvent<String, String> event3 = ur3.toMapEvent(); | ||
51 | + assertEquals(MapEvent.Type.REMOVE, event3.type()); | ||
52 | + assertEquals("k", event3.key()); | ||
53 | + assertEquals(oldValue, event3.value()); | ||
54 | + | ||
55 | + UpdateResult<String, String> ur4 = | ||
56 | + new UpdateResult<>(false, "foo", "k", oldValue, oldValue); | ||
57 | + assertNull(ur4.toMapEvent()); | ||
58 | + } | ||
59 | + | ||
60 | + @Test | ||
61 | + public void testMap() { | ||
62 | + Versioned<String> oldValue = new Versioned<String>("a", 1); | ||
63 | + Versioned<String> newValue = new Versioned<String>("b", 2); | ||
64 | + UpdateResult<String, String> ur1 = | ||
65 | + new UpdateResult<>(true, "foo", "k", oldValue, newValue); | ||
66 | + UpdateResult<Integer, Integer> ur2 = ur1.map(s -> s.length(), s -> s.length()); | ||
67 | + | ||
68 | + assertEquals(ur2.updated(), ur1.updated()); | ||
69 | + assertEquals(ur1.mapName(), ur2.mapName()); | ||
70 | + assertEquals(new Integer(1), ur2.key()); | ||
71 | + assertEquals(oldValue.map(s -> s.length()), ur2.oldValue()); | ||
72 | + assertEquals(newValue.map(s -> s.length()), ur2.newValue()); | ||
73 | + | ||
74 | + UpdateResult<String, String> ur3 = | ||
75 | + new UpdateResult<>(true, "foo", "k", null, newValue); | ||
76 | + UpdateResult<Integer, Integer> ur4 = ur3.map(s -> s.length(), s -> s.length()); | ||
77 | + | ||
78 | + assertEquals(ur3.updated(), ur4.updated()); | ||
79 | + assertEquals(ur3.mapName(), ur4.mapName()); | ||
80 | + assertEquals(new Integer(1), ur4.key()); | ||
81 | + assertNull(ur4.oldValue()); | ||
82 | + assertEquals(newValue.map(s -> s.length()), ur4.newValue()); | ||
83 | + } | ||
84 | +} |
-
Please register or login to post a comment