ONOS-792 Implement Unit Tests for Links and Devices REST APIs
Also added a missing attribute to the Devices codec Change-Id: I824a3559ca6278297a52169249f413c850204a9b
Showing
3 changed files
with
746 additions
and
1 deletions
| ... | @@ -33,12 +33,14 @@ public class DeviceCodec extends AnnotatedCodec<Device> { | ... | @@ -33,12 +33,14 @@ public class DeviceCodec extends AnnotatedCodec<Device> { |
| 33 | DeviceService service = context.get(DeviceService.class); | 33 | DeviceService service = context.get(DeviceService.class); |
| 34 | ObjectNode result = context.mapper().createObjectNode() | 34 | ObjectNode result = context.mapper().createObjectNode() |
| 35 | .put("id", device.id().toString()) | 35 | .put("id", device.id().toString()) |
| 36 | + .put("type", device.type().name()) | ||
| 36 | .put("available", service.isAvailable(device.id())) | 37 | .put("available", service.isAvailable(device.id())) |
| 37 | .put("role", service.getRole(device.id()).toString()) | 38 | .put("role", service.getRole(device.id()).toString()) |
| 38 | .put("mfr", device.manufacturer()) | 39 | .put("mfr", device.manufacturer()) |
| 39 | .put("hw", device.hwVersion()) | 40 | .put("hw", device.hwVersion()) |
| 40 | .put("sw", device.swVersion()) | 41 | .put("sw", device.swVersion()) |
| 41 | - .put("serial", device.serialNumber()); | 42 | + .put("serial", device.serialNumber()) |
| 43 | + .put("chassisId", device.chassisId().toString()); | ||
| 42 | return annotate(result, device, context); | 44 | return annotate(result, device, context); |
| 43 | } | 45 | } |
| 44 | 46 | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2015 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 | +package org.onosproject.rest; | ||
| 17 | + | ||
| 18 | +import java.util.List; | ||
| 19 | + | ||
| 20 | +import org.hamcrest.Description; | ||
| 21 | +import org.hamcrest.TypeSafeMatcher; | ||
| 22 | +import org.junit.After; | ||
| 23 | +import org.junit.Before; | ||
| 24 | +import org.junit.Test; | ||
| 25 | +import org.onlab.osgi.ServiceDirectory; | ||
| 26 | +import org.onlab.osgi.TestServiceDirectory; | ||
| 27 | +import org.onlab.rest.BaseResource; | ||
| 28 | +import org.onosproject.codec.CodecService; | ||
| 29 | +import org.onosproject.codec.impl.CodecManager; | ||
| 30 | +import org.onosproject.net.DefaultPort; | ||
| 31 | +import org.onosproject.net.Device; | ||
| 32 | +import org.onosproject.net.DeviceId; | ||
| 33 | +import org.onosproject.net.MastershipRole; | ||
| 34 | +import org.onosproject.net.Port; | ||
| 35 | +import org.onosproject.net.device.DeviceService; | ||
| 36 | + | ||
| 37 | +import com.eclipsesource.json.JsonArray; | ||
| 38 | +import com.eclipsesource.json.JsonObject; | ||
| 39 | +import com.google.common.collect.ImmutableList; | ||
| 40 | +import com.sun.jersey.api.client.UniformInterfaceException; | ||
| 41 | +import com.sun.jersey.api.client.WebResource; | ||
| 42 | +import com.sun.jersey.test.framework.JerseyTest; | ||
| 43 | + | ||
| 44 | +import static org.easymock.EasyMock.createMock; | ||
| 45 | +import static org.easymock.EasyMock.expect; | ||
| 46 | +import static org.easymock.EasyMock.isA; | ||
| 47 | +import static org.easymock.EasyMock.replay; | ||
| 48 | +import static org.easymock.EasyMock.verify; | ||
| 49 | +import static org.hamcrest.Matchers.containsString; | ||
| 50 | +import static org.hamcrest.Matchers.equalTo; | ||
| 51 | +import static org.hamcrest.Matchers.hasSize; | ||
| 52 | +import static org.hamcrest.Matchers.is; | ||
| 53 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 54 | +import static org.junit.Assert.assertThat; | ||
| 55 | +import static org.junit.Assert.fail; | ||
| 56 | +import static org.onosproject.net.NetTestTools.device; | ||
| 57 | +import static org.onosproject.net.NetTestTools.did; | ||
| 58 | +import static org.onosproject.net.PortNumber.portNumber; | ||
| 59 | + | ||
| 60 | +/** | ||
| 61 | + * Unit tests for devices REST APIs. | ||
| 62 | + */ | ||
| 63 | +public class DevicesResourceTest extends JerseyTest { | ||
| 64 | + DeviceService mockDeviceService; | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * Constructs the test. | ||
| 68 | + */ | ||
| 69 | + public DevicesResourceTest() { | ||
| 70 | + super("org.onosproject.rest"); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Hamcrest matcher to check that an device representation in JSON matches | ||
| 75 | + * the actual device. | ||
| 76 | + */ | ||
| 77 | + public static class DeviceJsonMatcher extends TypeSafeMatcher<JsonObject> { | ||
| 78 | + private final Device device; | ||
| 79 | + private String reason = ""; | ||
| 80 | + | ||
| 81 | + public DeviceJsonMatcher(Device deviceValue) { | ||
| 82 | + device = deviceValue; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + @Override | ||
| 86 | + public boolean matchesSafely(JsonObject jsonDevice) { | ||
| 87 | + // check id | ||
| 88 | + String jsonId = jsonDevice.get("id").asString(); | ||
| 89 | + if (!jsonId.equals(device.id().toString())) { | ||
| 90 | + reason = "id " + device.id().toString(); | ||
| 91 | + return false; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + // check type | ||
| 95 | + String jsonType = jsonDevice.get("type").asString(); | ||
| 96 | + if (!jsonType.equals(device.type().toString())) { | ||
| 97 | + reason = "appId " + device.type().toString(); | ||
| 98 | + return false; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + // check manufacturer | ||
| 102 | + String jsonManufacturer = jsonDevice.get("mfr").asString(); | ||
| 103 | + if (!jsonManufacturer.equals(device.manufacturer())) { | ||
| 104 | + reason = "manufacturer " + device.manufacturer(); | ||
| 105 | + return false; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + // check HW version field | ||
| 109 | + String jsonHwVersion = jsonDevice.get("hw").asString(); | ||
| 110 | + if (!jsonHwVersion.equals(device.hwVersion())) { | ||
| 111 | + reason = "hw Version " + device.hwVersion(); | ||
| 112 | + return false; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + // check SW version field | ||
| 116 | + String jsonSwVersion = jsonDevice.get("sw").asString(); | ||
| 117 | + if (!jsonSwVersion.equals(device.swVersion())) { | ||
| 118 | + reason = "sw Version " + device.swVersion(); | ||
| 119 | + return false; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + // check serial number field | ||
| 123 | + String jsonSerialNumber = jsonDevice.get("serial").asString(); | ||
| 124 | + if (!jsonSerialNumber.equals(device.serialNumber())) { | ||
| 125 | + reason = "serial number " + device.serialNumber(); | ||
| 126 | + return false; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + // check chassis id field | ||
| 130 | + String jsonChassisId = jsonDevice.get("chassisId").asString(); | ||
| 131 | + if (!jsonChassisId.equals(device.chassisId().toString())) { | ||
| 132 | + reason = "Chassis id " + device.chassisId().toString(); | ||
| 133 | + return false; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + return true; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + @Override | ||
| 140 | + public void describeTo(Description description) { | ||
| 141 | + description.appendText(reason); | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + /** | ||
| 146 | + * Factory to allocate an device matcher. | ||
| 147 | + * | ||
| 148 | + * @param device device object we are looking for | ||
| 149 | + * @return matcher | ||
| 150 | + */ | ||
| 151 | + private static DeviceJsonMatcher matchesDevice(Device device) { | ||
| 152 | + return new DeviceJsonMatcher(device); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + /** | ||
| 156 | + * Hamcrest matcher to check that an device is represented properly in a JSON | ||
| 157 | + * array of devices. | ||
| 158 | + */ | ||
| 159 | + private static class DeviceJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { | ||
| 160 | + private final Device device; | ||
| 161 | + private String reason = ""; | ||
| 162 | + | ||
| 163 | + public DeviceJsonArrayMatcher(Device deviceValue) { | ||
| 164 | + device = deviceValue; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + @Override | ||
| 168 | + public boolean matchesSafely(JsonArray json) { | ||
| 169 | + final int minExpectedAttributes = 9; | ||
| 170 | + final int maxExpectedAttributes = 10; | ||
| 171 | + | ||
| 172 | + boolean deviceFound = false; | ||
| 173 | + | ||
| 174 | + for (int jsonDeviceIndex = 0; jsonDeviceIndex < json.size(); | ||
| 175 | + jsonDeviceIndex++) { | ||
| 176 | + | ||
| 177 | + JsonObject jsonDevice = json.get(jsonDeviceIndex).asObject(); | ||
| 178 | + | ||
| 179 | + if (jsonDevice.names().size() < minExpectedAttributes || | ||
| 180 | + jsonDevice.names().size() > maxExpectedAttributes) { | ||
| 181 | + reason = "Found a device with the wrong number of attributes"; | ||
| 182 | + return false; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + String jsonDeviceId = jsonDevice.get("id").asString(); | ||
| 186 | + if (jsonDeviceId.equals(device.id().toString())) { | ||
| 187 | + deviceFound = true; | ||
| 188 | + | ||
| 189 | + // We found the correct device, check attribute values | ||
| 190 | + assertThat(jsonDevice, matchesDevice(device)); | ||
| 191 | + } | ||
| 192 | + } | ||
| 193 | + if (!deviceFound) { | ||
| 194 | + reason = "Device with id " + device.id().toString() + " not found"; | ||
| 195 | + return false; | ||
| 196 | + } else { | ||
| 197 | + return true; | ||
| 198 | + } | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + @Override | ||
| 202 | + public void describeTo(Description description) { | ||
| 203 | + description.appendText(reason); | ||
| 204 | + } | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + /** | ||
| 208 | + * Factory to allocate an device array matcher. | ||
| 209 | + * | ||
| 210 | + * @param device device object we are looking for | ||
| 211 | + * @return matcher | ||
| 212 | + */ | ||
| 213 | + private static DeviceJsonArrayMatcher hasDevice(Device device) { | ||
| 214 | + return new DeviceJsonArrayMatcher(device); | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + @Before | ||
| 218 | + public void setUp() { | ||
| 219 | + mockDeviceService = createMock(DeviceService.class); | ||
| 220 | + | ||
| 221 | + expect(mockDeviceService.isAvailable(isA(DeviceId.class))) | ||
| 222 | + .andReturn(true) | ||
| 223 | + .anyTimes(); | ||
| 224 | + expect(mockDeviceService.getRole(isA(DeviceId.class))) | ||
| 225 | + .andReturn(MastershipRole.MASTER) | ||
| 226 | + .anyTimes(); | ||
| 227 | + | ||
| 228 | + // Register the services needed for the test | ||
| 229 | + CodecManager codecService = new CodecManager(); | ||
| 230 | + codecService.activate(); | ||
| 231 | + ServiceDirectory testDirectory = | ||
| 232 | + new TestServiceDirectory() | ||
| 233 | + .add(DeviceService.class, mockDeviceService) | ||
| 234 | + .add(CodecService.class, codecService); | ||
| 235 | + | ||
| 236 | + BaseResource.setServiceDirectory(testDirectory); | ||
| 237 | + | ||
| 238 | + | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + @After | ||
| 242 | + public void tearDown() throws Exception { | ||
| 243 | + super.tearDown(); | ||
| 244 | + verify(mockDeviceService); | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + /** | ||
| 248 | + * Tests the result of the rest api GET when there are no devices. | ||
| 249 | + */ | ||
| 250 | + @Test | ||
| 251 | + public void testDevicesEmptyArray() { | ||
| 252 | + expect(mockDeviceService.getDevices()).andReturn(ImmutableList.of()); | ||
| 253 | + replay(mockDeviceService); | ||
| 254 | + | ||
| 255 | + WebResource rs = resource(); | ||
| 256 | + String response = rs.path("devices").get(String.class); | ||
| 257 | + assertThat(response, is("{\"devices\":[]}")); | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + /** | ||
| 261 | + * Tests the result of the rest api GET when there are devices present. | ||
| 262 | + */ | ||
| 263 | + @Test | ||
| 264 | + public void testDevices() { | ||
| 265 | + Device device1 = device("dev1"); | ||
| 266 | + Device device2 = device("dev2"); | ||
| 267 | + Device device3 = device("dev3"); | ||
| 268 | + | ||
| 269 | + expect(mockDeviceService.getDevices()) | ||
| 270 | + .andReturn(ImmutableList.of(device1, device2, device3)) | ||
| 271 | + .anyTimes(); | ||
| 272 | + | ||
| 273 | + replay(mockDeviceService); | ||
| 274 | + | ||
| 275 | + WebResource rs = resource(); | ||
| 276 | + String response = rs.path("devices").get(String.class); | ||
| 277 | + assertThat(response, containsString("{\"devices\":[")); | ||
| 278 | + | ||
| 279 | + JsonObject result = JsonObject.readFrom(response); | ||
| 280 | + assertThat(result, notNullValue()); | ||
| 281 | + | ||
| 282 | + assertThat(result.names(), hasSize(1)); | ||
| 283 | + assertThat(result.names().get(0), is("devices")); | ||
| 284 | + | ||
| 285 | + JsonArray jsonDevices = result.get("devices").asArray(); | ||
| 286 | + assertThat(jsonDevices, notNullValue()); | ||
| 287 | + assertThat(jsonDevices.size(), is(3)); | ||
| 288 | + | ||
| 289 | + assertThat(jsonDevices, hasDevice(device1)); | ||
| 290 | + assertThat(jsonDevices, hasDevice(device2)); | ||
| 291 | + assertThat(jsonDevices, hasDevice(device3)); | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + /** | ||
| 295 | + * Tests the result of a rest api GET for a single device. | ||
| 296 | + */ | ||
| 297 | + @Test | ||
| 298 | + public void testDevicesSingle() { | ||
| 299 | + | ||
| 300 | + String deviceIdString = "testdevice"; | ||
| 301 | + DeviceId deviceId = did(deviceIdString); | ||
| 302 | + Device device = device(deviceIdString); | ||
| 303 | + | ||
| 304 | + expect(mockDeviceService.getDevice(deviceId)) | ||
| 305 | + .andReturn(device) | ||
| 306 | + .once(); | ||
| 307 | + replay(mockDeviceService); | ||
| 308 | + | ||
| 309 | + WebResource rs = resource(); | ||
| 310 | + String response = rs.path("devices/" + deviceId).get(String.class); | ||
| 311 | + JsonObject result = JsonObject.readFrom(response); | ||
| 312 | + assertThat(result, matchesDevice(device)); | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + /** | ||
| 316 | + * Tests the result of a rest api GET for the ports of a single device. | ||
| 317 | + */ | ||
| 318 | + @Test | ||
| 319 | + public void testDeviceAndPorts() { | ||
| 320 | + | ||
| 321 | + String deviceIdString = "testdevice"; | ||
| 322 | + DeviceId deviceId = did(deviceIdString); | ||
| 323 | + Device device = device(deviceIdString); | ||
| 324 | + | ||
| 325 | + Port port1 = new DefaultPort(device, portNumber(1), true); | ||
| 326 | + Port port2 = new DefaultPort(device, portNumber(2), true); | ||
| 327 | + Port port3 = new DefaultPort(device, portNumber(3), true); | ||
| 328 | + List<Port> ports = ImmutableList.of(port1, port2, port3); | ||
| 329 | + | ||
| 330 | + expect(mockDeviceService.getDevice(deviceId)) | ||
| 331 | + .andReturn(device) | ||
| 332 | + .once(); | ||
| 333 | + | ||
| 334 | + expect(mockDeviceService.getPorts(deviceId)) | ||
| 335 | + .andReturn(ports) | ||
| 336 | + .once(); | ||
| 337 | + replay(mockDeviceService); | ||
| 338 | + | ||
| 339 | + WebResource rs = resource(); | ||
| 340 | + String response = | ||
| 341 | + rs.path("devices/" + deviceId + "/ports") | ||
| 342 | + .get(String.class); | ||
| 343 | + JsonObject result = JsonObject.readFrom(response); | ||
| 344 | + assertThat(result, matchesDevice(device)); | ||
| 345 | + | ||
| 346 | + JsonArray jsonPorts = result.get("ports").asArray(); | ||
| 347 | + assertThat(jsonPorts.size(), is(3)); | ||
| 348 | + for (int portIndex = 0; portIndex < jsonPorts.size(); portIndex++) { | ||
| 349 | + JsonObject jsonPort = jsonPorts.get(portIndex).asObject(); | ||
| 350 | + | ||
| 351 | + assertThat(jsonPort.size(), is(4)); | ||
| 352 | + assertThat(jsonPort.get("port").asString(), | ||
| 353 | + is(Integer.toString(portIndex + 1))); | ||
| 354 | + assertThat(jsonPort.get("isEnabled").asBoolean(), | ||
| 355 | + is(true)); | ||
| 356 | + assertThat(jsonPort.get("type").asString(), | ||
| 357 | + equalTo("copper")); | ||
| 358 | + assertThat(jsonPort.get("portSpeed").asLong(), | ||
| 359 | + is(1000L)); | ||
| 360 | + } | ||
| 361 | + } | ||
| 362 | + | ||
| 363 | + /** | ||
| 364 | + * Tests that a fetch of a non-existent device object throws an exception. | ||
| 365 | + */ | ||
| 366 | + @Test | ||
| 367 | + public void testBadGet() { | ||
| 368 | + | ||
| 369 | + expect(mockDeviceService.getDevice(isA(DeviceId.class))) | ||
| 370 | + .andReturn(null) | ||
| 371 | + .anyTimes(); | ||
| 372 | + replay(mockDeviceService); | ||
| 373 | + | ||
| 374 | + WebResource rs = resource(); | ||
| 375 | + try { | ||
| 376 | + rs.path("devices/0").get(String.class); | ||
| 377 | + fail("Fetch of non-existent device did not throw an exception"); | ||
| 378 | + } catch (UniformInterfaceException ex) { | ||
| 379 | + assertThat(ex.getMessage(), | ||
| 380 | + containsString("returned a response status of")); | ||
| 381 | + } | ||
| 382 | + } | ||
| 383 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 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 | +package org.onosproject.rest; | ||
| 17 | + | ||
| 18 | +import org.hamcrest.Description; | ||
| 19 | +import org.hamcrest.TypeSafeMatcher; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.osgi.ServiceDirectory; | ||
| 24 | +import org.onlab.osgi.TestServiceDirectory; | ||
| 25 | +import org.onlab.rest.BaseResource; | ||
| 26 | +import org.onosproject.codec.CodecService; | ||
| 27 | +import org.onosproject.codec.impl.CodecManager; | ||
| 28 | +import org.onosproject.net.ConnectPoint; | ||
| 29 | +import org.onosproject.net.DeviceId; | ||
| 30 | +import org.onosproject.net.Link; | ||
| 31 | +import org.onosproject.net.link.LinkService; | ||
| 32 | + | ||
| 33 | +import com.eclipsesource.json.JsonArray; | ||
| 34 | +import com.eclipsesource.json.JsonObject; | ||
| 35 | +import com.google.common.collect.ImmutableList; | ||
| 36 | +import com.google.common.collect.ImmutableSet; | ||
| 37 | +import com.sun.jersey.api.client.WebResource; | ||
| 38 | +import com.sun.jersey.test.framework.JerseyTest; | ||
| 39 | + | ||
| 40 | +import static org.easymock.EasyMock.createMock; | ||
| 41 | +import static org.easymock.EasyMock.expect; | ||
| 42 | +import static org.easymock.EasyMock.isA; | ||
| 43 | +import static org.easymock.EasyMock.replay; | ||
| 44 | +import static org.easymock.EasyMock.verify; | ||
| 45 | +import static org.hamcrest.Matchers.containsString; | ||
| 46 | +import static org.hamcrest.Matchers.hasSize; | ||
| 47 | +import static org.hamcrest.Matchers.is; | ||
| 48 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 49 | +import static org.junit.Assert.assertThat; | ||
| 50 | +import static org.onosproject.net.NetTestTools.link; | ||
| 51 | + | ||
| 52 | +/** | ||
| 53 | + * Unit tests for links REST APIs. | ||
| 54 | + */ | ||
| 55 | +public class LinksResourceTest extends JerseyTest { | ||
| 56 | + LinkService mockLinkService; | ||
| 57 | + | ||
| 58 | + Link link1 = link("src1", 1, "dst1", 1); | ||
| 59 | + Link link2 = link("src2", 2, "dst2", 2); | ||
| 60 | + Link link3 = link("src3", 3, "dst3", 3); | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Constructs the test. | ||
| 64 | + */ | ||
| 65 | + public LinksResourceTest() { | ||
| 66 | + super("org.onosproject.rest"); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Hamcrest matcher to check that an link representation in JSON matches | ||
| 71 | + * the actual link. | ||
| 72 | + */ | ||
| 73 | + public static class LinkJsonMatcher extends TypeSafeMatcher<JsonObject> { | ||
| 74 | + private final Link link; | ||
| 75 | + private String reason = ""; | ||
| 76 | + | ||
| 77 | + public LinkJsonMatcher(Link linkValue) { | ||
| 78 | + link = linkValue; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public boolean matchesSafely(JsonObject jsonLink) { | ||
| 83 | + JsonObject jsonSrc = jsonLink.get("src").asObject(); | ||
| 84 | + String jsonSrcDevice = jsonSrc.get("device").asString(); | ||
| 85 | + String jsonSrcPort = jsonSrc.get("port").asString(); | ||
| 86 | + | ||
| 87 | + JsonObject jsonDst = jsonLink.get("dst").asObject(); | ||
| 88 | + String jsonDstDevice = jsonDst.get("device").asString(); | ||
| 89 | + String jsonDstPort = jsonDst.get("port").asString(); | ||
| 90 | + | ||
| 91 | + return jsonSrcDevice.equals(link.src().deviceId().toString()) && | ||
| 92 | + jsonSrcPort.equals(link.src().port().toString()) && | ||
| 93 | + jsonDstDevice.equals(link.dst().deviceId().toString()) && | ||
| 94 | + jsonDstPort.equals(link.dst().port().toString()); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + @Override | ||
| 98 | + public void describeTo(Description description) { | ||
| 99 | + description.appendText(reason); | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * Factory to allocate an link matcher. | ||
| 105 | + * | ||
| 106 | + * @param link link object we are looking for | ||
| 107 | + * @return matcher | ||
| 108 | + */ | ||
| 109 | + private static LinkJsonMatcher matchesLink(Link link) { | ||
| 110 | + return new LinkJsonMatcher(link); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + /** | ||
| 114 | + * Hamcrest matcher to check that an link is represented properly in a JSON | ||
| 115 | + * array of links. | ||
| 116 | + */ | ||
| 117 | + private static class LinkJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { | ||
| 118 | + private final Link link; | ||
| 119 | + private String reason = ""; | ||
| 120 | + | ||
| 121 | + public LinkJsonArrayMatcher(Link linkValue) { | ||
| 122 | + link = linkValue; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + @Override | ||
| 126 | + public boolean matchesSafely(JsonArray json) { | ||
| 127 | + final int expectedAttributes = 2; | ||
| 128 | + | ||
| 129 | + for (int jsonLinkIndex = 0; jsonLinkIndex < json.size(); | ||
| 130 | + jsonLinkIndex++) { | ||
| 131 | + | ||
| 132 | + JsonObject jsonLink = json.get(jsonLinkIndex).asObject(); | ||
| 133 | + | ||
| 134 | + if (jsonLink.names().size() != expectedAttributes) { | ||
| 135 | + reason = "Found a link with the wrong number of attributes"; | ||
| 136 | + return false; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + if (matchesLink(link).matchesSafely(jsonLink)) { | ||
| 140 | + return true; | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + return false; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + @Override | ||
| 147 | + public void describeTo(Description description) { | ||
| 148 | + description.appendText(reason); | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + /** | ||
| 153 | + * Factory to allocate an link array matcher. | ||
| 154 | + * | ||
| 155 | + * @param link link object we are looking for | ||
| 156 | + * @return matcher | ||
| 157 | + */ | ||
| 158 | + private static LinkJsonArrayMatcher hasLink(Link link) { | ||
| 159 | + return new LinkJsonArrayMatcher(link); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + | ||
| 163 | + @Before | ||
| 164 | + public void setUp() { | ||
| 165 | + mockLinkService = createMock(LinkService.class); | ||
| 166 | + | ||
| 167 | + // Register the services needed for the test | ||
| 168 | + CodecManager codecService = new CodecManager(); | ||
| 169 | + codecService.activate(); | ||
| 170 | + ServiceDirectory testDirectory = | ||
| 171 | + new TestServiceDirectory() | ||
| 172 | + .add(LinkService.class, mockLinkService) | ||
| 173 | + .add(CodecService.class, codecService); | ||
| 174 | + | ||
| 175 | + BaseResource.setServiceDirectory(testDirectory); | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + @After | ||
| 179 | + public void tearDown() throws Exception { | ||
| 180 | + super.tearDown(); | ||
| 181 | + verify(mockLinkService); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + /** | ||
| 185 | + * Tests the result of the rest api GET when there are no links. | ||
| 186 | + */ | ||
| 187 | + @Test | ||
| 188 | + public void testLinksEmptyArray() { | ||
| 189 | + expect(mockLinkService.getLinks()).andReturn(ImmutableList.of()); | ||
| 190 | + replay(mockLinkService); | ||
| 191 | + | ||
| 192 | + WebResource rs = resource(); | ||
| 193 | + String response = rs.path("links").get(String.class); | ||
| 194 | + assertThat(response, is("{\"links\":[]}")); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + /** | ||
| 198 | + * Tests the result of the rest api GET when there are links present. | ||
| 199 | + */ | ||
| 200 | + @Test | ||
| 201 | + public void testLinks() { | ||
| 202 | + expect(mockLinkService.getLinks()) | ||
| 203 | + .andReturn(ImmutableList.of(link1, link2, link3)) | ||
| 204 | + .anyTimes(); | ||
| 205 | + | ||
| 206 | + replay(mockLinkService); | ||
| 207 | + | ||
| 208 | + WebResource rs = resource(); | ||
| 209 | + String response = rs.path("links").get(String.class); | ||
| 210 | + assertThat(response, containsString("{\"links\":[")); | ||
| 211 | + | ||
| 212 | + JsonObject result = JsonObject.readFrom(response); | ||
| 213 | + assertThat(result, notNullValue()); | ||
| 214 | + | ||
| 215 | + assertThat(result.names(), hasSize(1)); | ||
| 216 | + assertThat(result.names().get(0), is("links")); | ||
| 217 | + | ||
| 218 | + JsonArray jsonLinks = result.get("links").asArray(); | ||
| 219 | + assertThat(jsonLinks, notNullValue()); | ||
| 220 | + assertThat(jsonLinks.size(), is(3)); | ||
| 221 | + | ||
| 222 | + assertThat(jsonLinks, hasLink(link1)); | ||
| 223 | + assertThat(jsonLinks, hasLink(link2)); | ||
| 224 | + assertThat(jsonLinks, hasLink(link3)); | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + /** | ||
| 228 | + * Tests the result of the rest api GET of links for a specific device. | ||
| 229 | + */ | ||
| 230 | + @Test | ||
| 231 | + public void testLinksByDevice() { | ||
| 232 | + expect(mockLinkService.getDeviceLinks(isA(DeviceId.class))) | ||
| 233 | + .andReturn(ImmutableSet.of(link2)) | ||
| 234 | + .anyTimes(); | ||
| 235 | + | ||
| 236 | + replay(mockLinkService); | ||
| 237 | + | ||
| 238 | + WebResource rs = resource(); | ||
| 239 | + String response = rs | ||
| 240 | + .path("links") | ||
| 241 | + .queryParam("device", "src2") | ||
| 242 | + .get(String.class); | ||
| 243 | + assertThat(response, containsString("{\"links\":[")); | ||
| 244 | + | ||
| 245 | + JsonObject result = JsonObject.readFrom(response); | ||
| 246 | + assertThat(result, notNullValue()); | ||
| 247 | + | ||
| 248 | + assertThat(result.names(), hasSize(1)); | ||
| 249 | + assertThat(result.names().get(0), is("links")); | ||
| 250 | + | ||
| 251 | + JsonArray jsonLinks = result.get("links").asArray(); | ||
| 252 | + assertThat(jsonLinks, notNullValue()); | ||
| 253 | + assertThat(jsonLinks.size(), is(1)); | ||
| 254 | + | ||
| 255 | + assertThat(jsonLinks, hasLink(link2)); | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + /** | ||
| 259 | + * Tests the result of the rest api GET of links for a specific device | ||
| 260 | + * and port. | ||
| 261 | + */ | ||
| 262 | + @Test | ||
| 263 | + public void testLinksByDevicePort() { | ||
| 264 | + | ||
| 265 | + expect(mockLinkService.getLinks(isA(ConnectPoint.class))) | ||
| 266 | + .andReturn(ImmutableSet.of(link2)) | ||
| 267 | + .anyTimes(); | ||
| 268 | + | ||
| 269 | + replay(mockLinkService); | ||
| 270 | + | ||
| 271 | + WebResource rs = resource(); | ||
| 272 | + String response = rs | ||
| 273 | + .path("links") | ||
| 274 | + .queryParam("device", "src2") | ||
| 275 | + .queryParam("port", "2") | ||
| 276 | + .get(String.class); | ||
| 277 | + assertThat(response, containsString("{\"links\":[")); | ||
| 278 | + | ||
| 279 | + JsonObject result = JsonObject.readFrom(response); | ||
| 280 | + assertThat(result, notNullValue()); | ||
| 281 | + | ||
| 282 | + assertThat(result.names(), hasSize(1)); | ||
| 283 | + assertThat(result.names().get(0), is("links")); | ||
| 284 | + | ||
| 285 | + JsonArray jsonLinks = result.get("links").asArray(); | ||
| 286 | + assertThat(jsonLinks, notNullValue()); | ||
| 287 | + assertThat(jsonLinks.size(), is(1)); | ||
| 288 | + | ||
| 289 | + assertThat(jsonLinks, hasLink(link2)); | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + /** | ||
| 293 | + * Tests the result of the rest api GET of links for a specific | ||
| 294 | + * device, port, and direction. | ||
| 295 | + */ | ||
| 296 | + @Test | ||
| 297 | + public void testLinksByDevicePortDirection() { | ||
| 298 | + | ||
| 299 | + expect(mockLinkService.getIngressLinks(isA(ConnectPoint.class))) | ||
| 300 | + .andReturn(ImmutableSet.of(link2)) | ||
| 301 | + .anyTimes(); | ||
| 302 | + | ||
| 303 | + replay(mockLinkService); | ||
| 304 | + | ||
| 305 | + WebResource rs = resource(); | ||
| 306 | + String response = rs | ||
| 307 | + .path("links") | ||
| 308 | + .queryParam("device", "src2") | ||
| 309 | + .queryParam("port", "2") | ||
| 310 | + .queryParam("direction", "INGRESS") | ||
| 311 | + .get(String.class); | ||
| 312 | + assertThat(response, containsString("{\"links\":[")); | ||
| 313 | + | ||
| 314 | + JsonObject result = JsonObject.readFrom(response); | ||
| 315 | + assertThat(result, notNullValue()); | ||
| 316 | + | ||
| 317 | + assertThat(result.names(), hasSize(1)); | ||
| 318 | + assertThat(result.names().get(0), is("links")); | ||
| 319 | + | ||
| 320 | + JsonArray jsonLinks = result.get("links").asArray(); | ||
| 321 | + assertThat(jsonLinks, notNullValue()); | ||
| 322 | + assertThat(jsonLinks.size(), is(1)); | ||
| 323 | + | ||
| 324 | + assertThat(jsonLinks, hasLink(link2)); | ||
| 325 | + } | ||
| 326 | + | ||
| 327 | + /** | ||
| 328 | + * Tests the result of the rest api GET of links for a specific | ||
| 329 | + * device and direction. | ||
| 330 | + */ | ||
| 331 | + @Test | ||
| 332 | + public void testLinksByDeviceDirection() { | ||
| 333 | + | ||
| 334 | + expect(mockLinkService.getDeviceIngressLinks(isA(DeviceId.class))) | ||
| 335 | + .andReturn(ImmutableSet.of(link2)) | ||
| 336 | + .anyTimes(); | ||
| 337 | + | ||
| 338 | + replay(mockLinkService); | ||
| 339 | + | ||
| 340 | + WebResource rs = resource(); | ||
| 341 | + String response = rs | ||
| 342 | + .path("links") | ||
| 343 | + .queryParam("device", "src2") | ||
| 344 | + .queryParam("direction", "INGRESS") | ||
| 345 | + .get(String.class); | ||
| 346 | + assertThat(response, containsString("{\"links\":[")); | ||
| 347 | + | ||
| 348 | + JsonObject result = JsonObject.readFrom(response); | ||
| 349 | + assertThat(result, notNullValue()); | ||
| 350 | + | ||
| 351 | + assertThat(result.names(), hasSize(1)); | ||
| 352 | + assertThat(result.names().get(0), is("links")); | ||
| 353 | + | ||
| 354 | + JsonArray jsonLinks = result.get("links").asArray(); | ||
| 355 | + assertThat(jsonLinks, notNullValue()); | ||
| 356 | + assertThat(jsonLinks.size(), is(1)); | ||
| 357 | + | ||
| 358 | + assertThat(jsonLinks, hasLink(link2)); | ||
| 359 | + } | ||
| 360 | +} |
-
Please register or login to post a comment