Binding generator v2 - Action, ListAction fix
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / test / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / RpcActionGenHelperTest.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.javav2.generator.impl;
9
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;
16
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;
27 import java.util.Map;
28 import java.util.Set;
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;
45
46 public class RpcActionGenHelperTest {
47
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 = {};
55         try {
56             constructor.newInstance(objs);
57         } catch (final Exception e) {
58             throw e.getCause();
59         }
60     }
61
62     @SuppressWarnings({ "rawtypes", "unchecked" })
63     @Test
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);
72
73         final Object[] args = { dataSchemaNode };
74         final Optional<QName> result = (Optional<QName>) generate.invoke(RpcActionGenHelper.class, args);
75         assertNotNull(result);
76         assertTrue(!result.isPresent());
77     }
78
79     @SuppressWarnings({ "rawtypes", "unchecked" })
80     @Test
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);
86
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);
98
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());
104     }
105
106     @Ignore
107     @Test
108     public void actionMethodsToGenTypeContainerAsParentTest() throws Exception {
109         actionMethodsToGenType(ContainerSchemaNode.class, false);
110     }
111
112     @Ignore
113     @Test
114     public void actionMethodsToGenTypeListAsParentTest() throws Exception {
115         actionMethodsToGenType(ListSchemaNode.class, false);
116     }
117
118     @SuppressWarnings("rawtypes")
119     @Test
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);
126
127         final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
128
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);
134
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);
140
141         final Object[] args = { module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider };
142
143         try {
144             generate.invoke(RpcActionGenHelper.class, args);
145             fail();
146         } catch (final Exception e) {
147             assertNotNull(e);
148             assertTrue(e instanceof InvocationTargetException);
149             final Throwable cause = e.getCause();
150             assertNotNull(cause);
151             assertTrue(cause instanceof IllegalStateException);
152         }
153     }
154
155     @SuppressWarnings({ "rawtypes", "unchecked" })
156     @Test
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);
163
164         final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
165
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);
172
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);
178
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);
183     }
184
185     @Test
186     public void rpcMethodsToGenTypeRoutedRpcTest() throws Exception {
187         actionMethodsToGenType(ContainerSchemaNode.class, true);
188     }
189
190     @SuppressWarnings({ "rawtypes", "unchecked" })
191     @Test
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);
198
199         final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
200
201         final ContainerSchemaNode rpcParent = mock(ContainerSchemaNode.class);
202         final QName rpcParentQName = QName.create(rpcQName, "rpc-parent");
203         when(rpcParent.getQName()).thenReturn(rpcParentQName);
204
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);
217
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);
224
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);
231
232         final Map<Module, ModuleContext> genCtx = new HashMap<>();
233         final ModuleContext moduleContext = new ModuleContext();
234         genCtx.put(module, moduleContext);
235
236         final SchemaContext schemaContext = mock(SchemaContext.class);
237         when(schemaContext.findModuleByNamespaceAndRevision(rpcQName.getNamespace(), rpcQName.getRevision()))
238                 .thenReturn(module);
239
240         final boolean verboseClassComments = false;
241         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
242         final TypeProvider typeProvider = mock(TypeProvider.class);
243
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);
248     }
249
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 };
255         Method generate;
256         if (isRoutedRpc) {
257             generate = RpcActionGenHelper.class.getDeclaredMethod("rpcMethodsToGenType", parameterTypes);
258         } else {
259             generate = RpcActionGenHelper.class.getDeclaredMethod("actionMethodsToGenType", parameterTypes);
260         }
261         assertNotNull(generate);
262         generate.setAccessible(true);
263
264         final QName actionQName = QName.create("test.action", "2017-05-04", "action-test");
265
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());
270
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);
281         }
282
283         final Set<ActionDefinition> actions = new HashSet<>();
284
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);
289
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);
296
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);
303
304         actions.add(actionDefinition);
305         when(actionNodeContainer.getActions()).thenReturn(actions);
306         childNodes.add((DataSchemaNode) actionNodeContainer);
307         when(module.getChildNodes()).thenReturn(childNodes);
308
309         final Map<Module, ModuleContext> genCtx = new HashMap<>();
310         final ModuleContext moduleContext = new ModuleContext();
311         genCtx.put(module, moduleContext);
312
313         final SchemaContext schemaContext = mock(SchemaContext.class);
314         when(schemaContext.findModuleByNamespaceAndRevision(actionQName.getNamespace(), actionQName.getRevision()))
315                 .thenReturn(module);
316
317         final boolean verboseClassComments = false;
318         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
319         final TypeProvider typeProvider = mock(TypeProvider.class);
320
321         if (isRoutedRpc) {
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);
338             rpcs.add(rpcDef);
339             when(module.getRpcs()).thenReturn(rpcs);
340         }
341
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);
346     }
347 }