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