2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl;
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;
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.generator.impl.BindingGeneratorImpl;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.mdsal.binding.yang.types.BaseYangTypes;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 public class ExtendedTypedefTest {
30 public void constantGenerationTest() throws Exception {
31 File abstractTopology = new File(getClass().getResource("/typedef-of-typedef/typedef_of_typedef.yang").toURI());
32 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
34 final SchemaContext context = YangParserTestUtils.parseYangFiles(abstractTopology, ietfInetTypes);
35 assertNotNull("Schema Context is null", context);
37 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
38 final List<Type> genTypes = bindingGen.generateTypes(context);
40 GeneratedTransferObject simpleTypedef4 = null;
41 GeneratedTransferObject extendedTypedefUnion = null;
42 GeneratedTransferObject unionTypedef = null;
43 GeneratedTransferObject typedefFromImport = null;
44 for (final Type type : genTypes) {
45 if (type instanceof GeneratedTransferObject) {
46 if (type.getName().equals("SimpleTypedef4")) {
47 simpleTypedef4 = (GeneratedTransferObject) type;
48 } else if (type.getName().equals("ExtendedTypedefUnion")) {
49 extendedTypedefUnion = (GeneratedTransferObject) type;
50 } else if (type.getName().equals("UnionTypedef")) {
51 unionTypedef = (GeneratedTransferObject) type;
52 } else if (type.getName().equals("TypedefFromImport")) {
53 typedefFromImport = (GeneratedTransferObject) type;
58 // typedef-from-import
59 assertNotNull("TypedefFromImport not found", typedefFromImport);
60 List<GeneratedProperty> properties = typedefFromImport.getProperties();
61 assertTrue("Properties of TypedefFromImport should be empty", properties.isEmpty());
62 assertEquals("TypedefFromImport should be extended", "Ipv4Address", typedefFromImport.getSuperType().getName());
65 assertNotNull("SimpleTypedef4 not found", simpleTypedef4);
66 assertNotNull("ExtendedTypedefUnion not found", extendedTypedefUnion);
67 assertNotNull("UnionTypedef", unionTypedef);
69 properties = simpleTypedef4.getProperties();
70 assertTrue("SimpleTypedef4 shouldn't have properties.", properties.isEmpty());
72 GeneratedTransferObject extendTO = simpleTypedef4.getSuperType();
73 assertNotNull("SimpleTypedef4 should have extend.", extendTO);
74 assertEquals("Incorrect extension for SimpleTypedef4.", "SimpleTypedef3", extendTO.getName());
75 properties = extendTO.getProperties();
76 assertTrue("SimpleTypedef3 shouldn't have properties.", properties.isEmpty());
78 extendTO = extendTO.getSuperType();
79 assertNotNull("SimpleTypedef3 should have extend.", extendTO);
80 assertEquals("Incorrect extension for SimpleTypedef3.", "SimpleTypedef2", extendTO.getName());
81 properties = extendTO.getProperties();
82 assertTrue("SimpleTypedef2 shouldn't have properties.", properties.isEmpty());
84 extendTO = extendTO.getSuperType();
85 assertNotNull("SimpleTypedef2 should have extend.", extendTO);
86 assertEquals("SimpleTypedef2 should be extended with SimpleTypedef1.", "SimpleTypedef1", extendTO.getName());
87 properties = extendTO.getProperties();
88 assertEquals("Incorrect number of properties in class SimpleTypedef1.", 1, properties.size());
90 assertEquals("Incorrect property's name", "value", properties.get(0).getName());
91 assertEquals("Property's incorrect type", BaseYangTypes.UINT8_TYPE, properties.get(0).getReturnType());
93 extendTO = extendTO.getSuperType();
94 assertNull("SimpleTypedef1 shouldn't have extend.", extendTO);
96 // extended-typedef-union
97 assertNotNull("ExtendedTypedefUnion object not found", extendedTypedefUnion);
98 properties = extendedTypedefUnion.getProperties();
99 assertEquals("ExtendedTypedefUnion shouldn't have any property", 0, properties.size());
101 extendTO = extendedTypedefUnion.getSuperType();
102 assertEquals("Incorrect extension fo ExtendedTypedefUnion.", "UnionTypedef", extendTO.getName());
103 assertNull("UnionTypedef shouldn't be extended", extendTO.getSuperType());
104 assertEquals("Incorrect number of properties for UnionTypedef.", 5, extendTO.getProperties().size());
106 GeneratedProperty simpleTypedef4Property = null;
107 GeneratedProperty simpleTypedef1Property = null;
108 GeneratedProperty byteTypeProperty = null;
109 GeneratedProperty typedefEnumFruitProperty = null;
110 for (GeneratedProperty genProperty : extendTO.getProperties()) {
111 if (genProperty.getName().equals("simpleTypedef1")) {
112 simpleTypedef1Property = genProperty;
113 } else if (genProperty.getName().equals("simpleTypedef4")) {
114 simpleTypedef4Property = genProperty;
115 } else if (genProperty.getName().equals("byteType")) {
116 byteTypeProperty = genProperty;
117 } else if (genProperty.getName().equals("typedefEnumFruit")) {
118 typedefEnumFruitProperty = genProperty;
122 assertNotNull("simpleTypedef4 property not found in UnionTypedef", simpleTypedef4Property);
123 assertNotNull("simpleTypedef1 property not found in UnionTypedef", simpleTypedef1Property);
124 assertNotNull("byteType property not found in UnionTypedef", byteTypeProperty);
125 assertNotNull("typedefEnumFruit property not found in UnionTypedef", typedefEnumFruitProperty);
127 assertEquals("Incorrect type for property simpleTypedef4.", "SimpleTypedef4", simpleTypedef4Property
128 .getReturnType().getName());
129 assertEquals("Incorrect type for property simpleTypedef1.", "SimpleTypedef1", simpleTypedef1Property
130 .getReturnType().getName());
131 assertEquals("Incorrect type for property byteType.", "ByteType", byteTypeProperty.getReturnType().getName());
132 assertEquals("Incorrect type for property typedefEnumFruit.", "TypedefEnumFruit", typedefEnumFruitProperty
133 .getReturnType().getName());