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