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