Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import org.hamcrest.Description; |
| 23 | import org.hamcrest.TypeSafeMatcher; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.osgi.ServiceDirectory; |
| 28 | import org.onlab.osgi.TestServiceDirectory; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.rest.BaseResource; |
| 31 | import org.onosproject.codec.CodecService; |
| 32 | import org.onosproject.codec.impl.CodecManager; |
| 33 | import org.onosproject.core.DefaultGroupId; |
| 34 | import org.onosproject.core.GroupId; |
| 35 | import org.onosproject.net.DefaultDevice; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowEntry; |
| 42 | import org.onosproject.net.flow.FlowId; |
| 43 | import org.onosproject.net.flow.FlowRuleService; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.flow.criteria.Criterion; |
| 47 | import org.onosproject.net.flow.instructions.Instruction; |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.instructions.Instructions; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 49 | import com.eclipsesource.json.JsonArray; |
| 50 | import com.eclipsesource.json.JsonObject; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 51 | import com.google.common.collect.ImmutableSet; |
| 52 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 53 | import com.sun.jersey.api.client.WebResource; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 54 | |
| 55 | import static org.easymock.EasyMock.anyObject; |
| 56 | import static org.easymock.EasyMock.createMock; |
| 57 | import static org.easymock.EasyMock.expect; |
| 58 | import static org.easymock.EasyMock.replay; |
| 59 | import static org.easymock.EasyMock.verify; |
| 60 | import static org.hamcrest.Matchers.containsString; |
| 61 | import static org.hamcrest.Matchers.hasSize; |
| 62 | import static org.hamcrest.Matchers.is; |
| 63 | import static org.hamcrest.Matchers.not; |
| 64 | import static org.hamcrest.Matchers.notNullValue; |
| 65 | import static org.junit.Assert.assertThat; |
| 66 | import static org.junit.Assert.fail; |
| 67 | |
| 68 | /** |
| 69 | * Unit tests for Flows REST APIs. |
| 70 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 71 | public class FlowsResourceTest extends ResourceTest { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 72 | final FlowRuleService mockFlowService = createMock(FlowRuleService.class); |
| 73 | final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>(); |
| 74 | |
| 75 | final DeviceService mockDeviceService = createMock(DeviceService.class); |
| 76 | |
| 77 | final DeviceId deviceId1 = DeviceId.deviceId("1"); |
| 78 | final DeviceId deviceId2 = DeviceId.deviceId("2"); |
| 79 | final DeviceId deviceId3 = DeviceId.deviceId("3"); |
| 80 | final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER, |
| 81 | "", "", "", "", null); |
| 82 | final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER, |
| 83 | "", "", "", "", null); |
| 84 | |
| 85 | final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1); |
| 86 | final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2); |
| 87 | |
| 88 | final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3); |
| 89 | final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4); |
| 90 | |
| 91 | final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5); |
| 92 | final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6); |
| 93 | |
| 94 | /** |
| 95 | * Mock class for a flow entry. |
| 96 | */ |
| 97 | private static class MockFlowEntry implements FlowEntry { |
| 98 | final DeviceId deviceId; |
| 99 | final long baseValue; |
| 100 | TrafficTreatment treatment; |
| 101 | TrafficSelector selector; |
| 102 | |
| 103 | public MockFlowEntry(DeviceId deviceId, long id) { |
| 104 | this.deviceId = deviceId; |
| 105 | this.baseValue = id * 100; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public FlowEntryState state() { |
| 110 | return FlowEntryState.ADDED; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public long life() { |
| 115 | return baseValue + 11; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public long packets() { |
| 120 | return baseValue + 22; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public long bytes() { |
| 125 | return baseValue + 33; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public long lastSeen() { |
| 130 | return baseValue + 44; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int errType() { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int errCode() { |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public FlowId id() { |
| 145 | final long id = baseValue + 55; |
| 146 | return FlowId.valueOf(id); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public short appId() { |
| 151 | return 2; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public GroupId groupId() { |
| 156 | return new DefaultGroupId(3); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public int priority() { |
| 161 | return (int) (baseValue + 66); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public DeviceId deviceId() { |
| 166 | return deviceId; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public TrafficSelector selector() { |
| 171 | return selector; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public TrafficTreatment treatment() { |
| 176 | return treatment; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public int timeout() { |
| 181 | return (int) (baseValue + 77); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean isPermanent() { |
| 186 | return false; |
| 187 | } |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 188 | |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 189 | @Override |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 190 | public Type type() { |
| 191 | return Type.DEFAULT; |
| 192 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 195 | /** |
| 196 | * Populates some flows used as testing data. |
| 197 | */ |
| 198 | private void setupMockFlows() { |
| 199 | flow2.treatment = DefaultTrafficTreatment.builder() |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 200 | .add(Instructions.modL0Lambda((short) 4)) |
| 201 | .add(Instructions.modL0Lambda((short) 5)) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 202 | .setEthDst(MacAddress.BROADCAST) |
| 203 | .build(); |
| 204 | flow2.selector = DefaultTrafficSelector.builder() |
| 205 | .matchEthType((short) 3) |
| 206 | .matchIPProtocol((byte) 9) |
| 207 | .build(); |
| 208 | flow4.treatment = DefaultTrafficTreatment.builder() |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 209 | .add(Instructions.modL0Lambda((short) 6)) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 210 | .build(); |
| 211 | final Set<FlowEntry> flows1 = new HashSet<>(); |
| 212 | flows1.add(flow1); |
| 213 | flows1.add(flow2); |
| 214 | |
| 215 | final Set<FlowEntry> flows2 = new HashSet<>(); |
| 216 | flows1.add(flow3); |
| 217 | flows1.add(flow4); |
| 218 | |
| 219 | rules.put(deviceId1, flows1); |
| 220 | rules.put(deviceId2, flows2); |
| 221 | |
| 222 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 223 | .andReturn(rules.get(deviceId1)).anyTimes(); |
| 224 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 225 | .andReturn(rules.get(deviceId2)).anyTimes(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Sets up the global values for all the tests. |
| 230 | */ |
| 231 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 232 | public void setUpTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 233 | // Mock device service |
| 234 | expect(mockDeviceService.getDevice(deviceId1)) |
| 235 | .andReturn(device1); |
| 236 | expect(mockDeviceService.getDevice(deviceId2)) |
| 237 | .andReturn(device2); |
| 238 | expect(mockDeviceService.getDevices()) |
| 239 | .andReturn(ImmutableSet.of(device1, device2)); |
| 240 | |
| 241 | // Register the services needed for the test |
| 242 | final CodecManager codecService = new CodecManager(); |
| 243 | codecService.activate(); |
| 244 | ServiceDirectory testDirectory = |
| 245 | new TestServiceDirectory() |
| 246 | .add(FlowRuleService.class, mockFlowService) |
| 247 | .add(DeviceService.class, mockDeviceService) |
| 248 | .add(CodecService.class, codecService); |
| 249 | |
| 250 | BaseResource.setServiceDirectory(testDirectory); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Cleans up and verifies the mocks. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 255 | */ |
| 256 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 257 | public void tearDownTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 258 | verify(mockFlowService); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Hamcrest matcher to check that a flow representation in JSON matches |
| 263 | * the actual flow entry. |
| 264 | */ |
| 265 | public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 266 | private final FlowEntry flow; |
| 267 | private String reason = ""; |
| 268 | |
| 269 | public FlowJsonMatcher(FlowEntry flowValue) { |
| 270 | flow = flowValue; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public boolean matchesSafely(JsonObject jsonFlow) { |
| 275 | // check id |
| 276 | final String jsonId = jsonFlow.get("id").asString(); |
| 277 | final String flowId = Long.toString(flow.id().value()); |
| 278 | if (!jsonId.equals(flowId)) { |
| 279 | reason = "id " + flow.id().toString(); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // check application id |
| 284 | final int jsonAppId = jsonFlow.get("appId").asInt(); |
| 285 | if (jsonAppId != flow.appId()) { |
| 286 | reason = "appId " + Short.toString(flow.appId()); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | // check device id |
| 291 | final String jsonDeviceId = jsonFlow.get("deviceId").asString(); |
| 292 | if (!jsonDeviceId.equals(flow.deviceId().toString())) { |
| 293 | reason = "deviceId " + flow.deviceId(); |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | // check treatment and instructions array |
| 298 | if (flow.treatment() != null) { |
| 299 | final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject(); |
| 300 | final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray(); |
| 301 | if (flow.treatment().instructions().size() != jsonInstructions.size()) { |
| 302 | reason = "instructions array size of " + |
| 303 | Integer.toString(flow.treatment().instructions().size()); |
| 304 | return false; |
| 305 | } |
| 306 | for (final Instruction instruction : flow.treatment().instructions()) { |
| 307 | boolean instructionFound = false; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 308 | for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 309 | final String jsonType = |
| 310 | jsonInstructions.get(instructionIndex) |
| 311 | .asObject().get("type").asString(); |
| 312 | final String instructionType = instruction.type().name(); |
| 313 | if (jsonType.equals(instructionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 314 | instructionFound = true; |
| 315 | } |
| 316 | } |
| 317 | if (!instructionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 318 | reason = "instruction " + instruction.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // check selector and criteria array |
| 325 | if (flow.selector() != null) { |
| 326 | final JsonObject jsonTreatment = jsonFlow.get("selector").asObject(); |
| 327 | final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray(); |
| 328 | if (flow.selector().criteria().size() != jsonCriteria.size()) { |
| 329 | reason = "criteria array size of " + |
| 330 | Integer.toString(flow.selector().criteria().size()); |
| 331 | return false; |
| 332 | } |
| 333 | for (final Criterion criterion : flow.selector().criteria()) { |
| 334 | boolean criterionFound = false; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 335 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 336 | for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 337 | final String jsonType = |
| 338 | jsonCriteria.get(criterionIndex) |
| 339 | .asObject().get("type").asString(); |
| 340 | final String criterionType = criterion.type().name(); |
| 341 | if (jsonType.equals(criterionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 342 | criterionFound = true; |
| 343 | } |
| 344 | } |
| 345 | if (!criterionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 346 | reason = "criterion " + criterion.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void describeTo(Description description) { |
| 357 | description.appendText(reason); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Factory to allocate a flow matcher. |
| 363 | * |
| 364 | * @param flow flow object we are looking for |
| 365 | * @return matcher |
| 366 | */ |
| 367 | private static FlowJsonMatcher matchesFlow(FlowEntry flow) { |
| 368 | return new FlowJsonMatcher(flow); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Hamcrest matcher to check that a flow is represented properly in a JSON |
| 373 | * array of flows. |
| 374 | */ |
| 375 | public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 376 | private final FlowEntry flow; |
| 377 | private String reason = ""; |
| 378 | |
| 379 | public FlowJsonArrayMatcher(FlowEntry flowValue) { |
| 380 | flow = flowValue; |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public boolean matchesSafely(JsonArray json) { |
| 385 | boolean flowFound = false; |
| 386 | |
| 387 | for (int jsonFlowIndex = 0; jsonFlowIndex < json.size(); |
| 388 | jsonFlowIndex++) { |
| 389 | |
| 390 | final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject(); |
| 391 | |
| 392 | final String flowId = Long.toString(flow.id().value()); |
| 393 | final String jsonFlowId = jsonFlow.get("id").asString(); |
| 394 | if (jsonFlowId.equals(flowId)) { |
| 395 | flowFound = true; |
| 396 | |
| 397 | // We found the correct flow, check attribute values |
| 398 | assertThat(jsonFlow, matchesFlow(flow)); |
| 399 | } |
| 400 | } |
| 401 | if (!flowFound) { |
| 402 | reason = "Flow with id " + flow.id().toString() + " not found"; |
| 403 | return false; |
| 404 | } else { |
| 405 | return true; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public void describeTo(Description description) { |
| 411 | description.appendText(reason); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Factory to allocate a flow array matcher. |
| 417 | * |
| 418 | * @param flow flow object we are looking for |
| 419 | * @return matcher |
| 420 | */ |
| 421 | private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) { |
| 422 | return new FlowJsonArrayMatcher(flow); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Tests the result of the rest api GET when there are no flows. |
| 427 | */ |
| 428 | @Test |
| 429 | public void testFlowsEmptyArray() { |
| 430 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 431 | .andReturn(null).anyTimes(); |
| 432 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 433 | .andReturn(null).anyTimes(); |
| 434 | replay(mockFlowService); |
| 435 | replay(mockDeviceService); |
| 436 | final WebResource rs = resource(); |
| 437 | final String response = rs.path("flows").get(String.class); |
| 438 | assertThat(response, is("{\"flows\":[]}")); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Tests the result of the rest api GET when there are active flows. |
| 443 | */ |
| 444 | @Test |
| 445 | public void testFlowsPopulatedArray() { |
| 446 | setupMockFlows(); |
| 447 | replay(mockFlowService); |
| 448 | replay(mockDeviceService); |
| 449 | final WebResource rs = resource(); |
| 450 | final String response = rs.path("flows").get(String.class); |
| 451 | final JsonObject result = JsonObject.readFrom(response); |
| 452 | assertThat(result, notNullValue()); |
| 453 | |
| 454 | assertThat(result.names(), hasSize(1)); |
| 455 | assertThat(result.names().get(0), is("flows")); |
| 456 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 457 | assertThat(jsonFlows, notNullValue()); |
| 458 | assertThat(jsonFlows, hasFlow(flow1)); |
| 459 | assertThat(jsonFlows, hasFlow(flow2)); |
| 460 | assertThat(jsonFlows, hasFlow(flow3)); |
| 461 | assertThat(jsonFlows, hasFlow(flow4)); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Tests the result of a rest api GET for a device. |
| 466 | */ |
| 467 | @Test |
| 468 | public void testFlowsSingleDevice() { |
| 469 | setupMockFlows(); |
| 470 | final Set<FlowEntry> flows = new HashSet<>(); |
| 471 | flows.add(flow5); |
| 472 | flows.add(flow6); |
| 473 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 474 | .andReturn(flows).anyTimes(); |
| 475 | replay(mockFlowService); |
| 476 | replay(mockDeviceService); |
| 477 | final WebResource rs = resource(); |
| 478 | final String response = rs.path("flows/" + deviceId3).get(String.class); |
| 479 | final JsonObject result = JsonObject.readFrom(response); |
| 480 | assertThat(result, notNullValue()); |
| 481 | |
| 482 | assertThat(result.names(), hasSize(1)); |
| 483 | assertThat(result.names().get(0), is("flows")); |
| 484 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 485 | assertThat(jsonFlows, notNullValue()); |
| 486 | assertThat(jsonFlows, hasFlow(flow5)); |
| 487 | assertThat(jsonFlows, hasFlow(flow6)); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Tests the result of a rest api GET for a device. |
| 492 | */ |
| 493 | @Test |
| 494 | public void testFlowsSingleDeviceWithFlowId() { |
| 495 | setupMockFlows(); |
| 496 | final Set<FlowEntry> flows = new HashSet<>(); |
| 497 | flows.add(flow5); |
| 498 | flows.add(flow6); |
| 499 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 500 | .andReturn(flows).anyTimes(); |
| 501 | replay(mockFlowService); |
| 502 | replay(mockDeviceService); |
| 503 | final WebResource rs = resource(); |
| 504 | final String response = rs.path("flows/" + deviceId3 + "/" |
| 505 | + Long.toString(flow5.id().value())).get(String.class); |
| 506 | final JsonObject result = JsonObject.readFrom(response); |
| 507 | assertThat(result, notNullValue()); |
| 508 | |
| 509 | assertThat(result.names(), hasSize(1)); |
| 510 | assertThat(result.names().get(0), is("flows")); |
| 511 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 512 | assertThat(jsonFlows, notNullValue()); |
| 513 | assertThat(jsonFlows, hasFlow(flow5)); |
| 514 | assertThat(jsonFlows, not(hasFlow(flow6))); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Tests that a fetch of a non-existent device object throws an exception. |
| 519 | */ |
| 520 | @Test |
| 521 | public void testBadGet() { |
| 522 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 523 | .andReturn(null).anyTimes(); |
| 524 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 525 | .andReturn(null).anyTimes(); |
| 526 | replay(mockFlowService); |
| 527 | replay(mockDeviceService); |
| 528 | |
| 529 | WebResource rs = resource(); |
| 530 | try { |
| 531 | rs.path("flows/0").get(String.class); |
| 532 | fail("Fetch of non-existent device did not throw an exception"); |
| 533 | } catch (UniformInterfaceException ex) { |
| 534 | assertThat(ex.getMessage(), |
| 535 | containsString("returned a response status of")); |
| 536 | } |
| 537 | } |
| 538 | } |