blob: d7ace4d7238521a98550e14d6cfbad6055f3b268 [file] [log] [blame]
Ray Milkeyf195b022015-02-03 15:13:11 -08001/*
2 * Copyright 2015 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
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080018import com.eclipsesource.json.JsonArray;
19import com.eclipsesource.json.JsonObject;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
22import com.sun.jersey.api.client.WebResource;
Ray Milkeyf195b022015-02-03 15:13:11 -080023import org.hamcrest.Description;
24import org.hamcrest.TypeSafeMatcher;
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.onlab.osgi.ServiceDirectory;
29import org.onlab.osgi.TestServiceDirectory;
30import org.onlab.rest.BaseResource;
31import org.onosproject.app.ApplicationAdminService;
32import org.onosproject.app.ApplicationService;
33import org.onosproject.app.ApplicationState;
34import org.onosproject.codec.CodecService;
35import org.onosproject.codec.impl.ApplicationCodec;
36import org.onosproject.codec.impl.CodecManager;
37import org.onosproject.codec.impl.MockCodecContext;
38import org.onosproject.core.Application;
39import org.onosproject.core.ApplicationId;
40import org.onosproject.core.DefaultApplication;
41import org.onosproject.core.DefaultApplicationId;
42import org.onosproject.core.Version;
43
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080044import java.io.InputStream;
45import java.net.URI;
46import java.util.Optional;
Ray Milkeyf195b022015-02-03 15:13:11 -080047
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080048import static org.easymock.EasyMock.*;
Ray Milkeyf195b022015-02-03 15:13:11 -080049import static org.hamcrest.MatcherAssert.assertThat;
Yuta HIGUCHI293ede02015-02-05 00:08:41 -080050import static org.hamcrest.Matchers.containsString;
51import static org.hamcrest.Matchers.hasSize;
52import static org.hamcrest.Matchers.is;
53import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyf195b022015-02-03 15:13:11 -080054
55/**
56 * Unit tests for applications REST APIs.
57 */
58
59public class ApplicationsResourceTest extends ResourceTest {
60
61 private static class MockCodecContextWithService extends MockCodecContext {
62 private ApplicationAdminService service;
63
64 MockCodecContextWithService(ApplicationAdminService service) {
65 this.service = service;
66 }
67
68 @Override
69 @SuppressWarnings("unchecked")
70 public <T> T get(Class<T> serviceClass) {
71 return (T) service;
72 }
73 }
74
75 private ApplicationAdminService service;
76 private ApplicationId id1 = new DefaultApplicationId(1, "app1");
77 private ApplicationId id2 = new DefaultApplicationId(2, "app2");
78 private ApplicationId id3 = new DefaultApplicationId(3, "app3");
79 private ApplicationId id4 = new DefaultApplicationId(4, "app4");
80
81 private static final URI FURL = URI.create("mvn:org.foo-features/1.2a/xml/features");
82 private static final Version VER = Version.version(1, 2, "a", null);
83
84 private Application app1 =
85 new DefaultApplication(id1, VER,
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080086 "app1", "origin1", ImmutableSet.of(), Optional.of(FURL),
87 ImmutableList.of("My Feature"));
Ray Milkeyf195b022015-02-03 15:13:11 -080088 private Application app2 =
89 new DefaultApplication(id2, VER,
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080090 "app2", "origin2", ImmutableSet.of(), Optional.of(FURL),
91 ImmutableList.of("My Feature"));
Ray Milkeyf195b022015-02-03 15:13:11 -080092 private Application app3 =
93 new DefaultApplication(id3, VER,
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080094 "app3", "origin3", ImmutableSet.of(), Optional.of(FURL),
95 ImmutableList.of("My Feature"));
Ray Milkeyf195b022015-02-03 15:13:11 -080096 private Application app4 =
97 new DefaultApplication(id4, VER,
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080098 "app4", "origin4", ImmutableSet.of(), Optional.of(FURL),
99 ImmutableList.of("My Feature"));
Ray Milkeyf195b022015-02-03 15:13:11 -0800100
101 /**
102 * Hamcrest matcher to check that an device representation in JSON matches
103 * the actual device.
104 */
105 private static class AppJsonMatcher extends TypeSafeMatcher<JsonObject> {
106 private final Application app;
107 private String reason = "";
108
109 public AppJsonMatcher(Application appValue) {
110 app = appValue;
111 }
112
113 @Override
114 public boolean matchesSafely(JsonObject jsonApp) {
115 // check id
116 short jsonId = (short) jsonApp.get("id").asInt();
117 if (jsonId != app.id().id()) {
118 reason = "id " + app.id().id();
119 return false;
120 }
121
122 // check name
123 String jsonName = jsonApp.get("name").asString();
124 if (!jsonName.equals(app.id().name())) {
125 reason = "name " + app.id().name();
126 return false;
127 }
128
129 // check origin
130 String jsonOrigin = jsonApp.get("origin").asString();
131 if (!jsonOrigin.equals(app.origin())) {
132 reason = "manufacturer " + app.origin();
133 return false;
134 }
135
136 return true;
137 }
138
139 @Override
140 public void describeTo(Description description) {
141 description.appendText(reason);
142 }
143 }
144
145 /**
146 * Factory to allocate an device matcher.
147 *
148 * @param app application object we are looking for
149 * @return matcher
150 */
151 private static AppJsonMatcher matchesApp(Application app) {
152 return new AppJsonMatcher(app);
153 }
154
155
Yuta HIGUCHI293ede02015-02-05 00:08:41 -0800156 @Override
Ray Milkeyf195b022015-02-03 15:13:11 -0800157 @Before
158 public void setUp() {
159 service = createMock(ApplicationAdminService.class);
160
161 expect(service.getId("one"))
162 .andReturn(id1)
163 .anyTimes();
164 expect(service.getId("two"))
165 .andReturn(id2)
166 .anyTimes();
167 expect(service.getId("three"))
168 .andReturn(id3)
169 .anyTimes();
170 expect(service.getId("four"))
171 .andReturn(id4)
172 .anyTimes();
173
174 expect(service.getApplication(id3))
175 .andReturn(app3)
176 .anyTimes();
177 expect(service.getState(isA(ApplicationId.class)))
178 .andReturn(ApplicationState.ACTIVE)
179 .anyTimes();
180
181 // Register the services needed for the test
182 CodecManager codecService = new CodecManager();
183 codecService.activate();
184 ServiceDirectory testDirectory =
185 new TestServiceDirectory()
186 .add(ApplicationAdminService.class, service)
187 .add(ApplicationService.class, service)
188 .add(CodecService.class, codecService);
189
190 BaseResource.setServiceDirectory(testDirectory);
191 }
192
193 @After
194 public void shutDown() {
195 verify(service);
196 }
197
198 /**
199 * Tests a GET of all applications when no applications are present.
200 */
201 @Test
202 public void getAllApplicationsEmpty() {
203 expect(service.getApplications())
204 .andReturn(ImmutableSet.of());
205 replay(service);
206
207 WebResource rs = resource();
208 String response = rs.path("applications").get(String.class);
209 assertThat(response, is("{\"applications\":[]}"));
210 }
211
212 /**
213 * Tests a GET of all applications with data.
214 */
215 @Test
216 public void getAllApplicationsPopulated() {
217 expect(service.getApplications())
218 .andReturn(ImmutableSet.of(app1, app2, app3, app4));
219 replay(service);
220
221 WebResource rs = resource();
222 String response = rs.path("applications").get(String.class);
223 assertThat(response, containsString("{\"applications\":["));
224
225 JsonObject result = JsonObject.readFrom(response);
226 assertThat(result, notNullValue());
227
228 assertThat(result.names(), hasSize(1));
229 assertThat(result.names().get(0), is("applications"));
230
231 JsonArray jsonApps = result.get("applications").asArray();
232 assertThat(jsonApps, notNullValue());
233 assertThat(jsonApps.size(), is(4));
234
235 assertThat(jsonApps.get(0).asObject(), matchesApp(app1));
236 assertThat(jsonApps.get(1).asObject(), matchesApp(app2));
237 assertThat(jsonApps.get(2).asObject(), matchesApp(app3));
238 assertThat(jsonApps.get(3).asObject(), matchesApp(app4));
239 }
240
241 /**
242 * Tests a GET of a single application.
243 */
244 @Test
245 public void getSingleApplication() {
246 replay(service);
247
248 WebResource rs = resource();
249 String response = rs.path("applications/three").get(String.class);
250
251 JsonObject result = JsonObject.readFrom(response);
252 assertThat(result, notNullValue());
253
254 assertThat(result, matchesApp(app3));
255 }
256
257 /**
258 * Tests a DELETE of a single application - this should
259 * attempt to uninstall it.
260 */
261 @Test
262 public void deleteApplication() {
263 service.uninstall(id3);
264 expectLastCall();
265
266 replay(service);
267
268 WebResource rs = resource();
269 rs.path("applications/three").delete();
270 }
271
272 /**
273 * Tests a DELETE of a single active application - this should
274 * attempt to uninstall it.
275 */
276 @Test
277 public void deleteActiveApplication() {
278 service.deactivate(id3);
279 expectLastCall();
280
281 replay(service);
282
283 WebResource rs = resource();
284 rs.path("applications/three/active").delete();
285 }
286
287 /**
288 * Tests a POST operation to the "active" URL. This should attempt to
289 * activate the application.
290 */
291 @Test
292 public void postActiveApplication() {
293 service.activate(id3);
294 expectLastCall();
295
296 replay(service);
297
298 WebResource rs = resource();
299 rs.path("applications/three/active").post();
300 }
301
302 /**
303 * Tests a POST operation. This should attempt to
304 * install the application.
305 */
306 @Test
307 public void postApplication() {
308 expect(service.install(isA(InputStream.class)))
309 .andReturn(app4)
310 .once();
311
312 replay(service);
313
314 ApplicationCodec codec = new ApplicationCodec();
315 String app4Json = codec.encode(app4,
316 new MockCodecContextWithService(service))
317 .asText();
318
319 WebResource rs = resource();
320 String response = rs.path("applications").post(String.class, app4Json);
321
322 JsonObject result = JsonObject.readFrom(response);
323 assertThat(result, notNullValue());
324
325 assertThat(result, matchesApp(app4));
326 }
327}