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