Bug 6859 #2 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / ExtendedTypedefTest.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 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.File;
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.Type;
22 import org.opendaylight.yangtools.sal.binding.yang.types.BaseYangTypes;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class ExtendedTypedefTest {
27
28     @Test
29     public void constantGenerationTest() throws Exception {
30         File abstractTopology = new File(getClass().getResource("/typedef-of-typedef/typedef_of_typedef.yang").toURI());
31         File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
32
33         final SchemaContext context = YangParserTestUtils.parseYangSources(abstractTopology, ietfInetTypes);
34         assertNotNull("Schema Context is null", context);
35
36         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
37         final List<Type> genTypes = bindingGen.generateTypes(context);
38
39         GeneratedTransferObject simpleTypedef4 = null;
40         GeneratedTransferObject extendedTypedefUnion = null;
41         GeneratedTransferObject unionTypedef = null;
42         GeneratedTransferObject typedefFromImport = null;
43         for (final Type type : genTypes) {
44             if (type instanceof GeneratedTransferObject) {
45                 if (type.getName().equals("SimpleTypedef4")) {
46                     simpleTypedef4 = (GeneratedTransferObject) type;
47                 } else if (type.getName().equals("ExtendedTypedefUnion")) {
48                     extendedTypedefUnion = (GeneratedTransferObject) type;
49                 } else if (type.getName().equals("UnionTypedef")) {
50                     unionTypedef = (GeneratedTransferObject) type;
51                 } else if (type.getName().equals("TypedefFromImport")) {
52                     typedefFromImport = (GeneratedTransferObject) type;
53                 }
54             }
55         }
56
57         // typedef-from-import
58         assertNotNull("TypedefFromImport not found", typedefFromImport);
59         List<GeneratedProperty> properties = typedefFromImport.getProperties();
60         assertTrue("Properties of TypedefFromImport should be empty", properties.isEmpty());
61         assertEquals("TypedefFromImport should be extended", "Ipv4Address", typedefFromImport.getSuperType().getName());
62
63         // simple-typedef4
64         assertNotNull("SimpleTypedef4 not found", simpleTypedef4);
65         assertNotNull("ExtendedTypedefUnion not found", extendedTypedefUnion);
66         assertNotNull("UnionTypedef", unionTypedef);
67
68         properties = simpleTypedef4.getProperties();
69         assertTrue("SimpleTypedef4 shouldn't have properties.", properties.isEmpty());
70
71         GeneratedTransferObject extendTO = simpleTypedef4.getSuperType();
72         assertNotNull("SimpleTypedef4 should have extend.", extendTO);
73         assertEquals("Incorrect extension for SimpleTypedef4.", "SimpleTypedef3", extendTO.getName());
74         properties = extendTO.getProperties();
75         assertTrue("SimpleTypedef3 shouldn't have properties.", properties.isEmpty());
76
77         extendTO = extendTO.getSuperType();
78         assertNotNull("SimpleTypedef3 should have extend.", extendTO);
79         assertEquals("Incorrect extension for SimpleTypedef3.", "SimpleTypedef2", extendTO.getName());
80         properties = extendTO.getProperties();
81         assertTrue("SimpleTypedef2 shouldn't have properties.", properties.isEmpty());
82
83         extendTO = extendTO.getSuperType();
84         assertNotNull("SimpleTypedef2 should have extend.", extendTO);
85         assertEquals("SimpleTypedef2 should be extended with SimpleTypedef1.", "SimpleTypedef1", extendTO.getName());
86         properties = extendTO.getProperties();
87         assertEquals("Incorrect number of properties in class SimpleTypedef1.", 1, properties.size());
88
89         assertEquals("Incorrect property's name", "value", properties.get(0).getName());
90         assertEquals("Property's incorrect type", BaseYangTypes.UINT8_TYPE, properties.get(0).getReturnType());
91
92         extendTO = extendTO.getSuperType();
93         assertNull("SimpleTypedef1 shouldn't have extend.", extendTO);
94
95         // extended-typedef-union
96         assertNotNull("ExtendedTypedefUnion object not found", extendedTypedefUnion);
97         properties = extendedTypedefUnion.getProperties();
98         assertEquals("ExtendedTypedefUnion shouldn't have any property", 0, properties.size());
99
100         extendTO = extendedTypedefUnion.getSuperType();
101         assertEquals("Incorrect extension fo ExtendedTypedefUnion.", "UnionTypedef", extendTO.getName());
102         assertNull("UnionTypedef shouldn't be extended", extendTO.getSuperType());
103         assertEquals("Incorrect number of properties for UnionTypedef.", 5, extendTO.getProperties().size());
104
105         GeneratedProperty simpleTypedef4Property = null;
106         GeneratedProperty simpleTypedef1Property = null;
107         GeneratedProperty byteTypeProperty = null;
108         GeneratedProperty typedefEnumFruitProperty = null;
109         for (GeneratedProperty genProperty : extendTO.getProperties()) {
110             if (genProperty.getName().equals("simpleTypedef1")) {
111                 simpleTypedef1Property = genProperty;
112             } else if (genProperty.getName().equals("simpleTypedef4")) {
113                 simpleTypedef4Property = genProperty;
114             } else if (genProperty.getName().equals("byteType")) {
115                 byteTypeProperty = genProperty;
116             } else if (genProperty.getName().equals("typedefEnumFruit")) {
117                 typedefEnumFruitProperty = genProperty;
118             }
119         }
120
121         assertNotNull("simpleTypedef4 property not found in UnionTypedef", simpleTypedef4Property);
122         assertNotNull("simpleTypedef1 property not found in UnionTypedef", simpleTypedef1Property);
123         assertNotNull("byteType property not found in UnionTypedef", byteTypeProperty);
124         assertNotNull("typedefEnumFruit property not found in UnionTypedef", typedefEnumFruitProperty);
125
126         assertEquals("Incorrect type for property simpleTypedef4.", "SimpleTypedef4", simpleTypedef4Property
127                 .getReturnType().getName());
128         assertEquals("Incorrect type for property simpleTypedef1.", "SimpleTypedef1", simpleTypedef1Property
129                 .getReturnType().getName());
130         assertEquals("Incorrect type for property byteType.", "ByteType", byteTypeProperty.getReturnType().getName());
131         assertEquals("Incorrect type for property typedefEnumFruit.", "TypedefEnumFruit", typedefEnumFruitProperty
132                 .getReturnType().getName());
133     }
134
135 }