tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import static com.google.common.collect.ImmutableSet.of; |
| 6 | import static org.junit.Assert.*; |
| 7 | import static org.onlab.onos.net.DefaultAnnotations.builder; |
| 8 | |
| 9 | /** |
| 10 | * Tests of the default annotations. |
| 11 | */ |
| 12 | public class DefaultAnnotationsTest { |
| 13 | |
| 14 | private DefaultAnnotations annotations; |
| 15 | |
| 16 | @Test |
| 17 | public void basics() { |
| 18 | annotations = builder().set("foo", "1").set("bar", "2").build(); |
| 19 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 20 | assertEquals("incorrect value", "1", annotations.value("foo")); |
| 21 | assertEquals("incorrect value", "2", annotations.value("bar")); |
| 22 | } |
| 23 | |
| 24 | @Test |
| 25 | public void empty() { |
| 26 | annotations = builder().build(); |
| 27 | assertTrue("incorrect keys", annotations.keys().isEmpty()); |
| 28 | } |
| 29 | |
| 30 | @Test |
| 31 | public void remove() { |
| 32 | annotations = builder().remove("foo").set("bar", "2").build(); |
| 33 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 34 | assertNull("incorrect value", annotations.value("foo")); |
| 35 | assertEquals("incorrect value", "2", annotations.value("bar")); |
| 36 | } |
| 37 | |
| 38 | @Test |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame^] | 39 | public void union() { |
| 40 | annotations = builder().set("foo", "1").set("bar", "2").remove("buz").build(); |
| 41 | assertEquals("incorrect keys", of("foo", "bar", "buz"), annotations.keys()); |
| 42 | |
| 43 | SparseAnnotations updates = builder().remove("foo").set("bar", "3").set("goo", "4").remove("fuzz").build(); |
| 44 | |
| 45 | SparseAnnotations result = DefaultAnnotations.union(annotations, updates); |
| 46 | |
| 47 | assertTrue("remove instruction in original remains", result.isRemoved("buz")); |
| 48 | assertTrue("remove instruction in update remains", result.isRemoved("fuzz")); |
| 49 | assertEquals("incorrect keys", of("buz", "goo", "bar", "fuzz"), result.keys()); |
| 50 | assertNull("incorrect value", result.value("foo")); |
| 51 | assertEquals("incorrect value", "3", result.value("bar")); |
| 52 | assertEquals("incorrect value", "4", result.value("goo")); |
| 53 | } |
| 54 | |
| 55 | @Test |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 56 | public void merge() { |
| 57 | annotations = builder().set("foo", "1").set("bar", "2").build(); |
| 58 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 59 | |
| 60 | SparseAnnotations updates = builder().remove("foo").set("bar", "3").set("goo", "4").build(); |
| 61 | |
| 62 | annotations = DefaultAnnotations.merge(annotations, updates); |
| 63 | assertEquals("incorrect keys", of("goo", "bar"), annotations.keys()); |
| 64 | assertNull("incorrect value", annotations.value("foo")); |
| 65 | assertEquals("incorrect value", "3", annotations.value("bar")); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void noopMerge() { |
| 70 | annotations = builder().set("foo", "1").set("bar", "2").build(); |
| 71 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 72 | |
| 73 | SparseAnnotations updates = builder().build(); |
| 74 | assertSame("same annotations expected", annotations, |
| 75 | DefaultAnnotations.merge(annotations, updates)); |
| 76 | assertSame("same annotations expected", annotations, |
| 77 | DefaultAnnotations.merge(annotations, null)); |
| 78 | } |
| 79 | |
| 80 | @Test(expected = NullPointerException.class) |
| 81 | public void badMerge() { |
| 82 | DefaultAnnotations.merge(null, null); |
| 83 | } |
| 84 | |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame^] | 85 | } |