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