blob: bb81c778172135e492a3c9a8760f58333b6fb79a [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
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 */
16package org.onosproject.rest;
17
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080018import java.util.Collections;
Ray Milkey2b217142014-12-15 09:24:24 -080019import java.util.HashSet;
Ray Milkey2b217142014-12-15 09:24:24 -080020
21import org.hamcrest.Description;
22import org.hamcrest.TypeSafeMatcher;
23import org.junit.After;
24import org.junit.Before;
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080025import org.junit.Ignore;
Ray Milkey2b217142014-12-15 09:24:24 -080026import org.junit.Test;
27import org.onlab.osgi.ServiceDirectory;
28import org.onlab.osgi.TestServiceDirectory;
29import org.onlab.rest.BaseResource;
30import org.onosproject.codec.CodecService;
31import org.onosproject.codec.impl.CodecManager;
32import org.onosproject.core.ApplicationId;
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080033import org.onosproject.core.CoreService;
Ray Milkey2b217142014-12-15 09:24:24 -080034import org.onosproject.core.DefaultApplicationId;
35import org.onosproject.core.IdGenerator;
36import org.onosproject.net.NetworkResource;
37import org.onosproject.net.intent.Intent;
Ray Milkey2b217142014-12-15 09:24:24 -080038import org.onosproject.net.intent.IntentService;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080039import org.onosproject.net.intent.Key;
Ray Milkey43a28222015-02-23 13:57:58 -080040import org.onosproject.net.intent.MockIdGenerator;
Ray Milkey2b217142014-12-15 09:24:24 -080041
42import com.eclipsesource.json.JsonArray;
43import com.eclipsesource.json.JsonObject;
44import com.eclipsesource.json.JsonValue;
Ray Milkey2b217142014-12-15 09:24:24 -080045import com.sun.jersey.api.client.UniformInterfaceException;
46import com.sun.jersey.api.client.WebResource;
Ray Milkey2b217142014-12-15 09:24:24 -080047
48import static org.easymock.EasyMock.createMock;
49import static org.easymock.EasyMock.expect;
50import static org.easymock.EasyMock.replay;
51import static org.easymock.EasyMock.verify;
52import static org.hamcrest.Matchers.containsString;
53import static org.hamcrest.Matchers.hasSize;
54import static org.hamcrest.Matchers.is;
55import static org.hamcrest.Matchers.notNullValue;
56import static org.junit.Assert.assertThat;
57import static org.junit.Assert.fail;
Ray Milkey43a28222015-02-23 13:57:58 -080058import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080059
60/**
61 * Unit tests for Intents REST APIs.
62 */
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -080063@Ignore
Ray Milkey9c3d3362015-01-28 10:39:56 -080064public class IntentsResourceTest extends ResourceTest {
Ray Milkey2b217142014-12-15 09:24:24 -080065 final IntentService mockIntentService = createMock(IntentService.class);
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080066 final CoreService mockCoreService = createMock(CoreService.class);
Ray Milkey2b217142014-12-15 09:24:24 -080067 final HashSet<Intent> intents = new HashSet<>();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test");
Ray Milkey2b217142014-12-15 09:24:24 -080069 private IdGenerator mockGenerator;
70
Ray Milkey2b217142014-12-15 09:24:24 -080071 private class MockResource implements NetworkResource {
72 int id;
73
74 MockResource(int id) {
75 this.id = id;
76 }
77
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080078 @Override
Ray Milkey2b217142014-12-15 09:24:24 -080079 public String toString() {
80 return "Resource " + Integer.toString(id);
81 }
82 }
83
Ray Milkey2b217142014-12-15 09:24:24 -080084 /**
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 Milkey4f5de002014-12-17 19:26:11 -0800227
Ray Milkeyed0b1662015-02-05 09:34:29 -0800228 /**
229 * Initializes test mocks and environment.
230 */
Ray Milkey2b217142014-12-15 09:24:24 -0800231 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800232 public void setUpTest() {
Ray Milkey2b217142014-12-15 09:24:24 -0800233 expect(mockIntentService.getIntents()).andReturn(intents).anyTimes();
Ray Milkey2b217142014-12-15 09:24:24 -0800234 // 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 Koshibec06c89b2015-02-10 19:25:41 -0800240 .add(CodecService.class, codecService)
241 .add(CoreService.class, mockCoreService);
Ray Milkey2b217142014-12-15 09:24:24 -0800242
243 BaseResource.setServiceDirectory(testDirectory);
244
245 mockGenerator = new MockIdGenerator();
246 Intent.bindIdGenerator(mockGenerator);
247 }
248
Ray Milkeyed0b1662015-02-05 09:34:29 -0800249 /**
250 * Tears down and verifies test mocks and environment.
251 */
Ray Milkey2b217142014-12-15 09:24:24 -0800252 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800253 public void tearDownTest() {
Ray Milkey2b217142014-12-15 09:24:24 -0800254 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 Milkey43a28222015-02-23 13:57:58 -0800276 final Intent intent1 = new MockIntent(1L, Collections.emptyList());
Ray Milkey2b217142014-12-15 09:24:24 -0800277 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 Milkey43a28222015-02-23 13:57:58 -0800281 final Intent intent2 = new MockIntent(2L, resources);
Ray Milkey2b217142014-12-15 09:24:24 -0800282
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 Milkey43a28222015-02-23 13:57:58 -0800311 final Intent intent = new MockIntent(3L, resources);
Ray Milkey2b217142014-12-15 09:24:24 -0800312
313 intents.add(intent);
314
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800315 expect(mockIntentService.getIntent(Key.of(0, APP_ID)))
Ray Milkey2b217142014-12-15 09:24:24 -0800316 .andReturn(intent)
317 .anyTimes();
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800318 expect(mockIntentService.getIntent(Key.of("0", APP_ID)))
319 .andReturn(intent)
320 .anyTimes();
Ray Milkey2b217142014-12-15 09:24:24 -0800321 replay(mockIntentService);
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800322 expect(mockCoreService.getAppId(APP_ID.id()))
323 .andReturn(APP_ID).anyTimes();
324 replay(mockCoreService);
Ray Milkey2b217142014-12-15 09:24:24 -0800325 final WebResource rs = resource();
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800326 final String response = rs.path("intents/1/0").get(String.class);
Ray Milkey2b217142014-12-15 09:24:24 -0800327 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 Milkeyf9af43c2015-02-09 16:45:48 -0800337 expect(mockIntentService.getIntent(Key.of(0, APP_ID)))
Ray Milkey2b217142014-12-15 09:24:24 -0800338 .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}