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