Committed by
Gerrit Code Review
Backporting the fix from onos-1.6 (change-id : I82e1e0e55bbc017d6c0cce7d9a6af7a5…
…78d7196e ) for ONOS-4641 fix Change-Id: Ia857107564232eb9775f83d4e804ec36b04bf1db
Showing
8 changed files
with
363 additions
and
38 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 19 | +import org.onosproject.codec.CodecContext; | ||
| 20 | +import org.onosproject.codec.JsonCodec; | ||
| 21 | +import org.onosproject.core.ApplicationId; | ||
| 22 | + | ||
| 23 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * ApplicationId JSON codec. | ||
| 27 | + */ | ||
| 28 | +public final class ApplicationIdCodec extends JsonCodec<ApplicationId> { | ||
| 29 | + | ||
| 30 | + private static final String APP_NAME = "name"; | ||
| 31 | + | ||
| 32 | + private static final String MISSING_MEMBER_MESSAGE = " member is required in ApplicationId"; | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + public ObjectNode encode(ApplicationId appId, CodecContext context) { | ||
| 36 | + checkNotNull(appId, "ApplicationId cannot be null"); | ||
| 37 | + | ||
| 38 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 39 | + .put("name", appId.name()); | ||
| 40 | + | ||
| 41 | + return result; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | +} |
| ... | @@ -26,6 +26,7 @@ import org.onosproject.cluster.ControllerNode; | ... | @@ -26,6 +26,7 @@ import org.onosproject.cluster.ControllerNode; |
| 26 | import org.onosproject.codec.CodecService; | 26 | import org.onosproject.codec.CodecService; |
| 27 | import org.onosproject.codec.JsonCodec; | 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.core.Application; | 28 | import org.onosproject.core.Application; |
| 29 | +import org.onosproject.core.ApplicationId; | ||
| 29 | import org.onosproject.net.Annotations; | 30 | import org.onosproject.net.Annotations; |
| 30 | import org.onosproject.net.ConnectPoint; | 31 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.Device; | 32 | import org.onosproject.net.Device; |
| ... | @@ -87,6 +88,7 @@ public class CodecManager implements CodecService { | ... | @@ -87,6 +88,7 @@ public class CodecManager implements CodecService { |
| 87 | public void activate() { | 88 | public void activate() { |
| 88 | codecs.clear(); | 89 | codecs.clear(); |
| 89 | registerCodec(Application.class, new ApplicationCodec()); | 90 | registerCodec(Application.class, new ApplicationCodec()); |
| 91 | + registerCodec(ApplicationId.class, new ApplicationIdCodec()); | ||
| 90 | registerCodec(ControllerNode.class, new ControllerNodeCodec()); | 92 | registerCodec(ControllerNode.class, new ControllerNodeCodec()); |
| 91 | registerCodec(Annotations.class, new AnnotationsCodec()); | 93 | registerCodec(Annotations.class, new AnnotationsCodec()); |
| 92 | registerCodec(Device.class, new DeviceCodec()); | 94 | registerCodec(Device.class, new DeviceCodec()); | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 20 | +import org.hamcrest.Description; | ||
| 21 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
| 22 | +import org.junit.Before; | ||
| 23 | +import org.junit.Test; | ||
| 24 | +import org.onosproject.codec.JsonCodec; | ||
| 25 | +import org.onosproject.core.ApplicationId; | ||
| 26 | +import org.onosproject.core.DefaultApplicationId; | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 30 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Unit tests for ApplicationId codec. | ||
| 34 | + */ | ||
| 35 | +public final class ApplicationIdCodecTest { | ||
| 36 | + | ||
| 37 | + MockCodecContext context; | ||
| 38 | + JsonCodec<ApplicationId> applicationIdCodec; | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Sets up for each test. Creates a context and fetches the applicationId | ||
| 42 | + * codec. | ||
| 43 | + */ | ||
| 44 | + @Before | ||
| 45 | + public void setUp() { | ||
| 46 | + context = new MockCodecContext(); | ||
| 47 | + applicationIdCodec = context.codec(ApplicationId.class); | ||
| 48 | + assertThat(applicationIdCodec, notNullValue()); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Tests encoding of an application id object. | ||
| 53 | + */ | ||
| 54 | + @Test | ||
| 55 | + public void testApplicationIdEncode() { | ||
| 56 | + | ||
| 57 | + int id = 1; | ||
| 58 | + String name = "org.onosproject.foo"; | ||
| 59 | + ApplicationId appId = new DefaultApplicationId(id, name); | ||
| 60 | + | ||
| 61 | + ObjectNode applicationIdJson = applicationIdCodec.encode(appId, context); | ||
| 62 | + assertThat(applicationIdJson, ApplicationIdJsonMatcher.matchesApplicationId(appId)); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + private static final class ApplicationIdJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
| 66 | + | ||
| 67 | + private final ApplicationId applicationId; | ||
| 68 | + | ||
| 69 | + private ApplicationIdJsonMatcher(ApplicationId applicationId) { | ||
| 70 | + this.applicationId = applicationId; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @Override | ||
| 74 | + protected boolean matchesSafely(JsonNode jsonNode, Description description) { | ||
| 75 | + | ||
| 76 | + String jsonAppName = jsonNode.get("name").asText(); | ||
| 77 | + String appName = applicationId.name(); | ||
| 78 | + | ||
| 79 | + if (!jsonAppName.equals(appName)) { | ||
| 80 | + description.appendText("application name was " + jsonAppName); | ||
| 81 | + return false; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + return true; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + @Override | ||
| 88 | + public void describeTo(Description description) { | ||
| 89 | + description.appendText(applicationId.toString()); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + static ApplicationIdJsonMatcher matchesApplicationId(ApplicationId applicationId) { | ||
| 93 | + return new ApplicationIdJsonMatcher(applicationId); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | +} |
| ... | @@ -18,6 +18,7 @@ package org.onosproject.rest.resources; | ... | @@ -18,6 +18,7 @@ package org.onosproject.rest.resources; |
| 18 | import org.onosproject.app.ApplicationAdminService; | 18 | import org.onosproject.app.ApplicationAdminService; |
| 19 | import org.onosproject.core.Application; | 19 | import org.onosproject.core.Application; |
| 20 | import org.onosproject.core.ApplicationId; | 20 | import org.onosproject.core.ApplicationId; |
| 21 | +import org.onosproject.core.CoreService; | ||
| 21 | import org.onosproject.rest.AbstractWebResource; | 22 | import org.onosproject.rest.AbstractWebResource; |
| 22 | 23 | ||
| 23 | import javax.ws.rs.Consumes; | 24 | import javax.ws.rs.Consumes; |
| ... | @@ -143,9 +144,45 @@ public class ApplicationsWebResource extends AbstractWebResource { | ... | @@ -143,9 +144,45 @@ public class ApplicationsWebResource extends AbstractWebResource { |
| 143 | return response(service, appId); | 144 | return response(service, appId); |
| 144 | } | 145 | } |
| 145 | 146 | ||
| 147 | + /** | ||
| 148 | + * Registers an on or off platform application. | ||
| 149 | + * | ||
| 150 | + * @param name application name | ||
| 151 | + * @return 200 OK; 404; 401 | ||
| 152 | + * @onos.rsModel ApplicationId | ||
| 153 | + */ | ||
| 154 | + @POST | ||
| 155 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 156 | + @Path("{name}/register") | ||
| 157 | + public Response registerAppId(@PathParam("name") String name) { | ||
| 158 | + CoreService service = get(CoreService.class); | ||
| 159 | + ApplicationId appId = service.registerApplication(name); | ||
| 160 | + return response(appId); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * Gets a collection of application ids. | ||
| 165 | + * Returns array of all registered application ids. | ||
| 166 | + * | ||
| 167 | + * @return 200 OK; 404; 401 | ||
| 168 | + * @onos.rsModel ApplicationIds | ||
| 169 | + */ | ||
| 170 | + @GET | ||
| 171 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 172 | + @Path("ids") | ||
| 173 | + public Response getAppIds() { | ||
| 174 | + CoreService service = get(CoreService.class); | ||
| 175 | + Set<ApplicationId> appIds = service.getAppIds(); | ||
| 176 | + return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build(); | ||
| 177 | + } | ||
| 178 | + | ||
| 146 | private Response response(ApplicationAdminService service, ApplicationId appId) { | 179 | private Response response(ApplicationAdminService service, ApplicationId appId) { |
| 147 | Application app = service.getApplication(appId); | 180 | Application app = service.getApplication(appId); |
| 148 | return ok(codec(Application.class).encode(app, this)).build(); | 181 | return ok(codec(Application.class).encode(app, this)).build(); |
| 149 | } | 182 | } |
| 150 | 183 | ||
| 184 | + private Response response(ApplicationId appId) { | ||
| 185 | + return ok(codec(ApplicationId.class).encode(appId, this)).build(); | ||
| 186 | + } | ||
| 187 | + | ||
| 151 | } | 188 | } | ... | ... |
| 1 | +{ | ||
| 2 | + "type": "object", | ||
| 3 | + "title": "applicationIds", | ||
| 4 | + "required": [ | ||
| 5 | + "applicationIds" | ||
| 6 | + ], | ||
| 7 | + "properties": { | ||
| 8 | + "applicationIds": { | ||
| 9 | + "type": "array", | ||
| 10 | + "xml": { | ||
| 11 | + "name": "applicationIds", | ||
| 12 | + "wrapped": true | ||
| 13 | + }, | ||
| 14 | + "items": { | ||
| 15 | + "type": "object", | ||
| 16 | + "title": "applicationId", | ||
| 17 | + "required": [ | ||
| 18 | + "name" | ||
| 19 | + ], | ||
| 20 | + "properties": { | ||
| 21 | + "name": { | ||
| 22 | + "type": "string", | ||
| 23 | + "example": "org.onosproject.distributedprimitives" | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | +} |
| ... | @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableSet; | ... | @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableSet; |
| 23 | import com.sun.jersey.api.client.WebResource; | 23 | import com.sun.jersey.api.client.WebResource; |
| 24 | import org.hamcrest.Description; | 24 | import org.hamcrest.Description; |
| 25 | import org.hamcrest.TypeSafeMatcher; | 25 | import org.hamcrest.TypeSafeMatcher; |
| 26 | -import org.junit.After; | ||
| 27 | import org.junit.Before; | 26 | import org.junit.Before; |
| 28 | import org.junit.Test; | 27 | import org.junit.Test; |
| 29 | import org.onlab.osgi.ServiceDirectory; | 28 | import org.onlab.osgi.ServiceDirectory; |
| ... | @@ -39,6 +38,7 @@ import org.onosproject.codec.impl.MockCodecContext; | ... | @@ -39,6 +38,7 @@ import org.onosproject.codec.impl.MockCodecContext; |
| 39 | import org.onosproject.core.Application; | 38 | import org.onosproject.core.Application; |
| 40 | import org.onosproject.core.ApplicationId; | 39 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.ApplicationRole; | 40 | import org.onosproject.core.ApplicationRole; |
| 41 | +import org.onosproject.core.CoreService; | ||
| 42 | import org.onosproject.core.DefaultApplication; | 42 | import org.onosproject.core.DefaultApplication; |
| 43 | import org.onosproject.core.DefaultApplicationId; | 43 | import org.onosproject.core.DefaultApplicationId; |
| 44 | import org.onosproject.core.Version; | 44 | import org.onosproject.core.Version; |
| ... | @@ -60,21 +60,36 @@ import static org.hamcrest.Matchers.notNullValue; | ... | @@ -60,21 +60,36 @@ import static org.hamcrest.Matchers.notNullValue; |
| 60 | 60 | ||
| 61 | public class ApplicationsResourceTest extends ResourceTest { | 61 | public class ApplicationsResourceTest extends ResourceTest { |
| 62 | 62 | ||
| 63 | - private static class MockCodecContextWithService extends MockCodecContext { | 63 | + private static class MockCodecContextWithAppService extends MockCodecContext { |
| 64 | - private ApplicationAdminService service; | 64 | + private ApplicationAdminService appService; |
| 65 | 65 | ||
| 66 | - MockCodecContextWithService(ApplicationAdminService service) { | 66 | + MockCodecContextWithAppService(ApplicationAdminService appService) { |
| 67 | - this.service = service; | 67 | + this.appService = appService; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | @Override | 70 | @Override |
| 71 | @SuppressWarnings("unchecked") | 71 | @SuppressWarnings("unchecked") |
| 72 | public <T> T getService(Class<T> serviceClass) { | 72 | public <T> T getService(Class<T> serviceClass) { |
| 73 | - return (T) service; | 73 | + return (T) appService; |
| 74 | } | 74 | } |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | - private ApplicationAdminService service; | 77 | + private static class MockCodecContextWithCoreService extends MockCodecContext { |
| 78 | + private CoreService coreService; | ||
| 79 | + | ||
| 80 | + MockCodecContextWithCoreService(CoreService coreService) { | ||
| 81 | + this.coreService = coreService; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + @Override | ||
| 85 | + @SuppressWarnings("unchecked") | ||
| 86 | + public <T> T getService(Class<T> serviceClass) { | ||
| 87 | + return (T) coreService; | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + private ApplicationAdminService appService; | ||
| 92 | + private CoreService coreService; | ||
| 78 | private ApplicationId id1 = new DefaultApplicationId(1, "app1"); | 93 | private ApplicationId id1 = new DefaultApplicationId(1, "app1"); |
| 79 | private ApplicationId id2 = new DefaultApplicationId(2, "app2"); | 94 | private ApplicationId id2 = new DefaultApplicationId(2, "app2"); |
| 80 | private ApplicationId id3 = new DefaultApplicationId(3, "app3"); | 95 | private ApplicationId id3 = new DefaultApplicationId(3, "app3"); |
| ... | @@ -163,29 +178,69 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -163,29 +178,69 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 163 | } | 178 | } |
| 164 | 179 | ||
| 165 | /** | 180 | /** |
| 181 | + * Hamcrest matcher to check that an application id representation in JSON. | ||
| 182 | + */ | ||
| 183 | + private static final class AppIdJsonMatcher extends TypeSafeMatcher<JsonObject> { | ||
| 184 | + private final ApplicationId appId; | ||
| 185 | + private String reason = ""; | ||
| 186 | + | ||
| 187 | + private AppIdJsonMatcher(ApplicationId appId) { | ||
| 188 | + this.appId = appId; | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + @Override | ||
| 192 | + protected boolean matchesSafely(JsonObject jsonAppId) { | ||
| 193 | + // check name | ||
| 194 | + String jsonName = jsonAppId.get("name").asString(); | ||
| 195 | + if (!jsonName.equals(appId.name())) { | ||
| 196 | + reason = "name " + appId.name(); | ||
| 197 | + return false; | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + return true; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + @Override | ||
| 204 | + public void describeTo(Description description) { | ||
| 205 | + description.appendText(reason); | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + /** | ||
| 210 | + * Factory to allocate an application Id matcher. | ||
| 211 | + * | ||
| 212 | + * @param appId application Id object we are looking for | ||
| 213 | + * @return matcher | ||
| 214 | + */ | ||
| 215 | + private static AppIdJsonMatcher matchesAppId(ApplicationId appId) { | ||
| 216 | + return new AppIdJsonMatcher(appId); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + /** | ||
| 166 | * Initializes test mocks and environment. | 220 | * Initializes test mocks and environment. |
| 167 | */ | 221 | */ |
| 168 | @Before | 222 | @Before |
| 169 | public void setUpMocks() { | 223 | public void setUpMocks() { |
| 170 | - service = createMock(ApplicationAdminService.class); | 224 | + appService = createMock(ApplicationAdminService.class); |
| 225 | + coreService = createMock(CoreService.class); | ||
| 171 | 226 | ||
| 172 | - expect(service.getId("one")) | 227 | + expect(appService.getId("one")) |
| 173 | .andReturn(id1) | 228 | .andReturn(id1) |
| 174 | .anyTimes(); | 229 | .anyTimes(); |
| 175 | - expect(service.getId("two")) | 230 | + expect(appService.getId("two")) |
| 176 | .andReturn(id2) | 231 | .andReturn(id2) |
| 177 | .anyTimes(); | 232 | .anyTimes(); |
| 178 | - expect(service.getId("three")) | 233 | + expect(appService.getId("three")) |
| 179 | .andReturn(id3) | 234 | .andReturn(id3) |
| 180 | .anyTimes(); | 235 | .anyTimes(); |
| 181 | - expect(service.getId("four")) | 236 | + expect(appService.getId("four")) |
| 182 | .andReturn(id4) | 237 | .andReturn(id4) |
| 183 | .anyTimes(); | 238 | .anyTimes(); |
| 184 | 239 | ||
| 185 | - expect(service.getApplication(id3)) | 240 | + expect(appService.getApplication(id3)) |
| 186 | .andReturn(app3) | 241 | .andReturn(app3) |
| 187 | .anyTimes(); | 242 | .anyTimes(); |
| 188 | - expect(service.getState(isA(ApplicationId.class))) | 243 | + expect(appService.getState(isA(ApplicationId.class))) |
| 189 | .andReturn(ApplicationState.ACTIVE) | 244 | .andReturn(ApplicationState.ACTIVE) |
| 190 | .anyTimes(); | 245 | .anyTimes(); |
| 191 | 246 | ||
| ... | @@ -194,33 +249,27 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -194,33 +249,27 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 194 | codecService.activate(); | 249 | codecService.activate(); |
| 195 | ServiceDirectory testDirectory = | 250 | ServiceDirectory testDirectory = |
| 196 | new TestServiceDirectory() | 251 | new TestServiceDirectory() |
| 197 | - .add(ApplicationAdminService.class, service) | 252 | + .add(ApplicationAdminService.class, appService) |
| 198 | - .add(ApplicationService.class, service) | 253 | + .add(ApplicationService.class, appService) |
| 254 | + .add(CoreService.class, coreService) | ||
| 199 | .add(CodecService.class, codecService); | 255 | .add(CodecService.class, codecService); |
| 200 | 256 | ||
| 201 | BaseResource.setServiceDirectory(testDirectory); | 257 | BaseResource.setServiceDirectory(testDirectory); |
| 202 | } | 258 | } |
| 203 | 259 | ||
| 204 | /** | 260 | /** |
| 205 | - * Verifies test mocks. | ||
| 206 | - */ | ||
| 207 | - @After | ||
| 208 | - public void tearDownMocks() { | ||
| 209 | - verify(service); | ||
| 210 | - } | ||
| 211 | - | ||
| 212 | - /** | ||
| 213 | * Tests a GET of all applications when no applications are present. | 261 | * Tests a GET of all applications when no applications are present. |
| 214 | */ | 262 | */ |
| 215 | @Test | 263 | @Test |
| 216 | public void getAllApplicationsEmpty() { | 264 | public void getAllApplicationsEmpty() { |
| 217 | - expect(service.getApplications()) | 265 | + expect(appService.getApplications()) |
| 218 | .andReturn(ImmutableSet.of()); | 266 | .andReturn(ImmutableSet.of()); |
| 219 | - replay(service); | 267 | + replay(appService); |
| 220 | 268 | ||
| 221 | WebResource rs = resource(); | 269 | WebResource rs = resource(); |
| 222 | String response = rs.path("applications").get(String.class); | 270 | String response = rs.path("applications").get(String.class); |
| 223 | assertThat(response, is("{\"applications\":[]}")); | 271 | assertThat(response, is("{\"applications\":[]}")); |
| 272 | + verify(appService); | ||
| 224 | } | 273 | } |
| 225 | 274 | ||
| 226 | /** | 275 | /** |
| ... | @@ -228,9 +277,9 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -228,9 +277,9 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 228 | */ | 277 | */ |
| 229 | @Test | 278 | @Test |
| 230 | public void getAllApplicationsPopulated() { | 279 | public void getAllApplicationsPopulated() { |
| 231 | - expect(service.getApplications()) | 280 | + expect(appService.getApplications()) |
| 232 | .andReturn(ImmutableSet.of(app1, app2, app3, app4)); | 281 | .andReturn(ImmutableSet.of(app1, app2, app3, app4)); |
| 233 | - replay(service); | 282 | + replay(appService); |
| 234 | 283 | ||
| 235 | WebResource rs = resource(); | 284 | WebResource rs = resource(); |
| 236 | String response = rs.path("applications").get(String.class); | 285 | String response = rs.path("applications").get(String.class); |
| ... | @@ -250,6 +299,7 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -250,6 +299,7 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 250 | assertThat(jsonApps.get(1).asObject(), matchesApp(app2)); | 299 | assertThat(jsonApps.get(1).asObject(), matchesApp(app2)); |
| 251 | assertThat(jsonApps.get(2).asObject(), matchesApp(app3)); | 300 | assertThat(jsonApps.get(2).asObject(), matchesApp(app3)); |
| 252 | assertThat(jsonApps.get(3).asObject(), matchesApp(app4)); | 301 | assertThat(jsonApps.get(3).asObject(), matchesApp(app4)); |
| 302 | + verify(appService); | ||
| 253 | } | 303 | } |
| 254 | 304 | ||
| 255 | /** | 305 | /** |
| ... | @@ -257,7 +307,7 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -257,7 +307,7 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 257 | */ | 307 | */ |
| 258 | @Test | 308 | @Test |
| 259 | public void getSingleApplication() { | 309 | public void getSingleApplication() { |
| 260 | - replay(service); | 310 | + replay(appService); |
| 261 | 311 | ||
| 262 | WebResource rs = resource(); | 312 | WebResource rs = resource(); |
| 263 | String response = rs.path("applications/three").get(String.class); | 313 | String response = rs.path("applications/three").get(String.class); |
| ... | @@ -266,6 +316,7 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -266,6 +316,7 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 266 | assertThat(result, notNullValue()); | 316 | assertThat(result, notNullValue()); |
| 267 | 317 | ||
| 268 | assertThat(result, matchesApp(app3)); | 318 | assertThat(result, matchesApp(app3)); |
| 319 | + verify(appService); | ||
| 269 | } | 320 | } |
| 270 | 321 | ||
| 271 | /** | 322 | /** |
| ... | @@ -274,13 +325,14 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -274,13 +325,14 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 274 | */ | 325 | */ |
| 275 | @Test | 326 | @Test |
| 276 | public void deleteApplication() { | 327 | public void deleteApplication() { |
| 277 | - service.uninstall(id3); | 328 | + appService.uninstall(id3); |
| 278 | expectLastCall(); | 329 | expectLastCall(); |
| 279 | 330 | ||
| 280 | - replay(service); | 331 | + replay(appService); |
| 281 | 332 | ||
| 282 | WebResource rs = resource(); | 333 | WebResource rs = resource(); |
| 283 | rs.path("applications/three").delete(); | 334 | rs.path("applications/three").delete(); |
| 335 | + verify(appService); | ||
| 284 | } | 336 | } |
| 285 | 337 | ||
| 286 | /** | 338 | /** |
| ... | @@ -289,13 +341,14 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -289,13 +341,14 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 289 | */ | 341 | */ |
| 290 | @Test | 342 | @Test |
| 291 | public void deleteActiveApplication() { | 343 | public void deleteActiveApplication() { |
| 292 | - service.deactivate(id3); | 344 | + appService.deactivate(id3); |
| 293 | expectLastCall(); | 345 | expectLastCall(); |
| 294 | 346 | ||
| 295 | - replay(service); | 347 | + replay(appService); |
| 296 | 348 | ||
| 297 | WebResource rs = resource(); | 349 | WebResource rs = resource(); |
| 298 | rs.path("applications/three/active").delete(); | 350 | rs.path("applications/three/active").delete(); |
| 351 | + verify(appService); | ||
| 299 | } | 352 | } |
| 300 | 353 | ||
| 301 | /** | 354 | /** |
| ... | @@ -304,13 +357,14 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -304,13 +357,14 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 304 | */ | 357 | */ |
| 305 | @Test | 358 | @Test |
| 306 | public void postActiveApplication() { | 359 | public void postActiveApplication() { |
| 307 | - service.activate(id3); | 360 | + appService.activate(id3); |
| 308 | expectLastCall(); | 361 | expectLastCall(); |
| 309 | 362 | ||
| 310 | - replay(service); | 363 | + replay(appService); |
| 311 | 364 | ||
| 312 | WebResource rs = resource(); | 365 | WebResource rs = resource(); |
| 313 | rs.path("applications/three/active").post(); | 366 | rs.path("applications/three/active").post(); |
| 367 | + verify(appService); | ||
| 314 | } | 368 | } |
| 315 | 369 | ||
| 316 | /** | 370 | /** |
| ... | @@ -319,15 +373,15 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -319,15 +373,15 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 319 | */ | 373 | */ |
| 320 | @Test | 374 | @Test |
| 321 | public void postApplication() { | 375 | public void postApplication() { |
| 322 | - expect(service.install(isA(InputStream.class))) | 376 | + expect(appService.install(isA(InputStream.class))) |
| 323 | .andReturn(app4) | 377 | .andReturn(app4) |
| 324 | .once(); | 378 | .once(); |
| 325 | 379 | ||
| 326 | - replay(service); | 380 | + replay(appService); |
| 327 | 381 | ||
| 328 | ApplicationCodec codec = new ApplicationCodec(); | 382 | ApplicationCodec codec = new ApplicationCodec(); |
| 329 | String app4Json = codec.encode(app4, | 383 | String app4Json = codec.encode(app4, |
| 330 | - new MockCodecContextWithService(service)) | 384 | + new MockCodecContextWithAppService(appService)) |
| 331 | .asText(); | 385 | .asText(); |
| 332 | 386 | ||
| 333 | WebResource rs = resource(); | 387 | WebResource rs = resource(); |
| ... | @@ -337,5 +391,52 @@ public class ApplicationsResourceTest extends ResourceTest { | ... | @@ -337,5 +391,52 @@ public class ApplicationsResourceTest extends ResourceTest { |
| 337 | assertThat(result, notNullValue()); | 391 | assertThat(result, notNullValue()); |
| 338 | 392 | ||
| 339 | assertThat(result, matchesApp(app4)); | 393 | assertThat(result, matchesApp(app4)); |
| 394 | + verify(appService); | ||
| 395 | + } | ||
| 396 | + | ||
| 397 | + /** | ||
| 398 | + * Tests a POST operation to register appid. | ||
| 399 | + */ | ||
| 400 | + @Test | ||
| 401 | + public void postApplicationId() { | ||
| 402 | + expect(coreService.registerApplication("app1")).andReturn(id1).once(); | ||
| 403 | + | ||
| 404 | + replay(coreService); | ||
| 405 | + | ||
| 406 | + WebResource rs = resource(); | ||
| 407 | + rs.path("applications/app1/register").post(); | ||
| 408 | + verify(coreService); | ||
| 409 | + } | ||
| 410 | + | ||
| 411 | + /** | ||
| 412 | + * Tests a GET of all application Ids. | ||
| 413 | + */ | ||
| 414 | + @Test | ||
| 415 | + public void getAllApplicationIdsPopulated() { | ||
| 416 | + expect(coreService.getAppIds()) | ||
| 417 | + .andReturn(ImmutableSet.of(id1, id2, id3, id4)); | ||
| 418 | + replay(coreService); | ||
| 419 | + | ||
| 420 | + WebResource rs = resource(); | ||
| 421 | + String response = rs.path("applications/ids").get(String.class); | ||
| 422 | + assertThat(response, containsString("{\"applicationIds\":[")); | ||
| 423 | + | ||
| 424 | + JsonObject result = Json.parse(response).asObject(); | ||
| 425 | + assertThat(result, notNullValue()); | ||
| 426 | + | ||
| 427 | + assertThat(result.names(), hasSize(1)); | ||
| 428 | + assertThat(result.names().get(0), is("applicationIds")); | ||
| 429 | + | ||
| 430 | + JsonArray jsonApps = result.get("applicationIds").asArray(); | ||
| 431 | + assertThat(jsonApps, notNullValue()); | ||
| 432 | + assertThat(jsonApps.size(), is(4)); | ||
| 433 | + | ||
| 434 | + assertThat(jsonApps.get(0).asObject(), matchesAppId(id1)); | ||
| 435 | + assertThat(jsonApps.get(1).asObject(), matchesAppId(id2)); | ||
| 436 | + assertThat(jsonApps.get(2).asObject(), matchesAppId(id3)); | ||
| 437 | + assertThat(jsonApps.get(3).asObject(), matchesAppId(id4)); | ||
| 438 | + | ||
| 439 | + verify(coreService); | ||
| 340 | } | 440 | } |
| 441 | + | ||
| 341 | } | 442 | } | ... | ... |
-
Please register or login to post a comment