blob: ab0cd2dc0d2edd3186128686a6bb78d6f080cc94 [file] [log] [blame]
Simon Hunt977aa052016-07-20 17:08:29 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.ui.impl.topo;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21import org.junit.Test;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070022import org.onosproject.net.Annotated;
23import org.onosproject.net.Annotations;
Simon Hunt977aa052016-07-20 17:08:29 -070024import org.onosproject.ui.impl.AbstractUiImplTest;
25import org.onosproject.ui.model.topo.UiNode;
26
27import java.util.List;
28import java.util.Set;
29
30import static org.junit.Assert.assertEquals;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070031import static org.junit.Assert.assertNull;
Simon Hunt977aa052016-07-20 17:08:29 -070032import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT;
33import static org.onosproject.ui.model.topo.UiNode.LAYER_OPTICAL;
34import static org.onosproject.ui.model.topo.UiNode.LAYER_PACKET;
35
36/**
37 * Unit tests for {@link Topo2ViewMessageHandler}.
38 */
39public class Topo2JsonifierTest extends AbstractUiImplTest {
40
41 // mock node class for testing
42 private static class MockNode extends UiNode {
43 private final String id;
44
45 MockNode(String id, String layer) {
46 this.id = id;
47 setLayer(layer);
48 }
49
50 @Override
51 public String idAsString() {
52 return id;
53 }
54
55 @Override
56 public String toString() {
57 return id;
58 }
59 }
60
61 private static final List<String> ALL_TAGS = ImmutableList.of(
62 LAYER_OPTICAL, LAYER_PACKET, LAYER_DEFAULT
63 );
64
65 private static final List<String> PKT_DEF_TAGS = ImmutableList.of(
66 LAYER_PACKET, LAYER_DEFAULT
67 );
68
69 private static final List<String> DEF_TAG_ONLY = ImmutableList.of(
70 LAYER_DEFAULT
71 );
72
73 private static final MockNode NODE_A = new MockNode("A-O", LAYER_OPTICAL);
74 private static final MockNode NODE_B = new MockNode("B-P", LAYER_PACKET);
75 private static final MockNode NODE_C = new MockNode("C-O", LAYER_OPTICAL);
76 private static final MockNode NODE_D = new MockNode("D-D", LAYER_DEFAULT);
77 private static final MockNode NODE_E = new MockNode("E-P", LAYER_PACKET);
78 private static final MockNode NODE_F = new MockNode("F-r", "random");
79
80 private static final Set<MockNode> NODES = ImmutableSet.of(
81 NODE_A, NODE_B, NODE_C, NODE_D, NODE_E, NODE_F
82 );
83
84 private Topo2Jsonifier t2 = new Topo2Jsonifier();
85
86 @Test
87 public void threeLayers() {
88 print("threeLayers()");
89
90 List<Set<UiNode>> result = t2.splitByLayer(ALL_TAGS, NODES);
91 print(result);
92
93 assertEquals("wrong split size", 3, result.size());
94 Set<UiNode> opt = result.get(0);
95 Set<UiNode> pkt = result.get(1);
96 Set<UiNode> def = result.get(2);
97
98 assertEquals("opt bad size", 2, opt.size());
99 assertEquals("missing node A", true, opt.contains(NODE_A));
100 assertEquals("missing node C", true, opt.contains(NODE_C));
101
102 assertEquals("pkt bad size", 2, pkt.size());
103 assertEquals("missing node B", true, pkt.contains(NODE_B));
104 assertEquals("missing node E", true, pkt.contains(NODE_E));
105
106 assertEquals("def bad size", 2, def.size());
107 assertEquals("missing node D", true, def.contains(NODE_D));
108 assertEquals("missing node F", true, def.contains(NODE_F));
109 }
110
111 @Test
112 public void twoLayers() {
113 print("twoLayers()");
114
115 List<Set<UiNode>> result = t2.splitByLayer(PKT_DEF_TAGS, NODES);
116 print(result);
117
118 assertEquals("wrong split size", 2, result.size());
119 Set<UiNode> pkt = result.get(0);
120 Set<UiNode> def = result.get(1);
121
122 assertEquals("pkt bad size", 2, pkt.size());
123 assertEquals("missing node B", true, pkt.contains(NODE_B));
124 assertEquals("missing node E", true, pkt.contains(NODE_E));
125
126 assertEquals("def bad size", 4, def.size());
127 assertEquals("missing node D", true, def.contains(NODE_D));
128 assertEquals("missing node F", true, def.contains(NODE_F));
129 assertEquals("missing node A", true, def.contains(NODE_A));
130 assertEquals("missing node C", true, def.contains(NODE_C));
131 }
132
133 @Test
134 public void oneLayer() {
135 print("oneLayer()");
136
137 List<Set<UiNode>> result = t2.splitByLayer(DEF_TAG_ONLY, NODES);
138 print(result);
139
140 assertEquals("wrong split size", 1, result.size());
141 Set<UiNode> def = result.get(0);
142
143 assertEquals("def bad size", 6, def.size());
144 assertEquals("missing node D", true, def.contains(NODE_D));
145 assertEquals("missing node F", true, def.contains(NODE_F));
146 assertEquals("missing node A", true, def.contains(NODE_A));
147 assertEquals("missing node C", true, def.contains(NODE_C));
148 assertEquals("missing node B", true, def.contains(NODE_B));
149 assertEquals("missing node E", true, def.contains(NODE_E));
150 }
Simon Hunt6a8cb4f2016-08-09 15:08:57 -0700151
152 private static final String K1 = "K1";
153 private static final String K2 = "K2";
154 private static final String K3 = "K3";
155 private static final String K4 = "K4";
156
157 private static final String V1 = "V1";
158 private static final String V2 = "V2";
159 private static final String V3 = "V3";
160
161 private static final Annotations ANNOTS = new Annotations() {
162 @Override
163 public Set<String> keys() {
164 return ImmutableSet.of(K1, K2, K3);
165 }
166
167 @Override
168 public String value(String key) {
169 switch (key) {
170 case K1:
171 return V1;
172 case K2:
173 return V2;
174 case K3:
175 return V3;
176 default:
177 return null;
178 }
179 }
180 };
181
182 private static final Annotated THING = () -> ANNOTS;
183
184 private void verifyValues(List<String> vals, String... exp) {
185 print(vals);
186 if (exp.length == 0) {
187 // don't expect any results
188 assertNull("huh?", vals);
189 } else {
190 assertEquals("wrong list len", exp.length, vals.size());
191
192 for (int i = 0; i < exp.length; i++) {
193 assertEquals("wrong value " + i, exp[i], vals.get(i));
194 }
195 }
196 }
197
198 @Test
199 public void annotValues() {
200 print("annotValues()");
201 verifyValues(t2.getAnnotValues(THING, K1), V1);
202 verifyValues(t2.getAnnotValues(THING, K3, K1), V3, V1);
203 verifyValues(t2.getAnnotValues(THING, K1, K2, K3), V1, V2, V3);
204 verifyValues(t2.getAnnotValues(THING, K1, K4));
205 }
Simon Hunt977aa052016-07-20 17:08:29 -0700206}