Jian Li | 3168e8b | 2017-10-18 02:54:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.incubator.protobuf.services.nb; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import io.grpc.BindableService; |
| 22 | import io.grpc.ManagedChannel; |
| 23 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onosproject.app.ApplicationListener; |
| 28 | import org.onosproject.app.ApplicationService; |
| 29 | import org.onosproject.app.ApplicationState; |
| 30 | import org.onosproject.core.Application; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.ApplicationRole; |
| 33 | import org.onosproject.core.DefaultApplication; |
| 34 | import org.onosproject.core.DefaultApplicationId; |
| 35 | import org.onosproject.core.Version; |
| 36 | import org.onosproject.grpc.app.models.ApplicationEnumsProto; |
| 37 | import org.onosproject.grpc.core.models.ApplicationIdProtoOuterClass; |
| 38 | import org.onosproject.grpc.core.models.ApplicationProtoOuterClass; |
| 39 | import org.onosproject.grpc.nb.app.ApplicationServiceGrpc; |
| 40 | import org.onosproject.incubator.protobuf.models.core.ApplicationEnumsProtoTranslator; |
| 41 | import org.onosproject.incubator.protobuf.models.core.ApplicationIdProtoTranslator; |
| 42 | import org.onosproject.incubator.protobuf.models.core.ApplicationProtoTranslator; |
| 43 | import org.onosproject.security.Permission; |
| 44 | |
| 45 | import java.io.IOException; |
| 46 | import java.util.Optional; |
| 47 | import java.util.Set; |
| 48 | |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.onosproject.grpc.nb.app.ApplicationServiceNb.*; |
| 51 | |
| 52 | /** |
| 53 | * Unit tests for applications gRPC NB services. |
| 54 | */ |
| 55 | public class GrpcNbApplicationServiceTest { |
| 56 | |
| 57 | private static InProcessServer<BindableService> inprocessServer; |
| 58 | private static ApplicationServiceGrpc.ApplicationServiceBlockingStub blockingStub; |
| 59 | private static ManagedChannel channel; |
| 60 | |
| 61 | private final ApplicationService appService = new MockApplicationService(); |
| 62 | |
| 63 | private ApplicationId id1 = new DefaultApplicationId(1, "app1"); |
| 64 | private ApplicationId id2 = new DefaultApplicationId(2, "app2"); |
| 65 | private ApplicationId id3 = new DefaultApplicationId(3, "app3"); |
| 66 | private ApplicationId id4 = new DefaultApplicationId(4, "app4"); |
| 67 | |
| 68 | private static final Version VER = Version.version(1, 2, "a", "b"); |
| 69 | |
| 70 | private DefaultApplication.Builder baseBuilder = DefaultApplication.builder() |
| 71 | .withVersion(VER) |
| 72 | .withIcon(new byte[0]) |
| 73 | .withRole(ApplicationRole.ADMIN) |
| 74 | .withPermissions(ImmutableSet.of()) |
| 75 | .withFeaturesRepo(Optional.empty()) |
| 76 | .withFeatures(ImmutableList.of("My Feature")) |
| 77 | .withRequiredApps(ImmutableList.of()); |
| 78 | |
| 79 | private Application app1 = |
| 80 | DefaultApplication.builder(baseBuilder) |
| 81 | .withAppId(id1) |
| 82 | .withTitle("title1") |
| 83 | .withDescription("desc1") |
| 84 | .withOrigin("origin1") |
| 85 | .withCategory("category1") |
| 86 | .withUrl("url1") |
| 87 | .withReadme("readme1") |
| 88 | .build(); |
| 89 | private Application app2 = |
| 90 | DefaultApplication.builder(baseBuilder) |
| 91 | .withAppId(id2) |
| 92 | .withTitle("title2") |
| 93 | .withDescription("desc2") |
| 94 | .withOrigin("origin2") |
| 95 | .withCategory("category2") |
| 96 | .withUrl("url2") |
| 97 | .withReadme("readme2") |
| 98 | .build(); |
| 99 | private Application app3 = |
| 100 | DefaultApplication.builder(baseBuilder) |
| 101 | .withAppId(id3) |
| 102 | .withTitle("title3") |
| 103 | .withDescription("desc3") |
| 104 | .withOrigin("origin3") |
| 105 | .withCategory("category3") |
| 106 | .withUrl("url3") |
| 107 | .withReadme("readme3") |
| 108 | .build(); |
| 109 | private Application app4 = |
| 110 | DefaultApplication.builder(baseBuilder) |
| 111 | .withAppId(id4) |
| 112 | .withTitle("title4") |
| 113 | .withDescription("desc4") |
| 114 | .withOrigin("origin4") |
| 115 | .withCategory("category4") |
| 116 | .withUrl("url4") |
| 117 | .withReadme("readme4") |
| 118 | .build(); |
| 119 | |
| 120 | private Set apps = ImmutableSet.of(app1, app2, app3, app4); |
| 121 | |
| 122 | /** |
| 123 | * Initializes the test environment. |
| 124 | */ |
| 125 | @Before |
| 126 | public void setUp() throws IllegalAccessException, IOException, InstantiationException { |
| 127 | |
| 128 | GrpcNbApplicationService grpcAppService = new GrpcNbApplicationService(); |
| 129 | grpcAppService.applicationService = appService; |
| 130 | inprocessServer = grpcAppService.registerInProcessServer(); |
| 131 | inprocessServer.start(); |
| 132 | |
| 133 | channel = InProcessChannelBuilder.forName("test").directExecutor() |
| 134 | .usePlaintext(true).build(); |
| 135 | |
| 136 | blockingStub = ApplicationServiceGrpc.newBlockingStub(channel); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Finalizes the test setup. |
| 141 | */ |
| 142 | @After |
| 143 | public void tearDown() { |
| 144 | channel.shutdownNow(); |
| 145 | inprocessServer.stop(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests the invocation result of getApplications method. |
| 150 | * |
| 151 | * @throws InterruptedException |
| 152 | */ |
| 153 | @Test |
| 154 | public void testGetApplications() throws InterruptedException { |
| 155 | getApplicationsRequest request = getApplicationsRequest.getDefaultInstance(); |
| 156 | getApplicationsReply reply; |
| 157 | |
| 158 | reply = blockingStub.getApplications(request); |
| 159 | assertEquals(4, reply.getApplicationCount()); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests the invocation result of getId method. |
| 164 | * |
| 165 | * @throws InterruptedException |
| 166 | */ |
| 167 | @Test |
| 168 | public void testGetId() throws InterruptedException { |
| 169 | getIdRequest request = getIdRequest.newBuilder() |
| 170 | .setName("one") |
| 171 | .build(); |
| 172 | |
| 173 | getIdReply reply = blockingStub.getId(request); |
| 174 | ApplicationIdProtoOuterClass.ApplicationIdProto appIdProto = reply.getApplicationId(); |
| 175 | assertEquals(id1, ApplicationIdProtoTranslator.translate(appIdProto)); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Tests the invocation result of getApplication method. |
| 180 | * |
| 181 | * @throws InterruptedException |
| 182 | */ |
| 183 | @Test |
| 184 | public void testGetApplication() throws InterruptedException { |
| 185 | getApplicationRequest request = getApplicationRequest.newBuilder() |
| 186 | .setApplicationId(ApplicationIdProtoTranslator.translate(id1)) |
| 187 | .build(); |
| 188 | |
| 189 | getApplicationReply reply = blockingStub.getApplication(request); |
| 190 | ApplicationProtoOuterClass.ApplicationProto appProto = reply.getApplication(); |
| 191 | assertEquals(app1, ApplicationProtoTranslator.translate(appProto)); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Tests the invocation result of getState method. |
| 196 | * |
| 197 | * @throws InterruptedException |
| 198 | */ |
| 199 | @Test |
| 200 | public void testGetState() throws InterruptedException { |
| 201 | getStateRequest request = getStateRequest.newBuilder() |
| 202 | .setApplicationId(ApplicationIdProtoTranslator.translate(id1)) |
| 203 | .build(); |
| 204 | |
| 205 | getStateReply reply = blockingStub.getState(request); |
| 206 | ApplicationEnumsProto.ApplicationStateProto stateProto = reply.getState(); |
| 207 | assertEquals(Optional.of(ApplicationState.INSTALLED), |
| 208 | ApplicationEnumsProtoTranslator.translate(stateProto)); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Mock class for application service. |
| 213 | */ |
| 214 | private class MockApplicationService implements ApplicationService { |
| 215 | |
| 216 | MockApplicationService() {} |
| 217 | |
| 218 | @Override |
| 219 | public Set<Application> getApplications() { |
| 220 | return apps; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public ApplicationId getId(String name) { |
| 225 | |
| 226 | if ("one".equals(name)) { |
| 227 | return id1; |
| 228 | } |
| 229 | |
| 230 | if ("two".equals(name)) { |
| 231 | return id2; |
| 232 | } |
| 233 | |
| 234 | if ("three".equals(name)) { |
| 235 | return id3; |
| 236 | } |
| 237 | |
| 238 | if ("four".equals(name)) { |
| 239 | return id4; |
| 240 | } |
| 241 | |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Application getApplication(ApplicationId appId) { |
| 247 | |
| 248 | if (id1.equals(appId)) { |
| 249 | return app1; |
| 250 | } |
| 251 | |
| 252 | if (id2.equals(appId)) { |
| 253 | return app2; |
| 254 | } |
| 255 | |
| 256 | if (id3.equals(appId)) { |
| 257 | return app3; |
| 258 | } |
| 259 | |
| 260 | if (id4.equals(appId)) { |
| 261 | return app4; |
| 262 | } |
| 263 | |
| 264 | return null; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public ApplicationState getState(ApplicationId appId) { |
| 269 | return ApplicationState.INSTALLED; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public Set<Permission> getPermissions(ApplicationId appId) { |
| 274 | return null; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void registerDeactivateHook(ApplicationId appId, Runnable hook) { |
| 279 | |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void addListener(ApplicationListener listener) { |
| 284 | |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void removeListener(ApplicationListener listener) { |
| 289 | |
| 290 | } |
| 291 | } |
| 292 | } |