Shorten java.util.Optional reference
[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.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import java.lang.reflect.Constructor;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
28 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
32 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
42
43 public class RpcActionGenHelperTest {
44     // Bridge for method references
45     @FunctionalInterface
46     private interface GeneratorMethod {
47         Map<Module, ModuleContext> generate(Module module, Map<Module, ModuleContext> genCtx,
48                 SchemaContext schemaContext, boolean verboseClassComments,
49                 Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, TypeProvider typeProvider);
50     }
51
52     @SuppressWarnings({"checkstyle:avoidHidingCauseException", "checkstyle:illegalThrows", "checkstyle:illegalCatch"})
53     @Test(expected = UnsupportedOperationException.class)
54     public void constructorTest() throws Throwable {
55         final Constructor<RpcActionGenHelper> constructor = RpcActionGenHelper.class.getDeclaredConstructor();
56         constructor.setAccessible(true);
57         try {
58             constructor.newInstance();
59         } catch (final Exception e) {
60             throw e.getCause();
61         }
62     }
63
64     @Test
65     public void getRoutingContextAbsentTest() {
66         final DataSchemaNode dataSchemaNode = mock(DataSchemaNode.class);
67         final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
68         when(dataSchemaNode.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
69
70         final Optional<QName> result = RpcActionGenHelper.getRoutingContext(dataSchemaNode);
71         assertNotNull(result);
72         assertTrue(!result.isPresent());
73     }
74
75     @Test
76     public void getRoutingContextTest() {
77         final DataSchemaNode dataSchemaNode = mock(DataSchemaNode.class);
78         final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
79         final UnknownSchemaNode unknownSchemaNode = mock(UnknownSchemaNode.class);
80         when(unknownSchemaNode.getNodeType()).thenReturn(RpcActionGenHelper.CONTEXT_REFERENCE);
81         final QName qname = QName.create("test", "2017-05-04", "unknown");
82         when(unknownSchemaNode.getQName()).thenReturn(qname);
83         unknownSchemaNodes.add(unknownSchemaNode);
84         when(dataSchemaNode.getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
85
86         final Optional<QName> result = RpcActionGenHelper.getRoutingContext(dataSchemaNode);
87         assertNotNull(result);
88         assertTrue(result.isPresent());
89         assertEquals(qname, result.get());
90     }
91
92     @Ignore
93     @Test
94     public void actionMethodsToGenTypeContainerAsParentTest() {
95         actionMethodsToGenType(ContainerSchemaNode.class, false);
96     }
97
98     @Ignore
99     @Test
100     public void actionMethodsToGenTypeListAsParentTest() {
101         actionMethodsToGenType(ListSchemaNode.class, false);
102     }
103
104     @Test(expected = IllegalStateException.class)
105     public void rpcMethodsToGenTypeNullRpcsTest() {
106         final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
107
108         final Module module = mock(Module.class);
109         when(module.getName()).thenReturn("module-name");
110         when(module.getRevision()).thenReturn(rpcQName.getRevision());
111         when(module.getNamespace()).thenReturn(rpcQName.getNamespace());
112         when(module.getRpcs()).thenReturn(null);
113
114         final Map<Module, ModuleContext> genCtx = new HashMap<>();
115         final SchemaContext schemaContext = mock(SchemaContext.class);
116         final boolean verboseClassComments = false;
117         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
118         final TypeProvider typeProvider = mock(TypeProvider.class);
119
120         RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments, genTypeBuilders,
121             typeProvider);
122     }
123
124     @Test
125     public void rpcMethodsToGenTypeEmptyRpcsTest() {
126         final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
127
128         final Module module = mock(Module.class);
129         when(module.getName()).thenReturn("module-name");
130         when(module.getRevision()).thenReturn(rpcQName.getRevision());
131         when(module.getNamespace()).thenReturn(rpcQName.getNamespace());
132         final Set<RpcDefinition> rpcs = new HashSet<>();
133         when(module.getRpcs()).thenReturn(rpcs);
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 Map<Module, ModuleContext> result = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext,
142             verboseClassComments, genTypeBuilders, typeProvider);
143         assertNotNull(result);
144     }
145
146     @Test
147     public void rpcMethodsToGenTypeRoutedRpcTest() {
148         actionMethodsToGenType(ContainerSchemaNode.class, true);
149     }
150
151     @Test
152     public void rpcMethodsToGenTypeRpcTest() {
153         final QName rpcQName = QName.create("test.rpc", "2017-05-04", "rpc-test");
154
155         final ContainerSchemaNode rpcParent = mock(ContainerSchemaNode.class);
156         final QName rpcParentQName = QName.create(rpcQName, "rpc-parent");
157         when(rpcParent.getQName()).thenReturn(rpcParentQName);
158
159         final Module module = mock(Module.class);
160         when(module.getName()).thenReturn("module-name");
161         when(module.getRevision()).thenReturn(rpcQName.getRevision());
162         when(module.getNamespace()).thenReturn(rpcQName.getNamespace());
163         when(module.getDescription()).thenReturn(Optional.empty());
164         when(module.getReference()).thenReturn(Optional.empty());
165         final Set<RpcDefinition> rpcs = new HashSet<>();
166         final RpcDefinition rpcDefinition = mock(RpcDefinition.class);
167         when(rpcDefinition.getDescription()).thenReturn(Optional.empty());
168         when(rpcDefinition.getReference()).thenReturn(Optional.empty());
169         final SchemaPath rpcPath = SchemaPath.create(true, rpcParentQName, rpcQName);
170         when(rpcDefinition.getPath()).thenReturn(rpcPath);
171         when(rpcDefinition.getQName()).thenReturn(rpcQName);
172
173         when(module.getDataChildByName(rpcParentQName)).thenReturn(rpcParent);
174         rpcs.add(rpcDefinition);
175         when(module.getRpcs()).thenReturn(rpcs);
176
177         final ContainerSchemaNode input = mock(ContainerSchemaNode.class);
178         final QName qnameInput = QName.create(rpcQName, "rpc-input");
179         final SchemaPath inputSchemaPath = SchemaPath.create(true, rpcQName, qnameInput);
180         when(input.getQName()).thenReturn(qnameInput);
181         when(input.getPath()).thenReturn(inputSchemaPath);
182         when(input.getDescription()).thenReturn(Optional.empty());
183         when(input.getReference()).thenReturn(Optional.empty());
184
185         when(rpcDefinition.getInput()).thenReturn(input);
186
187         final ContainerSchemaNode output = mock(ContainerSchemaNode.class);
188         final QName qnameOutput = QName.create(rpcQName, "rpc-output");
189         final SchemaPath outputSchemaPath = SchemaPath.create(true, rpcQName, qnameOutput);
190         when(output.getQName()).thenReturn(qnameOutput);
191         when(output.getPath()).thenReturn(outputSchemaPath);
192         when(output.getDescription()).thenReturn(Optional.empty());
193         when(output.getReference()).thenReturn(Optional.empty());
194         when(rpcDefinition.getOutput()).thenReturn(output);
195
196         final Map<Module, ModuleContext> genCtx = new HashMap<>();
197         final ModuleContext moduleContext = new ModuleContext();
198         genCtx.put(module, moduleContext);
199
200         final SchemaContext schemaContext = mock(SchemaContext.class);
201         when(schemaContext.findModule(rpcQName.getModule())).thenReturn(Optional.of(module));
202
203         final boolean verboseClassComments = false;
204         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
205         final TypeProvider typeProvider = mock(TypeProvider.class);
206
207         final Map<Module, ModuleContext> result = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext,
208             verboseClassComments, genTypeBuilders, typeProvider);
209         assertNotNull(result);
210     }
211
212     private static <T extends ActionNodeContainer> void actionMethodsToGenType(final Class<T> clazz,
213             final boolean isRoutedRpc) {
214         final GeneratorMethod generate;
215         if (isRoutedRpc) {
216             generate = RpcActionGenHelper::rpcMethodsToGenType;
217         } else {
218             generate = RpcActionGenHelper::actionMethodsToGenType;
219         }
220
221         final QName actionQName = QName.create("test.action", "2017-05-04", "action-test");
222
223         final Module module = mock(Module.class);
224         when(module.getName()).thenReturn("module-name");
225         when(module.getRevision()).thenReturn(actionQName.getRevision());
226         when(module.getNamespace()).thenReturn(actionQName.getNamespace());
227         when(module.getDescription()).thenReturn(Optional.empty());
228         when(module.getReference()).thenReturn(Optional.empty());
229
230         final Collection<DataSchemaNode> childNodes = new ArrayList<>();
231         final T actionNodeContainer = mock(clazz);
232         final QName actionParentQName = QName.create(actionQName, "action-parent");
233         final SchemaPath actionParentPath = SchemaPath.create(true, actionParentQName);
234         when(((SchemaNode) actionNodeContainer).getPath()).thenReturn(actionParentPath);
235         when(((SchemaNode) actionNodeContainer).getQName()).thenReturn(actionParentQName);
236         if (clazz == ListSchemaNode.class) {
237             final List<QName> keyQNames = new ArrayList<>();
238             keyQNames.add(QName.create(actionParentQName, "keyActions"));
239             when(((ListSchemaNode) actionNodeContainer).getKeyDefinition()).thenReturn(keyQNames);
240         }
241
242         final Set<ActionDefinition> actions = new HashSet<>();
243
244         final ActionDefinition actionDefinition = mock(ActionDefinition.class);
245         when(actionDefinition.getQName()).thenReturn(actionQName);
246         final SchemaPath actionPath = SchemaPath.create(true, actionQName);
247         when(actionDefinition.getPath()).thenReturn(actionPath);
248         when(actionDefinition.getDescription()).thenReturn(Optional.empty());
249         when(actionDefinition.getReference()).thenReturn(Optional.empty());
250
251         final ContainerSchemaNode input = mock(ContainerSchemaNode.class);
252         final QName qnameInput = QName.create(actionQName, "action-input");
253         final SchemaPath inputSchemaPath = SchemaPath.create(true, actionQName, qnameInput);
254         when(input.getQName()).thenReturn(qnameInput);
255         when(input.getPath()).thenReturn(inputSchemaPath);
256         when(input.getDescription()).thenReturn(Optional.empty());
257         when(input.getReference()).thenReturn(Optional.empty());
258         when(actionDefinition.getInput()).thenReturn(input);
259
260         final ContainerSchemaNode output = mock(ContainerSchemaNode.class);
261         final QName qnameOutput = QName.create(actionQName, "action-output");
262         final SchemaPath outputSchemaPath = SchemaPath.create(true, actionQName, qnameOutput);
263         when(output.getQName()).thenReturn(qnameOutput);
264         when(output.getPath()).thenReturn(outputSchemaPath);
265         when(output.getDescription()).thenReturn(Optional.empty());
266         when(output.getReference()).thenReturn(Optional.empty());
267         when(actionDefinition.getOutput()).thenReturn(output);
268
269         actions.add(actionDefinition);
270         when(actionNodeContainer.getActions()).thenReturn(actions);
271         childNodes.add((DataSchemaNode) actionNodeContainer);
272         when(module.getChildNodes()).thenReturn(childNodes);
273
274         final Map<Module, ModuleContext> genCtx = new HashMap<>();
275         final ModuleContext moduleContext = new ModuleContext();
276         genCtx.put(module, moduleContext);
277
278         final SchemaContext schemaContext = mock(SchemaContext.class);
279         when(schemaContext.findModule(actionQName.getModule())).thenReturn(Optional.of(module));
280
281         final boolean verboseClassComments = false;
282         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
283         final TypeProvider typeProvider = mock(TypeProvider.class);
284
285         if (isRoutedRpc) {
286             final Set<RpcDefinition> rpcs = new HashSet<>();
287             final RpcDefinition rpcDef = mock(RpcDefinition.class);
288             when(rpcDef.getPath()).thenReturn(outputSchemaPath);
289             when(rpcDef.getQName()).thenReturn(qnameOutput);
290             when(rpcDef.getDescription()).thenReturn(Optional.empty());
291             when(rpcDef.getReference()).thenReturn(Optional.empty());
292
293             when(module.getDataChildByName(actionQName)).thenReturn((ContainerSchemaNode) actionNodeContainer);
294             final List<UnknownSchemaNode> unknownSchemaNodes = new ArrayList<>();
295             final UnknownSchemaNode unknownSchemaNode = mock(UnknownSchemaNode.class);
296             when(unknownSchemaNode.getNodeType()).thenReturn(RpcActionGenHelper.CONTEXT_REFERENCE);
297             when(unknownSchemaNode.getQName()).thenReturn(RpcActionGenHelper.CONTEXT_REFERENCE);
298             unknownSchemaNodes.add(unknownSchemaNode);
299             when(((DataSchemaNode) actionNodeContainer).getUnknownSchemaNodes()).thenReturn(unknownSchemaNodes);
300             when(rpcDef.getInput()).thenReturn(input);
301             when(rpcDef.getOutput()).thenReturn(output);
302             rpcs.add(rpcDef);
303             when(module.getRpcs()).thenReturn(rpcs);
304         }
305
306         final Map<Module, ModuleContext> result = generate.generate(module, genCtx, schemaContext, verboseClassComments,
307             genTypeBuilders, typeProvider);
308         assertNotNull(result);
309     }
310 }