Generate bindingToString() and use it in generated implementations
[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.test.util.YangParserTestUtils;
23
24 public class GeneratedTypesTest {
25     @Test
26     public void testMultipleModulesResolving() {
27         final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(
28             GeneratedTypesTest.class, "/abstract-topology.yang", "/ietf/ietf-inet-types.yang"));
29
30         assertNotNull(genTypes);
31         assertEquals(29, genTypes.size());
32     }
33
34     @Test
35     public void testContainerResolving() {
36         final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
37             "/simple-container-demo.yang"));
38
39         assertNotNull(genTypes);
40         assertEquals(3, genTypes.size());
41
42         GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
43         GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
44         for (Type t : genTypes) {
45             if ("SimpleContainer".equals(t.getName())) {
46                 simpleContainer = (GeneratedType) t;
47             } else if ("NestedContainer".equals(t.getName())) {
48                 nestedContainer = (GeneratedType) t;
49             }
50         }
51         assertNotNull(simpleContainer);
52         assertNotNull(nestedContainer);
53         // FIXME: split this into getter/default/static asserts
54         assertEquals(5, simpleContainer.getMethodDefinitions().size());
55         // FIXME: split this into getter/default/static asserts
56         assertEquals(4, 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<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
120             "/simple-leaf-list-demo.yang"));
121
122         assertNotNull(genTypes);
123         assertEquals(3, genTypes.size());
124
125         GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
126         GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
127         for (Type t : genTypes) {
128             if ("SimpleContainer".equals(t.getName())) {
129                 simpleContainer = (GeneratedType) t;
130             } else if ("NestedContainer".equals(t.getName())) {
131                 nestedContainer = (GeneratedType) t;
132             }
133         }
134         assertNotNull(simpleContainer);
135         assertNotNull(nestedContainer);
136         // FIXME: split this into getter/default/static asserts
137         assertEquals(5, simpleContainer.getMethodDefinitions().size());
138         // FIXME: split this into getter/default/static asserts
139         assertEquals(4, 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("List", 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("List", getBarMethodReturnTypeName);
200     }
201
202     @Test
203     public void testListResolving() {
204         final List<Type> 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         int listKeyClassPropertyCount = 0;
227         String listKeyClassPropertyName = "";
228         String listKeyClassPropertyTypeName = "";
229         boolean listKeyClassPropertyReadOnly = false;
230
231         int hashMethodParameterCount = 0;
232         String hashMethodParameterName = "";
233         String hashMethodParameterReturnTypeName = "";
234
235         int equalMethodParameterCount = 0;
236         String equalMethodParameterName = "";
237         String equalMethodParameterReturnTypeName = "";
238
239         for (final Type type : genTypes) {
240             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
241                 final GeneratedType genType = (GeneratedType) type;
242                 if (genType.getName().equals("ListParentContainer")) {
243                     listParentContainerMethodsCount = genType.getMethodDefinitions().size();
244                 } else if (genType.getName().equals("SimpleList")) {
245                     simpleListMethodsCount = genType.getMethodDefinitions().size();
246                     final List<MethodSignature> methods = genType.getMethodDefinitions();
247                     for (final MethodSignature method : methods) {
248                         switch (method.getName()) {
249                             case BindingMapping.IDENTIFIABLE_KEY_NAME:
250                                 getSimpleListKeyMethodCount++;
251                                 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
252                                 break;
253                             case "getListChildContainer":
254                                 getListChildContainerMethodCount++;
255                                 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
256                                 break;
257                             case "getFoo":
258                                 getFooMethodCount++;
259                                 break;
260                             case "setFoo":
261                                 setFooMethodCount++;
262                                 break;
263                             case "getSimpleLeafList":
264                                 getSimpleLeafListMethodCount++;
265                                 break;
266                             case "setSimpleLeafList":
267                                 setSimpleLeafListMethodCount++;
268                                 break;
269                             case "getBar":
270                                 getBarMethodCount++;
271                                 break;
272                             default:
273                         }
274                     }
275                 } else if (genType.getName().equals("ListChildContainer")) {
276                     listChildContainerMethodsCount = genType.getMethodDefinitions().size();
277                 }
278             } else if (type instanceof GeneratedTransferObject) {
279                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
280                 final List<GeneratedProperty> properties = genTO.getProperties();
281                 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
282                 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
283
284                 listKeyClassCount++;
285                 listKeyClassPropertyCount = properties.size();
286                 listKeyClassPropertyName = properties.get(0).getName();
287                 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
288                 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
289
290                 hashMethodParameterCount = hashProps.size();
291                 hashMethodParameterName = hashProps.get(0).getName();
292                 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
293
294                 equalMethodParameterCount = equalProps.size();
295                 equalMethodParameterName = equalProps.get(0).getName();
296                 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
297
298             }
299         }
300
301         // FIXME: split this into getter/default/static asserts
302         assertEquals(4, listParentContainerMethodsCount);
303         // FIXME: split this into getter/default/static asserts
304         assertEquals(3, listChildContainerMethodsCount);
305         assertEquals(1, getSimpleListKeyMethodCount);
306         assertEquals(1, listKeyClassCount);
307
308         assertEquals(1, listKeyClassPropertyCount);
309         assertEquals("listKey", listKeyClassPropertyName);
310         assertEquals("Byte", listKeyClassPropertyTypeName);
311         assertTrue(listKeyClassPropertyReadOnly);
312         assertEquals(1, hashMethodParameterCount);
313         assertEquals("listKey", hashMethodParameterName);
314         assertEquals("Byte", hashMethodParameterReturnTypeName);
315         assertEquals(1, equalMethodParameterCount);
316         assertEquals("listKey", equalMethodParameterName);
317         assertEquals("Byte", equalMethodParameterReturnTypeName);
318
319         assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
320
321         assertEquals(1, getListChildContainerMethodCount);
322         assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
323         assertEquals(1, getFooMethodCount);
324         assertEquals(0, setFooMethodCount);
325         assertEquals(1, getSimpleLeafListMethodCount);
326         assertEquals(0, setSimpleLeafListMethodCount);
327         assertEquals(1, getBarMethodCount);
328
329         // FIXME: split this into getter/default/static asserts
330         assertEquals(8, simpleListMethodsCount);
331     }
332
333     @Test
334     public void testListCompositeKeyResolving() {
335         final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
336             "/list-composite-key.yang"));
337
338         assertNotNull(genTypes);
339         assertEquals(7, genTypes.size());
340
341         int genTypesCount = 0;
342         int genTOsCount = 0;
343
344         int compositeKeyListKeyPropertyCount = 0;
345         int compositeKeyListKeyCount = 0;
346         int innerListKeyPropertyCount = 0;
347
348         for (final Type type : genTypes) {
349             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
350                 genTypesCount++;
351             } else if (type instanceof GeneratedTransferObject) {
352                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
353
354                 if (genTO.getName().equals("CompositeKeyListKey")) {
355                     compositeKeyListKeyCount++;
356                     final List<GeneratedProperty> properties = genTO.getProperties();
357                     for (final GeneratedProperty prop : properties) {
358                         if (prop.getName().equals("key1")) {
359                             compositeKeyListKeyPropertyCount++;
360                         } else if (prop.getName().equals("key2")) {
361                             compositeKeyListKeyPropertyCount++;
362                         }
363                     }
364                     genTOsCount++;
365                 } else if (genTO.getName().equals("InnerListKey")) {
366                     final List<GeneratedProperty> properties = genTO.getProperties();
367                     innerListKeyPropertyCount = properties.size();
368                     genTOsCount++;
369                 }
370             }
371         }
372         assertEquals(1, compositeKeyListKeyCount);
373         assertEquals(2, compositeKeyListKeyPropertyCount);
374
375         assertEquals(1, innerListKeyPropertyCount);
376
377         assertEquals(5, genTypesCount);
378         assertEquals(2, genTOsCount);
379     }
380
381     @Test
382     public void testGeneratedTypes() {
383         final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
384             "/demo-topology.yang"));
385
386         assertNotNull(genTypes);
387         assertEquals(14, genTypes.size());
388
389         int genTypesCount = 0;
390         int genTOsCount = 0;
391         for (final Type type : genTypes) {
392             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
393                 genTypesCount++;
394             } else if (type instanceof GeneratedTransferObject) {
395                 genTOsCount++;
396             }
397         }
398
399         assertEquals(11, genTypesCount);
400         assertEquals(3, genTOsCount);
401     }
402 }