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