Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonArray; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 22 | import org.hamcrest.Description; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 23 | import org.hamcrest.Matchers; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 34 | import org.onosproject.codec.impl.FlowRuleCodec; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 35 | import org.onosproject.core.CoreService; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 36 | import org.onosproject.core.DefaultGroupId; |
| 37 | import org.onosproject.core.GroupId; |
| 38 | import org.onosproject.net.DefaultDevice; |
| 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 41 | import org.onosproject.net.NetTestTools; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 42 | import org.onosproject.net.device.DeviceService; |
| 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 45 | import org.onosproject.net.flow.FlowEntry; |
| 46 | import org.onosproject.net.flow.FlowId; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.FlowRule; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleService; |
| 50 | import org.onosproject.net.flow.TrafficSelector; |
| 51 | import org.onosproject.net.flow.TrafficTreatment; |
| 52 | import org.onosproject.net.flow.criteria.Criterion; |
| 53 | import org.onosproject.net.flow.instructions.Instruction; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 54 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 55 | import javax.ws.rs.NotFoundException; |
| 56 | import javax.ws.rs.client.Entity; |
| 57 | import javax.ws.rs.client.WebTarget; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 58 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 59 | import javax.ws.rs.core.Response; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 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 | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 226 | .setEthDst(MacAddress.BROADCAST) |
| 227 | .build(); |
| 228 | flow2.selector = DefaultTrafficSelector.builder() |
| 229 | .matchEthType((short) 3) |
| 230 | .matchIPProtocol((byte) 9) |
| 231 | .build(); |
| 232 | flow4.treatment = DefaultTrafficTreatment.builder() |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 233 | .build(); |
| 234 | final Set<FlowEntry> flows1 = new HashSet<>(); |
| 235 | flows1.add(flow1); |
| 236 | flows1.add(flow2); |
| 237 | |
| 238 | final Set<FlowEntry> flows2 = new HashSet<>(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 239 | flows2.add(flow3); |
| 240 | flows2.add(flow4); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 241 | |
| 242 | rules.put(deviceId1, flows1); |
| 243 | rules.put(deviceId2, flows2); |
| 244 | |
| 245 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 246 | .andReturn(rules.get(deviceId1)).anyTimes(); |
| 247 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 248 | .andReturn(rules.get(deviceId2)).anyTimes(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Sets up the global values for all the tests. |
| 253 | */ |
| 254 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 255 | public void setUpTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 256 | // Mock device service |
| 257 | expect(mockDeviceService.getDevice(deviceId1)) |
| 258 | .andReturn(device1); |
| 259 | expect(mockDeviceService.getDevice(deviceId2)) |
| 260 | .andReturn(device2); |
| 261 | expect(mockDeviceService.getDevices()) |
| 262 | .andReturn(ImmutableSet.of(device1, device2)); |
| 263 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 264 | // Mock Core Service |
| 265 | expect(mockCoreService.getAppId(anyShort())) |
| 266 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 267 | expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID)) |
| 268 | .andReturn(APP_ID).anyTimes(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 269 | replay(mockCoreService); |
| 270 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 271 | // Register the services needed for the test |
| 272 | final CodecManager codecService = new CodecManager(); |
| 273 | codecService.activate(); |
| 274 | ServiceDirectory testDirectory = |
| 275 | new TestServiceDirectory() |
| 276 | .add(FlowRuleService.class, mockFlowService) |
| 277 | .add(DeviceService.class, mockDeviceService) |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 278 | .add(CodecService.class, codecService) |
| 279 | .add(CoreService.class, mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 280 | |
| 281 | BaseResource.setServiceDirectory(testDirectory); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Cleans up and verifies the mocks. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 286 | */ |
| 287 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 288 | public void tearDownTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 289 | verify(mockFlowService); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 290 | verify(mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Hamcrest matcher to check that a flow representation in JSON matches |
| 295 | * the actual flow entry. |
| 296 | */ |
| 297 | public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 298 | private final FlowEntry flow; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 299 | private final String expectedAppId; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 300 | private String reason = ""; |
| 301 | |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 302 | public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 303 | flow = flowValue; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 304 | expectedAppId = expectedAppIdValue; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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 |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 318 | final String jsonAppId = jsonFlow.get("appId").asString(); |
| 319 | if (!jsonAppId.equals(expectedAppId)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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 | */ |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 401 | private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) { |
| 402 | return new FlowJsonMatcher(flow, expectedAppName); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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 |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 432 | assertThat(jsonFlow, matchesFlow(flow, APP_ID.name())); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 470 | final WebTarget wt = target(); |
| 471 | final String response = wt.path("flows").request().get(String.class); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 483 | final WebTarget wt = target(); |
| 484 | final String response = wt.path("flows").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 485 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 511 | final WebTarget wt = target(); |
| 512 | final String response = wt.path("flows/" + deviceId3).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 513 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 537 | final WebTarget wt = target(); |
| 538 | final String response = wt.path("flows/" + deviceId3 + "/" |
| 539 | + Long.toString(flow5.id().value())).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 540 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 556 | expect(mockFlowService.getFlowEntries(anyObject())) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 557 | .andReturn(null).anyTimes(); |
| 558 | replay(mockFlowService); |
| 559 | replay(mockDeviceService); |
| 560 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 561 | WebTarget wt = target(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 562 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 563 | wt.path("flows/0").request().get(String.class); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 564 | fail("Fetch of non-existent device did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 565 | } catch (NotFoundException ex) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 566 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 567 | containsString("HTTP 404 Not Found")); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 570 | |
| 571 | /** |
| 572 | * Tests creating a flow with POST. |
| 573 | */ |
| 574 | @Test |
| 575 | public void testPost() { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 576 | mockFlowService.applyFlowRules(anyObject()); |
| 577 | expectLastCall(); |
| 578 | replay(mockFlowService); |
| 579 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 580 | WebTarget wt = target(); |
Phaneendra Manda | 2be2f88 | 2015-11-11 15:23:40 +0530 | [diff] [blame] | 581 | InputStream jsonStream = FlowsResourceTest.class |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 582 | .getResourceAsStream("post-flow.json"); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 583 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 584 | Response response = wt.path("flows/of:0000000000000001") |
| 585 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 586 | .post(Entity.json(jsonStream)); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 587 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 588 | String location = response.getLocation().getPath(); |
| 589 | assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/")); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Tests deleting a flow. |
| 594 | */ |
| 595 | @Test |
| 596 | public void testDelete() { |
| 597 | setupMockFlows(); |
| 598 | mockFlowService.removeFlowRules(anyObject()); |
| 599 | expectLastCall(); |
| 600 | replay(mockFlowService); |
| 601 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 602 | WebTarget wt = target(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 603 | |
| 604 | String location = "/flows/1/155"; |
| 605 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 606 | Response deleteResponse = wt.path(location) |
| 607 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 608 | .delete(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 609 | assertThat(deleteResponse.getStatus(), |
| 610 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 611 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 612 | } |