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 | |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 18 | import java.io.InputStream; |
| 19 | import java.net.HttpURLConnection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.HashSet; |
| 22 | |
| 23 | import javax.ws.rs.NotFoundException; |
| 24 | import javax.ws.rs.client.Entity; |
| 25 | import javax.ws.rs.client.WebTarget; |
| 26 | import javax.ws.rs.core.MediaType; |
| 27 | import javax.ws.rs.core.Response; |
| 28 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 29 | import org.hamcrest.Description; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 30 | import org.hamcrest.Matchers; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 31 | import org.hamcrest.TypeSafeMatcher; |
| 32 | import org.junit.After; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.onlab.osgi.ServiceDirectory; |
| 36 | import org.onlab.osgi.TestServiceDirectory; |
| 37 | import org.onlab.rest.BaseResource; |
| 38 | import org.onosproject.codec.CodecService; |
| 39 | import org.onosproject.codec.impl.CodecManager; |
| 40 | import org.onosproject.core.ApplicationId; |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 41 | import org.onosproject.core.CoreService; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 42 | import org.onosproject.core.DefaultApplicationId; |
| 43 | import org.onosproject.core.IdGenerator; |
| 44 | import org.onosproject.net.NetworkResource; |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 45 | import org.onosproject.net.intent.FakeIntentManager; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 48 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.Key; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 50 | |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 51 | import com.eclipsesource.json.Json; |
| 52 | import com.eclipsesource.json.JsonArray; |
| 53 | import com.eclipsesource.json.JsonObject; |
| 54 | import com.eclipsesource.json.JsonValue; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 55 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 56 | import static org.easymock.EasyMock.anyObject; |
| 57 | import static org.easymock.EasyMock.createMock; |
| 58 | import static org.easymock.EasyMock.expect; |
| 59 | import static org.easymock.EasyMock.expectLastCall; |
| 60 | import static org.easymock.EasyMock.replay; |
| 61 | import static org.easymock.EasyMock.verify; |
| 62 | import static org.hamcrest.Matchers.containsString; |
| 63 | import static org.hamcrest.Matchers.hasSize; |
| 64 | import static org.hamcrest.Matchers.is; |
| 65 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 66 | import static org.junit.Assert.assertThat; |
| 67 | import static org.junit.Assert.fail; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 69 | import static org.onosproject.net.intent.MockIdGenerator.bindNewGenerator; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Unit tests for Intents REST APIs. |
| 73 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 74 | public class IntentsResourceTest extends ResourceTest { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 75 | final IntentService mockIntentService = createMock(IntentService.class); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 76 | final CoreService mockCoreService = createMock(CoreService.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 77 | final HashSet<Intent> intents = new HashSet<>(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 78 | private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test"); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 79 | private IdGenerator mockGenerator; |
| 80 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 81 | private class MockResource implements NetworkResource { |
| 82 | int id; |
| 83 | |
| 84 | MockResource(int id) { |
| 85 | this.id = id; |
| 86 | } |
| 87 | |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 88 | @Override |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 89 | public String toString() { |
| 90 | return "Resource " + Integer.toString(id); |
| 91 | } |
| 92 | } |
| 93 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Hamcrest matcher to check that an intent representation in JSON matches |
| 96 | * the actual intent. |
| 97 | */ |
| 98 | public static class IntentJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 99 | private final Intent intent; |
| 100 | private String reason = ""; |
| 101 | |
| 102 | public IntentJsonMatcher(Intent intentValue) { |
| 103 | intent = intentValue; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean matchesSafely(JsonObject jsonIntent) { |
| 108 | // check id |
| 109 | final String jsonId = jsonIntent.get("id").asString(); |
| 110 | if (!jsonId.equals(intent.id().toString())) { |
| 111 | reason = "id " + intent.id().toString(); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // check application id |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 116 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 117 | final String jsonAppId = jsonIntent.get("appId").asString(); |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 118 | final String appId = intent.appId().name(); |
| 119 | if (!jsonAppId.equals(appId)) { |
| 120 | reason = "appId was " + jsonAppId; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // check intent type |
| 125 | final String jsonType = jsonIntent.get("type").asString(); |
| 126 | if (!jsonType.equals("MockIntent")) { |
| 127 | reason = "type MockIntent"; |
| 128 | return false; |
| 129 | } |
| 130 | |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 131 | // check state field |
| 132 | final String jsonState = jsonIntent.get("state").asString(); |
| 133 | if (!jsonState.equals("INSTALLED")) { |
| 134 | reason = "state INSTALLED"; |
| 135 | return false; |
| 136 | } |
| 137 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 138 | // check resources array |
| 139 | final JsonArray jsonResources = jsonIntent.get("resources").asArray(); |
| 140 | if (intent.resources() != null) { |
| 141 | if (intent.resources().size() != jsonResources.size()) { |
| 142 | reason = "resources array size of " + Integer.toString(intent.resources().size()); |
| 143 | return false; |
| 144 | } |
| 145 | for (final NetworkResource resource : intent.resources()) { |
| 146 | boolean resourceFound = false; |
| 147 | final String resourceString = resource.toString(); |
| 148 | for (int resourceIndex = 0; resourceIndex < jsonResources.size(); resourceIndex++) { |
| 149 | final JsonValue value = jsonResources.get(resourceIndex); |
| 150 | if (value.asString().equals(resourceString)) { |
| 151 | resourceFound = true; |
| 152 | } |
| 153 | } |
| 154 | if (!resourceFound) { |
| 155 | reason = "resource " + resourceString; |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | } else if (jsonResources.size() != 0) { |
| 160 | reason = "resources array empty"; |
| 161 | return false; |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void describeTo(Description description) { |
| 168 | description.appendText(reason); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Factory to allocate an intent matcher. |
| 174 | * |
| 175 | * @param intent intent object we are looking for |
| 176 | * @return matcher |
| 177 | */ |
| 178 | private static IntentJsonMatcher matchesIntent(Intent intent) { |
| 179 | return new IntentJsonMatcher(intent); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Hamcrest matcher to check that an intent is represented properly in a JSON |
| 184 | * array of intents. |
| 185 | */ |
| 186 | public static class IntentJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 187 | private final Intent intent; |
| 188 | private String reason = ""; |
| 189 | |
| 190 | public IntentJsonArrayMatcher(Intent intentValue) { |
| 191 | intent = intentValue; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean matchesSafely(JsonArray json) { |
| 196 | boolean intentFound = false; |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 197 | final int expectedAttributes = 5; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 198 | for (int jsonIntentIndex = 0; jsonIntentIndex < json.size(); |
| 199 | jsonIntentIndex++) { |
| 200 | |
| 201 | final JsonObject jsonIntent = json.get(jsonIntentIndex).asObject(); |
| 202 | |
| 203 | if (jsonIntent.names().size() != expectedAttributes) { |
| 204 | reason = "Found an intent with the wrong number of attributes"; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | final String jsonIntentId = jsonIntent.get("id").asString(); |
| 209 | if (jsonIntentId.equals(intent.id().toString())) { |
| 210 | intentFound = true; |
| 211 | |
| 212 | // We found the correct intent, check attribute values |
| 213 | assertThat(jsonIntent, matchesIntent(intent)); |
| 214 | } |
| 215 | } |
| 216 | if (!intentFound) { |
| 217 | reason = "Intent with id " + intent.id().toString() + " not found"; |
| 218 | return false; |
| 219 | } else { |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void describeTo(Description description) { |
| 226 | description.appendText(reason); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Factory to allocate an intent array matcher. |
| 232 | * |
| 233 | * @param intent intent object we are looking for |
| 234 | * @return matcher |
| 235 | */ |
| 236 | private static IntentJsonArrayMatcher hasIntent(Intent intent) { |
| 237 | return new IntentJsonArrayMatcher(intent); |
| 238 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 239 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 240 | /** |
| 241 | * Initializes test mocks and environment. |
| 242 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 243 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 244 | public void setUpTest() { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 245 | expect(mockIntentService.getIntents()).andReturn(intents).anyTimes(); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 246 | expect(mockIntentService.getIntentState(anyObject())) |
| 247 | .andReturn(IntentState.INSTALLED) |
| 248 | .anyTimes(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 249 | // Register the services needed for the test |
| 250 | final CodecManager codecService = new CodecManager(); |
| 251 | codecService.activate(); |
| 252 | ServiceDirectory testDirectory = |
| 253 | new TestServiceDirectory() |
| 254 | .add(IntentService.class, mockIntentService) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 255 | .add(CodecService.class, codecService) |
| 256 | .add(CoreService.class, mockCoreService); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 257 | |
| 258 | BaseResource.setServiceDirectory(testDirectory); |
| 259 | |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 260 | bindNewGenerator(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 263 | /** |
| 264 | * Tears down and verifies test mocks and environment. |
| 265 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 266 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 267 | public void tearDownTest() { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 268 | verify(mockIntentService); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Tests the result of the rest api GET when there are no intents. |
| 273 | */ |
| 274 | @Test |
| 275 | public void testIntentsEmptyArray() { |
| 276 | replay(mockIntentService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 277 | final WebTarget wt = target(); |
| 278 | final String response = wt.path("intents").request().get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 279 | assertThat(response, is("{\"intents\":[]}")); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Tests the result of the rest api GET when intents are defined. |
| 284 | */ |
| 285 | @Test |
| 286 | public void testIntentsArray() { |
| 287 | replay(mockIntentService); |
| 288 | |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 289 | final Intent intent1 = new MockIntent(1L, Collections.emptyList()); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 290 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 291 | resources.add(new MockResource(1)); |
| 292 | resources.add(new MockResource(2)); |
| 293 | resources.add(new MockResource(3)); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 294 | final Intent intent2 = new MockIntent(2L, resources); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 295 | |
| 296 | intents.add(intent1); |
| 297 | intents.add(intent2); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 298 | final WebTarget wt = target(); |
| 299 | final String response = wt.path("intents").request().get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 300 | assertThat(response, containsString("{\"intents\":[")); |
| 301 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 302 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 303 | assertThat(result, notNullValue()); |
| 304 | |
| 305 | assertThat(result.names(), hasSize(1)); |
| 306 | assertThat(result.names().get(0), is("intents")); |
| 307 | |
| 308 | final JsonArray jsonIntents = result.get("intents").asArray(); |
| 309 | assertThat(jsonIntents, notNullValue()); |
| 310 | |
| 311 | assertThat(jsonIntents, hasIntent(intent1)); |
| 312 | assertThat(jsonIntents, hasIntent(intent2)); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Tests the result of a rest api GET for a single intent. |
| 317 | */ |
| 318 | @Test |
| 319 | public void testIntentsSingle() { |
| 320 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 321 | resources.add(new MockResource(1)); |
| 322 | resources.add(new MockResource(2)); |
| 323 | resources.add(new MockResource(3)); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 324 | final Intent intent = new MockIntent(3L, resources); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 325 | |
| 326 | intents.add(intent); |
| 327 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 328 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 329 | .andReturn(intent) |
| 330 | .anyTimes(); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 331 | expect(mockIntentService.getIntent(Key.of("0", APP_ID))) |
| 332 | .andReturn(intent) |
| 333 | .anyTimes(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 334 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
| 335 | .andReturn(intent) |
| 336 | .anyTimes(); |
| 337 | expect(mockIntentService.getIntent(Key.of("0x0", APP_ID))) |
| 338 | .andReturn(null) |
| 339 | .anyTimes(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 340 | replay(mockIntentService); |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 341 | expect(mockCoreService.getAppId(APP_ID.name())) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 342 | .andReturn(APP_ID).anyTimes(); |
| 343 | replay(mockCoreService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 344 | final WebTarget wt = target(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 345 | |
| 346 | // Test get using key string |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 347 | final String response = wt.path("intents/" + APP_ID.name() |
| 348 | + "/0").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 349 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 350 | assertThat(result, matchesIntent(intent)); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 351 | |
| 352 | // Test get using numeric value |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 353 | final String responseNumeric = wt.path("intents/" + APP_ID.name() |
| 354 | + "/0x0").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 355 | final JsonObject resultNumeric = Json.parse(responseNumeric).asObject(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 356 | assertThat(resultNumeric, matchesIntent(intent)); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Tests that a fetch of a non-existent intent object throws an exception. |
| 361 | */ |
| 362 | @Test |
| 363 | public void testBadGet() { |
| 364 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 365 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 366 | .andReturn(null) |
| 367 | .anyTimes(); |
| 368 | replay(mockIntentService); |
| 369 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 370 | WebTarget wt = target(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 371 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 372 | wt.path("intents/0").request().get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 373 | fail("Fetch of non-existent intent did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 374 | } catch (NotFoundException ex) { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 375 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 376 | containsString("HTTP 404 Not Found")); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 379 | |
| 380 | /** |
| 381 | * Tests creating an intent with POST. |
| 382 | */ |
| 383 | @Test |
| 384 | public void testPost() { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 385 | ApplicationId testId = new DefaultApplicationId(2, "myApp"); |
| 386 | expect(mockCoreService.getAppId("myApp")) |
| 387 | .andReturn(testId); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 388 | replay(mockCoreService); |
| 389 | |
| 390 | mockIntentService.submit(anyObject()); |
| 391 | expectLastCall(); |
| 392 | replay(mockIntentService); |
| 393 | |
| 394 | InputStream jsonStream = IntentsResourceTest.class |
| 395 | .getResourceAsStream("post-intent.json"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 396 | WebTarget wt = target(); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 397 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 398 | Response response = wt.path("intents") |
| 399 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 400 | .post(Entity.json(jsonStream)); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 401 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 402 | String location = response.getLocation().getPath(); |
Ray Milkey | 8d07640 | 2015-08-31 15:43:18 -0700 | [diff] [blame] | 403 | assertThat(location, Matchers.startsWith("/intents/myApp/")); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 404 | } |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 405 | |
| 406 | /** |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 407 | * Tests creating an intent with POST and illegal JSON. |
| 408 | */ |
| 409 | @Test |
| 410 | public void testBadPost() { |
| 411 | replay(mockCoreService); |
| 412 | replay(mockIntentService); |
| 413 | |
| 414 | String json = "this is invalid!"; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 415 | WebTarget wt = target(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 416 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 417 | Response response = wt.path("intents") |
| 418 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 419 | .post(Entity.json(json)); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 420 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST)); |
| 421 | } |
| 422 | |
| 423 | /** |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 424 | * Tests removing an intent with DELETE. |
| 425 | */ |
| 426 | @Test |
| 427 | public void testRemove() { |
| 428 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 429 | resources.add(new MockResource(1)); |
| 430 | resources.add(new MockResource(2)); |
| 431 | resources.add(new MockResource(3)); |
| 432 | final Intent intent = new MockIntent(3L, resources); |
| 433 | final ApplicationId appId = new DefaultApplicationId(2, "app"); |
| 434 | IntentService fakeManager = new FakeIntentManager(); |
| 435 | |
| 436 | expect(mockCoreService.getAppId("app")) |
| 437 | .andReturn(appId).once(); |
| 438 | replay(mockCoreService); |
| 439 | |
| 440 | mockIntentService.withdraw(anyObject()); |
| 441 | expectLastCall().andDelegateTo(fakeManager).once(); |
| 442 | expect(mockIntentService.getIntent(Key.of(2, appId))) |
| 443 | .andReturn(intent) |
| 444 | .once(); |
| 445 | expect(mockIntentService.getIntent(Key.of("0x2", appId))) |
| 446 | .andReturn(null) |
| 447 | .once(); |
| 448 | |
| 449 | mockIntentService.addListener(anyObject()); |
| 450 | expectLastCall().andDelegateTo(fakeManager).once(); |
| 451 | mockIntentService.removeListener(anyObject()); |
| 452 | expectLastCall().andDelegateTo(fakeManager).once(); |
| 453 | |
| 454 | replay(mockIntentService); |
| 455 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 456 | WebTarget wt = target(); |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 457 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 458 | Response response = wt.path("intents/app/0x2") |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 459 | .request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 460 | .delete(); |
Ray Milkey | 7b15851 | 2015-07-21 16:32:43 -0700 | [diff] [blame] | 461 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 462 | } |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * Tests removal of a non existent intent with DELETE. |
| 466 | */ |
| 467 | @Test |
| 468 | public void testBadRemove() { |
| 469 | final ApplicationId appId = new DefaultApplicationId(2, "app"); |
| 470 | |
| 471 | expect(mockCoreService.getAppId("app")) |
| 472 | .andReturn(appId).once(); |
| 473 | replay(mockCoreService); |
| 474 | |
| 475 | expect(mockIntentService.getIntent(Key.of(2, appId))) |
| 476 | .andReturn(null) |
| 477 | .once(); |
| 478 | expect(mockIntentService.getIntent(Key.of("0x2", appId))) |
| 479 | .andReturn(null) |
| 480 | .once(); |
| 481 | |
| 482 | replay(mockIntentService); |
| 483 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 484 | WebTarget wt = target(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 485 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 486 | Response response = wt.path("intents/app/0x2") |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 487 | .request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 488 | .delete(); |
Ray Milkey | 05b169d | 2015-08-13 14:33:33 -0700 | [diff] [blame] | 489 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 490 | } |
| 491 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 492 | } |