Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import static com.google.common.collect.ImmutableSet.of; |
| 21 | import static org.junit.Assert.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import static org.onosproject.net.DefaultAnnotations.builder; |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Tests of the default annotations. |
| 26 | */ |
| 27 | public class DefaultAnnotationsTest { |
| 28 | |
| 29 | private DefaultAnnotations annotations; |
| 30 | |
| 31 | @Test |
| 32 | public void basics() { |
| 33 | annotations = builder().set("foo", "1").set("bar", "2").build(); |
| 34 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 35 | assertEquals("incorrect value", "1", annotations.value("foo")); |
| 36 | assertEquals("incorrect value", "2", annotations.value("bar")); |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void empty() { |
| 41 | annotations = builder().build(); |
| 42 | assertTrue("incorrect keys", annotations.keys().isEmpty()); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void remove() { |
| 47 | annotations = builder().remove("foo").set("bar", "2").build(); |
| 48 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 49 | assertNull("incorrect value", annotations.value("foo")); |
| 50 | assertEquals("incorrect value", "2", annotations.value("bar")); |
| 51 | } |
| 52 | |
| 53 | @Test |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 54 | public void union() { |
| 55 | annotations = builder().set("foo", "1").set("bar", "2").remove("buz").build(); |
| 56 | assertEquals("incorrect keys", of("foo", "bar", "buz"), annotations.keys()); |
| 57 | |
| 58 | SparseAnnotations updates = builder().remove("foo").set("bar", "3").set("goo", "4").remove("fuzz").build(); |
| 59 | |
| 60 | SparseAnnotations result = DefaultAnnotations.union(annotations, updates); |
| 61 | |
| 62 | assertTrue("remove instruction in original remains", result.isRemoved("buz")); |
| 63 | assertTrue("remove instruction in update remains", result.isRemoved("fuzz")); |
| 64 | assertEquals("incorrect keys", of("buz", "goo", "bar", "fuzz"), result.keys()); |
| 65 | assertNull("incorrect value", result.value("foo")); |
| 66 | assertEquals("incorrect value", "3", result.value("bar")); |
| 67 | assertEquals("incorrect value", "4", result.value("goo")); |
| 68 | } |
| 69 | |
| 70 | @Test |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 71 | public void merge() { |
| 72 | annotations = builder().set("foo", "1").set("bar", "2").build(); |
| 73 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 74 | |
| 75 | SparseAnnotations updates = builder().remove("foo").set("bar", "3").set("goo", "4").build(); |
| 76 | |
| 77 | annotations = DefaultAnnotations.merge(annotations, updates); |
| 78 | assertEquals("incorrect keys", of("goo", "bar"), annotations.keys()); |
| 79 | assertNull("incorrect value", annotations.value("foo")); |
| 80 | assertEquals("incorrect value", "3", annotations.value("bar")); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void noopMerge() { |
| 85 | annotations = builder().set("foo", "1").set("bar", "2").build(); |
| 86 | assertEquals("incorrect keys", of("foo", "bar"), annotations.keys()); |
| 87 | |
| 88 | SparseAnnotations updates = builder().build(); |
| 89 | assertSame("same annotations expected", annotations, |
| 90 | DefaultAnnotations.merge(annotations, updates)); |
| 91 | assertSame("same annotations expected", annotations, |
| 92 | DefaultAnnotations.merge(annotations, null)); |
| 93 | } |
| 94 | |
| 95 | @Test(expected = NullPointerException.class) |
| 96 | public void badMerge() { |
| 97 | DefaultAnnotations.merge(null, null); |
| 98 | } |
| 99 | |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 100 | } |