Fix minor bug in FRM proactive flow code path
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / controller / sal / java / api / generator / test / GenerateInnerClassForBitsAndUnionInLeavesTest.java
1 package org.opendaylight.controller.sal.java.api.generator.test;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Set;
10
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;
23
24 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
25
26     private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
27         final YangModelParser parser = new YangParserImpl();
28
29         final List<File> inputFiles = new ArrayList<File>();
30         for (int i = 0; i < yangFiles.length; ++i) {
31             inputFiles.add(new File(yangFiles[i]));
32         }
33
34         final Set<Module> modules = parser.parseYangModels(inputFiles);
35         return parser.resolveSchemaContext(modules);
36     }
37
38     @Test
39     public void testInnerClassCreationForBitsAndUnionsInLeafes() {
40         final String yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").getPath();
41
42         final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
43         assertTrue(context != null);
44
45         final BindingGenerator bindingGen = new BindingGeneratorImpl();
46         final List<Type> genTypes = bindingGen.generateTypes(context);
47         assertTrue(genTypes != null);
48
49         boolean parentContainerFound = false;
50         boolean bitLeafTOFound = false;
51         boolean unionLeafTOFound = false;
52
53         boolean firstBitPropertyFound = false;
54         boolean secondBitPropertyFound = false;
55         boolean thirdBitPropertyFound = false;
56
57         boolean firstBitPropertyTypeOK = false;
58         boolean secondBitPropertyTypeOK = false;
59         boolean thirdBitPropertyTypeOK = false;
60
61         boolean int32UnionPropertyFound = false;
62         boolean int32UnionPropertyTypeOK = false;
63         boolean stringUnionPropertyFound = false;
64         boolean stringUnionPropertyTypeOK = false;
65         boolean uint8UnionPropertyFound = false;
66         boolean uint8UnionPropertyTypeOK = false;
67
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;
79
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;
87                                         }
88                                     } else if (bitLeafProperty.getName().equals("secondBit")) {
89                                         secondBitPropertyFound = true;
90                                         if (bitLeafPropertyType.equals("Boolean")) {
91                                             secondBitPropertyTypeOK = true;
92                                         }
93                                     } else if (bitLeafProperty.getName().equals("thirdBit")) {
94                                         thirdBitPropertyFound = true;
95                                         if (bitLeafPropertyType.equals("Boolean")) {
96                                             thirdBitPropertyTypeOK = true;
97                                         }
98                                     }
99
100                                 }
101
102                             } else if (genType.getName().equals("UnionLeaf")) {
103                                 unionLeafTOFound = true;
104                                 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
105
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;
113                                         }
114                                     } else if (unionLeafProperty.getName().equals("string")) {
115                                         stringUnionPropertyFound = true;
116                                         if (unionLeafPropertyType.equals("String")) {
117                                             stringUnionPropertyTypeOK = true;
118                                         }
119                                     } else if (unionLeafProperty.getName().equals("uint8")) {
120                                         uint8UnionPropertyFound = true;
121                                         if (unionLeafPropertyType.equals("Short")) {
122                                             uint8UnionPropertyTypeOK = true;
123                                         }
124                                     }
125
126                                 }
127
128                             }
129                         }
130                     }
131                 }
132             }
133         }
134         assertTrue(parentContainerFound);
135
136         assertTrue(bitLeafTOFound);
137         assertTrue(firstBitPropertyFound);
138         assertTrue(secondBitPropertyFound);
139         assertTrue(thirdBitPropertyFound);
140
141         assertTrue(firstBitPropertyTypeOK);
142         assertTrue(secondBitPropertyTypeOK);
143         assertTrue(thirdBitPropertyTypeOK);
144
145         assertTrue(unionLeafTOFound);
146         assertTrue(int32UnionPropertyFound);
147         assertTrue(int32UnionPropertyTypeOK);
148         assertTrue(stringUnionPropertyFound);
149         assertTrue(stringUnionPropertyTypeOK);
150         assertTrue(uint8UnionPropertyFound);
151         assertTrue(uint8UnionPropertyTypeOK);
152
153     }
154 }