Change mapping of uint{8,16,32,64}
[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(4, simpleContainer.getMethodDefinitions().size());
64         assertEquals(3, 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("Uint8", 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(4, simpleContainer.getMethodDefinitions().size());
148         assertEquals(3, 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.isDefault()) {
159                 continue;
160             }
161             if (method.getName().equals("getFoo")) {
162                 getFooMethodCounter++;
163                 getFooMethodReturnTypeName = method.getReturnType().getName();
164             }
165
166             if (method.getName().equals("getBar")) {
167                 getBarMethodCounter++;
168                 getBarMethodReturnTypeName = method.getReturnType().getName();
169             }
170
171             if (method.getName().equals("getNestedContainer")) {
172                 getNestedContainerCounter++;
173                 getNestedContainerReturnTypeName = method.getReturnType().getName();
174             }
175         }
176
177         assertEquals(1, getFooMethodCounter);
178         assertEquals("List", getFooMethodReturnTypeName);
179
180         assertEquals(1, getBarMethodCounter);
181         assertEquals("String", getBarMethodReturnTypeName);
182
183         assertEquals(1, getNestedContainerCounter);
184         assertEquals("NestedContainer", getNestedContainerReturnTypeName);
185
186         getFooMethodCounter = 0;
187         getBarMethodCounter = 0;
188
189         getFooMethodReturnTypeName = "";
190         getBarMethodReturnTypeName = "";
191
192         for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
193             if (method.getName().equals("getFoo")) {
194                 getFooMethodCounter++;
195                 getFooMethodReturnTypeName = method.getReturnType().getName();
196             }
197
198             if (method.getName().equals("getBar")) {
199                 getBarMethodCounter++;
200                 getBarMethodReturnTypeName = method.getReturnType().getName();
201             }
202         }
203
204         assertEquals(1, getFooMethodCounter);
205         assertEquals("Uint8", getFooMethodReturnTypeName);
206
207         assertEquals(1, getBarMethodCounter);
208         assertEquals("List", getBarMethodReturnTypeName);
209     }
210
211     @Test
212     public void testListResolving() {
213         final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-list-demo.yang");
214         assertNotNull(context);
215
216         final BindingGenerator bindingGen = new BindingGeneratorImpl();
217         final List<Type> genTypes = bindingGen.generateTypes(context);
218
219         assertNotNull(genTypes);
220         assertEquals(5, genTypes.size());
221
222         int listParentContainerMethodsCount = 0;
223         int simpleListMethodsCount = 0;
224         int listChildContainerMethodsCount = 0;
225         int listKeyClassCount = 0;
226
227         int getSimpleListKeyMethodCount = 0;
228         int getListChildContainerMethodCount = 0;
229         int getFooMethodCount = 0;
230         int setFooMethodCount = 0;
231         int getSimpleLeafListMethodCount = 0;
232         int setSimpleLeafListMethodCount = 0;
233         int getBarMethodCount = 0;
234
235         String getSimpleListKeyMethodReturnTypeName = "";
236         String getListChildContainerMethodReturnTypeName = "";
237
238         int listKeyClassPropertyCount = 0;
239         String listKeyClassPropertyName = "";
240         String listKeyClassPropertyTypeName = "";
241         boolean listKeyClassPropertyReadOnly = false;
242
243         int hashMethodParameterCount = 0;
244         String hashMethodParameterName = "";
245         String hashMethodParameterReturnTypeName = "";
246
247         int equalMethodParameterCount = 0;
248         String equalMethodParameterName = "";
249         String equalMethodParameterReturnTypeName = "";
250
251         for (final Type type : genTypes) {
252             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
253                 final GeneratedType genType = (GeneratedType) type;
254                 if (genType.getName().equals("ListParentContainer")) {
255                     listParentContainerMethodsCount = genType.getMethodDefinitions().size();
256                 } else if (genType.getName().equals("SimpleList")) {
257                     simpleListMethodsCount = genType.getMethodDefinitions().size();
258                     final List<MethodSignature> methods = genType.getMethodDefinitions();
259                     for (final MethodSignature method : methods) {
260                         switch (method.getName()) {
261                             case BindingMapping.IDENTIFIABLE_KEY_NAME:
262                                 getSimpleListKeyMethodCount++;
263                                 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
264                                 break;
265                             case "getListChildContainer":
266                                 getListChildContainerMethodCount++;
267                                 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
268                                 break;
269                             case "getFoo":
270                                 getFooMethodCount++;
271                                 break;
272                             case "setFoo":
273                                 setFooMethodCount++;
274                                 break;
275                             case "getSimpleLeafList":
276                                 getSimpleLeafListMethodCount++;
277                                 break;
278                             case "setSimpleLeafList":
279                                 setSimpleLeafListMethodCount++;
280                                 break;
281                             case "getBar":
282                                 getBarMethodCount++;
283                                 break;
284                             default:
285                         }
286                     }
287                 } else if (genType.getName().equals("ListChildContainer")) {
288                     listChildContainerMethodsCount = genType.getMethodDefinitions().size();
289                 }
290             } else if (type instanceof GeneratedTransferObject) {
291                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
292                 final List<GeneratedProperty> properties = genTO.getProperties();
293                 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
294                 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
295
296                 listKeyClassCount++;
297                 listKeyClassPropertyCount = properties.size();
298                 listKeyClassPropertyName = properties.get(0).getName();
299                 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
300                 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
301
302                 hashMethodParameterCount = hashProps.size();
303                 hashMethodParameterName = hashProps.get(0).getName();
304                 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
305
306                 equalMethodParameterCount = equalProps.size();
307                 equalMethodParameterName = equalProps.get(0).getName();
308                 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
309
310             }
311         }
312
313         assertEquals(3, listParentContainerMethodsCount);
314         assertEquals(2, listChildContainerMethodsCount);
315         assertEquals(1, getSimpleListKeyMethodCount);
316         assertEquals(1, listKeyClassCount);
317
318         assertEquals(1, listKeyClassPropertyCount);
319         assertEquals("listKey", listKeyClassPropertyName);
320         assertEquals("Byte", listKeyClassPropertyTypeName);
321         assertTrue(listKeyClassPropertyReadOnly);
322         assertEquals(1, hashMethodParameterCount);
323         assertEquals("listKey", hashMethodParameterName);
324         assertEquals("Byte", hashMethodParameterReturnTypeName);
325         assertEquals(1, equalMethodParameterCount);
326         assertEquals("listKey", equalMethodParameterName);
327         assertEquals("Byte", equalMethodParameterReturnTypeName);
328
329         assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
330
331         assertEquals(1, getListChildContainerMethodCount);
332         assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
333         assertEquals(1, getFooMethodCount);
334         assertEquals(0, setFooMethodCount);
335         assertEquals(1, getSimpleLeafListMethodCount);
336         assertEquals(0, setSimpleLeafListMethodCount);
337         assertEquals(1, getBarMethodCount);
338
339         assertEquals(7, simpleListMethodsCount);
340     }
341
342     @Test
343     public void testListCompositeKeyResolving() {
344         final SchemaContext context = YangParserTestUtils.parseYangResource("/list-composite-key.yang");
345         assertNotNull(context);
346
347         final BindingGenerator bindingGen = new BindingGeneratorImpl();
348         final List<Type> genTypes = bindingGen.generateTypes(context);
349
350         assertNotNull(genTypes);
351         assertEquals(7, genTypes.size());
352
353         int genTypesCount = 0;
354         int genTOsCount = 0;
355
356         int compositeKeyListKeyPropertyCount = 0;
357         int compositeKeyListKeyCount = 0;
358         int innerListKeyPropertyCount = 0;
359
360         for (final Type type : genTypes) {
361             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
362                 genTypesCount++;
363             } else if (type instanceof GeneratedTransferObject) {
364                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
365
366                 if (genTO.getName().equals("CompositeKeyListKey")) {
367                     compositeKeyListKeyCount++;
368                     final List<GeneratedProperty> properties = genTO.getProperties();
369                     for (final GeneratedProperty prop : properties) {
370                         if (prop.getName().equals("key1")) {
371                             compositeKeyListKeyPropertyCount++;
372                         } else if (prop.getName().equals("key2")) {
373                             compositeKeyListKeyPropertyCount++;
374                         }
375                     }
376                     genTOsCount++;
377                 } else if (genTO.getName().equals("InnerListKey")) {
378                     final List<GeneratedProperty> properties = genTO.getProperties();
379                     innerListKeyPropertyCount = properties.size();
380                     genTOsCount++;
381                 }
382             }
383         }
384         assertEquals(1, compositeKeyListKeyCount);
385         assertEquals(2, compositeKeyListKeyPropertyCount);
386
387         assertEquals(1, innerListKeyPropertyCount);
388
389         assertEquals(5, genTypesCount);
390         assertEquals(2, genTOsCount);
391     }
392
393     @Test
394     public void testGeneratedTypes() {
395         final SchemaContext context = YangParserTestUtils.parseYangResource("/demo-topology.yang");
396         assertNotNull(context);
397
398         final BindingGenerator bindingGen = new BindingGeneratorImpl();
399         final List<Type> genTypes = bindingGen.generateTypes(context);
400
401         assertNotNull(genTypes);
402         assertEquals(14, genTypes.size());
403
404         int genTypesCount = 0;
405         int genTOsCount = 0;
406         for (final Type type : genTypes) {
407             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
408                 genTypesCount++;
409             } else if (type instanceof GeneratedTransferObject) {
410                 genTOsCount++;
411             }
412         }
413
414         assertEquals(11, genTypesCount);
415         assertEquals(3, genTOsCount);
416     }
417 }