Ray Milkey
Committed by Gerrit Code Review

Improve test coverage for ConsistentMapEvent and WallClockTimestamp

Change-Id: Id8276966c227e2a66d40fca9ac43145b8a419982
...@@ -22,7 +22,7 @@ import java.util.Objects; ...@@ -22,7 +22,7 @@ import java.util.Objects;
22 /** 22 /**
23 * Representation of a EventuallyConsistentMap update notification. 23 * Representation of a EventuallyConsistentMap update notification.
24 */ 24 */
25 -public class EventuallyConsistentMapEvent<K, V> { 25 +public final class EventuallyConsistentMapEvent<K, V> {
26 26
27 public enum Type { 27 public enum Type {
28 /** 28 /**
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.service;
17 +
18 +import org.junit.Test;
19 +
20 +import com.google.common.testing.EqualsTester;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
25 +import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
26 +import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
27 +
28 +/**
29 + * Unit tests for the EventuallyConsistentMapEvent class.
30 + */
31 +public class EventuallyConsistentMapEventTest {
32 +
33 + EventuallyConsistentMapEvent<String, String> event1 =
34 + new EventuallyConsistentMapEvent<>("map1", PUT, "k1", "v1");
35 + EventuallyConsistentMapEvent<String, String> event2 =
36 + new EventuallyConsistentMapEvent<>("map1", REMOVE, "k1", "v1");
37 + EventuallyConsistentMapEvent<String, String> sameAsEvent2 =
38 + new EventuallyConsistentMapEvent<>("map1", REMOVE, "k1", "v1");
39 + EventuallyConsistentMapEvent<String, String> event3 =
40 + new EventuallyConsistentMapEvent<>("map1", PUT, "k2", "v1");
41 + EventuallyConsistentMapEvent<String, String> event4 =
42 + new EventuallyConsistentMapEvent<>("map1", PUT, "k1", "v2");
43 + EventuallyConsistentMapEvent<String, String> event5 =
44 + new EventuallyConsistentMapEvent<>("map2", REMOVE, "k1", "v2");
45 +
46 + /**
47 + * Checks the equals(), hashCode() and toString() operations.
48 + */
49 + @Test
50 + public void testEquals() {
51 + new EqualsTester()
52 + .addEqualityGroup(event1)
53 + .addEqualityGroup(event2, sameAsEvent2)
54 + .addEqualityGroup(event3)
55 + .addEqualityGroup(event4)
56 + .addEqualityGroup(event5)
57 + .testEquals();
58 + }
59 +
60 + /**
61 + * Checks that the EventuallyConsistentMapEvent class is immutable.
62 + */
63 + @Test
64 + public void testImmutability() {
65 + assertThatClassIsImmutable(EventuallyConsistentMapEvent.class);
66 + }
67 +
68 + /**
69 + * Checks that construction of the object is correct.
70 + */
71 + @Test
72 + public void testConstruction() {
73 + assertThat(event1.type(), is(PUT));
74 + assertThat(event1.key(), is("k1"));
75 + assertThat(event1.value(), is("v1"));
76 + assertThat(event1.name(), is("map1"));
77 + }
78 +}
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onosproject.store.impl; 16 +package org.onosproject.store.service;
17 17
18 import static org.junit.Assert.assertTrue; 18 import static org.junit.Assert.assertTrue;
19 19
...@@ -24,7 +24,6 @@ import org.onosproject.store.Timestamp; ...@@ -24,7 +24,6 @@ import org.onosproject.store.Timestamp;
24 import org.onlab.util.KryoNamespace; 24 import org.onlab.util.KryoNamespace;
25 25
26 import com.google.common.testing.EqualsTester; 26 import com.google.common.testing.EqualsTester;
27 -import org.onosproject.store.service.WallClockTimestamp;
28 27
29 /** 28 /**
30 * Tests for {@link WallClockTimestamp}. 29 * Tests for {@link WallClockTimestamp}.
...@@ -36,15 +35,20 @@ public class WallClockTimestampTest { ...@@ -36,15 +35,20 @@ public class WallClockTimestampTest {
36 WallClockTimestamp ts1 = new WallClockTimestamp(); 35 WallClockTimestamp ts1 = new WallClockTimestamp();
37 Thread.sleep(50); 36 Thread.sleep(50);
38 WallClockTimestamp ts2 = new WallClockTimestamp(); 37 WallClockTimestamp ts2 = new WallClockTimestamp();
38 + long stamp = System.currentTimeMillis() + 10000;
39 + WallClockTimestamp ts3 = new WallClockTimestamp(stamp);
40 +
39 41
40 assertTrue(ts1.compareTo(ts1) == 0); 42 assertTrue(ts1.compareTo(ts1) == 0);
41 assertTrue(ts2.compareTo(ts1) > 0); 43 assertTrue(ts2.compareTo(ts1) > 0);
42 assertTrue(ts1.compareTo(ts2) < 0); 44 assertTrue(ts1.compareTo(ts2) < 0);
45 + assertTrue(ts3.unixTimestamp() == stamp);
43 } 46 }
44 47
45 @Test 48 @Test
46 public final void testKryoSerializable() { 49 public final void testKryoSerializable() {
47 WallClockTimestamp ts1 = new WallClockTimestamp(); 50 WallClockTimestamp ts1 = new WallClockTimestamp();
51 + WallClockTimestamp ts2 = new WallClockTimestamp(System.currentTimeMillis() + 10000);
48 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); 52 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
49 final KryoNamespace kryos = KryoNamespace.newBuilder() 53 final KryoNamespace kryos = KryoNamespace.newBuilder()
50 .register(WallClockTimestamp.class) 54 .register(WallClockTimestamp.class)
...@@ -55,7 +59,8 @@ public class WallClockTimestampTest { ...@@ -55,7 +59,8 @@ public class WallClockTimestampTest {
55 Timestamp copy = kryos.deserialize(buffer); 59 Timestamp copy = kryos.deserialize(buffer);
56 60
57 new EqualsTester() 61 new EqualsTester()
58 - .addEqualityGroup(ts1, copy) 62 + .addEqualityGroup(ts1, copy)
59 - .testEquals(); 63 + .addEqualityGroup(ts2)
64 + .testEquals();
60 } 65 }
61 } 66 }
......