1 package org.opendaylight.controller.sal.java.api.generator.test;
3 import static org.junit.Assert.assertTrue;
6 import java.util.ArrayList;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
12 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
13 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
14 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
15 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
16 import org.opendaylight.controller.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
20 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
22 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
24 private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
25 final YangModelParser parser = new YangParserImpl();
27 final List<File> inputFiles = new ArrayList<File>();
28 for (int i = 0; i < yangFiles.length; ++i) {
29 inputFiles.add(new File(yangFiles[i]));
32 final Set<Module> modules = parser.parseYangModels(inputFiles);
33 return parser.resolveSchemaContext(modules);
37 public void testInnerClassCreationForBitsAndUnionsInLeafes() {
38 final String yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").getPath();
40 final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
41 assertTrue(context != null);
43 final BindingGenerator bindingGen = new BindingGeneratorImpl();
44 final List<Type> genTypes = bindingGen.generateTypes(context);
45 assertTrue(genTypes != null);
47 boolean parentContainerFound = false;
48 boolean bitLeafTOFound = false;
49 boolean unionLeafTOFound = false;
51 boolean firstBitPropertyFound = false;
52 boolean secondBitPropertyFound = false;
53 boolean thirdBitPropertyFound = false;
55 boolean firstBitPropertyTypeOK = false;
56 boolean secondBitPropertyTypeOK = false;
57 boolean thirdBitPropertyTypeOK = false;
59 boolean int32UnionPropertyFound = false;
60 boolean int32UnionPropertyTypeOK = false;
61 boolean stringUnionPropertyFound = false;
62 boolean stringUnionPropertyTypeOK = false;
63 boolean uint8UnionPropertyFound = false;
64 boolean uint8UnionPropertyTypeOK = false;
66 for (Type type : genTypes) {
67 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
68 if (type.getName().equals("ParentContainer")) {
69 parentContainerFound = true;
70 GeneratedType parentContainer = (GeneratedType) type;
71 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
72 for (GeneratedType genType : enclosedTypes) {
73 if (genType instanceof GeneratedTransferObject) {
74 if (genType.getName().equals("BitLeaf")) {
75 bitLeafTOFound = true;
76 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
78 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
79 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
80 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
81 if (bitLeafProperty.getName().equals("firstBit")) {
82 firstBitPropertyFound = true;
83 if (bitLeafPropertyType.equals("Boolean")) {
84 firstBitPropertyTypeOK = true;
86 } else if (bitLeafProperty.getName().equals("secondBit")) {
87 secondBitPropertyFound = true;
88 if (bitLeafPropertyType.equals("Boolean")) {
89 secondBitPropertyTypeOK = true;
91 } else if (bitLeafProperty.getName().equals("thirdBit")) {
92 thirdBitPropertyFound = true;
93 if (bitLeafPropertyType.equals("Boolean")) {
94 thirdBitPropertyTypeOK = true;
100 } else if (genType.getName().equals("UnionLeaf")) {
101 unionLeafTOFound = true;
102 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
104 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
105 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
106 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
107 if (unionLeafProperty.getName().equals("int32")) {
108 int32UnionPropertyFound = true;
109 if (unionLeafPropertyType.equals("Integer")) {
110 int32UnionPropertyTypeOK = true;
112 } else if (unionLeafProperty.getName().equals("string")) {
113 stringUnionPropertyFound = true;
114 if (unionLeafPropertyType.equals("String")) {
115 stringUnionPropertyTypeOK = true;
117 } else if (unionLeafProperty.getName().equals("uint8")) {
118 uint8UnionPropertyFound = true;
119 if (unionLeafPropertyType.equals("Short")) {
120 uint8UnionPropertyTypeOK = true;
132 assertTrue(parentContainerFound);
134 assertTrue(bitLeafTOFound);
135 assertTrue(firstBitPropertyFound);
136 assertTrue(secondBitPropertyFound);
137 assertTrue(thirdBitPropertyFound);
139 assertTrue(firstBitPropertyTypeOK);
140 assertTrue(secondBitPropertyTypeOK);
141 assertTrue(thirdBitPropertyTypeOK);
143 assertTrue(unionLeafTOFound);
144 assertTrue(int32UnionPropertyFound);
145 assertTrue(int32UnionPropertyTypeOK);
146 assertTrue(stringUnionPropertyFound);
147 assertTrue(stringUnionPropertyTypeOK);
148 assertTrue(uint8UnionPropertyFound);
149 assertTrue(uint8UnionPropertyTypeOK);