1 package org.opendaylight.controller.sal.java.api.generator.test;
3 import static org.junit.Assert.assertTrue;
6 import java.io.IOException;
7 import java.util.ArrayList;
11 import org.junit.Test;
12 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
13 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
14 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
15 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
16 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
17 import org.opendaylight.controller.sal.binding.model.api.Type;
18 import org.opendaylight.controller.sal.java.api.generator.ClassCodeGenerator;
19 import org.opendaylight.controller.yang.model.api.Module;
20 import org.opendaylight.controller.yang.model.api.SchemaContext;
21 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
22 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
24 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
26 private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
27 final YangModelParser parser = new YangParserImpl();
29 final List<File> inputFiles = new ArrayList<File>();
30 for (int i = 0; i < yangFiles.length; ++i) {
31 inputFiles.add(new File(yangFiles[i]));
34 final Set<Module> modules = parser.parseYangModels(inputFiles);
35 return parser.resolveSchemaContext(modules);
39 public void testInnerClassCreationForBitsAndUnionsInLeafes() {
40 final String yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").getPath();
42 final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
43 assertTrue(context != null);
45 final BindingGenerator bindingGen = new BindingGeneratorImpl();
46 final List<Type> genTypes = bindingGen.generateTypes(context);
47 assertTrue(genTypes != null);
49 boolean parentContainerFound = false;
50 boolean bitLeafTOFound = false;
51 boolean unionLeafTOFound = false;
53 boolean firstBitPropertyFound = false;
54 boolean secondBitPropertyFound = false;
55 boolean thirdBitPropertyFound = false;
57 boolean firstBitPropertyTypeOK = false;
58 boolean secondBitPropertyTypeOK = false;
59 boolean thirdBitPropertyTypeOK = false;
61 boolean int32UnionPropertyFound = false;
62 boolean int32UnionPropertyTypeOK = false;
63 boolean stringUnionPropertyFound = false;
64 boolean stringUnionPropertyTypeOK = false;
65 boolean uint8UnionPropertyFound = false;
66 boolean uint8UnionPropertyTypeOK = false;
68 for (Type type : genTypes) {
69 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
70 if (type.getName().equals("ParentContainer")) {
71 parentContainerFound = true;
72 GeneratedType parentContainer = (GeneratedType) type;
73 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
74 for (GeneratedType genType : enclosedTypes) {
75 if (genType instanceof GeneratedTransferObject) {
76 if (genType.getName().equals("BitLeaf")) {
77 bitLeafTOFound = true;
78 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
80 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
81 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
82 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
83 if (bitLeafProperty.getName().equals("firstBit")) {
84 firstBitPropertyFound = true;
85 if (bitLeafPropertyType.equals("Boolean")) {
86 firstBitPropertyTypeOK = true;
88 } else if (bitLeafProperty.getName().equals("secondBit")) {
89 secondBitPropertyFound = true;
90 if (bitLeafPropertyType.equals("Boolean")) {
91 secondBitPropertyTypeOK = true;
93 } else if (bitLeafProperty.getName().equals("thirdBit")) {
94 thirdBitPropertyFound = true;
95 if (bitLeafPropertyType.equals("Boolean")) {
96 thirdBitPropertyTypeOK = true;
102 } else if (genType.getName().equals("UnionLeaf")) {
103 unionLeafTOFound = true;
104 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
106 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
107 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
108 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
109 if (unionLeafProperty.getName().equals("int32")) {
110 int32UnionPropertyFound = true;
111 if (unionLeafPropertyType.equals("Integer")) {
112 int32UnionPropertyTypeOK = true;
114 } else if (unionLeafProperty.getName().equals("string")) {
115 stringUnionPropertyFound = true;
116 if (unionLeafPropertyType.equals("String")) {
117 stringUnionPropertyTypeOK = true;
119 } else if (unionLeafProperty.getName().equals("uint8")) {
120 uint8UnionPropertyFound = true;
121 if (unionLeafPropertyType.equals("Short")) {
122 uint8UnionPropertyTypeOK = true;
134 assertTrue(parentContainerFound);
136 assertTrue(bitLeafTOFound);
137 assertTrue(firstBitPropertyFound);
138 assertTrue(secondBitPropertyFound);
139 assertTrue(thirdBitPropertyFound);
141 assertTrue(firstBitPropertyTypeOK);
142 assertTrue(secondBitPropertyTypeOK);
143 assertTrue(thirdBitPropertyTypeOK);
145 assertTrue(unionLeafTOFound);
146 assertTrue(int32UnionPropertyFound);
147 assertTrue(int32UnionPropertyTypeOK);
148 assertTrue(stringUnionPropertyFound);
149 assertTrue(stringUnionPropertyTypeOK);
150 assertTrue(uint8UnionPropertyFound);
151 assertTrue(uint8UnionPropertyTypeOK);