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