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