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