Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -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. |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.topo; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DefaultDevice; |
| 26 | import org.onosproject.net.DefaultHost; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 27 | import org.onosproject.net.DefaultLink; |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Host; |
| 31 | import org.onosproject.net.HostId; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 32 | import org.onosproject.net.Link; |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 35 | import org.onosproject.net.host.HostService; |
| 36 | import org.onosproject.net.host.HostServiceAdapter; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 37 | import org.onosproject.net.link.LinkService; |
| 38 | import org.onosproject.net.link.LinkServiceAdapter; |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 39 | |
| 40 | import static org.junit.Assert.*; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 41 | import static org.onosproject.net.Link.Type.DIRECT; |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Unit tests for {@link NodeSelection}. |
| 45 | */ |
| 46 | public class NodeSelectionTest { |
| 47 | |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 48 | private static class FakeDevice extends DefaultDevice { |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 49 | FakeDevice(DeviceId id) { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 50 | super(null, id, null, null, null, null, null, null); |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 54 | private static class FakeHost extends DefaultHost { |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 55 | FakeHost(HostId id) { |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 56 | super(null, id, null, null, null, ImmutableSet.of()); |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 60 | 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 Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 66 | private final ObjectMapper mapper = new ObjectMapper(); |
| 67 | |
| 68 | private static final String IDS = "ids"; |
| 69 | private static final String HOVER = "hover"; |
| 70 | |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 71 | private static final DeviceId DEVICE_1_ID = DeviceId.deviceId("Device1"); |
| 72 | private static final DeviceId DEVICE_2_ID = DeviceId.deviceId("Device2"); |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 73 | 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 75 | 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 Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 78 | |
| 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 83 | 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 Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 85 | |
| 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 114 | 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 Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 126 | private DeviceService deviceService = new FakeDevices(); |
| 127 | private HostService hostService = new FakeHosts(); |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 128 | private LinkService linkService = new FakeLinks(); |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 129 | |
| 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 141 | return new NodeSelection(payload, deviceService, hostService, linkService); |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 142 | } |
| 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 167 | 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 Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 174 | |
| 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 244 | 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 Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 259 | 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 | } |