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