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