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