Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 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; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 23 | import org.hamcrest.Description; |
| 24 | import org.hamcrest.TypeSafeMatcher; |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 25 | import org.junit.Assert; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.osgi.ServiceDirectory; |
| 29 | import org.onlab.osgi.TestServiceDirectory; |
| 30 | import org.onlab.rest.BaseResource; |
| 31 | import org.onosproject.app.ApplicationAdminService; |
| 32 | import org.onosproject.app.ApplicationService; |
| 33 | import org.onosproject.app.ApplicationState; |
| 34 | import org.onosproject.codec.CodecService; |
| 35 | import org.onosproject.codec.impl.ApplicationCodec; |
| 36 | import org.onosproject.codec.impl.CodecManager; |
| 37 | import org.onosproject.codec.impl.MockCodecContext; |
| 38 | import org.onosproject.core.Application; |
| 39 | import org.onosproject.core.ApplicationId; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationRole; |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 41 | import org.onosproject.core.CoreService; |
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 | |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 46 | import javax.ws.rs.NotFoundException; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 47 | import javax.ws.rs.client.Entity; |
| 48 | import javax.ws.rs.client.WebTarget; |
| 49 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 50 | import java.io.InputStream; |
| 51 | import java.net.URI; |
| 52 | import java.util.Optional; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 53 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 54 | import static org.easymock.EasyMock.createMock; |
| 55 | import static org.easymock.EasyMock.expect; |
| 56 | import static org.easymock.EasyMock.expectLastCall; |
| 57 | import static org.easymock.EasyMock.isA; |
| 58 | import static org.easymock.EasyMock.replay; |
| 59 | import static org.easymock.EasyMock.verify; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 60 | import static org.hamcrest.MatcherAssert.assertThat; |
Yuta HIGUCHI | 293ede0 | 2015-02-05 00:08:41 -0800 | [diff] [blame] | 61 | import static org.hamcrest.Matchers.containsString; |
| 62 | import static org.hamcrest.Matchers.hasSize; |
| 63 | import static org.hamcrest.Matchers.is; |
| 64 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Unit tests for applications REST APIs. |
| 68 | */ |
| 69 | |
| 70 | public class ApplicationsResourceTest extends ResourceTest { |
| 71 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 72 | private static class MockCodecContextWithAppService extends MockCodecContext { |
| 73 | private ApplicationAdminService appService; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 74 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 75 | MockCodecContextWithAppService(ApplicationAdminService appService) { |
| 76 | this.appService = appService; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | @SuppressWarnings("unchecked") |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 81 | public <T> T getService(Class<T> serviceClass) { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 82 | return (T) appService; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 86 | private static class MockCodecContextWithCoreService extends MockCodecContext { |
| 87 | private CoreService coreService; |
| 88 | |
| 89 | MockCodecContextWithCoreService(CoreService coreService) { |
| 90 | this.coreService = coreService; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | @SuppressWarnings("unchecked") |
| 95 | public <T> T getService(Class<T> serviceClass) { |
| 96 | return (T) coreService; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private ApplicationAdminService appService; |
| 101 | private CoreService coreService; |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 102 | private ApplicationId id1 = new DefaultApplicationId(1, "app1"); |
| 103 | private ApplicationId id2 = new DefaultApplicationId(2, "app2"); |
| 104 | private ApplicationId id3 = new DefaultApplicationId(3, "app3"); |
| 105 | private ApplicationId id4 = new DefaultApplicationId(4, "app4"); |
| 106 | |
| 107 | private static final URI FURL = URI.create("mvn:org.foo-features/1.2a/xml/features"); |
| 108 | private static final Version VER = Version.version(1, 2, "a", null); |
| 109 | |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 110 | private DefaultApplication.Builder baseBuilder = DefaultApplication.builder() |
| 111 | .withVersion(VER) |
| 112 | .withIcon(new byte[0]) |
| 113 | .withRole(ApplicationRole.ADMIN) |
| 114 | .withPermissions(ImmutableSet.of()) |
| 115 | .withFeaturesRepo(Optional.of(FURL)) |
| 116 | .withFeatures(ImmutableList.of("My Feature")) |
| 117 | .withRequiredApps(ImmutableList.of()); |
| 118 | |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 119 | private Application app1 = |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 120 | DefaultApplication.builder(baseBuilder) |
| 121 | .withAppId(id1) |
| 122 | .withTitle("title1") |
| 123 | .withDescription("desc1") |
| 124 | .withOrigin("origin1") |
| 125 | .withCategory("category1") |
| 126 | .withUrl("url1") |
| 127 | .withReadme("readme1") |
| 128 | .build(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 129 | private Application app2 = |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 130 | DefaultApplication.builder(baseBuilder) |
| 131 | .withAppId(id2) |
| 132 | .withTitle("title2") |
| 133 | .withDescription("desc2") |
| 134 | .withOrigin("origin2") |
| 135 | .withCategory("category2") |
| 136 | .withUrl("url2") |
| 137 | .withReadme("readme2") |
| 138 | .build(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 139 | private Application app3 = |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 140 | DefaultApplication.builder(baseBuilder) |
| 141 | .withAppId(id3) |
| 142 | .withTitle("title3") |
| 143 | .withDescription("desc3") |
| 144 | .withOrigin("origin3") |
| 145 | .withCategory("category3") |
| 146 | .withUrl("url3") |
| 147 | .withReadme("readme3") |
| 148 | .build(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 149 | private Application app4 = |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 150 | DefaultApplication.builder(baseBuilder) |
| 151 | .withAppId(id4) |
| 152 | .withTitle("title4") |
| 153 | .withDescription("desc4") |
| 154 | .withOrigin("origin4") |
| 155 | .withCategory("category4") |
| 156 | .withUrl("url4") |
| 157 | .withReadme("readme4") |
| 158 | .build(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 161 | * Hamcrest matcher to check that an application representation in JSON matches |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 162 | * the actual device. |
| 163 | */ |
| 164 | private static class AppJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 165 | private final Application app; |
| 166 | private String reason = ""; |
| 167 | |
| 168 | public AppJsonMatcher(Application appValue) { |
| 169 | app = appValue; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public boolean matchesSafely(JsonObject jsonApp) { |
| 174 | // check id |
| 175 | short jsonId = (short) jsonApp.get("id").asInt(); |
| 176 | if (jsonId != app.id().id()) { |
| 177 | reason = "id " + app.id().id(); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // check name |
| 182 | String jsonName = jsonApp.get("name").asString(); |
| 183 | if (!jsonName.equals(app.id().name())) { |
| 184 | reason = "name " + app.id().name(); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | // check origin |
| 189 | String jsonOrigin = jsonApp.get("origin").asString(); |
| 190 | if (!jsonOrigin.equals(app.origin())) { |
| 191 | reason = "manufacturer " + app.origin(); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void describeTo(Description description) { |
| 200 | description.appendText(reason); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 205 | * Hamcrest matcher to check that an application id representation in JSON. |
| 206 | */ |
| 207 | private static final class AppIdJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 208 | private final ApplicationId appId; |
| 209 | private String reason = ""; |
| 210 | |
| 211 | private AppIdJsonMatcher(ApplicationId appId) { |
| 212 | this.appId = appId; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected boolean matchesSafely(JsonObject jsonAppId) { |
| 217 | // check id |
| 218 | short jsonId = (short) jsonAppId.get("id").asInt(); |
| 219 | if (jsonId != appId.id()) { |
| 220 | reason = "id " + appId.id(); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // check name |
| 225 | String jsonName = jsonAppId.get("name").asString(); |
| 226 | if (!jsonName.equals(appId.name())) { |
| 227 | reason = "name " + appId.name(); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void describeTo(Description description) { |
| 236 | description.appendText(reason); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 241 | * Factory to allocate an application matcher. |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 242 | * |
| 243 | * @param app application object we are looking for |
| 244 | * @return matcher |
| 245 | */ |
| 246 | private static AppJsonMatcher matchesApp(Application app) { |
| 247 | return new AppJsonMatcher(app); |
| 248 | } |
| 249 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 250 | /** |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 251 | * Factory to allocate an application Id matcher. |
| 252 | * |
| 253 | * @param appId application Id object we are looking for |
| 254 | * @return matcher |
| 255 | */ |
| 256 | private static AppIdJsonMatcher matchesAppId(ApplicationId appId) { |
| 257 | return new AppIdJsonMatcher(appId); |
| 258 | } |
| 259 | |
| 260 | /** |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 261 | * Initializes test mocks and environment. |
| 262 | */ |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 263 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 264 | public void setUpMocks() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 265 | appService = createMock(ApplicationAdminService.class); |
| 266 | coreService = createMock(CoreService.class); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 267 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 268 | expect(appService.getId("one")) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 269 | .andReturn(id1) |
| 270 | .anyTimes(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 271 | expect(appService.getId("two")) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 272 | .andReturn(id2) |
| 273 | .anyTimes(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 274 | expect(appService.getId("three")) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 275 | .andReturn(id3) |
| 276 | .anyTimes(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 277 | expect(appService.getId("four")) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 278 | .andReturn(id4) |
| 279 | .anyTimes(); |
| 280 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 281 | expect(appService.getApplication(id3)) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 282 | .andReturn(app3) |
| 283 | .anyTimes(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 284 | expect(appService.getState(isA(ApplicationId.class))) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 285 | .andReturn(ApplicationState.ACTIVE) |
| 286 | .anyTimes(); |
| 287 | |
| 288 | // Register the services needed for the test |
| 289 | CodecManager codecService = new CodecManager(); |
| 290 | codecService.activate(); |
| 291 | ServiceDirectory testDirectory = |
| 292 | new TestServiceDirectory() |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 293 | .add(ApplicationAdminService.class, appService) |
| 294 | .add(ApplicationService.class, appService) |
| 295 | .add(CoreService.class, coreService) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 296 | .add(CodecService.class, codecService); |
| 297 | |
| 298 | BaseResource.setServiceDirectory(testDirectory); |
| 299 | } |
| 300 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 301 | /** |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 302 | * Tests a GET of all applications when no applications are present. |
| 303 | */ |
| 304 | @Test |
| 305 | public void getAllApplicationsEmpty() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 306 | expect(appService.getApplications()) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 307 | .andReturn(ImmutableSet.of()); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 308 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 309 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 310 | WebTarget wt = target(); |
| 311 | String response = wt.path("applications").request().get(String.class); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 312 | assertThat(response, is("{\"applications\":[]}")); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 313 | |
| 314 | verify(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Tests a GET of all applications with data. |
| 319 | */ |
| 320 | @Test |
| 321 | public void getAllApplicationsPopulated() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 322 | expect(appService.getApplications()) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 323 | .andReturn(ImmutableSet.of(app1, app2, app3, app4)); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 324 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 325 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 326 | WebTarget wt = target(); |
| 327 | String response = wt.path("applications").request().get(String.class); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 328 | assertThat(response, containsString("{\"applications\":[")); |
| 329 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 330 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 331 | assertThat(result, notNullValue()); |
| 332 | |
| 333 | assertThat(result.names(), hasSize(1)); |
| 334 | assertThat(result.names().get(0), is("applications")); |
| 335 | |
| 336 | JsonArray jsonApps = result.get("applications").asArray(); |
| 337 | assertThat(jsonApps, notNullValue()); |
| 338 | assertThat(jsonApps.size(), is(4)); |
| 339 | |
| 340 | assertThat(jsonApps.get(0).asObject(), matchesApp(app1)); |
| 341 | assertThat(jsonApps.get(1).asObject(), matchesApp(app2)); |
| 342 | assertThat(jsonApps.get(2).asObject(), matchesApp(app3)); |
| 343 | assertThat(jsonApps.get(3).asObject(), matchesApp(app4)); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 344 | |
| 345 | verify(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Tests a GET of a single application. |
| 350 | */ |
| 351 | @Test |
| 352 | public void getSingleApplication() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 353 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 354 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 355 | WebTarget wt = target(); |
| 356 | String response = wt.path("applications/three").request().get(String.class); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 357 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 358 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 359 | assertThat(result, notNullValue()); |
| 360 | |
| 361 | assertThat(result, matchesApp(app3)); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 362 | |
| 363 | verify(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Tests a DELETE of a single application - this should |
| 368 | * attempt to uninstall it. |
| 369 | */ |
| 370 | @Test |
| 371 | public void deleteApplication() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 372 | appService.uninstall(id3); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 373 | expectLastCall(); |
| 374 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 375 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 376 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 377 | WebTarget wt = target(); |
| 378 | wt.path("applications/three").request().delete(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 379 | |
| 380 | verify(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Tests a DELETE of a single active application - this should |
| 385 | * attempt to uninstall it. |
| 386 | */ |
| 387 | @Test |
| 388 | public void deleteActiveApplication() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 389 | appService.deactivate(id3); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 390 | expectLastCall(); |
| 391 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 392 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 393 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 394 | WebTarget wt = target(); |
| 395 | wt.path("applications/three/active").request().delete(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 396 | |
| 397 | verify(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Tests a POST operation to the "active" URL. This should attempt to |
| 402 | * activate the application. |
| 403 | */ |
| 404 | @Test |
| 405 | public void postActiveApplication() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 406 | appService.activate(id3); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 407 | expectLastCall(); |
| 408 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 409 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 410 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 411 | WebTarget wt = target(); |
| 412 | wt.path("applications/three/active").request().post(null); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 413 | |
| 414 | verify(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Tests a POST operation. This should attempt to |
| 419 | * install the application. |
| 420 | */ |
| 421 | @Test |
| 422 | public void postApplication() { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 423 | expect(appService.install(isA(InputStream.class))) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 424 | .andReturn(app4) |
| 425 | .once(); |
| 426 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 427 | replay(appService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 428 | |
| 429 | ApplicationCodec codec = new ApplicationCodec(); |
| 430 | String app4Json = codec.encode(app4, |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 431 | new MockCodecContextWithAppService(appService)) |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 432 | .asText(); |
| 433 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 434 | WebTarget wt = target(); |
| 435 | String response = wt.path("applications").request().post( |
| 436 | Entity.entity(app4Json, MediaType.APPLICATION_OCTET_STREAM), String.class); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 437 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 438 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 439 | assertThat(result, notNullValue()); |
| 440 | |
| 441 | assertThat(result, matchesApp(app4)); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 442 | |
| 443 | verify(appService); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Tests a POST operation. This should attempt to register an on/off platform |
| 448 | * application ID. |
| 449 | */ |
| 450 | @Test |
| 451 | public void postRegisterAppId() { |
| 452 | expect(coreService.registerApplication("app1")).andReturn(id1).anyTimes(); |
| 453 | replay(coreService); |
| 454 | |
| 455 | WebTarget wt = target(); |
| 456 | wt.path("applications/app1/register").request().post(null); |
| 457 | |
| 458 | verify(coreService); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Tests a GET of all application Ids when no applications are present. |
| 463 | */ |
| 464 | @Test |
| 465 | public void getAllApplicationIdsEmpty() { |
| 466 | expect(coreService.getAppIds()).andReturn(ImmutableSet.of()); |
| 467 | replay(coreService); |
| 468 | |
| 469 | WebTarget wt = target(); |
| 470 | String response = wt.path("applications/ids").request().get(String.class); |
| 471 | assertThat(response, is("{\"applicationIds\":[]}")); |
| 472 | |
| 473 | verify(coreService); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Tests a GET of all application Ids. |
| 478 | */ |
| 479 | @Test |
| 480 | public void getAllApplicationIdsPopulated() { |
| 481 | expect(coreService.getAppIds()) |
| 482 | .andReturn(ImmutableSet.of(id1, id2, id3, id4)); |
| 483 | replay(coreService); |
| 484 | |
| 485 | WebTarget wt = target(); |
| 486 | String response = wt.path("applications/ids").request().get(String.class); |
| 487 | |
| 488 | assertThat(response, containsString("{\"applicationIds\":[")); |
| 489 | |
| 490 | JsonObject result = Json.parse(response).asObject(); |
| 491 | assertThat(result, notNullValue()); |
| 492 | |
| 493 | assertThat(result.names(), hasSize(1)); |
| 494 | assertThat(result.names().get(0), is("applicationIds")); |
| 495 | |
| 496 | JsonArray jsonApps = result.get("applicationIds").asArray(); |
| 497 | assertThat(jsonApps, notNullValue()); |
| 498 | assertThat(jsonApps.size(), is(4)); |
| 499 | |
| 500 | assertThat(jsonApps.get(0).asObject(), matchesAppId(id1)); |
| 501 | assertThat(jsonApps.get(1).asObject(), matchesAppId(id2)); |
| 502 | assertThat(jsonApps.get(2).asObject(), matchesAppId(id3)); |
| 503 | assertThat(jsonApps.get(3).asObject(), matchesAppId(id4)); |
| 504 | |
| 505 | verify(coreService); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Tests a GET of an applicationId entry with the given numeric id. |
| 510 | */ |
| 511 | @Test |
| 512 | public void getAppIdByShortId() { |
| 513 | expect(coreService.getAppId((short) 1)).andReturn(id1); |
| 514 | replay(coreService); |
| 515 | |
| 516 | WebTarget wt = target(); |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 517 | String response = wt.path("applications/ids/entry") |
| 518 | .queryParam("id", 1).request().get(String.class); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 519 | |
| 520 | JsonObject result = Json.parse(response).asObject(); |
| 521 | assertThat(result, notNullValue()); |
| 522 | |
| 523 | assertThat(result, matchesAppId(id1)); |
| 524 | |
| 525 | verify(coreService); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Tests a GET of an applicationId entry with the given application name. |
| 530 | */ |
| 531 | @Test |
| 532 | public void getAppIdByName() { |
| 533 | expect(coreService.getAppId("app2")).andReturn(id2); |
| 534 | replay(coreService); |
| 535 | |
| 536 | WebTarget wt = target(); |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 537 | String response = wt.path("applications/ids/entry") |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 538 | .queryParam("name", "app2").request().get(String.class); |
| 539 | |
| 540 | JsonObject result = Json.parse(response).asObject(); |
| 541 | assertThat(result, notNullValue()); |
| 542 | |
| 543 | assertThat(result, matchesAppId(id2)); |
| 544 | |
| 545 | verify(coreService); |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 546 | } |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 547 | |
| 548 | /** |
| 549 | * Tests a GET of an applicationId without specifying any parameters. |
| 550 | */ |
| 551 | @Test |
| 552 | public void getAppWithNoParam() { |
| 553 | WebTarget wt = target(); |
| 554 | |
| 555 | try { |
| 556 | wt.path("applications/ids/entry").request().get(); |
| 557 | } catch (NotFoundException ex) { |
| 558 | Assert.assertThat(ex.getMessage(), |
| 559 | containsString("HTTP 404 Not Found")); |
| 560 | } |
| 561 | } |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 562 | } |