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