Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame^] | 18 | import java.io.InputStream; |
| 19 | import java.net.HttpURLConnection; |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 20 | import java.util.Collections; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 21 | import java.util.HashSet; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 22 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame^] | 23 | import javax.ws.rs.core.MediaType; |
| 24 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 25 | import org.hamcrest.Description; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame^] | 26 | import org.hamcrest.Matchers; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 27 | import org.hamcrest.TypeSafeMatcher; |
| 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.onlab.osgi.ServiceDirectory; |
| 32 | import org.onlab.osgi.TestServiceDirectory; |
| 33 | import org.onlab.rest.BaseResource; |
| 34 | import org.onosproject.codec.CodecService; |
| 35 | import org.onosproject.codec.impl.CodecManager; |
| 36 | import org.onosproject.core.ApplicationId; |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 37 | import org.onosproject.core.CoreService; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 38 | import org.onosproject.core.DefaultApplicationId; |
| 39 | import org.onosproject.core.IdGenerator; |
| 40 | import org.onosproject.net.NetworkResource; |
| 41 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.Key; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.MockIdGenerator; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 46 | |
| 47 | import com.eclipsesource.json.JsonArray; |
| 48 | import com.eclipsesource.json.JsonObject; |
| 49 | import com.eclipsesource.json.JsonValue; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame^] | 50 | import com.sun.jersey.api.client.ClientResponse; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 51 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 52 | import com.sun.jersey.api.client.WebResource; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 53 | |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 54 | import static org.easymock.EasyMock.anyObject; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 55 | import static org.easymock.EasyMock.createMock; |
| 56 | import static org.easymock.EasyMock.expect; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame^] | 57 | import static org.easymock.EasyMock.expectLastCall; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 58 | import static org.easymock.EasyMock.replay; |
| 59 | import static org.easymock.EasyMock.verify; |
| 60 | import static org.hamcrest.Matchers.containsString; |
| 61 | import static org.hamcrest.Matchers.hasSize; |
| 62 | import static org.hamcrest.Matchers.is; |
| 63 | import static org.hamcrest.Matchers.notNullValue; |
| 64 | import static org.junit.Assert.assertThat; |
| 65 | import static org.junit.Assert.fail; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 66 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Unit tests for Intents REST APIs. |
| 70 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 71 | public class IntentsResourceTest extends ResourceTest { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 72 | final IntentService mockIntentService = createMock(IntentService.class); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 73 | final CoreService mockCoreService = createMock(CoreService.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 74 | final HashSet<Intent> intents = new HashSet<>(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 75 | private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test"); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 76 | private IdGenerator mockGenerator; |
| 77 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 78 | private class MockResource implements NetworkResource { |
| 79 | int id; |
| 80 | |
| 81 | MockResource(int id) { |
| 82 | this.id = id; |
| 83 | } |
| 84 | |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 85 | @Override |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 86 | public String toString() { |
| 87 | return "Resource " + Integer.toString(id); |
| 88 | } |
| 89 | } |
| 90 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Hamcrest matcher to check that an intent representation in JSON matches |
| 93 | * the actual intent. |
| 94 | */ |
| 95 | public static class IntentJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 96 | private final Intent intent; |
| 97 | private String reason = ""; |
| 98 | |
| 99 | public IntentJsonMatcher(Intent intentValue) { |
| 100 | intent = intentValue; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean matchesSafely(JsonObject jsonIntent) { |
| 105 | // check id |
| 106 | final String jsonId = jsonIntent.get("id").asString(); |
| 107 | if (!jsonId.equals(intent.id().toString())) { |
| 108 | reason = "id " + intent.id().toString(); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | // check application id |
| 113 | final String jsonAppId = jsonIntent.get("appId").asString(); |
| 114 | if (!jsonAppId.equals(intent.appId().toString())) { |
| 115 | reason = "appId " + intent.appId().toString(); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // check intent type |
| 120 | final String jsonType = jsonIntent.get("type").asString(); |
| 121 | if (!jsonType.equals("MockIntent")) { |
| 122 | reason = "type MockIntent"; |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // check details field |
| 127 | final String jsonDetails = jsonIntent.get("details").asString(); |
| 128 | if (!jsonDetails.equals(intent.toString())) { |
| 129 | reason = "details " + intent.toString(); |
| 130 | return false; |
| 131 | } |
| 132 | |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 133 | // check state field |
| 134 | final String jsonState = jsonIntent.get("state").asString(); |
| 135 | if (!jsonState.equals("INSTALLED")) { |
| 136 | reason = "state INSTALLED"; |
| 137 | return false; |
| 138 | } |
| 139 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 140 | // check resources array |
| 141 | final JsonArray jsonResources = jsonIntent.get("resources").asArray(); |
| 142 | if (intent.resources() != null) { |
| 143 | if (intent.resources().size() != jsonResources.size()) { |
| 144 | reason = "resources array size of " + Integer.toString(intent.resources().size()); |
| 145 | return false; |
| 146 | } |
| 147 | for (final NetworkResource resource : intent.resources()) { |
| 148 | boolean resourceFound = false; |
| 149 | final String resourceString = resource.toString(); |
| 150 | for (int resourceIndex = 0; resourceIndex < jsonResources.size(); resourceIndex++) { |
| 151 | final JsonValue value = jsonResources.get(resourceIndex); |
| 152 | if (value.asString().equals(resourceString)) { |
| 153 | resourceFound = true; |
| 154 | } |
| 155 | } |
| 156 | if (!resourceFound) { |
| 157 | reason = "resource " + resourceString; |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | } else if (jsonResources.size() != 0) { |
| 162 | reason = "resources array empty"; |
| 163 | return false; |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void describeTo(Description description) { |
| 170 | description.appendText(reason); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Factory to allocate an intent matcher. |
| 176 | * |
| 177 | * @param intent intent object we are looking for |
| 178 | * @return matcher |
| 179 | */ |
| 180 | private static IntentJsonMatcher matchesIntent(Intent intent) { |
| 181 | return new IntentJsonMatcher(intent); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Hamcrest matcher to check that an intent is represented properly in a JSON |
| 186 | * array of intents. |
| 187 | */ |
| 188 | public static class IntentJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 189 | private final Intent intent; |
| 190 | private String reason = ""; |
| 191 | |
| 192 | public IntentJsonArrayMatcher(Intent intentValue) { |
| 193 | intent = intentValue; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public boolean matchesSafely(JsonArray json) { |
| 198 | boolean intentFound = false; |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 199 | final int expectedAttributes = 6; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 200 | for (int jsonIntentIndex = 0; jsonIntentIndex < json.size(); |
| 201 | jsonIntentIndex++) { |
| 202 | |
| 203 | final JsonObject jsonIntent = json.get(jsonIntentIndex).asObject(); |
| 204 | |
| 205 | if (jsonIntent.names().size() != expectedAttributes) { |
| 206 | reason = "Found an intent with the wrong number of attributes"; |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | final String jsonIntentId = jsonIntent.get("id").asString(); |
| 211 | if (jsonIntentId.equals(intent.id().toString())) { |
| 212 | intentFound = true; |
| 213 | |
| 214 | // We found the correct intent, check attribute values |
| 215 | assertThat(jsonIntent, matchesIntent(intent)); |
| 216 | } |
| 217 | } |
| 218 | if (!intentFound) { |
| 219 | reason = "Intent with id " + intent.id().toString() + " not found"; |
| 220 | return false; |
| 221 | } else { |
| 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void describeTo(Description description) { |
| 228 | description.appendText(reason); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Factory to allocate an intent array matcher. |
| 234 | * |
| 235 | * @param intent intent object we are looking for |
| 236 | * @return matcher |
| 237 | */ |
| 238 | private static IntentJsonArrayMatcher hasIntent(Intent intent) { |
| 239 | return new IntentJsonArrayMatcher(intent); |
| 240 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 241 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Initializes test mocks and environment. |
| 244 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 245 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 246 | public void setUpTest() { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 247 | expect(mockIntentService.getIntents()).andReturn(intents).anyTimes(); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 248 | expect(mockIntentService.getIntentState(anyObject())) |
| 249 | .andReturn(IntentState.INSTALLED) |
| 250 | .anyTimes(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 251 | // Register the services needed for the test |
| 252 | final CodecManager codecService = new CodecManager(); |
| 253 | codecService.activate(); |
| 254 | ServiceDirectory testDirectory = |
| 255 | new TestServiceDirectory() |
| 256 | .add(IntentService.class, mockIntentService) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 257 | .add(CodecService.class, codecService) |
| 258 | .add(CoreService.class, mockCoreService); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 259 | |
| 260 | BaseResource.setServiceDirectory(testDirectory); |
| 261 | |
| 262 | mockGenerator = new MockIdGenerator(); |
| 263 | Intent.bindIdGenerator(mockGenerator); |
| 264 | } |
| 265 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Tears down and verifies test mocks and environment. |
| 268 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 269 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 270 | public void tearDownTest() { |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 271 | verify(mockIntentService); |
| 272 | Intent.unbindIdGenerator(mockGenerator); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Tests the result of the rest api GET when there are no intents. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testIntentsEmptyArray() { |
| 280 | replay(mockIntentService); |
| 281 | final WebResource rs = resource(); |
| 282 | final String response = rs.path("intents").get(String.class); |
| 283 | assertThat(response, is("{\"intents\":[]}")); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Tests the result of the rest api GET when intents are defined. |
| 288 | */ |
| 289 | @Test |
| 290 | public void testIntentsArray() { |
| 291 | replay(mockIntentService); |
| 292 | |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 293 | final Intent intent1 = new MockIntent(1L, Collections.emptyList()); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 294 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 295 | resources.add(new MockResource(1)); |
| 296 | resources.add(new MockResource(2)); |
| 297 | resources.add(new MockResource(3)); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 298 | final Intent intent2 = new MockIntent(2L, resources); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 299 | |
| 300 | intents.add(intent1); |
| 301 | intents.add(intent2); |
| 302 | final WebResource rs = resource(); |
| 303 | final String response = rs.path("intents").get(String.class); |
| 304 | assertThat(response, containsString("{\"intents\":[")); |
| 305 | |
| 306 | final JsonObject result = JsonObject.readFrom(response); |
| 307 | assertThat(result, notNullValue()); |
| 308 | |
| 309 | assertThat(result.names(), hasSize(1)); |
| 310 | assertThat(result.names().get(0), is("intents")); |
| 311 | |
| 312 | final JsonArray jsonIntents = result.get("intents").asArray(); |
| 313 | assertThat(jsonIntents, notNullValue()); |
| 314 | |
| 315 | assertThat(jsonIntents, hasIntent(intent1)); |
| 316 | assertThat(jsonIntents, hasIntent(intent2)); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Tests the result of a rest api GET for a single intent. |
| 321 | */ |
| 322 | @Test |
| 323 | public void testIntentsSingle() { |
| 324 | final HashSet<NetworkResource> resources = new HashSet<>(); |
| 325 | resources.add(new MockResource(1)); |
| 326 | resources.add(new MockResource(2)); |
| 327 | resources.add(new MockResource(3)); |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 328 | final Intent intent = new MockIntent(3L, resources); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 329 | |
| 330 | intents.add(intent); |
| 331 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 332 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 333 | .andReturn(intent) |
| 334 | .anyTimes(); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 335 | expect(mockIntentService.getIntent(Key.of("0", APP_ID))) |
| 336 | .andReturn(intent) |
| 337 | .anyTimes(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 338 | replay(mockIntentService); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 339 | expect(mockCoreService.getAppId(APP_ID.id())) |
| 340 | .andReturn(APP_ID).anyTimes(); |
| 341 | replay(mockCoreService); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 342 | final WebResource rs = resource(); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 343 | final String response = rs.path("intents/1/0").get(String.class); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 344 | final JsonObject result = JsonObject.readFrom(response); |
| 345 | assertThat(result, matchesIntent(intent)); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Tests that a fetch of a non-existent intent object throws an exception. |
| 350 | */ |
| 351 | @Test |
| 352 | public void testBadGet() { |
| 353 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 354 | expect(mockIntentService.getIntent(Key.of(0, APP_ID))) |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 355 | .andReturn(null) |
| 356 | .anyTimes(); |
| 357 | replay(mockIntentService); |
| 358 | |
| 359 | WebResource rs = resource(); |
| 360 | try { |
| 361 | rs.path("intents/0").get(String.class); |
| 362 | fail("Fetch of non-existent intent did not throw an exception"); |
| 363 | } catch (UniformInterfaceException ex) { |
| 364 | assertThat(ex.getMessage(), |
| 365 | containsString("returned a response status of")); |
| 366 | } |
| 367 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame^] | 368 | |
| 369 | /** |
| 370 | * Tests creating an intent with POST. |
| 371 | */ |
| 372 | @Test |
| 373 | public void testPost() { |
| 374 | expect(mockCoreService.getAppId((short) 2)) |
| 375 | .andReturn(new DefaultApplicationId(2, "app")); |
| 376 | replay(mockCoreService); |
| 377 | |
| 378 | mockIntentService.submit(anyObject()); |
| 379 | expectLastCall(); |
| 380 | replay(mockIntentService); |
| 381 | |
| 382 | InputStream jsonStream = IntentsResourceTest.class |
| 383 | .getResourceAsStream("post-intent.json"); |
| 384 | WebResource rs = resource(); |
| 385 | |
| 386 | ClientResponse response = rs.path("intents") |
| 387 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 388 | .post(ClientResponse.class, jsonStream); |
| 389 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 390 | String location = response.getLocation().getPath(); |
| 391 | assertThat(location, Matchers.startsWith("/intents/2/")); |
| 392 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 393 | } |