Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | |
| 23 | import org.junit.Test; |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 24 | import org.onosproject.store.LogicalTimestamp; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 25 | import org.onosproject.store.Timestamp; |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 26 | import org.onosproject.store.primitives.impl.MapValue.Digest; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Unit tests for MapValue. |
| 30 | */ |
| 31 | public class MapValueTest { |
| 32 | |
| 33 | @Test |
| 34 | public void testConstruction() { |
| 35 | Timestamp ts = new LogicalTimestamp(10); |
| 36 | MapValue<String> mv = new MapValue<>("foo", ts); |
| 37 | assertEquals("foo", mv.get()); |
| 38 | assertEquals(ts, mv.timestamp()); |
| 39 | assertTrue(mv.isAlive()); |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void testDigest() { |
| 44 | Timestamp ts = new LogicalTimestamp(10); |
| 45 | MapValue<String> mv = new MapValue<>("foo", ts); |
| 46 | Digest actual = mv.digest(); |
| 47 | Digest expected = new MapValue.Digest(ts, false); |
| 48 | assertEquals(actual, expected); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testComparison() { |
| 53 | Timestamp ts1 = new LogicalTimestamp(9); |
| 54 | Timestamp ts2 = new LogicalTimestamp(10); |
| 55 | Timestamp ts3 = new LogicalTimestamp(11); |
| 56 | MapValue<String> mv1 = new MapValue<>("foo", ts1); |
| 57 | MapValue<String> mv2 = new MapValue<>("foo", ts2); |
| 58 | MapValue<String> mv3 = new MapValue<>("foo", ts3); |
| 59 | assertTrue(mv2.isNewerThan(mv1)); |
| 60 | assertFalse(mv1.isNewerThan(mv3)); |
| 61 | |
| 62 | assertTrue(mv3.isNewerThan(ts2)); |
| 63 | assertFalse(mv1.isNewerThan(ts2)); |
| 64 | |
| 65 | assertTrue(mv1.compareTo(mv2) < 0); |
| 66 | assertTrue(mv1.compareTo(mv1) == 0); |
| 67 | assertTrue(mv3.compareTo(mv2) > 0); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testTombstone() { |
| 72 | Timestamp ts1 = new LogicalTimestamp(9); |
| 73 | MapValue<String> mv = MapValue.tombstone(ts1); |
| 74 | assertTrue(mv.isTombstone()); |
| 75 | assertFalse(mv.isAlive()); |
| 76 | assertNull(mv.get()); |
| 77 | assertEquals(ts1, mv.timestamp()); |
| 78 | } |
| 79 | } |