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.yangtools.sal.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;
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;
24 public class ExtendedTypedefTest {
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());
31 final SchemaContext context = RetestUtils.parseYangSources(abstractTopology, ietfInetTypes);
32 assertNotNull("Schema Context is null", context);
34 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
35 final List<Type> genTypes = bindingGen.generateTypes(context);
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;
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());
62 assertNotNull("SimpleTypedef4 not found", simpleTypedef4);
63 assertNotNull("ExtendedTypedefUnion not found", extendedTypedefUnion);
64 assertNotNull("UnionTypedef", unionTypedef);
66 properties = simpleTypedef4.getProperties();
67 assertTrue("SimpleTypedef4 shouldn't have properties.", properties.isEmpty());
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());
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());
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());
87 assertEquals("Incorrect property's name", "value", properties.get(0).getName());
88 assertEquals("Property's incorrect type", BaseYangTypes.UINT8_TYPE, properties.get(0).getReturnType());
90 extendTO = extendTO.getSuperType();
91 assertNull("SimpleTypedef1 shouldn't have extend.", extendTO);
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());
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());
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;
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);
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());