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