blob: 1bac285d86a3255dc6c231e779b613eb8dea4097 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom5a9383a2014-10-02 07:33:52 -070017
18import org.junit.Test;
19
20import static com.google.common.collect.ImmutableSet.of;
21import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import static org.onosproject.net.DefaultAnnotations.builder;
tom5a9383a2014-10-02 07:33:52 -070023
24/**
25 * Tests of the default annotations.
26 */
27public 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 HIGUCHI885be1d2014-10-04 21:47:26 -070054 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
tom5a9383a2014-10-02 07:33:52 -070071 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 HIGUCHI885be1d2014-10-04 21:47:26 -0700100}