blob: 63a44441f18cfb9ddbc4de3de63e057ae44b229f [file] [log] [blame]
Simon Hunt72297212015-08-25 10:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt72297212015-08-25 10:15:33 -07003 *
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.
Simon Hunt72297212015-08-25 10:15:33 -070015 */
16
17package org.onosproject.ui.topo;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080022import com.google.common.collect.ImmutableSet;
Simon Hunt72297212015-08-25 10:15:33 -070023import org.junit.Test;
Prince Pereira46c82d42016-09-19 13:30:50 +053024import org.onosproject.net.ConnectPoint;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080025import org.onosproject.net.DefaultDevice;
26import org.onosproject.net.DefaultHost;
Prince Pereira46c82d42016-09-19 13:30:50 +053027import org.onosproject.net.DefaultLink;
Simon Hunt72297212015-08-25 10:15:33 -070028import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Host;
31import org.onosproject.net.HostId;
Prince Pereira46c82d42016-09-19 13:30:50 +053032import org.onosproject.net.Link;
Simon Hunt72297212015-08-25 10:15:33 -070033import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.device.DeviceServiceAdapter;
35import org.onosproject.net.host.HostService;
36import org.onosproject.net.host.HostServiceAdapter;
Prince Pereira46c82d42016-09-19 13:30:50 +053037import org.onosproject.net.link.LinkService;
38import org.onosproject.net.link.LinkServiceAdapter;
Simon Hunt72297212015-08-25 10:15:33 -070039
40import static org.junit.Assert.*;
Prince Pereira46c82d42016-09-19 13:30:50 +053041import static org.onosproject.net.Link.Type.DIRECT;
Simon Hunt72297212015-08-25 10:15:33 -070042
43/**
44 * Unit tests for {@link NodeSelection}.
45 */
46public class NodeSelectionTest {
47
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080048 private static class FakeDevice extends DefaultDevice {
Simon Hunt72297212015-08-25 10:15:33 -070049 FakeDevice(DeviceId id) {
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080050 super(null, id, null, null, null, null, null, null);
Simon Hunt72297212015-08-25 10:15:33 -070051 }
52 }
53
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080054 private static class FakeHost extends DefaultHost {
Simon Hunt72297212015-08-25 10:15:33 -070055 FakeHost(HostId id) {
Charles Chancd06c692017-04-27 20:46:06 -070056 super(null, id, null, null, ImmutableSet.of(), ImmutableSet.of(), false);
Simon Hunt72297212015-08-25 10:15:33 -070057 }
58 }
59
Prince Pereira46c82d42016-09-19 13:30:50 +053060 private static class FakeLink extends DefaultLink {
61 FakeLink(ConnectPoint src, ConnectPoint dst) {
62 super(null, src, dst, DIRECT, Link.State.ACTIVE);
63 }
64 }
65
Simon Hunt72297212015-08-25 10:15:33 -070066 private final ObjectMapper mapper = new ObjectMapper();
67
68 private static final String IDS = "ids";
69 private static final String HOVER = "hover";
70
Prince Pereira46c82d42016-09-19 13:30:50 +053071 private static final DeviceId DEVICE_1_ID = DeviceId.deviceId("Device1");
72 private static final DeviceId DEVICE_2_ID = DeviceId.deviceId("Device2");
Simon Hunt72297212015-08-25 10:15:33 -070073 private static final HostId HOST_A_ID = HostId.hostId("aa:aa:aa:aa:aa:aa/1");
74 private static final HostId HOST_B_ID = HostId.hostId("bb:bb:bb:bb:bb:bb/2");
Prince Pereira46c82d42016-09-19 13:30:50 +053075 private static final String LINK_1_ID = "Device1/1-Device2/2";
76 private static final ConnectPoint CP_SRC = ConnectPoint.deviceConnectPoint("Device1/1");
77 private static final ConnectPoint CP_DST = ConnectPoint.deviceConnectPoint("Device2/2");
Simon Hunt72297212015-08-25 10:15:33 -070078
79 private static final Device DEVICE_1 = new FakeDevice(DEVICE_1_ID);
80 private static final Device DEVICE_2 = new FakeDevice(DEVICE_2_ID);
81 private static final Host HOST_A = new FakeHost(HOST_A_ID);
82 private static final Host HOST_B = new FakeHost(HOST_B_ID);
Prince Pereira46c82d42016-09-19 13:30:50 +053083 private static final Link LINK_A = new FakeLink(CP_SRC, CP_DST);
84 private static final Link LINK_B = new FakeLink(CP_DST, CP_SRC);
Simon Hunt72297212015-08-25 10:15:33 -070085
86 // ==================
87 // == FAKE SERVICES
88 private static class FakeDevices extends DeviceServiceAdapter {
89 @Override
90 public Device getDevice(DeviceId deviceId) {
91 if (DEVICE_1_ID.equals(deviceId)) {
92 return DEVICE_1;
93 }
94 if (DEVICE_2_ID.equals(deviceId)) {
95 return DEVICE_2;
96 }
97 return null;
98 }
99 }
100
101 private static class FakeHosts extends HostServiceAdapter {
102 @Override
103 public Host getHost(HostId hostId) {
104 if (HOST_A_ID.equals(hostId)) {
105 return HOST_A;
106 }
107 if (HOST_B_ID.equals(hostId)) {
108 return HOST_B;
109 }
110 return null;
111 }
112 }
113
Prince Pereira46c82d42016-09-19 13:30:50 +0530114 private static class FakeLinks extends LinkServiceAdapter {
115 @Override
116 public Link getLink(ConnectPoint src, ConnectPoint dst) {
117 if (CP_SRC.equals(src) && CP_DST.equals(dst)) {
118 return LINK_A;
119 } else if (CP_SRC.equals(dst) && CP_DST.equals(src)) {
120 return LINK_B;
121 }
122 return null;
123 }
124 }
125
Simon Hunt72297212015-08-25 10:15:33 -0700126 private DeviceService deviceService = new FakeDevices();
127 private HostService hostService = new FakeHosts();
Prince Pereira46c82d42016-09-19 13:30:50 +0530128 private LinkService linkService = new FakeLinks();
Simon Hunt72297212015-08-25 10:15:33 -0700129
130 private NodeSelection ns;
131
132 private ObjectNode objectNode() {
133 return mapper.createObjectNode();
134 }
135
136 private ArrayNode arrayNode() {
137 return mapper.createArrayNode();
138 }
139
140 private NodeSelection createNodeSelection(ObjectNode payload) {
Prince Pereira46c82d42016-09-19 13:30:50 +0530141 return new NodeSelection(payload, deviceService, hostService, linkService);
Simon Hunt72297212015-08-25 10:15:33 -0700142 }
143
144 // selection JSON payload creation methods
145 private ObjectNode emptySelection() {
146 ObjectNode payload = objectNode();
147 ArrayNode ids = arrayNode();
148 payload.set(IDS, ids);
149 return payload;
150 }
151
152 private ObjectNode oneDeviceSelected() {
153 ObjectNode payload = objectNode();
154 ArrayNode ids = arrayNode();
155 payload.set(IDS, ids);
156 ids.add(DEVICE_1_ID.toString());
157 return payload;
158 }
159
160 private ObjectNode oneHostSelected() {
161 ObjectNode payload = objectNode();
162 ArrayNode ids = arrayNode();
163 payload.set(IDS, ids);
164 ids.add(HOST_A_ID.toString());
165 return payload;
166 }
Prince Pereira46c82d42016-09-19 13:30:50 +0530167 private ObjectNode oneLinkSelected() {
168 ObjectNode payload = objectNode();
169 ArrayNode ids = arrayNode();
170 payload.set(IDS, ids);
171 ids.add(LINK_1_ID.toString());
172 return payload;
173 }
Simon Hunt72297212015-08-25 10:15:33 -0700174
175 private ObjectNode twoHostsOneDeviceSelected() {
176 ObjectNode payload = objectNode();
177 ArrayNode ids = arrayNode();
178 payload.set(IDS, ids);
179 ids.add(HOST_A_ID.toString());
180 ids.add(DEVICE_1_ID.toString());
181 ids.add(HOST_B_ID.toString());
182 return payload;
183 }
184
185 private ObjectNode oneHostAndHoveringDeviceSelected() {
186 ObjectNode payload = objectNode();
187 ArrayNode ids = arrayNode();
188 payload.set(IDS, ids);
189 ids.add(HOST_A_ID.toString());
190 payload.put(HOVER, DEVICE_2_ID.toString());
191 return payload;
192 }
193
194 private ObjectNode twoDevicesOneHostAndHoveringHostSelected() {
195 ObjectNode payload = objectNode();
196 ArrayNode ids = arrayNode();
197 payload.set(IDS, ids);
198 ids.add(HOST_A_ID.toString());
199 ids.add(DEVICE_1_ID.toString());
200 ids.add(DEVICE_2_ID.toString());
201 payload.put(HOVER, HOST_B_ID.toString());
202 return payload;
203 }
204
205
206 @Test
207 public void basic() {
208 ns = createNodeSelection(emptySelection());
209 assertEquals("unexpected devices", 0, ns.devices().size());
210 assertEquals("unexpected devices w/hover", 0, ns.devicesWithHover().size());
211 assertEquals("unexpected hosts", 0, ns.hosts().size());
212 assertEquals("unexpected hosts w/hover", 0, ns.hostsWithHover().size());
213 assertTrue("unexpected selection", ns.none());
214 assertNull("hover?", ns.hovered());
215 }
216
217 @Test
218 public void oneDevice() {
219 ns = createNodeSelection(oneDeviceSelected());
220 assertEquals("missing device", 1, ns.devices().size());
221 assertTrue("missing device 1", ns.devices().contains(DEVICE_1));
222 assertEquals("missing device w/hover", 1, ns.devicesWithHover().size());
223 assertTrue("missing device 1 w/hover", ns.devicesWithHover().contains(DEVICE_1));
224 assertEquals("unexpected hosts", 0, ns.hosts().size());
225 assertEquals("unexpected hosts w/hover", 0, ns.hostsWithHover().size());
226 assertFalse("unexpected selection", ns.none());
227 assertNull("hover?", ns.hovered());
228 }
229
230 @Test
231 public void oneHost() {
232 ns = createNodeSelection(oneHostSelected());
233 assertEquals("unexpected devices", 0, ns.devices().size());
234 assertEquals("unexpected devices w/hover", 0, ns.devicesWithHover().size());
235 assertEquals("missing host", 1, ns.hosts().size());
236 assertTrue("missing host A", ns.hosts().contains(HOST_A));
237 assertEquals("missing host w/hover", 1, ns.hostsWithHover().size());
238 assertTrue("missing host A w/hover", ns.hostsWithHover().contains(HOST_A));
239 assertFalse("unexpected selection", ns.none());
240 assertNull("hover?", ns.hovered());
241 }
242
243 @Test
Prince Pereira46c82d42016-09-19 13:30:50 +0530244 public void oneLink() {
245 ns = createNodeSelection(oneLinkSelected());
246 assertEquals("unexpected devices", 0, ns.devices().size());
247 assertEquals("unexpected devices w/hover", 0, ns.devicesWithHover().size());
248 assertEquals("unexpected hosts", 0, ns.hosts().size());
249 assertEquals("unexpected hosts w/hover", 0, ns.hostsWithHover().size());
250 assertEquals("missing link", 1, ns.links().size());
251 assertTrue("missing link A", ns.links().contains(LINK_A));
252 assertEquals("missing link w/hover", 1, ns.linksWithHover().size());
253 assertTrue("missing link A w/hover", ns.linksWithHover().contains(LINK_A));
254 assertFalse("unexpected selection", ns.none());
255 assertNull("hover?", ns.hovered());
256 }
257
258 @Test
Simon Hunt72297212015-08-25 10:15:33 -0700259 public void twoHostsOneDevice() {
260 ns = createNodeSelection(twoHostsOneDeviceSelected());
261 assertEquals("missing device", 1, ns.devices().size());
262 assertTrue("missing device 1", ns.devices().contains(DEVICE_1));
263 assertEquals("missing device w/hover", 1, ns.devicesWithHover().size());
264 assertTrue("missing device 1 w/hover", ns.devicesWithHover().contains(DEVICE_1));
265 assertEquals("unexpected hosts", 2, ns.hosts().size());
266 assertTrue("missing host A", ns.hosts().contains(HOST_A));
267 assertTrue("missing host B", ns.hosts().contains(HOST_B));
268 assertEquals("unexpected hosts w/hover", 2, ns.hostsWithHover().size());
269 assertTrue("missing host A w/hover", ns.hostsWithHover().contains(HOST_A));
270 assertTrue("missing host B w/hover", ns.hostsWithHover().contains(HOST_B));
271 assertFalse("unexpected selection", ns.none());
272 assertNull("hover?", ns.hovered());
273 }
274
275 @Test
276 public void oneHostAndHoveringDevice() {
277 ns = createNodeSelection(oneHostAndHoveringDeviceSelected());
278 assertEquals("unexpected devices", 0, ns.devices().size());
279 assertEquals("unexpected devices w/hover", 1, ns.devicesWithHover().size());
280 assertTrue("missing device 2 w/hover", ns.devicesWithHover().contains(DEVICE_2));
281 assertEquals("missing host", 1, ns.hosts().size());
282 assertTrue("missing host A", ns.hosts().contains(HOST_A));
283 assertEquals("missing host w/hover", 1, ns.hostsWithHover().size());
284 assertTrue("missing host A w/hover", ns.hostsWithHover().contains(HOST_A));
285 assertFalse("unexpected selection", ns.none());
286 assertEquals("missing hover device 2", DEVICE_2, ns.hovered());
287 }
288
289 @Test
290 public void twoDevicesOneHostAndHoveringHost() {
291 ns = createNodeSelection(twoDevicesOneHostAndHoveringHostSelected());
292 assertEquals("missing devices", 2, ns.devices().size());
293 assertTrue("missing device 1", ns.devices().contains(DEVICE_1));
294 assertTrue("missing device 2", ns.devices().contains(DEVICE_2));
295 assertEquals("missing devices w/hover", 2, ns.devicesWithHover().size());
296 assertTrue("missing device 1 w/hover", ns.devicesWithHover().contains(DEVICE_1));
297 assertTrue("missing device 2 w/hover", ns.devicesWithHover().contains(DEVICE_2));
298 assertEquals("missing host", 1, ns.hosts().size());
299 assertTrue("missing host A", ns.hosts().contains(HOST_A));
300 assertEquals("missing host w/hover", 2, ns.hostsWithHover().size());
301 assertTrue("missing host A w/hover", ns.hostsWithHover().contains(HOST_A));
302 assertTrue("missing host B w/hover", ns.hostsWithHover().contains(HOST_B));
303 assertFalse("unexpected selection", ns.none());
304 assertEquals("missing hover host B", HOST_B, ns.hovered());
305 }
306}