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; |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 23 | import org.hamcrest.Description; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 24 | import org.hamcrest.Matchers; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 25 | import org.hamcrest.TypeSafeMatcher; |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.osgi.ServiceDirectory; |
| 30 | import org.onlab.osgi.TestServiceDirectory; |
| 31 | import org.onlab.packet.MacAddress; |
| 32 | import org.onlab.rest.BaseResource; |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 33 | import org.onosproject.app.ApplicationService; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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 | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 43 | import org.onosproject.net.NetTestTools; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceService; |
| 45 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onosproject.net.flow.FlowEntry; |
| 48 | import org.onosproject.net.flow.FlowId; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRule; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.FlowRuleService; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.flow.criteria.Criterion; |
| 55 | import org.onosproject.net.flow.instructions.Instruction; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 56 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 57 | import javax.ws.rs.NotFoundException; |
| 58 | import javax.ws.rs.client.Entity; |
| 59 | import javax.ws.rs.client.WebTarget; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 60 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 61 | import javax.ws.rs.core.Response; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 62 | import java.io.InputStream; |
| 63 | import java.net.HttpURLConnection; |
| 64 | import java.util.HashMap; |
| 65 | import java.util.HashSet; |
| 66 | import java.util.Set; |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame^] | 67 | import java.util.concurrent.TimeUnit; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 68 | |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame^] | 69 | import static java.util.concurrent.TimeUnit.SECONDS; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 70 | import static org.easymock.EasyMock.anyObject; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 71 | import static org.easymock.EasyMock.anyShort; |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 72 | import static org.easymock.EasyMock.anyString; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 73 | import static org.easymock.EasyMock.createMock; |
| 74 | import static org.easymock.EasyMock.expect; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 75 | import static org.easymock.EasyMock.expectLastCall; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 76 | import static org.easymock.EasyMock.replay; |
| 77 | import static org.easymock.EasyMock.verify; |
| 78 | import static org.hamcrest.Matchers.containsString; |
| 79 | import static org.hamcrest.Matchers.hasSize; |
| 80 | import static org.hamcrest.Matchers.is; |
| 81 | import static org.hamcrest.Matchers.not; |
| 82 | import static org.hamcrest.Matchers.notNullValue; |
| 83 | import static org.junit.Assert.assertThat; |
| 84 | import static org.junit.Assert.fail; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 85 | import static org.onosproject.net.NetTestTools.APP_ID; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Unit tests for Flows REST APIs. |
| 89 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 90 | public class FlowsResourceTest extends ResourceTest { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 91 | final FlowRuleService mockFlowService = createMock(FlowRuleService.class); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 92 | CoreService mockCoreService = createMock(CoreService.class); |
| 93 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 94 | final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>(); |
| 95 | |
| 96 | final DeviceService mockDeviceService = createMock(DeviceService.class); |
| 97 | |
| 98 | final DeviceId deviceId1 = DeviceId.deviceId("1"); |
| 99 | final DeviceId deviceId2 = DeviceId.deviceId("2"); |
| 100 | final DeviceId deviceId3 = DeviceId.deviceId("3"); |
| 101 | final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER, |
| 102 | "", "", "", "", null); |
| 103 | final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER, |
| 104 | "", "", "", "", null); |
| 105 | |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 106 | final ApplicationService mockApplicationService = createMock(ApplicationService.class); |
| 107 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 108 | final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1); |
| 109 | final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2); |
| 110 | |
| 111 | final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3); |
| 112 | final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4); |
| 113 | |
| 114 | final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5); |
| 115 | final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6); |
| 116 | |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 117 | final MockFlowRule flowRule1 = new MockFlowRule(deviceId1, 1); |
| 118 | final MockFlowRule flowRule2 = new MockFlowRule(deviceId1, 2); |
| 119 | |
| 120 | final MockFlowRule flowRule3 = new MockFlowRule(deviceId2, 3); |
| 121 | final MockFlowRule flowRule4 = new MockFlowRule(deviceId2, 4); |
| 122 | |
| 123 | final Set<FlowRule> flowRules = Sets.newHashSet(); |
| 124 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Mock class for a flow entry. |
| 127 | */ |
| 128 | private static class MockFlowEntry implements FlowEntry { |
| 129 | final DeviceId deviceId; |
| 130 | final long baseValue; |
| 131 | TrafficTreatment treatment; |
| 132 | TrafficSelector selector; |
| 133 | |
| 134 | public MockFlowEntry(DeviceId deviceId, long id) { |
| 135 | this.deviceId = deviceId; |
| 136 | this.baseValue = id * 100; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public FlowEntryState state() { |
| 141 | return FlowEntryState.ADDED; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public long life() { |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame^] | 146 | return life(SECONDS); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public long life(TimeUnit timeUnit) { |
| 151 | return SECONDS.convert(baseValue + 11, timeUnit); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public long packets() { |
| 156 | return baseValue + 22; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public long bytes() { |
| 161 | return baseValue + 33; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public long lastSeen() { |
| 166 | return baseValue + 44; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public int errType() { |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public int errCode() { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public FlowId id() { |
| 181 | final long id = baseValue + 55; |
| 182 | return FlowId.valueOf(id); |
| 183 | } |
| 184 | |
| 185 | @Override |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 186 | public GroupId groupId() { |
| 187 | return new DefaultGroupId(3); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 191 | public short appId() { |
| 192 | return 2; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public int priority() { |
| 197 | return (int) (baseValue + 66); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public DeviceId deviceId() { |
| 202 | return deviceId; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public TrafficSelector selector() { |
| 207 | return selector; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public TrafficTreatment treatment() { |
| 212 | return treatment; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public int timeout() { |
| 217 | return (int) (baseValue + 77); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public boolean isPermanent() { |
| 222 | return false; |
| 223 | } |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 224 | |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 225 | @Override |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 226 | public int tableId() { |
| 227 | return 0; |
| 228 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 229 | |
| 230 | @Override |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 231 | public boolean exactMatch(FlowRule rule) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | @Override |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 236 | public FlowRuleExtPayLoad payLoad() { |
| 237 | return null; |
| 238 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 241 | /** |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 242 | * Mock class for a flow rule. |
| 243 | */ |
| 244 | private static class MockFlowRule implements FlowRule { |
| 245 | |
| 246 | final DeviceId deviceId; |
| 247 | final long baseValue; |
| 248 | TrafficTreatment treatment; |
| 249 | TrafficSelector selector; |
| 250 | |
| 251 | public MockFlowRule(DeviceId deviceId, long id) { |
| 252 | this.deviceId = deviceId; |
| 253 | this.baseValue = id * 100; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public FlowId id() { |
| 258 | final long id = baseValue + 55; |
| 259 | return FlowId.valueOf(id); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public short appId() { |
| 264 | return 4; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public GroupId groupId() { |
| 269 | return new DefaultGroupId(3); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public int priority() { |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public DeviceId deviceId() { |
| 279 | return deviceId; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public TrafficSelector selector() { |
| 284 | return selector; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public TrafficTreatment treatment() { |
| 289 | return treatment; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public int timeout() { |
| 294 | return (int) (baseValue + 77); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public boolean isPermanent() { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public int tableId() { |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public boolean exactMatch(FlowRule rule) { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public FlowRuleExtPayLoad payLoad() { |
| 314 | return null; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 319 | * Populates some flows used as testing data. |
| 320 | */ |
| 321 | private void setupMockFlows() { |
| 322 | flow2.treatment = DefaultTrafficTreatment.builder() |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 323 | .setEthDst(MacAddress.BROADCAST) |
| 324 | .build(); |
| 325 | flow2.selector = DefaultTrafficSelector.builder() |
| 326 | .matchEthType((short) 3) |
| 327 | .matchIPProtocol((byte) 9) |
| 328 | .build(); |
| 329 | flow4.treatment = DefaultTrafficTreatment.builder() |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 330 | .build(); |
| 331 | final Set<FlowEntry> flows1 = new HashSet<>(); |
| 332 | flows1.add(flow1); |
| 333 | flows1.add(flow2); |
| 334 | |
| 335 | final Set<FlowEntry> flows2 = new HashSet<>(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 336 | flows2.add(flow3); |
| 337 | flows2.add(flow4); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 338 | |
| 339 | rules.put(deviceId1, flows1); |
| 340 | rules.put(deviceId2, flows2); |
| 341 | |
| 342 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 343 | .andReturn(rules.get(deviceId1)).anyTimes(); |
| 344 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 345 | .andReturn(rules.get(deviceId2)).anyTimes(); |
| 346 | } |
| 347 | |
| 348 | /** |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 349 | * Populates some flow rules used as testing data. |
| 350 | */ |
| 351 | private void setupMockFlowRules() { |
| 352 | flowRule2.treatment = DefaultTrafficTreatment.builder() |
| 353 | .setEthDst(MacAddress.BROADCAST) |
| 354 | .build(); |
| 355 | flowRule2.selector = DefaultTrafficSelector.builder() |
| 356 | .matchEthType((short) 3) |
| 357 | .matchIPProtocol((byte) 9) |
| 358 | .build(); |
| 359 | flowRule4.treatment = DefaultTrafficTreatment.builder() |
| 360 | .build(); |
| 361 | |
| 362 | flowRules.add(flowRule1); |
| 363 | flowRules.add(flowRule2); |
| 364 | flowRules.add(flowRule3); |
| 365 | flowRules.add(flowRule4); |
| 366 | } |
| 367 | |
| 368 | /** |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 369 | * Sets up the global values for all the tests. |
| 370 | */ |
| 371 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 372 | public void setUpTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 373 | // Mock device service |
| 374 | expect(mockDeviceService.getDevice(deviceId1)) |
| 375 | .andReturn(device1); |
| 376 | expect(mockDeviceService.getDevice(deviceId2)) |
| 377 | .andReturn(device2); |
| 378 | expect(mockDeviceService.getDevices()) |
| 379 | .andReturn(ImmutableSet.of(device1, device2)); |
| 380 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 381 | // Mock Core Service |
| 382 | expect(mockCoreService.getAppId(anyShort())) |
| 383 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 384 | expect(mockCoreService.getAppId(anyString())) |
| 385 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 386 | expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID)) |
| 387 | .andReturn(APP_ID).anyTimes(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 388 | replay(mockCoreService); |
| 389 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 390 | // Register the services needed for the test |
| 391 | final CodecManager codecService = new CodecManager(); |
| 392 | codecService.activate(); |
| 393 | ServiceDirectory testDirectory = |
| 394 | new TestServiceDirectory() |
| 395 | .add(FlowRuleService.class, mockFlowService) |
| 396 | .add(DeviceService.class, mockDeviceService) |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 397 | .add(CodecService.class, codecService) |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 398 | .add(CoreService.class, mockCoreService) |
| 399 | .add(ApplicationService.class, mockApplicationService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 400 | |
| 401 | BaseResource.setServiceDirectory(testDirectory); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Cleans up and verifies the mocks. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 406 | */ |
| 407 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 408 | public void tearDownTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 409 | verify(mockFlowService); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 410 | verify(mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Hamcrest matcher to check that a flow representation in JSON matches |
| 415 | * the actual flow entry. |
| 416 | */ |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 417 | public static class FlowEntryJsonMatcher extends TypeSafeMatcher<JsonObject> { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 418 | private final FlowEntry flow; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 419 | private final String expectedAppId; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 420 | private String reason = ""; |
| 421 | |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 422 | public FlowEntryJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 423 | flow = flowValue; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 424 | expectedAppId = expectedAppIdValue; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public boolean matchesSafely(JsonObject jsonFlow) { |
| 429 | // check id |
| 430 | final String jsonId = jsonFlow.get("id").asString(); |
| 431 | final String flowId = Long.toString(flow.id().value()); |
| 432 | if (!jsonId.equals(flowId)) { |
| 433 | reason = "id " + flow.id().toString(); |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | // check application id |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 438 | final String jsonAppId = jsonFlow.get("appId").asString(); |
| 439 | if (!jsonAppId.equals(expectedAppId)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 440 | reason = "appId " + Short.toString(flow.appId()); |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | // check device id |
| 445 | final String jsonDeviceId = jsonFlow.get("deviceId").asString(); |
| 446 | if (!jsonDeviceId.equals(flow.deviceId().toString())) { |
| 447 | reason = "deviceId " + flow.deviceId(); |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | // check treatment and instructions array |
| 452 | if (flow.treatment() != null) { |
| 453 | final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject(); |
| 454 | final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray(); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 455 | if (flow.treatment().immediate().size() != jsonInstructions.size()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 456 | reason = "instructions array size of " + |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 457 | Integer.toString(flow.treatment().immediate().size()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 458 | return false; |
| 459 | } |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 460 | for (final Instruction instruction : flow.treatment().immediate()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 461 | boolean instructionFound = false; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 462 | for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 463 | final String jsonType = |
| 464 | jsonInstructions.get(instructionIndex) |
| 465 | .asObject().get("type").asString(); |
| 466 | final String instructionType = instruction.type().name(); |
| 467 | if (jsonType.equals(instructionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 468 | instructionFound = true; |
| 469 | } |
| 470 | } |
| 471 | if (!instructionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 472 | reason = "instruction " + instruction.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // check selector and criteria array |
| 479 | if (flow.selector() != null) { |
| 480 | final JsonObject jsonTreatment = jsonFlow.get("selector").asObject(); |
| 481 | final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray(); |
| 482 | if (flow.selector().criteria().size() != jsonCriteria.size()) { |
| 483 | reason = "criteria array size of " + |
| 484 | Integer.toString(flow.selector().criteria().size()); |
| 485 | return false; |
| 486 | } |
| 487 | for (final Criterion criterion : flow.selector().criteria()) { |
| 488 | boolean criterionFound = false; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 489 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 490 | for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 491 | final String jsonType = |
| 492 | jsonCriteria.get(criterionIndex) |
| 493 | .asObject().get("type").asString(); |
| 494 | final String criterionType = criterion.type().name(); |
| 495 | if (jsonType.equals(criterionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 496 | criterionFound = true; |
| 497 | } |
| 498 | } |
| 499 | if (!criterionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 500 | reason = "criterion " + criterion.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 501 | return false; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void describeTo(Description description) { |
| 511 | description.appendText(reason); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Factory to allocate a flow matcher. |
| 517 | * |
| 518 | * @param flow flow object we are looking for |
| 519 | * @return matcher |
| 520 | */ |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 521 | private static FlowEntryJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) { |
| 522 | return new FlowEntryJsonMatcher(flow, expectedAppName); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Hamcrest matcher to check that a flow is represented properly in a JSON |
| 527 | * array of flows. |
| 528 | */ |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 529 | public static class FlowEntryJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 530 | private final FlowEntry flow; |
| 531 | private String reason = ""; |
| 532 | |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 533 | public FlowEntryJsonArrayMatcher(FlowEntry flowValue) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 534 | flow = flowValue; |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public boolean matchesSafely(JsonArray json) { |
| 539 | boolean flowFound = false; |
| 540 | |
| 541 | for (int jsonFlowIndex = 0; jsonFlowIndex < json.size(); |
| 542 | jsonFlowIndex++) { |
| 543 | |
| 544 | final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject(); |
| 545 | |
| 546 | final String flowId = Long.toString(flow.id().value()); |
| 547 | final String jsonFlowId = jsonFlow.get("id").asString(); |
| 548 | if (jsonFlowId.equals(flowId)) { |
| 549 | flowFound = true; |
| 550 | |
| 551 | // We found the correct flow, check attribute values |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 552 | assertThat(jsonFlow, matchesFlow(flow, APP_ID.name())); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | if (!flowFound) { |
| 556 | reason = "Flow with id " + flow.id().toString() + " not found"; |
| 557 | return false; |
| 558 | } else { |
| 559 | return true; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | @Override |
| 564 | public void describeTo(Description description) { |
| 565 | description.appendText(reason); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Factory to allocate a flow array matcher. |
| 571 | * |
| 572 | * @param flow flow object we are looking for |
| 573 | * @return matcher |
| 574 | */ |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 575 | private static FlowEntryJsonArrayMatcher hasFlow(FlowEntry flow) { |
| 576 | return new FlowEntryJsonArrayMatcher(flow); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Hamcrest matcher to check that a flow representation in JSON matches |
| 581 | * the actual flow rule. |
| 582 | */ |
| 583 | public static class FlowRuleJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 584 | private final FlowRule flow; |
| 585 | private final String expectedAppId; |
| 586 | private String reason = ""; |
| 587 | |
| 588 | public FlowRuleJsonMatcher(FlowRule flowValue, String expectedAppIdValue) { |
| 589 | flow = flowValue; |
| 590 | expectedAppId = expectedAppIdValue; |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public boolean matchesSafely(JsonObject jsonFlow) { |
| 595 | // check id |
| 596 | final String jsonId = jsonFlow.get("id").asString(); |
| 597 | final String flowId = Long.toString(flow.id().value()); |
| 598 | if (!jsonId.equals(flowId)) { |
| 599 | reason = "id " + flow.id().toString(); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | // check application id |
| 604 | final String jsonAppId = jsonFlow.get("appId").asString(); |
| 605 | if (!jsonAppId.equals(expectedAppId)) { |
| 606 | reason = "appId " + Short.toString(flow.appId()); |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | // check device id |
| 611 | final String jsonDeviceId = jsonFlow.get("deviceId").asString(); |
| 612 | if (!jsonDeviceId.equals(flow.deviceId().toString())) { |
| 613 | reason = "deviceId " + flow.deviceId(); |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | // check treatment and instructions array |
| 618 | if (flow.treatment() != null) { |
| 619 | final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject(); |
| 620 | final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray(); |
| 621 | if (flow.treatment().immediate().size() != jsonInstructions.size()) { |
| 622 | reason = "instructions array size of " + |
| 623 | Integer.toString(flow.treatment().immediate().size()); |
| 624 | return false; |
| 625 | } |
| 626 | for (final Instruction instruction : flow.treatment().immediate()) { |
| 627 | boolean instructionFound = false; |
| 628 | for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) { |
| 629 | final String jsonType = |
| 630 | jsonInstructions.get(instructionIndex) |
| 631 | .asObject().get("type").asString(); |
| 632 | final String instructionType = instruction.type().name(); |
| 633 | if (jsonType.equals(instructionType)) { |
| 634 | instructionFound = true; |
| 635 | } |
| 636 | } |
| 637 | if (!instructionFound) { |
| 638 | reason = "instruction " + instruction.toString(); |
| 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // check selector and criteria array |
| 645 | if (flow.selector() != null) { |
| 646 | final JsonObject jsonTreatment = jsonFlow.get("selector").asObject(); |
| 647 | final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray(); |
| 648 | if (flow.selector().criteria().size() != jsonCriteria.size()) { |
| 649 | reason = "criteria array size of " + |
| 650 | Integer.toString(flow.selector().criteria().size()); |
| 651 | return false; |
| 652 | } |
| 653 | for (final Criterion criterion : flow.selector().criteria()) { |
| 654 | boolean criterionFound = false; |
| 655 | |
| 656 | for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) { |
| 657 | final String jsonType = |
| 658 | jsonCriteria.get(criterionIndex) |
| 659 | .asObject().get("type").asString(); |
| 660 | final String criterionType = criterion.type().name(); |
| 661 | if (jsonType.equals(criterionType)) { |
| 662 | criterionFound = true; |
| 663 | } |
| 664 | } |
| 665 | if (!criterionFound) { |
| 666 | reason = "criterion " + criterion.toString(); |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | @Override |
| 676 | public void describeTo(Description description) { |
| 677 | description.appendText(reason); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Factory to allocate a flow matcher. |
| 683 | * |
| 684 | * @param flow flow rule object we are looking for |
| 685 | * @return matcher |
| 686 | */ |
| 687 | private static FlowRuleJsonMatcher matchesFlowRule(FlowRule flow, String expectedAppName) { |
| 688 | return new FlowRuleJsonMatcher(flow, expectedAppName); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Hamcrest matcher to check that a flow is represented properly in a JSON |
| 693 | * array of flow rules. |
| 694 | */ |
| 695 | public static class FlowRuleJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 696 | private final FlowRule flow; |
| 697 | private String reason = ""; |
| 698 | |
| 699 | public FlowRuleJsonArrayMatcher(FlowRule flowValue) { |
| 700 | flow = flowValue; |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public boolean matchesSafely(JsonArray json) { |
| 705 | boolean flowFound = false; |
| 706 | |
| 707 | for (int jsonFlowIndex = 0; jsonFlowIndex < json.size(); |
| 708 | jsonFlowIndex++) { |
| 709 | |
| 710 | final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject(); |
| 711 | |
| 712 | final String flowId = Long.toString(flow.id().value()); |
| 713 | final String jsonFlowId = jsonFlow.get("id").asString(); |
| 714 | if (jsonFlowId.equals(flowId)) { |
| 715 | flowFound = true; |
| 716 | |
| 717 | // We found the correct flow, check attribute values |
| 718 | assertThat(jsonFlow, matchesFlowRule(flow, APP_ID.name())); |
| 719 | } |
| 720 | } |
| 721 | if (!flowFound) { |
| 722 | reason = "Flow with id " + flow.id().toString() + " not found"; |
| 723 | return false; |
| 724 | } else { |
| 725 | return true; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | @Override |
| 730 | public void describeTo(Description description) { |
| 731 | description.appendText(reason); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Factory to allocate a flow array matcher. |
| 737 | * |
| 738 | * @param flow flow rule object we are looking for |
| 739 | * @return matcher |
| 740 | */ |
| 741 | private static FlowRuleJsonArrayMatcher hasFlowRule(FlowRule flow) { |
| 742 | return new FlowRuleJsonArrayMatcher(flow); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Tests the result of the rest api GET when there are no flows. |
| 747 | */ |
| 748 | @Test |
| 749 | public void testFlowsEmptyArray() { |
| 750 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 751 | .andReturn(null).anyTimes(); |
| 752 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 753 | .andReturn(null).anyTimes(); |
| 754 | replay(mockFlowService); |
| 755 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 756 | final WebTarget wt = target(); |
| 757 | final String response = wt.path("flows").request().get(String.class); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 758 | assertThat(response, is("{\"flows\":[]}")); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Tests the result of the rest api GET when there are active flows. |
| 763 | */ |
| 764 | @Test |
| 765 | public void testFlowsPopulatedArray() { |
| 766 | setupMockFlows(); |
| 767 | replay(mockFlowService); |
| 768 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 769 | final WebTarget wt = target(); |
| 770 | final String response = wt.path("flows").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 771 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 772 | assertThat(result, notNullValue()); |
| 773 | |
| 774 | assertThat(result.names(), hasSize(1)); |
| 775 | assertThat(result.names().get(0), is("flows")); |
| 776 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 777 | assertThat(jsonFlows, notNullValue()); |
| 778 | assertThat(jsonFlows, hasFlow(flow1)); |
| 779 | assertThat(jsonFlows, hasFlow(flow2)); |
| 780 | assertThat(jsonFlows, hasFlow(flow3)); |
| 781 | assertThat(jsonFlows, hasFlow(flow4)); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Tests the result of a rest api GET for a device. |
| 786 | */ |
| 787 | @Test |
| 788 | public void testFlowsSingleDevice() { |
| 789 | setupMockFlows(); |
| 790 | final Set<FlowEntry> flows = new HashSet<>(); |
| 791 | flows.add(flow5); |
| 792 | flows.add(flow6); |
| 793 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 794 | .andReturn(flows).anyTimes(); |
| 795 | replay(mockFlowService); |
| 796 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 797 | final WebTarget wt = target(); |
| 798 | final String response = wt.path("flows/" + deviceId3).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 799 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 800 | assertThat(result, notNullValue()); |
| 801 | |
| 802 | assertThat(result.names(), hasSize(1)); |
| 803 | assertThat(result.names().get(0), is("flows")); |
| 804 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 805 | assertThat(jsonFlows, notNullValue()); |
| 806 | assertThat(jsonFlows, hasFlow(flow5)); |
| 807 | assertThat(jsonFlows, hasFlow(flow6)); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Tests the result of a rest api GET for a device. |
| 812 | */ |
| 813 | @Test |
| 814 | public void testFlowsSingleDeviceWithFlowId() { |
| 815 | setupMockFlows(); |
| 816 | final Set<FlowEntry> flows = new HashSet<>(); |
| 817 | flows.add(flow5); |
| 818 | flows.add(flow6); |
| 819 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 820 | .andReturn(flows).anyTimes(); |
| 821 | replay(mockFlowService); |
| 822 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 823 | final WebTarget wt = target(); |
| 824 | final String response = wt.path("flows/" + deviceId3 + "/" |
| 825 | + Long.toString(flow5.id().value())).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 826 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 827 | assertThat(result, notNullValue()); |
| 828 | |
| 829 | assertThat(result.names(), hasSize(1)); |
| 830 | assertThat(result.names().get(0), is("flows")); |
| 831 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 832 | assertThat(jsonFlows, notNullValue()); |
| 833 | assertThat(jsonFlows, hasFlow(flow5)); |
| 834 | assertThat(jsonFlows, not(hasFlow(flow6))); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Tests that a fetch of a non-existent device object throws an exception. |
| 839 | */ |
| 840 | @Test |
| 841 | public void testBadGet() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 842 | expect(mockFlowService.getFlowEntries(anyObject())) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 843 | .andReturn(null).anyTimes(); |
| 844 | replay(mockFlowService); |
| 845 | replay(mockDeviceService); |
| 846 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 847 | WebTarget wt = target(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 848 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 849 | wt.path("flows/0").request().get(String.class); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 850 | fail("Fetch of non-existent device did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 851 | } catch (NotFoundException ex) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 852 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 853 | containsString("HTTP 404 Not Found")); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 854 | } |
| 855 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 856 | |
| 857 | /** |
| 858 | * Tests creating a flow with POST. |
| 859 | */ |
| 860 | @Test |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 861 | public void testPostWithoutAppId() { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 862 | mockFlowService.applyFlowRules(anyObject()); |
| 863 | expectLastCall(); |
| 864 | replay(mockFlowService); |
| 865 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 866 | WebTarget wt = target(); |
Phaneendra Manda | 2be2f88 | 2015-11-11 15:23:40 +0530 | [diff] [blame] | 867 | InputStream jsonStream = FlowsResourceTest.class |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 868 | .getResourceAsStream("post-flow.json"); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 869 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 870 | Response response = wt.path("flows/of:0000000000000001") |
| 871 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 872 | .post(Entity.json(jsonStream)); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 873 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 874 | String location = response.getLocation().getPath(); |
| 875 | assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/")); |
| 876 | } |
| 877 | |
| 878 | /** |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 879 | * Tests creating a flow with POST while specifying application identifier. |
| 880 | */ |
| 881 | @Test |
| 882 | public void testPostWithAppId() { |
| 883 | mockFlowService.applyFlowRules(anyObject()); |
| 884 | expectLastCall(); |
| 885 | replay(mockFlowService); |
| 886 | |
| 887 | WebTarget wt = target(); |
| 888 | InputStream jsonStream = FlowsResourceTest.class |
| 889 | .getResourceAsStream("post-flow.json"); |
| 890 | |
| 891 | Response response = wt.path("flows/of:0000000000000001") |
| 892 | .queryParam("appId", "org.onosproject.rest") |
| 893 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 894 | .post(Entity.json(jsonStream)); |
| 895 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 896 | String location = response.getLocation().getPath(); |
| 897 | assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/")); |
| 898 | } |
| 899 | |
| 900 | /** |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 901 | * Tests deleting a flow. |
| 902 | */ |
| 903 | @Test |
| 904 | public void testDelete() { |
| 905 | setupMockFlows(); |
| 906 | mockFlowService.removeFlowRules(anyObject()); |
| 907 | expectLastCall(); |
| 908 | replay(mockFlowService); |
| 909 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 910 | WebTarget wt = target(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 911 | |
| 912 | String location = "/flows/1/155"; |
| 913 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 914 | Response deleteResponse = wt.path(location) |
| 915 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 916 | .delete(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 917 | assertThat(deleteResponse.getStatus(), |
| 918 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 919 | } |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 920 | |
| 921 | /** |
| 922 | * Tests the result of a rest api GET for an application. |
| 923 | */ |
| 924 | @Test |
| 925 | public void testGetFlowByAppId() { |
| 926 | setupMockFlowRules(); |
| 927 | |
| 928 | expect(mockApplicationService.getId(anyObject())).andReturn(APP_ID).anyTimes(); |
| 929 | replay(mockApplicationService); |
| 930 | |
| 931 | expect(mockFlowService.getFlowRulesById(APP_ID)).andReturn(flowRules).anyTimes(); |
| 932 | replay(mockFlowService); |
| 933 | |
| 934 | final WebTarget wt = target(); |
| 935 | final String response = wt.path("flows/application/1").request().get(String.class); |
| 936 | final JsonObject result = Json.parse(response).asObject(); |
| 937 | assertThat(result, notNullValue()); |
| 938 | |
| 939 | assertThat(result.names(), hasSize(1)); |
| 940 | assertThat(result.names().get(0), is("flows")); |
| 941 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 942 | assertThat(jsonFlows, notNullValue()); |
| 943 | assertThat(jsonFlows, hasFlowRule(flowRule1)); |
| 944 | assertThat(jsonFlows, hasFlowRule(flowRule2)); |
| 945 | assertThat(jsonFlows, hasFlowRule(flowRule3)); |
| 946 | assertThat(jsonFlows, hasFlowRule(flowRule4)); |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * Tests the result of a rest api DELETE for an application. |
| 951 | */ |
| 952 | @Test |
| 953 | public void testRemoveFlowByAppId() { |
| 954 | expect(mockApplicationService.getId(anyObject())).andReturn(APP_ID).anyTimes(); |
| 955 | replay(mockApplicationService); |
| 956 | |
| 957 | mockFlowService.removeFlowRulesById(APP_ID); |
| 958 | expectLastCall(); |
| 959 | replay(mockFlowService); |
| 960 | |
| 961 | WebTarget wt = target(); |
| 962 | |
| 963 | String location = "/flows/application/1"; |
| 964 | |
| 965 | Response deleteResponse = wt.path(location) |
| 966 | .request() |
| 967 | .delete(); |
| 968 | assertThat(deleteResponse.getStatus(), |
| 969 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 970 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 971 | } |