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