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