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