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