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