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