Generate nonnull default wrappers for list getters
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedTypesTest.java
1 /*
2  * Copyright (c) 2016 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.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
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class GeneratedTypesTest {
27
28     @Test
29     public void testMultipleModulesResolving() {
30         final SchemaContext context = YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
31             "/abstract-topology.yang", "/ietf/ietf-inet-types.yang");
32         assertNotNull(context);
33
34         final BindingGenerator bindingGen = new BindingGeneratorImpl();
35         final List<Type> genTypes = bindingGen.generateTypes(context);
36
37         assertNotNull(genTypes);
38         assertEquals(29, genTypes.size());
39     }
40
41     @Test
42     public void testContainerResolving() {
43         final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-container-demo.yang");
44         assertNotNull(context);
45
46         final BindingGenerator bindingGen = new BindingGeneratorImpl();
47         final List<Type> genTypes = bindingGen.generateTypes(context);
48
49         assertNotNull(genTypes);
50         assertEquals(3, genTypes.size());
51
52         GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
53         GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
54         for (Type t : genTypes) {
55             if ("SimpleContainer".equals(t.getName())) {
56                 simpleContainer = (GeneratedType) t;
57             } else if ("NestedContainer".equals(t.getName())) {
58                 nestedContainer = (GeneratedType) t;
59             }
60         }
61         assertNotNull(simpleContainer);
62         assertNotNull(nestedContainer);
63         assertEquals(3, simpleContainer.getMethodDefinitions().size());
64         assertEquals(2, nestedContainer.getMethodDefinitions().size());
65
66         int getFooMethodCounter = 0;
67         int getBarMethodCounter = 0;
68         int getNestedContainerCounter = 0;
69
70         String getFooMethodReturnTypeName = "";
71         String getBarMethodReturnTypeName = "";
72         String getNestedContainerReturnTypeName = "";
73         for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
74             if (method.getName().equals("getFoo")) {
75                 getFooMethodCounter++;
76                 getFooMethodReturnTypeName = method.getReturnType().getName();
77             }
78
79             if (method.getName().equals("getBar")) {
80                 getBarMethodCounter++;
81                 getBarMethodReturnTypeName = method.getReturnType().getName();
82             }
83
84             if (method.getName().equals("getNestedContainer")) {
85                 getNestedContainerCounter++;
86                 getNestedContainerReturnTypeName = method.getReturnType().getName();
87             }
88         }
89
90         assertEquals(1, getFooMethodCounter);
91         assertEquals("Integer", getFooMethodReturnTypeName);
92
93         assertEquals(1, getBarMethodCounter);
94         assertEquals("String", getBarMethodReturnTypeName);
95
96         assertEquals(1, getNestedContainerCounter);
97         assertEquals("NestedContainer", getNestedContainerReturnTypeName);
98
99         getFooMethodCounter = 0;
100         getBarMethodCounter = 0;
101
102         getFooMethodReturnTypeName = "";
103         getBarMethodReturnTypeName = "";
104
105         for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
106
107             if (method.getName().equals("getFoo")) {
108                 getFooMethodCounter++;
109                 getFooMethodReturnTypeName = method.getReturnType().getName();
110             }
111
112             if (method.getName().equals("getBar")) {
113                 getBarMethodCounter++;
114                 getBarMethodReturnTypeName = method.getReturnType().getName();
115             }
116         }
117
118         assertEquals(1, getFooMethodCounter);
119         assertEquals("Short", getFooMethodReturnTypeName);
120
121         assertEquals(1, getBarMethodCounter);
122         assertEquals("String", getBarMethodReturnTypeName);
123     }
124
125     @Test
126     public void testLeafListResolving() {
127         final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-leaf-list-demo.yang");
128         assertNotNull(context);
129
130         final BindingGenerator bindingGen = new BindingGeneratorImpl();
131         final List<Type> genTypes = bindingGen.generateTypes(context);
132
133         assertNotNull(genTypes);
134         assertEquals(3, genTypes.size());
135
136         GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
137         GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
138         for (Type t : genTypes) {
139             if ("SimpleContainer".equals(t.getName())) {
140                 simpleContainer = (GeneratedType) t;
141             } else if ("NestedContainer".equals(t.getName())) {
142                 nestedContainer = (GeneratedType) t;
143             }
144         }
145         assertNotNull(simpleContainer);
146         assertNotNull(nestedContainer);
147         assertEquals(3, simpleContainer.getMethodDefinitions().size());
148         assertEquals(2, nestedContainer.getMethodDefinitions().size());
149
150         int getFooMethodCounter = 0;
151         int getBarMethodCounter = 0;
152         int getNestedContainerCounter = 0;
153
154         String getFooMethodReturnTypeName = "";
155         String getBarMethodReturnTypeName = "";
156         String getNestedContainerReturnTypeName = "";
157         for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
158             if (method.getName().equals("getFoo")) {
159                 getFooMethodCounter++;
160                 getFooMethodReturnTypeName = method.getReturnType().getName();
161             }
162
163             if (method.getName().equals("getBar")) {
164                 getBarMethodCounter++;
165                 getBarMethodReturnTypeName = method.getReturnType().getName();
166             }
167
168             if (method.getName().equals("getNestedContainer")) {
169                 getNestedContainerCounter++;
170                 getNestedContainerReturnTypeName = method.getReturnType().getName();
171             }
172         }
173
174         assertEquals(1, getFooMethodCounter);
175         assertEquals("List", getFooMethodReturnTypeName);
176
177         assertEquals(1, getBarMethodCounter);
178         assertEquals("String", getBarMethodReturnTypeName);
179
180         assertEquals(1, getNestedContainerCounter);
181         assertEquals("NestedContainer", getNestedContainerReturnTypeName);
182
183         getFooMethodCounter = 0;
184         getBarMethodCounter = 0;
185
186         getFooMethodReturnTypeName = "";
187         getBarMethodReturnTypeName = "";
188
189         for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
190             if (method.getName().equals("getFoo")) {
191                 getFooMethodCounter++;
192                 getFooMethodReturnTypeName = method.getReturnType().getName();
193             }
194
195             if (method.getName().equals("getBar")) {
196                 getBarMethodCounter++;
197                 getBarMethodReturnTypeName = method.getReturnType().getName();
198             }
199         }
200
201         assertEquals(1, getFooMethodCounter);
202         assertEquals("Short", getFooMethodReturnTypeName);
203
204         assertEquals(1, getBarMethodCounter);
205         assertEquals("List", getBarMethodReturnTypeName);
206     }
207
208     @Test
209     public void testListResolving() {
210         final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-list-demo.yang");
211         assertNotNull(context);
212
213         final BindingGenerator bindingGen = new BindingGeneratorImpl();
214         final List<Type> genTypes = bindingGen.generateTypes(context);
215
216         assertNotNull(genTypes);
217         assertEquals(5, genTypes.size());
218
219         int listParentContainerMethodsCount = 0;
220         int simpleListMethodsCount = 0;
221         int listChildContainerMethodsCount = 0;
222         int listKeyClassCount = 0;
223
224         int getSimpleListKeyMethodCount = 0;
225         int getListChildContainerMethodCount = 0;
226         int getFooMethodCount = 0;
227         int setFooMethodCount = 0;
228         int getSimpleLeafListMethodCount = 0;
229         int setSimpleLeafListMethodCount = 0;
230         int getBarMethodCount = 0;
231
232         String getSimpleListKeyMethodReturnTypeName = "";
233         String getListChildContainerMethodReturnTypeName = "";
234
235         int listKeyClassPropertyCount = 0;
236         String listKeyClassPropertyName = "";
237         String listKeyClassPropertyTypeName = "";
238         boolean listKeyClassPropertyReadOnly = false;
239
240         int hashMethodParameterCount = 0;
241         String hashMethodParameterName = "";
242         String hashMethodParameterReturnTypeName = "";
243
244         int equalMethodParameterCount = 0;
245         String equalMethodParameterName = "";
246         String equalMethodParameterReturnTypeName = "";
247
248         for (final Type type : genTypes) {
249             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
250                 final GeneratedType genType = (GeneratedType) type;
251                 if (genType.getName().equals("ListParentContainer")) {
252                     listParentContainerMethodsCount = genType.getMethodDefinitions().size();
253                 } else if (genType.getName().equals("SimpleList")) {
254                     simpleListMethodsCount = genType.getMethodDefinitions().size();
255                     final List<MethodSignature> methods = genType.getMethodDefinitions();
256                     for (final MethodSignature method : methods) {
257                         switch (method.getName()) {
258                             case BindingMapping.IDENTIFIABLE_KEY_NAME:
259                                 getSimpleListKeyMethodCount++;
260                                 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
261                                 break;
262                             case "getListChildContainer":
263                                 getListChildContainerMethodCount++;
264                                 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
265                                 break;
266                             case "getFoo":
267                                 getFooMethodCount++;
268                                 break;
269                             case "setFoo":
270                                 setFooMethodCount++;
271                                 break;
272                             case "getSimpleLeafList":
273                                 getSimpleLeafListMethodCount++;
274                                 break;
275                             case "setSimpleLeafList":
276                                 setSimpleLeafListMethodCount++;
277                                 break;
278                             case "getBar":
279                                 getBarMethodCount++;
280                                 break;
281                             default:
282                         }
283                     }
284                 } else if (genType.getName().equals("ListChildContainer")) {
285                     listChildContainerMethodsCount = genType.getMethodDefinitions().size();
286                 }
287             } else if (type instanceof GeneratedTransferObject) {
288                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
289                 final List<GeneratedProperty> properties = genTO.getProperties();
290                 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
291                 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
292
293                 listKeyClassCount++;
294                 listKeyClassPropertyCount = properties.size();
295                 listKeyClassPropertyName = properties.get(0).getName();
296                 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
297                 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
298
299                 hashMethodParameterCount = hashProps.size();
300                 hashMethodParameterName = hashProps.get(0).getName();
301                 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
302
303                 equalMethodParameterCount = equalProps.size();
304                 equalMethodParameterName = equalProps.get(0).getName();
305                 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
306
307             }
308         }
309
310         assertEquals(2, listParentContainerMethodsCount);
311         assertEquals(1, listChildContainerMethodsCount);
312         assertEquals(1, getSimpleListKeyMethodCount);
313         assertEquals(1, listKeyClassCount);
314
315         assertEquals(1, listKeyClassPropertyCount);
316         assertEquals("listKey", listKeyClassPropertyName);
317         assertEquals("Byte", listKeyClassPropertyTypeName);
318         assertTrue(listKeyClassPropertyReadOnly);
319         assertEquals(1, hashMethodParameterCount);
320         assertEquals("listKey", hashMethodParameterName);
321         assertEquals("Byte", hashMethodParameterReturnTypeName);
322         assertEquals(1, equalMethodParameterCount);
323         assertEquals("listKey", equalMethodParameterName);
324         assertEquals("Byte", equalMethodParameterReturnTypeName);
325
326         assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
327
328         assertEquals(1, getListChildContainerMethodCount);
329         assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
330         assertEquals(1, getFooMethodCount);
331         assertEquals(0, setFooMethodCount);
332         assertEquals(1, getSimpleLeafListMethodCount);
333         assertEquals(0, setSimpleLeafListMethodCount);
334         assertEquals(1, getBarMethodCount);
335
336         assertEquals(6, simpleListMethodsCount);
337     }
338
339     @Test
340     public void testListCompositeKeyResolving() {
341         final SchemaContext context = YangParserTestUtils.parseYangResource("/list-composite-key.yang");
342         assertNotNull(context);
343
344         final BindingGenerator bindingGen = new BindingGeneratorImpl();
345         final List<Type> genTypes = bindingGen.generateTypes(context);
346
347         assertNotNull(genTypes);
348         assertEquals(7, genTypes.size());
349
350         int genTypesCount = 0;
351         int genTOsCount = 0;
352
353         int compositeKeyListKeyPropertyCount = 0;
354         int compositeKeyListKeyCount = 0;
355         int innerListKeyPropertyCount = 0;
356
357         for (final Type type : genTypes) {
358             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
359                 genTypesCount++;
360             } else if (type instanceof GeneratedTransferObject) {
361                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
362
363                 if (genTO.getName().equals("CompositeKeyListKey")) {
364                     compositeKeyListKeyCount++;
365                     final List<GeneratedProperty> properties = genTO.getProperties();
366                     for (final GeneratedProperty prop : properties) {
367                         if (prop.getName().equals("key1")) {
368                             compositeKeyListKeyPropertyCount++;
369                         } else if (prop.getName().equals("key2")) {
370                             compositeKeyListKeyPropertyCount++;
371                         }
372                     }
373                     genTOsCount++;
374                 } else if (genTO.getName().equals("InnerListKey")) {
375                     final List<GeneratedProperty> properties = genTO.getProperties();
376                     innerListKeyPropertyCount = properties.size();
377                     genTOsCount++;
378                 }
379             }
380         }
381         assertEquals(1, compositeKeyListKeyCount);
382         assertEquals(2, compositeKeyListKeyPropertyCount);
383
384         assertEquals(1, innerListKeyPropertyCount);
385
386         assertEquals(5, genTypesCount);
387         assertEquals(2, genTOsCount);
388     }
389
390     @Test
391     public void testGeneratedTypes() {
392         final SchemaContext context = YangParserTestUtils.parseYangResource("/demo-topology.yang");
393         assertNotNull(context);
394
395         final BindingGenerator bindingGen = new BindingGeneratorImpl();
396         final List<Type> genTypes = bindingGen.generateTypes(context);
397
398         assertNotNull(genTypes);
399         assertEquals(14, genTypes.size());
400
401         int genTypesCount = 0;
402         int genTOsCount = 0;
403         for (final Type type : genTypes) {
404             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
405                 genTypesCount++;
406             } else if (type instanceof GeneratedTransferObject) {
407                 genTOsCount++;
408             }
409         }
410
411         assertEquals(11, genTypesCount);
412         assertEquals(3, genTOsCount);
413     }
414 }