Merge "OF plugin classes must have a strict dependency on Connection Service"
[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.util.ArrayList;
7 import java.util.List;
8 import java.util.Set;
9
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;
21
22 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
23
24     private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
25         final YangModelParser parser = new YangParserImpl();
26
27         final List<File> inputFiles = new ArrayList<File>();
28         for (int i = 0; i < yangFiles.length; ++i) {
29             inputFiles.add(new File(yangFiles[i]));
30         }
31
32         final Set<Module> modules = parser.parseYangModels(inputFiles);
33         return parser.resolveSchemaContext(modules);
34     }
35
36     @Test
37     public void testInnerClassCreationForBitsAndUnionsInLeafes() {
38         final String yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").getPath();
39
40         final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
41         assertTrue(context != null);
42
43         final BindingGenerator bindingGen = new BindingGeneratorImpl();
44         final List<Type> genTypes = bindingGen.generateTypes(context);
45         assertTrue(genTypes != null);
46
47         boolean parentContainerFound = false;
48         boolean bitLeafTOFound = false;
49         boolean unionLeafTOFound = false;
50
51         boolean firstBitPropertyFound = false;
52         boolean secondBitPropertyFound = false;
53         boolean thirdBitPropertyFound = false;
54
55         boolean firstBitPropertyTypeOK = false;
56         boolean secondBitPropertyTypeOK = false;
57         boolean thirdBitPropertyTypeOK = false;
58
59         boolean int32UnionPropertyFound = false;
60         boolean int32UnionPropertyTypeOK = false;
61         boolean stringUnionPropertyFound = false;
62         boolean stringUnionPropertyTypeOK = false;
63         boolean uint8UnionPropertyFound = false;
64         boolean uint8UnionPropertyTypeOK = false;
65
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;
77
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;
85                                         }
86                                     } else if (bitLeafProperty.getName().equals("secondBit")) {
87                                         secondBitPropertyFound = true;
88                                         if (bitLeafPropertyType.equals("Boolean")) {
89                                             secondBitPropertyTypeOK = true;
90                                         }
91                                     } else if (bitLeafProperty.getName().equals("thirdBit")) {
92                                         thirdBitPropertyFound = true;
93                                         if (bitLeafPropertyType.equals("Boolean")) {
94                                             thirdBitPropertyTypeOK = true;
95                                         }
96                                     }
97
98                                 }
99
100                             } else if (genType.getName().equals("UnionLeaf")) {
101                                 unionLeafTOFound = true;
102                                 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
103
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;
111                                         }
112                                     } else if (unionLeafProperty.getName().equals("string")) {
113                                         stringUnionPropertyFound = true;
114                                         if (unionLeafPropertyType.equals("String")) {
115                                             stringUnionPropertyTypeOK = true;
116                                         }
117                                     } else if (unionLeafProperty.getName().equals("uint8")) {
118                                         uint8UnionPropertyFound = true;
119                                         if (unionLeafPropertyType.equals("Short")) {
120                                             uint8UnionPropertyTypeOK = true;
121                                         }
122                                     }
123
124                                 }
125
126                             }
127                         }
128                     }
129                 }
130             }
131         }
132         assertTrue(parentContainerFound);
133
134         assertTrue(bitLeafTOFound);
135         assertTrue(firstBitPropertyFound);
136         assertTrue(secondBitPropertyFound);
137         assertTrue(thirdBitPropertyFound);
138
139         assertTrue(firstBitPropertyTypeOK);
140         assertTrue(secondBitPropertyTypeOK);
141         assertTrue(thirdBitPropertyTypeOK);
142
143         assertTrue(unionLeafTOFound);
144         assertTrue(int32UnionPropertyFound);
145         assertTrue(int32UnionPropertyTypeOK);
146         assertTrue(stringUnionPropertyFound);
147         assertTrue(stringUnionPropertyTypeOK);
148         assertTrue(uint8UnionPropertyFound);
149         assertTrue(uint8UnionPropertyTypeOK);
150
151     }
152 }