2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.javav2.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import com.google.common.base.Optional;
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.Field;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
32 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
35 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
46 public class RpcActionGenHelperTest {
48 @SuppressWarnings("unchecked")
49 @Test(expected = UnsupportedOperationException.class)
50 public void constructorTest() throws Throwable {
51 final Constructor<RpcActionGenHelper> constructor =
52 (Constructor<RpcActionGenHelper>) RpcActionGenHelper.class.getDeclaredConstructors()[0];
53 constructor.setAccessible(true);
54 final Object[] objs = {};
56 constructor.newInstance(objs);
57 } catch (final Exception e) {
62 @SuppressWarnings({ "rawtypes", "unchecked" })
64 public void getRoutingContextAbsentTest() throws Exception {
65 final Class[] parameterTypes = { DataSchemaNode.class };
66 final Method generate = RpcActionGenHelper.class.getDeclaredMethod("getRoutingContext", parameterTypes);
67 assertNotNull(generate);
68 generate.setAccessible(true);
69 final DataSchemaNode dataSchemaNode = mock(DataSchemaNode.class);
70 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
71 when(dataSchemaNode.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
73 final Object[] args = { dataSchemaNode };
74 final Optional<QName> result = (Optional<QName>) generate.invoke(RpcActionGenHelper.class, args);
75 assertNotNull(result);
76 assertTrue(!result.isPresent());
79 @SuppressWarnings({ "rawtypes", "unchecked" })
81 public void getRoutingContextTest() throws Exception {
82 final Class[] parameterTypes = { DataSchemaNode.class };
83 final Method generate = RpcActionGenHelper.class.getDeclaredMethod("getRoutingContext", parameterTypes);
84 assertNotNull(generate);
85 generate.setAccessible(true);
87 final DataSchemaNode dataSchemaNode = mock(DataSchemaNode.class);
88 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
89 final Field contextRef = RpcActionGenHelper.class.getDeclaredField("CONTEXT_REFERENCE");
90 contextRef.setAccessible(true);
91 final QName nodeType = (QName) contextRef.get(RpcActionGenHelper.class);
92 final UnknownSchemaNode unknownSchemaNode = mock(UnknownSchemaNode.class);
93 when(unknownSchemaNode.getNodeType()).thenReturn(nodeType);
94 final QName qname = QName.create("test", "2017-05-04", "unknown");
95 when(unknownSchemaNode.getQName()).thenReturn(qname);
96 unknownSchemaNodes.add(unknownSchemaNode);
97 when(dataSchemaNode.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
99 final Object[] args = { dataSchemaNode };
100 final Optional<QName> result = (Optional<QName>) generate.invoke(RpcActionGenHelper.class, args);
101 assertNotNull(result);
102 assertTrue(result.isPresent());
103 assertEquals(qname, result.get());
108 public void actionMethodsToGenTypeContainerAsParentTest() throws Exception {
109 actionMethodsToGenType(ContainerSchemaNode.class, false);
114 public void actionMethodsToGenTypeListAsParentTest() throws Exception {
115 actionMethodsToGenType(ListSchemaNode.class, false);
118 @SuppressWarnings("rawtypes")
120 public void rpcMethodsToGenTypeNullRpcsTest() throws Exception {
121 final Class[] parameterTypes =
122 { Module.class, Map.class, SchemaContext.class, boolean.class, Map.class, TypeProvider.class };
123 final Method generate = RpcActionGenHelper.class.getDeclaredMethod("rpcMethodsToGenType", parameterTypes);
124 assertNotNull(generate);
125 generate.setAccessible(true);
127 final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
129 final Module module = mock(Module.class);
130 when(module.getName()).thenReturn("module-name");
131 when(module.getRevision()).thenReturn(rpcQName.getRevision());
132 when(module.getNamespace()).thenReturn(rpcQName.getNamespace());
133 when(module.getRpcs()).thenReturn(null);
135 final Map<Module, ModuleContext> genCtx = new HashMap<>();
136 final SchemaContext schemaContext = mock(SchemaContext.class);
137 final boolean verboseClassComments = false;
138 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
139 final TypeProvider typeProvider = mock(TypeProvider.class);
141 final Object[] args = { module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider };
144 generate.invoke(RpcActionGenHelper.class, args);
146 } catch (final Exception e) {
148 assertTrue(e instanceof InvocationTargetException);
149 final Throwable cause = e.getCause();
150 assertNotNull(cause);
151 assertTrue(cause instanceof IllegalStateException);
155 @SuppressWarnings({ "rawtypes", "unchecked" })
157 public void rpcMethodsToGenTypeEmptyRpcsTest() throws Exception {
158 final Class[] parameterTypes =
159 { Module.class, Map.class, SchemaContext.class, boolean.class, Map.class, TypeProvider.class };
160 final Method generate = RpcActionGenHelper.class.getDeclaredMethod("rpcMethodsToGenType", parameterTypes);
161 assertNotNull(generate);
162 generate.setAccessible(true);
164 final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
166 final Module module = mock(Module.class);
167 when(module.getName()).thenReturn("module-name");
168 when(module.getRevision()).thenReturn(rpcQName.getRevision());
169 when(module.getNamespace()).thenReturn(rpcQName.getNamespace());
170 final Set<RpcDefinition> rpcs = new HashSet<>();
171 when(module.getRpcs()).thenReturn(rpcs);
173 final Map<Module, ModuleContext> genCtx = new HashMap<>();
174 final SchemaContext schemaContext = mock(SchemaContext.class);
175 final boolean verboseClassComments = false;
176 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
177 final TypeProvider typeProvider = mock(TypeProvider.class);
179 final Object[] args = { module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider };
180 final Map<Module, ModuleContext> result =
181 (Map<Module, ModuleContext>) generate.invoke(RpcActionGenHelper.class, args);
182 assertNotNull(result);
186 public void rpcMethodsToGenTypeRoutedRpcTest() throws Exception {
187 actionMethodsToGenType(ContainerSchemaNode.class, true);
190 @SuppressWarnings({ "rawtypes", "unchecked" })
192 public void rpcMethodsToGenTypeRpcTest() throws Exception {
193 final Class[] parameterTypes =
194 { Module.class, Map.class, SchemaContext.class, boolean.class, Map.class, TypeProvider.class };
195 final Method generate = RpcActionGenHelper.class.getDeclaredMethod("rpcMethodsToGenType", parameterTypes);
196 assertNotNull(generate);
197 generate.setAccessible(true);
199 final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
201 final ContainerSchemaNode rpcParent = mock(ContainerSchemaNode.class);
202 final QName rpcParentQName = QName.create(rpcQName, "rpc-parent");
203 when(rpcParent.getQName()).thenReturn(rpcParentQName);
205 final Module module = mock(Module.class);
206 when(module.getName()).thenReturn("module-name");
207 when(module.getRevision()).thenReturn(rpcQName.getRevision());
208 when(module.getNamespace()).thenReturn(rpcQName.getNamespace());
209 final Set<RpcDefinition> rpcs = new HashSet<>();
210 final RpcDefinition rpcDefinition = mock(RpcDefinition.class);
211 final SchemaPath rpcPath = SchemaPath.create(true, rpcParentQName, rpcQName);
212 when(rpcDefinition.getPath()).thenReturn(rpcPath);
213 when(rpcDefinition.getQName()).thenReturn(rpcQName);
214 when(module.getDataChildByName(rpcParentQName)).thenReturn(rpcParent);
215 rpcs.add(rpcDefinition);
216 when(module.getRpcs()).thenReturn(rpcs);
218 final ContainerSchemaNode input = mock(ContainerSchemaNode.class);
219 final QName qnameInput = QName.create(rpcQName, "rpc-input");
220 final SchemaPath inputSchemaPath = SchemaPath.create(true, rpcQName, qnameInput);
221 when(input.getQName()).thenReturn(qnameInput);
222 when(input.getPath()).thenReturn(inputSchemaPath);
223 when(rpcDefinition.getInput()).thenReturn(input);
225 final ContainerSchemaNode output = mock(ContainerSchemaNode.class);
226 final QName qnameOutput = QName.create(rpcQName, "rpc-output");
227 final SchemaPath outputSchemaPath = SchemaPath.create(true, rpcQName, qnameOutput);
228 when(output.getQName()).thenReturn(qnameOutput);
229 when(output.getPath()).thenReturn(outputSchemaPath);
230 when(rpcDefinition.getOutput()).thenReturn(output);
232 final Map<Module, ModuleContext> genCtx = new HashMap<>();
233 final ModuleContext moduleContext = new ModuleContext();
234 genCtx.put(module, moduleContext);
236 final SchemaContext schemaContext = mock(SchemaContext.class);
237 when(schemaContext.findModuleByNamespaceAndRevision(rpcQName.getNamespace(), rpcQName.getRevision()))
240 final boolean verboseClassComments = false;
241 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
242 final TypeProvider typeProvider = mock(TypeProvider.class);
244 final Object[] args = { module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider };
245 final Map<Module, ModuleContext> result =
246 (Map<Module, ModuleContext>) generate.invoke(RpcActionGenHelper.class, args);
247 assertNotNull(result);
250 @SuppressWarnings({ "unchecked", "rawtypes" })
251 private <T extends ActionNodeContainer> void actionMethodsToGenType(final Class<T> clazz,
252 final boolean isRoutedRpc) throws Exception {
253 final Class[] parameterTypes =
254 { Module.class, Map.class, SchemaContext.class, boolean.class, Map.class, TypeProvider.class };
257 generate = RpcActionGenHelper.class.getDeclaredMethod("rpcMethodsToGenType", parameterTypes);
259 generate = RpcActionGenHelper.class.getDeclaredMethod("actionMethodsToGenType", parameterTypes);
261 assertNotNull(generate);
262 generate.setAccessible(true);
264 final QName actionQName = QName.create("test.action", "2017-05-04", "action-test");
266 final Module module = mock(Module.class);
267 when(module.getName()).thenReturn("module-name");
268 when(module.getRevision()).thenReturn(actionQName.getRevision());
269 when(module.getNamespace()).thenReturn(actionQName.getNamespace());
271 final Collection<DataSchemaNode> childNodes = new ArrayList<>();
272 final T actionNodeContainer = mock(clazz);
273 final QName actionParentQName = QName.create(actionQName, "action-parent");
274 final SchemaPath actionParentPath = SchemaPath.create(true, actionParentQName);
275 when(((SchemaNode) actionNodeContainer).getPath()).thenReturn(actionParentPath);
276 when(((SchemaNode) actionNodeContainer).getQName()).thenReturn(actionParentQName);
277 if (clazz == ListSchemaNode.class) {
278 final List<QName> keyQNames = new ArrayList<>();
279 keyQNames.add(QName.create(actionParentQName, "keyActions"));
280 when(((ListSchemaNode) actionNodeContainer).getKeyDefinition()).thenReturn(keyQNames);
283 final Set<ActionDefinition> actions = new HashSet<>();
285 final ActionDefinition actionDefinition = mock(ActionDefinition.class);
286 when(actionDefinition.getQName()).thenReturn(actionQName);
287 final SchemaPath actionPath = SchemaPath.create(true, actionQName);
288 when(actionDefinition.getPath()).thenReturn(actionPath);
290 final ContainerSchemaNode input = mock(ContainerSchemaNode.class);
291 final QName qnameInput = QName.create(actionQName, "action-input");
292 final SchemaPath inputSchemaPath = SchemaPath.create(true, actionQName, qnameInput);
293 when(input.getQName()).thenReturn(qnameInput);
294 when(input.getPath()).thenReturn(inputSchemaPath);
295 when(actionDefinition.getInput()).thenReturn(input);
297 final ContainerSchemaNode output = mock(ContainerSchemaNode.class);
298 final QName qnameOutput = QName.create(actionQName, "action-output");
299 final SchemaPath outputSchemaPath = SchemaPath.create(true, actionQName, qnameOutput);
300 when(output.getQName()).thenReturn(qnameOutput);
301 when(output.getPath()).thenReturn(outputSchemaPath);
302 when(actionDefinition.getOutput()).thenReturn(output);
304 actions.add(actionDefinition);
305 when(actionNodeContainer.getActions()).thenReturn(actions);
306 childNodes.add((DataSchemaNode) actionNodeContainer);
307 when(module.getChildNodes()).thenReturn(childNodes);
309 final Map<Module, ModuleContext> genCtx = new HashMap<>();
310 final ModuleContext moduleContext = new ModuleContext();
311 genCtx.put(module, moduleContext);
313 final SchemaContext schemaContext = mock(SchemaContext.class);
314 when(schemaContext.findModuleByNamespaceAndRevision(actionQName.getNamespace(), actionQName.getRevision()))
317 final boolean verboseClassComments = false;
318 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
319 final TypeProvider typeProvider = mock(TypeProvider.class);
322 final Set<RpcDefinition> rpcs = new HashSet<>();
323 final RpcDefinition rpcDef = mock(RpcDefinition.class);
324 when(rpcDef.getPath()).thenReturn(outputSchemaPath);
325 when(rpcDef.getQName()).thenReturn(qnameOutput);
326 when(module.getDataChildByName(actionQName)).thenReturn((ContainerSchemaNode) actionNodeContainer);
327 final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
328 final UnknownSchemaNode unknownSchemaNode = mock(UnknownSchemaNode.class);
329 final Field contextRef = RpcActionGenHelper.class.getDeclaredField("CONTEXT_REFERENCE");
330 contextRef.setAccessible(true);
331 final QName nodeType = (QName) contextRef.get(RpcActionGenHelper.class);
332 when(unknownSchemaNode.getNodeType()).thenReturn(nodeType);
333 when(unknownSchemaNode.getQName()).thenReturn(nodeType);
334 unknownSchemaNodes.add(unknownSchemaNode);
335 when(((DataSchemaNode) actionNodeContainer).getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
336 when(rpcDef.getInput()).thenReturn(input);
337 when(rpcDef.getOutput()).thenReturn(output);
339 when(module.getRpcs()).thenReturn(rpcs);
342 final Object[] args = { module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider };
343 final Map<Module, ModuleContext> result =
344 (Map<Module, ModuleContext>) generate.invoke(RpcActionGenHelper.class, args);
345 assertNotNull(result);