Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -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 | 2b21714 | 2014-12-15 09:24:24 -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 | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 17 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
| 19 | import com.eclipsesource.json.JsonArray; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import com.eclipsesource.json.JsonValue; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 22 | import org.hamcrest.Description; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 23 | import org.hamcrest.Matchers; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -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; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 31 | import org.onlab.rest.BaseResource; |
| 32 | import org.onosproject.codec.CodecService; |
| 33 | import org.onosproject.codec.impl.CodecManager; |
| 34 | import org.onosproject.core.ApplicationId; |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 35 | import org.onosproject.core.CoreService; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 36 | import org.onosproject.core.DefaultApplicationId; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 37 | import org.onosproject.core.DefaultGroupId; |
| 38 | import org.onosproject.core.GroupId; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 39 | import org.onosproject.core.IdGenerator; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 41 | import org.onosproject.net.NetworkResource; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.FlowEntry; |
| 45 | import org.onosproject.net.flow.FlowId; |
| 46 | import org.onosproject.net.flow.FlowRule; |
| 47 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
| 48 | import org.onosproject.net.flow.FlowRuleService; |
| 49 | import org.onosproject.net.flow.TrafficSelector; |
| 50 | import org.onosproject.net.flow.TrafficTreatment; |
| 51 | import org.onosproject.net.flow.criteria.Criterion; |
| 52 | import org.onosproject.net.flow.instructions.Instruction; |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 53 | import org.onosproject.net.intent.FakeIntentManager; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 54 | import org.onosproject.net.intent.FlowRuleIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 55 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 56 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 57 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 58 | import org.onosproject.net.intent.Key; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 59 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 60 | import javax.ws.rs.NotFoundException; |
| 61 | import javax.ws.rs.client.Entity; |
| 62 | import javax.ws.rs.client.WebTarget; |
| 63 | import javax.ws.rs.core.MediaType; |
| 64 | import javax.ws.rs.core.Response; |
| 65 | import java.io.InputStream; |
| 66 | import java.net.HttpURLConnection; |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Collections; |
| 69 | import java.util.HashSet; |
| 70 | import java.util.List; |
| 71 | import java.util.concurrent.TimeUnit; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 72 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 73 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 74 | import static org.easymock.EasyMock.*; |
| 75 | import static org.hamcrest.Matchers.*; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 76 | import static org.junit.Assert.assertThat; |
| 77 | import static org.junit.Assert.fail; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 78 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.intent.MockIdGenerator.bindNewGenerator; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Unit tests for Intents REST APIs. |
| 83 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 84 | public class IntentsResourceTest extends ResourceTest { |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 85 | |
| 86 | |
| 87 | private static final String APPID = "appId"; |
| 88 | private static final String CRITERIA = "criteria"; |
| 89 | private static final String DEVICE_ID = "deviceId"; |
| 90 | private static final String ID = "id"; |
| 91 | private static final String INSTRUCTIONS = "instructions"; |
| 92 | private static final String PATHS = "paths"; |
| 93 | private static final String SELECTOR = "selector"; |
| 94 | private static final String SPACE = " "; |
| 95 | private static final String TREATMENT = "treatment"; |
| 96 | private static final String TYPE = "type"; |
| 97 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 98 | final IntentService mockIntentService = createMock(IntentService.class); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 99 | final CoreService mockCoreService = createMock(CoreService.class); |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 100 | final FlowRuleService mockFlowService = createMock(FlowRuleService.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 101 | final HashSet<Intent> intents = new HashSet<>(); |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 102 | final List<org.onosproject.net.intent.Intent> installableIntents = new ArrayList<>(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 103 | private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test"); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 104 | private IdGenerator mockGenerator; |
| 105 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 106 | final DeviceId deviceId1 = DeviceId.deviceId("1"); |
| 107 | |
| 108 | final TrafficTreatment treatment1 = DefaultTrafficTreatment.builder() |
| 109 | .setEthDst(MacAddress.BROADCAST) |
| 110 | .build(); |
| 111 | final TrafficTreatment treatment2 = DefaultTrafficTreatment.builder() |
| 112 | .setEthDst(MacAddress.IPV4_MULTICAST) |
| 113 | .build(); |
| 114 | |
| 115 | final TrafficSelector selector1 = DefaultTrafficSelector.builder() |
| 116 | .matchEthType((short) 3) |
| 117 | .matchIPProtocol((byte) 9) |
| 118 | .build(); |
| 119 | final TrafficSelector selector2 = DefaultTrafficSelector.builder() |
| 120 | .matchEthType((short) 4) |
| 121 | .matchIPProtocol((byte) 10) |
| 122 | .build(); |
| 123 | |
| 124 | final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1, treatment1, selector1); |
| 125 | final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2, treatment2, selector2); |
| 126 | |
| 127 | final MockFlowRule flowRule1 = new MockFlowRule(deviceId1, 1, treatment1, selector1); |
| 128 | final MockFlowRule flowRule2 = new MockFlowRule(deviceId1, 2, treatment2, selector2); |
| 129 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 130 | private class MockResource implements NetworkResource { |
| 131 | int id; |
| 132 | |
| 133 | MockResource(int id) { |
| 134 | this.id = id; |
| 135 | } |
| 136 | |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 137 | @Override |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 138 | public String toString() { |
| 139 | return "Resource " + Integer.toString(id); |
| 140 | } |
| 141 | } |
| 142 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 143 | /** |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 144 | * Mock class for a flow entry. |
| 145 | */ |
| 146 | private static class MockFlowEntry implements FlowEntry { |
| 147 | final DeviceId deviceId; |
| 148 | final long baseValue; |
| 149 | TrafficTreatment treatment; |
| 150 | TrafficSelector selector; |
| 151 | |
| 152 | public MockFlowEntry(DeviceId deviceId, long id, |
| 153 | TrafficTreatment treatment, |
| 154 | TrafficSelector selector) { |
| 155 | this.deviceId = deviceId; |
| 156 | this.baseValue = id * 100; |
| 157 | this.treatment = treatment; |
| 158 | this.selector = selector; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public FlowEntryState state() { |
| 163 | return FlowEntryState.ADDED; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public long life() { |
| 168 | return life(SECONDS); |
| 169 | } |
| 170 | |
| 171 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 172 | public FlowLiveType liveType() { |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | @Override |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 177 | public long life(TimeUnit timeUnit) { |
| 178 | return SECONDS.convert(baseValue + 11, timeUnit); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public long packets() { |
| 183 | return baseValue + 22; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public long bytes() { |
| 188 | return baseValue + 33; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public long lastSeen() { |
| 193 | return baseValue + 44; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public int errType() { |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public int errCode() { |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public FlowId id() { |
| 208 | final long id = baseValue + 55; |
| 209 | return FlowId.valueOf(id); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public GroupId groupId() { |
| 214 | return new DefaultGroupId(3); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public short appId() { |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int priority() { |
| 224 | return (int) (baseValue + 66); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public DeviceId deviceId() { |
| 229 | return deviceId; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public TrafficSelector selector() { |
| 234 | return selector; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public TrafficTreatment treatment() { |
| 239 | return treatment; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public int timeout() { |
| 244 | return (int) (baseValue + 77); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public int hardTimeout() { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public FlowRule.FlowRemoveReason reason() { |
| 254 | return FlowRule.FlowRemoveReason.NO_REASON; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean isPermanent() { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public int tableId() { |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public boolean exactMatch(FlowRule rule) { |
| 269 | return this.appId() == rule.appId() && |
| 270 | this.deviceId().equals(rule.deviceId()) && |
| 271 | this.id().equals(rule.id()) && |
| 272 | this.treatment.equals(rule.treatment()) && |
| 273 | this.selector().equals(rule.selector()); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public FlowRuleExtPayLoad payLoad() { |
| 278 | return null; |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public String toString() { |
| 283 | return id().id().toString(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Mock class for a flow rule. |
| 289 | */ |
| 290 | private static class MockFlowRule implements FlowRule { |
| 291 | |
| 292 | final DeviceId deviceId; |
| 293 | final long baseValue; |
| 294 | TrafficTreatment treatment; |
| 295 | TrafficSelector selector; |
| 296 | |
| 297 | public MockFlowRule(DeviceId deviceId, |
| 298 | long id, |
| 299 | TrafficTreatment treatment, |
| 300 | TrafficSelector selector) { |
| 301 | this.deviceId = deviceId; |
| 302 | this.baseValue = id * 100; |
| 303 | this.treatment = treatment; |
| 304 | this.selector = selector; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public FlowId id() { |
| 309 | long id = baseValue + 55; |
| 310 | return FlowId.valueOf(id); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public short appId() { |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public GroupId groupId() { |
| 320 | return new DefaultGroupId(3); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public int priority() { |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public DeviceId deviceId() { |
| 330 | return deviceId; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public TrafficSelector selector() { |
| 335 | return selector; |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public TrafficTreatment treatment() { |
| 340 | return treatment; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public int timeout() { |
| 345 | return (int) (baseValue + 77); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public int hardTimeout() { |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public FlowRemoveReason reason() { |
| 355 | return FlowRemoveReason.NO_REASON; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public boolean isPermanent() { |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public int tableId() { |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public boolean exactMatch(FlowRule rule) { |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public FlowRuleExtPayLoad payLoad() { |
| 375 | return null; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 380 | * Hamcrest matcher to check that an intent representation in JSON matches |
| 381 | * the actual intent. |
| 382 | */ |
| 383 | public static class IntentJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 384 | private final Intent intent; |
| 385 | private String reason = ""; |
| 386 | |
| 387 | public IntentJsonMatcher(Intent intentValue) { |
| 388 | intent = intentValue; |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public boolean matchesSafely(JsonObject jsonIntent) { |
| 393 | // check id |
| 394 | final String jsonId = jsonIntent.get("id").asString(); |
| 395 | if (!jsonId.equals(intent.id().toString())) { |
| 396 | reason = "id " + intent.id().toString(); |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | // check application id |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 401 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 402 | final String jsonAppId = jsonIntent.get("appId").asString(); |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 403 | final String appId = intent.appId().name(); |
| 404 | if (!jsonAppId.equals(appId)) { |
| 405 | reason = "appId was " + jsonAppId; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | |
| 409 | // check intent type |
| 410 | final String jsonType = jsonIntent.get("type").asString(); |
| 411 | if (!jsonType.equals("MockIntent")) { |
| 412 | reason = "type MockIntent"; |
| 413 | return false; |
| 414 | } |
| 415 | |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 416 | // check state field |
| 417 | final String jsonState = jsonIntent.get("state").asString(); |
| 418 | if (!jsonState.equals("INSTALLED")) { |
| 419 | reason = "state INSTALLED"; |
| 420 | return false; |
| 421 | } |
| 422 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 423 | // check resources array |
| 424 | final JsonArray jsonResources = jsonIntent.get("resources").asArray(); |
| 425 | if (intent.resources() != null) { |
| 426 | if (intent.resources().size() != jsonResources.size()) { |
| 427 | reason = "resources array size of " + Integer.toString(intent.resources().size()); |
| 428 | return false; |
| 429 | } |
| 430 | for (final NetworkResource resource : intent.resources()) { |
| 431 | boolean resourceFound = false; |
| 432 | final String resourceString = resource.toString(); |
| 433 | for (int resourceIndex = 0; resourceIndex < jsonResources.size(); resourceIndex++) { |
| 434 | final JsonValue value = jsonResources.get(resourceIndex); |
| 435 | if (value.asString().equals(resourceString)) { |
| 436 | resourceFound = true; |
| 437 | } |
| 438 | } |
| 439 | if (!resourceFound) { |
| 440 | reason = "resource " + resourceString; |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | } else if (jsonResources.size() != 0) { |
| 445 | reason = "resources array empty"; |
| 446 | return false; |
| 447 | } |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public void describeTo(Description description) { |
| 453 | description.appendText(reason); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Factory to allocate an intent matcher. |
| 459 | * |
| 460 | * @param intent intent object we are looking for |
| 461 | * @return matcher |
| 462 | */ |
| 463 | private static IntentJsonMatcher matchesIntent(Intent intent) { |
| 464 | return new IntentJsonMatcher(intent); |
| 465 | } |
| 466 | |
| 467 | /** |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 468 | * Factory to allocate an IntentRelatedFlows matcher. |
| 469 | * |
| 470 | * @param pathEntries list of path conatining flow entries of a particular intent |
| 471 | * @param expectedAppId expected app id we are looking for |
| 472 | * @return matcher |
| 473 | */ |
| 474 | private static IntentStatsJsonMatcher matchesRelatedFlowEntries( |
| 475 | List<List<FlowEntry>> pathEntries, |
| 476 | final String expectedAppId) { |
| 477 | return new IntentStatsJsonMatcher(pathEntries, expectedAppId); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Hamcrest matcher to check that an list of flowEntries in JSON matches |
| 482 | * the actual list of flow entries. |
| 483 | */ |
| 484 | public static class IntentStatsJsonMatcher extends |
| 485 | TypeSafeMatcher<JsonObject> { |
| 486 | |
| 487 | private final List<List<FlowEntry>> pathEntries; |
| 488 | private final String expectedAppId; |
| 489 | private String reason = ""; |
| 490 | |
| 491 | public IntentStatsJsonMatcher( |
| 492 | final List<List<FlowEntry>> pathEntries, |
| 493 | final String expectedAppId) { |
| 494 | this.pathEntries = pathEntries; |
| 495 | this.expectedAppId = expectedAppId; |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean matchesSafely(JsonObject jsonIntent) { |
| 500 | int jsonPathIndex = 0; |
| 501 | JsonArray jsonPaths = jsonIntent.get(PATHS).asArray(); |
| 502 | |
| 503 | if (pathEntries != null) { |
| 504 | |
rohitsharan | fd74760 | 2017-01-23 21:57:28 +0530 | [diff] [blame] | 505 | if (pathEntries.size() == 0) { |
| 506 | reason = "pathEntries array empty"; |
| 507 | return false; |
| 508 | } |
| 509 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 510 | if (pathEntries.size() != jsonPaths.size()) { |
| 511 | reason = "path entries array size of " + |
| 512 | Integer.toString(pathEntries.size()); |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | for (List<FlowEntry> flowEntries : pathEntries) { |
| 517 | JsonArray jsonFlowEntries = jsonPaths.get( |
| 518 | jsonPathIndex++).asArray(); |
| 519 | |
| 520 | if (flowEntries.size() != jsonFlowEntries.size()) { |
| 521 | reason = "flow entries array size of " + |
| 522 | Integer.toString(pathEntries.size()); |
| 523 | |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | int jsonFlowEntryIndex = 0; |
| 528 | for (FlowEntry flow : flowEntries) { |
| 529 | |
| 530 | JsonObject jsonFlow = jsonFlowEntries.get( |
| 531 | jsonFlowEntryIndex++).asObject(); |
| 532 | |
| 533 | String jsonId = jsonFlow.get(ID).asString(); |
| 534 | String flowId = Long.toString(flow.id().value()); |
| 535 | if (!jsonId.equals(flowId)) { |
| 536 | reason = ID + SPACE + flow.id(); |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | // check application id |
| 541 | String jsonAppId = jsonFlow.get(APPID).asString(); |
| 542 | if (!jsonAppId.equals(expectedAppId)) { |
| 543 | reason = APPID + SPACE + Short.toString(flow.appId()); |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | // check device id |
| 548 | String jsonDeviceId = |
| 549 | jsonFlow.get(DEVICE_ID).asString(); |
| 550 | |
| 551 | if (!jsonDeviceId.equals(flow.deviceId().toString())) { |
| 552 | reason = DEVICE_ID + SPACE + flow.deviceId(); |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | if (!checkFlowTreatment(flow, jsonFlow)) { |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | if (!checkFlowSelector(flow, jsonFlow)) { |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | } |
| 565 | |
| 566 | } |
rohitsharan | fd74760 | 2017-01-23 21:57:28 +0530 | [diff] [blame] | 567 | } else { |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 568 | reason = "pathEntries array empty"; |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | // check treatment and instructions array. |
| 576 | private boolean checkFlowTreatment(FlowEntry flow, JsonObject jsonFlow) { |
| 577 | |
| 578 | if (flow.treatment() != null) { |
| 579 | JsonObject jsonTreatment = |
| 580 | jsonFlow.get(TREATMENT).asObject(); |
| 581 | JsonArray jsonInstructions = |
| 582 | jsonTreatment.get(INSTRUCTIONS).asArray(); |
| 583 | |
| 584 | if (flow.treatment().immediate().size() != |
| 585 | jsonInstructions.size()) { |
| 586 | reason = "instructions array size of " + |
| 587 | flow.treatment().immediate().size(); |
| 588 | |
| 589 | return false; |
| 590 | } |
| 591 | for (Instruction instruction : |
| 592 | flow.treatment().immediate()) { |
| 593 | boolean instructionFound = false; |
| 594 | for (int instructionIndex = 0; |
| 595 | instructionIndex < jsonInstructions.size(); |
| 596 | instructionIndex++) { |
| 597 | String jsonType = |
| 598 | jsonInstructions.get(instructionIndex) |
| 599 | .asObject().get(TYPE).asString(); |
| 600 | |
| 601 | String instructionType = |
| 602 | instruction.type().name(); |
| 603 | |
| 604 | if (jsonType.equals(instructionType)) { |
| 605 | instructionFound = true; |
| 606 | } |
| 607 | } |
| 608 | if (!instructionFound) { |
| 609 | reason = INSTRUCTIONS + SPACE + instruction; |
| 610 | return false; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | return true; |
| 615 | } |
| 616 | |
| 617 | // check selector and criteria array. |
| 618 | private boolean checkFlowSelector(FlowEntry flow, JsonObject jsonFlow) { |
| 619 | |
| 620 | if (flow.selector() != null) { |
| 621 | JsonObject jsonTreatment = |
| 622 | jsonFlow.get(SELECTOR).asObject(); |
| 623 | |
| 624 | JsonArray jsonCriteria = |
| 625 | jsonTreatment.get(CRITERIA).asArray(); |
| 626 | |
| 627 | if (flow.selector().criteria().size() != jsonCriteria.size()) { |
| 628 | reason = CRITERIA + " array size of " + |
| 629 | Integer.toString(flow.selector().criteria().size()); |
| 630 | return false; |
| 631 | } |
| 632 | for (Criterion criterion : flow.selector().criteria()) { |
| 633 | boolean criterionFound = false; |
| 634 | |
| 635 | for (int criterionIndex = 0; |
| 636 | criterionIndex < jsonCriteria.size(); |
| 637 | criterionIndex++) { |
| 638 | String jsonType = |
| 639 | jsonCriteria.get(criterionIndex) |
| 640 | .asObject().get(TYPE).asString(); |
| 641 | String criterionType = criterion.type().name(); |
| 642 | if (jsonType.equals(criterionType)) { |
| 643 | criterionFound = true; |
| 644 | } |
| 645 | } |
| 646 | if (!criterionFound) { |
| 647 | reason = "criterion " + criterion; |
| 648 | return false; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | @Override |
| 656 | public void describeTo(Description description) { |
| 657 | description.appendText(reason); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /** |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 662 | * Hamcrest matcher to check that an intent is represented properly in a JSON |
| 663 | * array of intents. |
| 664 | */ |
| 665 | public static class IntentJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 666 | private final Intent intent; |
| 667 | private String reason = ""; |
| 668 | |
| 669 | public IntentJsonArrayMatcher(Intent intentValue) { |
| 670 | intent = intentValue; |
| 671 | } |
| 672 | |
| 673 | @Override |
| 674 | public boolean matchesSafely(JsonArray json) { |
| 675 | boolean intentFound = false; |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 676 | final int expectedAttributes = 5; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 677 | for (int jsonIntentIndex = 0; jsonIntentIndex < json.size(); |
| 678 | jsonIntentIndex++) { |
| 679 | |
| 680 | final JsonObject jsonIntent = json.get(jsonIntentIndex).asObject(); |
| 681 | |
| 682 | if (jsonIntent.names().size() != expectedAttributes) { |
| 683 | reason = "Found an intent with the wrong number of attributes"; |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | final String jsonIntentId = jsonIntent.get("id").asString(); |
| 688 | if (jsonIntentId.equals(intent.id().toString())) { |
| 689 | intentFound = true; |
| 690 | |
| 691 | // We found the correct intent, check attribute values |
| 692 | assertThat(jsonIntent, matchesIntent(intent)); |
| 693 | } |
| 694 | } |
| 695 | if (!intentFound) { |
| 696 | reason = "Intent with id " + intent.id().toString() + " not found"; |
| 697 | return false; |
| 698 | } else { |
| 699 | return true; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public void describeTo(Description description) { |
| 705 | description.appendText(reason); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Factory to allocate an intent array matcher. |
| 711 | * |
| 712 | * @param intent intent object we are looking for |
| 713 | * @return matcher |
| 714 | */ |
| 715 | private static IntentJsonArrayMatcher hasIntent(Intent intent) { |
| 716 | return new IntentJsonArrayMatcher(intent); |
| 717 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 718 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 719 | /** |
| 720 | * Initializes test mocks and environment. |
| 721 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 722 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 723 | public void setUpTest() { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 724 | expect(mockIntentService.getIntents()).andReturn(intents).anyTimes(); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 725 | expect(mockIntentService.getIntentState(anyObject())) |
| 726 | .andReturn(IntentState.INSTALLED) |
| 727 | .anyTimes(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 728 | // Register the services needed for the test |
| 729 | final CodecManager codecService = new CodecManager(); |
| 730 | codecService.activate(); |
| 731 | ServiceDirectory testDirectory = |
| 732 | new TestServiceDirectory() |
| 733 | .add(IntentService.class, mockIntentService) |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 734 | .add(FlowRuleService.class, mockFlowService) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 735 | .add(CodecService.class, codecService) |
| 736 | .add(CoreService.class, mockCoreService); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 737 | |
| 738 | BaseResource.setServiceDirectory(testDirectory); |
| 739 | |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 740 | bindNewGenerator(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 743 | /** |
| 744 | * Tears down and verifies test mocks and environment. |
| 745 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 746 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 747 | public void tearDownTest() { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 748 | verify(mockIntentService); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Tests the result of the rest api GET when there are no intents. |
| 753 | */ |
| 754 | @Test |
| 755 | public void testIntentsEmptyArray() { |
| 756 | replay(mockIntentService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 757 | final WebTarget wt = target(); |
| 758 | final String response = wt.path("intents").request().get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 759 | assertThat(response, is("{\"intents\":[]}")); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Tests the result of the rest api GET when intents are defined. |
| 764 | */ |
| 765 | @Test |
| 766 | public void testIntentsArray() { |
| 767 | replay(mockIntentService); |
| 768 | |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 769 | final Intent intent1 = new MockIntent(1L, Collections.emptyList()); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 770 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 771 | resources.add(new MockResource(1)); |
| 772 | resources.add(new MockResource(2)); |
| 773 | resources.add(new MockResource(3)); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 774 | final Intent intent2 = new MockIntent(2L, resources); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 775 | |
| 776 | intents.add(intent1); |
| 777 | intents.add(intent2); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 778 | final WebTarget wt = target(); |
| 779 | final String response = wt.path("intents").request().get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 780 | assertThat(response, containsString("{\"intents\":[")); |
| 781 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 782 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 783 | assertThat(result, notNullValue()); |
| 784 | |
| 785 | assertThat(result.names(), hasSize(1)); |
| 786 | assertThat(result.names().get(0), is("intents")); |
| 787 | |
| 788 | final JsonArray jsonIntents = result.get("intents").asArray(); |
| 789 | assertThat(jsonIntents, notNullValue()); |
| 790 | |
| 791 | assertThat(jsonIntents, hasIntent(intent1)); |
| 792 | assertThat(jsonIntents, hasIntent(intent2)); |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Tests the result of a rest api GET for a single intent. |
| 797 | */ |
| 798 | @Test |
| 799 | public void testIntentsSingle() { |
| 800 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 801 | resources.add(new MockResource(1)); |
| 802 | resources.add(new MockResource(2)); |
| 803 | resources.add(new MockResource(3)); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 804 | final Intent intent = new MockIntent(3L, resources); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 805 | |
| 806 | intents.add(intent); |
| 807 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 808 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 809 | .andReturn(intent) |
| 810 | .anyTimes(); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 811 | expect(mockIntentService.getIntent(Key.of("0", APP_ID))) |
| 812 | .andReturn(intent) |
| 813 | .anyTimes(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 814 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
| 815 | .andReturn(intent) |
| 816 | .anyTimes(); |
| 817 | expect(mockIntentService.getIntent(Key.of("0x0", APP_ID))) |
| 818 | .andReturn(null) |
| 819 | .anyTimes(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 820 | replay(mockIntentService); |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 821 | expect(mockCoreService.getAppId(APP_ID.name())) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 822 | .andReturn(APP_ID).anyTimes(); |
| 823 | replay(mockCoreService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 824 | final WebTarget wt = target(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 825 | |
| 826 | // Test get using key string |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 827 | final String response = wt.path("intents/" + APP_ID.name() |
| 828 | + "/0").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 829 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 830 | assertThat(result, matchesIntent(intent)); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 831 | |
| 832 | // Test get using numeric value |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 833 | final String responseNumeric = wt.path("intents/" + APP_ID.name() |
| 834 | + "/0x0").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 835 | final JsonObject resultNumeric = Json.parse(responseNumeric).asObject(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 836 | assertThat(resultNumeric, matchesIntent(intent)); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 840 | * Tests the result of a rest api GET for related flows for single intent. |
| 841 | */ |
| 842 | @Test |
| 843 | public void testRelatedFlowsForIntents() { |
| 844 | List<FlowEntry> flowEntries = new ArrayList<>(); |
| 845 | flowEntries.add(flow1); |
| 846 | flowEntries.add(flow2); |
| 847 | List<List<FlowEntry>> paths = new ArrayList<>(); |
| 848 | paths.add(flowEntries); |
| 849 | List<FlowRule> flowRules = new ArrayList<>(); |
| 850 | flowRules.add(flowRule1); |
| 851 | flowRules.add(flowRule2); |
| 852 | FlowRuleIntent flowRuleIntent = new FlowRuleIntent( |
| 853 | APP_ID, |
| 854 | flowRules, |
| 855 | new HashSet<NetworkResource>()); |
| 856 | Intent intent = new MockIntent(3L); |
| 857 | installableIntents.add(flowRuleIntent); |
| 858 | intents.add(intent); |
| 859 | |
| 860 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
| 861 | .andReturn(intent) |
| 862 | .anyTimes(); |
| 863 | expect(mockIntentService.getIntent(Key.of("0", APP_ID))) |
| 864 | .andReturn(intent) |
| 865 | .anyTimes(); |
| 866 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
| 867 | .andReturn(intent) |
| 868 | .anyTimes(); |
| 869 | expect(mockIntentService.getIntent(Key.of("0x0", APP_ID))) |
| 870 | .andReturn(null) |
| 871 | .anyTimes(); |
| 872 | expect(mockIntentService.getInstallableIntents(intent.key())) |
| 873 | .andReturn(installableIntents) |
| 874 | .anyTimes(); |
| 875 | replay(mockIntentService); |
| 876 | |
| 877 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 878 | .andReturn(flowEntries).anyTimes(); |
| 879 | replay(mockFlowService); |
| 880 | |
| 881 | expect(mockCoreService.getAppId(APP_ID.name())) |
| 882 | .andReturn(APP_ID).anyTimes(); |
| 883 | expect(mockCoreService.getAppId(APP_ID.id())) |
| 884 | .andReturn(APP_ID).anyTimes(); |
| 885 | replay(mockCoreService); |
| 886 | |
| 887 | final WebTarget wt = target(); |
| 888 | |
| 889 | // Test get using key string |
| 890 | final String response = wt.path("intents/relatedflows/" + APP_ID.name() |
| 891 | + "/0").request().get(String.class); |
| 892 | final JsonObject result = Json.parse(response).asObject(); |
| 893 | assertThat(result, matchesRelatedFlowEntries(paths, APP_ID.name())); |
| 894 | |
| 895 | // Test get using numeric value |
| 896 | final String responseNumeric = wt.path("intents/relatedflows/" + APP_ID.name() |
| 897 | + "/0x0").request().get(String.class); |
| 898 | final JsonObject resultNumeric = Json.parse(responseNumeric).asObject(); |
| 899 | assertThat(resultNumeric, matchesRelatedFlowEntries(paths, APP_ID.name())); |
| 900 | } |
| 901 | |
| 902 | /** |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 903 | * Tests that a fetch of a non-existent intent object throws an exception. |
| 904 | */ |
| 905 | @Test |
| 906 | public void testBadGet() { |
| 907 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 908 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 909 | .andReturn(null) |
| 910 | .anyTimes(); |
| 911 | replay(mockIntentService); |
| 912 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 913 | WebTarget wt = target(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 914 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 915 | wt.path("intents/0").request().get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 916 | fail("Fetch of non-existent intent did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 917 | } catch (NotFoundException ex) { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 918 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 919 | containsString("HTTP 404 Not Found")); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 920 | } |
| 921 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 922 | |
| 923 | /** |
| 924 | * Tests creating an intent with POST. |
| 925 | */ |
| 926 | @Test |
| 927 | public void testPost() { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 928 | ApplicationId testId = new DefaultApplicationId(2, "myApp"); |
| 929 | expect(mockCoreService.getAppId("myApp")) |
| 930 | .andReturn(testId); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 931 | replay(mockCoreService); |
| 932 | |
| 933 | mockIntentService.submit(anyObject()); |
| 934 | expectLastCall(); |
| 935 | replay(mockIntentService); |
| 936 | |
| 937 | InputStream jsonStream = IntentsResourceTest.class |
| 938 | .getResourceAsStream("post-intent.json"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 939 | WebTarget wt = target(); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 940 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 941 | Response response = wt.path("intents") |
| 942 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 943 | .post(Entity.json(jsonStream)); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 944 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 945 | String location = response.getLocation().getPath(); |
Ray Milkey | 8d07640 | 2015-08-31 15:43:18 -0700 | [diff] [blame] | 946 | assertThat(location, Matchers.startsWith("/intents/myApp/")); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 947 | } |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 948 | |
| 949 | /** |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 950 | * Tests creating an intent with POST and illegal JSON. |
| 951 | */ |
| 952 | @Test |
| 953 | public void testBadPost() { |
| 954 | replay(mockCoreService); |
| 955 | replay(mockIntentService); |
| 956 | |
| 957 | String json = "this is invalid!"; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 958 | WebTarget wt = target(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 959 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 960 | Response response = wt.path("intents") |
| 961 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 962 | .post(Entity.json(json)); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 963 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST)); |
| 964 | } |
| 965 | |
| 966 | /** |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 967 | * Tests removing an intent with DELETE. |
| 968 | */ |
| 969 | @Test |
| 970 | public void testRemove() { |
| 971 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 972 | resources.add(new MockResource(1)); |
| 973 | resources.add(new MockResource(2)); |
| 974 | resources.add(new MockResource(3)); |
| 975 | final Intent intent = new MockIntent(3L, resources); |
| 976 | final ApplicationId appId = new DefaultApplicationId(2, "app"); |
| 977 | IntentService fakeManager = new FakeIntentManager(); |
| 978 | |
| 979 | expect(mockCoreService.getAppId("app")) |
| 980 | .andReturn(appId).once(); |
| 981 | replay(mockCoreService); |
| 982 | |
| 983 | mockIntentService.withdraw(anyObject()); |
| 984 | expectLastCall().andDelegateTo(fakeManager).once(); |
| 985 | expect(mockIntentService.getIntent(Key.of(2, appId))) |
| 986 | .andReturn(intent) |
| 987 | .once(); |
| 988 | expect(mockIntentService.getIntent(Key.of("0x2", appId))) |
| 989 | .andReturn(null) |
| 990 | .once(); |
| 991 | |
| 992 | mockIntentService.addListener(anyObject()); |
| 993 | expectLastCall().andDelegateTo(fakeManager).once(); |
| 994 | mockIntentService.removeListener(anyObject()); |
| 995 | expectLastCall().andDelegateTo(fakeManager).once(); |
| 996 | |
| 997 | replay(mockIntentService); |
| 998 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 999 | WebTarget wt = target(); |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 1000 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 1001 | Response response = wt.path("intents/app/0x2") |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 1002 | .request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 1003 | .delete(); |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 1004 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 1005 | } |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 1006 | |
| 1007 | /** |
| 1008 | * Tests removal of a non existent intent with DELETE. |
| 1009 | */ |
| 1010 | @Test |
| 1011 | public void testBadRemove() { |
| 1012 | final ApplicationId appId = new DefaultApplicationId(2, "app"); |
| 1013 | |
| 1014 | expect(mockCoreService.getAppId("app")) |
| 1015 | .andReturn(appId).once(); |
| 1016 | replay(mockCoreService); |
| 1017 | |
| 1018 | expect(mockIntentService.getIntent(Key.of(2, appId))) |
| 1019 | .andReturn(null) |
| 1020 | .once(); |
| 1021 | expect(mockIntentService.getIntent(Key.of("0x2", appId))) |
| 1022 | .andReturn(null) |
| 1023 | .once(); |
| 1024 | |
| 1025 | replay(mockIntentService); |
| 1026 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 1027 | WebTarget wt = target(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 1028 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 1029 | Response response = wt.path("intents/app/0x2") |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 1030 | .request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 1031 | .delete(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 1032 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 1033 | } |
| 1034 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 1035 | } |