blob: 2b6021c40394a807bd5d1f5d176b225bdb64bfff [file] [log] [blame]
Frank Wangcaef3142017-08-09 15:24:45 +08001/*
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
17package org.onosproject.incubator.protobuf.services.nb;
18
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Maps;
21import io.grpc.BindableService;
22import io.grpc.ManagedChannel;
23import io.grpc.inprocess.InProcessChannelBuilder;
24import org.junit.AfterClass;
25import org.junit.BeforeClass;
26import org.junit.Test;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.cfg.ConfigProperty;
29import org.onosproject.grpc.cfg.models.ConfigPropertyProtoOuterClass;
30import org.onosproject.grpc.nb.cfg.ComponentConfigServiceGrpc;
31import org.onosproject.grpc.nb.cfg.ComponentConfigServiceNb;
32import org.onosproject.grpc.nb.cfg.ComponentConfigServiceGrpc.ComponentConfigServiceBlockingStub;
33import org.onosproject.incubator.protobuf.models.cfg.ConfigPropertyProtoTranslator;
34import java.io.IOException;
35import java.util.Collections;
36import java.util.HashSet;
37import java.util.Map;
38import java.util.Set;
39
40import static org.onosproject.grpc.nb.cfg.ComponentConfigServiceNb.*;
41import static org.junit.Assert.assertTrue;
42import static org.onosproject.cfg.ConfigProperty.Type.STRING;
43
44/**
45 * Unit tests of gRPC northbound component config service.
46 */
47public class GrpcNbComponentConfigServiceTest {
48
49 private static InProcessServer<BindableService> inprocessServer;
50 private static ComponentConfigServiceBlockingStub blockingStub;
51 private static ManagedChannel channel;
52
53 private static Set<String> componentNames = new HashSet<>();
54 private static final Map<String, ConfigProperty> PROPERTY_MAP = Maps.newConcurrentMap();
55 private static final Map<String, ConfigProperty> PROPERTY_MAP1 = Maps.newConcurrentMap();
56 private static final Map<String, String> STRING_MAP = Maps.newConcurrentMap();
57 private static final Map<String, String> STRING_MAP1 = Maps.newConcurrentMap();
58 private static final Map<String, String> STRING_MAP2 = Maps.newConcurrentMap();
59 private static final ComponentConfigService MOCK_COMPONENTCONFIG = new MockComponentConfigService();
60 private static final String COMPONENTCONFIGNAME = "org.onosprject.test";
61 private static final String COMPONENTCONFIGNAME1 = "org.onosprject.test1";
62 private static final String COMPONENTCONFIGNAME2 = "org.onosprject.test2";
63 private static final String PROPERTY_TEST_KEY = COMPONENTCONFIGNAME + "#" + "test";
64 private static final ConfigProperty C1 = ConfigProperty.defineProperty("foo", STRING, "dingo", "FOO");
65 private static final ConfigProperty C2 = ConfigProperty.defineProperty("bar", STRING, "bat", "BAR");
66
67 public GrpcNbComponentConfigServiceTest() {}
68
69 private static void populateComponentNames() {
70
71 componentNames.add(COMPONENTCONFIGNAME);
72 componentNames.add(COMPONENTCONFIGNAME1);
73 componentNames.add(COMPONENTCONFIGNAME2);
74 PROPERTY_MAP1.put(COMPONENTCONFIGNAME, C1);
75 STRING_MAP2.put(PROPERTY_TEST_KEY, "true");
76 }
77
78 /**
79 * Tests gRPC getComponentNames interface.
80 */
81 @Test
82 public void testGetComponentNames() throws InterruptedException {
83 getComponentNamesRequest request = ComponentConfigServiceNb.getComponentNamesRequest.getDefaultInstance();
84 getComponentNamesReply reply;
85
86 try {
87 reply = blockingStub.getComponentNames(request);
88 assertTrue(componentNames.size() == reply.getNamesCount());
89
90 Set expectedNames = Collections.emptySet();
91 expectedNames.addAll(componentNames);
92 assertTrue(reply.equals(expectedNames));
93 } catch (Exception e) {
94 e.printStackTrace();
95 }
96 }
97
98 /**
99 * Tests gRPC registerProperties interface.
100 */
101 @Test
102 public void testRegisterProperties() throws InterruptedException {
103 registerPropertiesRequest request = ComponentConfigServiceNb.registerPropertiesRequest
104 .newBuilder().setComponentClass(COMPONENTCONFIGNAME).build();
105
106 try {
107 blockingStub.registerProperties(request);
108 assertTrue(PROPERTY_MAP.get(COMPONENTCONFIGNAME).equals(C1));
109 } catch (Exception e) {
110 e.printStackTrace();
111 }
112 }
113
114 /**
115 * Tests gRPC unregisterProperties interface.
116 */
117 @Test
118 public void testUnregisterProperties() throws InterruptedException {
119 unregisterPropertiesRequest request = ComponentConfigServiceNb.unregisterPropertiesRequest
120 .newBuilder().setComponentClass(COMPONENTCONFIGNAME).build();
121
122 try {
123 blockingStub.unregisterProperties(request);
124 assertTrue(PROPERTY_MAP1.get(COMPONENTCONFIGNAME) == null);
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 }
129
130 /**
131 * Tests gRPC getProperty interface.
132 */
133 @Test
134 public void tesGetProperties() throws InterruptedException {
135 getPropertiesRequest request = ComponentConfigServiceNb.getPropertiesRequest.newBuilder()
136 .setComponentName(COMPONENTCONFIGNAME).build();
137 getPropertiesReply reply;
138
139 try {
140 reply = blockingStub.getProperties(request);
141
142 Set<ConfigProperty> configProperties = new HashSet<>();
143 for (ConfigPropertyProtoOuterClass.ConfigPropertyProto cfg : reply.getConfigPropertiesList()) {
144 configProperties.add(ConfigPropertyProtoTranslator.translate(cfg));
145 }
146
147 assertTrue(configProperties.equals(ImmutableSet.of(C1, C2)));
148 } catch (Exception e) {
149 e.printStackTrace();
150 }
151 }
152
153 /**
154 * Tests gRPC setProperty interface.
155 */
156 @Test
157 public void testSetProperty() throws InterruptedException {
158 setPropertyRequest request = ComponentConfigServiceNb.setPropertyRequest.newBuilder()
159 .setComponentName(COMPONENTCONFIGNAME)
160 .setName("test")
161 .setValue("true")
162 .build();
163
164 try {
165 blockingStub.setProperty(request);
166 assertTrue(STRING_MAP.get(PROPERTY_TEST_KEY).equals("true"));
167 } catch (Exception e) {
168 e.printStackTrace();
169 }
170 }
171
172 /**
173 * Tests gRPC preSetProperty interface.
174 */
175 @Test
176 public void testPreSetProperty() throws InterruptedException {
177 preSetPropertyRequest request = ComponentConfigServiceNb.preSetPropertyRequest.newBuilder()
178 .setComponentName(COMPONENTCONFIGNAME)
179 .setName("test")
180 .setValue("true")
181 .build();
182
183 try {
184 blockingStub.preSetProperty(request);
185 assertTrue(STRING_MAP1.get(PROPERTY_TEST_KEY).equals("true"));
186 } catch (Exception e) {
187 e.printStackTrace();
188 }
189 }
190
191 /**
192 * Tests gRPC unsetProperty interface.
193 */
194 @Test
195 public void testUnsetProperty() throws InterruptedException {
196 unsetPropertyRequest request = ComponentConfigServiceNb.unsetPropertyRequest.newBuilder()
197 .setComponentName(COMPONENTCONFIGNAME)
198 .setName("test")
199 .build();
200
201 try {
202 blockingStub.unsetProperty(request);
203 assertTrue(STRING_MAP2.get(PROPERTY_TEST_KEY) == null);
204 } catch (Exception e) {
205 e.printStackTrace();
206 }
207 }
208
209 /**
210 * Initialization before start testing gRPC northbound component config service.
211 */
212 @BeforeClass
213 public static void beforeClass() throws InstantiationException, IllegalAccessException, IOException {
214 GrpcNbComponentConfigService componentConfigService = new GrpcNbComponentConfigService();
215 componentConfigService.componentConfigService = MOCK_COMPONENTCONFIG;
216 inprocessServer = componentConfigService.registerInProcessServer();
217 inprocessServer.start();
218
219 channel = InProcessChannelBuilder.forName("test").directExecutor()
220 // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
221 // needing certificates.
222 .usePlaintext(true).build();
223 blockingStub = ComponentConfigServiceGrpc.newBlockingStub(channel);
224 populateComponentNames();
225 }
226
227 /**
228 * Finalization after test gRPC northbound component config service.
229 */
230 @AfterClass
231 public static void afterClass() {
232
233 channel.shutdownNow();
234 inprocessServer.stop();
235 }
236
237 private static class MockComponentConfigService implements ComponentConfigService {
238
239 MockComponentConfigService() {
240 }
241
242 @Override
243 public Set<String> getComponentNames() {
244 return componentNames;
245 }
246
247 @Override
248 public void registerProperties(Class<?> componentClass) {
249 PROPERTY_MAP.put(componentClass.getName(), C1);
250 }
251
252 @Override
253 public void unregisterProperties(Class<?> componentClass, boolean clear) {
254 PROPERTY_MAP1.remove(componentClass.getName());
255 }
256
257 @Override
258 public Set<ConfigProperty> getProperties(String componentName) {
259 return ImmutableSet.of(C1, C2);
260 }
261
262 @Override
263 public void setProperty(String componentName, String name, String value) {
264 STRING_MAP.put(componentName + "#" + name, value);
265 }
266
267 @Override
268 public void preSetProperty(String componentName, String name, String value) {
269 STRING_MAP1.put(componentName + "#" + name, value);
270 }
271
272 @Override
273 public void unsetProperty(String componentName, String name) {
274 STRING_MAP2.remove(componentName + "#" + name);
275 }
psnehaf31ac6f2018-08-02 01:16:46 -0400276
277 @Override
278 public ConfigProperty getProperty(String componentName, String attribute) {
279 return null;
280 }
Frank Wangcaef3142017-08-09 15:24:45 +0800281 }
282}