Update BindingRuntime{Context,Generator,Types}
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / BitAndUnionTOEnclosingTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsAttributes;
13 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsMethods;
14
15 import java.util.List;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23
24 public class BitAndUnionTOEnclosingTest {
25
26     private static List<Type> genTypes = null;
27     private static GeneratedType parentContainer = null;
28
29     @BeforeClass
30     public static void loadTestResources() {
31         genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource("/bit_and_union.yang"));
32
33         for (Type type : genTypes) {
34             if (type instanceof GeneratedType) {
35                 GeneratedType genType = (GeneratedType) type;
36                 if (genType.getName().equals("ParentContainer") && !(genType instanceof GeneratedTransferObject)) {
37                     parentContainer = genType;
38                 }
39             }
40         }
41     }
42
43     @Test
44     public void testNestedTypesInLeaf() {
45         GeneratedTransferObject lfLeaf = null;
46         int lfLeafCounter = 0;
47         GeneratedTransferObject lf1Leaf = null;
48         int lf1LeafCounter = 0;
49         GeneratedTransferObject lf2Leaf = null;
50         int lf2LeafCounter = 0;
51         List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
52         for (GeneratedType genType : enclosedTypes) {
53             if (genType instanceof GeneratedTransferObject) {
54                 if (genType.getName().equals("Lf")) {
55                     lfLeaf = (GeneratedTransferObject) genType;
56                     lfLeafCounter++;
57                 } else if (genType.getName().equals("Lf$1")) {
58                     lf1Leaf = (GeneratedTransferObject) genType;
59                     lf1LeafCounter++;
60                 } else if (genType.getName().equals("Lf$2")) {
61                     lf2Leaf = (GeneratedTransferObject) genType;
62                     lf2LeafCounter++;
63                 }
64
65             }
66         }
67
68         // nested types in leaf, contains Lf?
69         assertNotNull("Lf TO wasn't found.", lfLeaf);
70         assertEquals("Lf TO has incorrect number of occurences.", 1, lfLeafCounter);
71         assertEquals("Lf has incorrect package name.",
72                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
73                 lfLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
74
75         assertEquals("Lf generated TO has incorrect number of properties", 2, lfLeaf.getProperties().size());
76         containsAttributes(lfLeaf, true, true, true, new NameTypePattern("string", "String"));
77         containsAttributes(lfLeaf, true, false, true, new NameTypePattern("lf$1", "Lf$1"));
78
79         // nested types in leaf, contains Lf1?
80         assertNotNull("Lf$1 TO wasn't found.", lf1Leaf);
81         assertEquals("Lf$1 TO has incorrect number of occurences.", 1, lf1LeafCounter);
82         assertEquals("Lf$1 has incorrect package name.",
83                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
84                 lf1Leaf.getIdentifier().immediatelyEnclosingClass().get().toString());
85
86         assertEquals("Lf generated TO has incorrect number of properties", 4, lf1Leaf.getProperties().size());
87         containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("uint32", "Uint32"));
88         containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("int8", "Byte"));
89         containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("string", "String"));
90         containsAttributes(lf1Leaf, true, false, true, new NameTypePattern("lf$2", "Lf$2"));
91
92         // nested types in leaf, contains Lf2?
93         assertNotNull("Lf$2 TO wasn't found.", lf2Leaf);
94         assertEquals("Lf$2 TO has incorrect number of occurences.", 1, lf2LeafCounter);
95         assertEquals("Lf$2 has incorrect package name.",
96                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
97                 lf2Leaf.getIdentifier().immediatelyEnclosingClass().get().toString());
98
99         assertEquals("Lf generated TO has incorrect number of properties", 2, lf2Leaf.getProperties().size());
100         containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("string", "String"));
101         containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("uint64", "Uint64"));
102     }
103
104     @Test
105     public void testNestedTypesInTypedef() {
106
107         GeneratedTransferObject typeUnionTypedef = null;
108         int typeUnionTypedefCounter = 0;
109
110         for (Type type : genTypes) {
111             if (type instanceof GeneratedType) {
112                 GeneratedType genType = (GeneratedType) type;
113                 if (genType.getName().equals("TypeUnion") && genType instanceof GeneratedTransferObject) {
114                     typeUnionTypedef = (GeneratedTransferObject) genType;
115                     typeUnionTypedefCounter++;
116                 }
117             }
118         }
119
120         assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
121         assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
122
123         assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
124         assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
125         assertEquals("TypeUnion has incorrect package name.",
126                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnionTypedef.getPackageName());
127
128         assertEquals("TypeUnion generated TO has incorrect number of properties", 2, typeUnionTypedef.getProperties()
129                 .size());
130         containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
131         containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion$1", "TypeUnion$1"));
132
133         List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
134         assertEquals("Incorrect number of nested unions", 2, nestedUnions.size());
135
136         GeneratedTransferObject typeUnion1 = null;
137         int typeUnion1Counter = 0;
138         GeneratedTransferObject typeUnion2 = null;
139         int typeUnion2Counter = 0;
140         for (GeneratedType genType : nestedUnions) {
141             if (genType instanceof GeneratedTransferObject) {
142                 if (genType.getName().equals("TypeUnion$1")) {
143                     typeUnion1 = (GeneratedTransferObject) genType;
144                     typeUnion1Counter++;
145                 } else if (genType.getName().equals("TypeUnion$2")) {
146                     typeUnion2 = (GeneratedTransferObject) genType;
147                     typeUnion2Counter++;
148                 }
149             }
150         }
151
152         assertNotNull("TypeUnion$1 TO wasn't found.", typeUnion1);
153         assertEquals("TypeUnion$1 TO has incorrect number of occurences.", 1, typeUnion1Counter);
154
155         assertEquals("TypeUnion$1 has incorrect package name.",
156                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion1.getPackageName());
157
158         assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4,
159             typeUnion1.getProperties().size());
160
161         containsAttributes(typeUnion1, true, true, true, new NameTypePattern("uint32", "Uint32"));
162         containsAttributes(typeUnion1, true, true, true, new NameTypePattern("int8", "Byte"));
163         containsAttributes(typeUnion1, true, true, true, new NameTypePattern("string", "String"));
164         containsAttributes(typeUnion1, true, false, true, new NameTypePattern("typeUnion$2", "TypeUnion$2"));
165
166         assertNotNull("TypeUnion$2 TO wasn't found.", typeUnion2);
167         assertEquals("TypeUnion$2 TO has incorrect number of occurences.", 1, typeUnion2Counter);
168
169         assertEquals("TypeUnion$2 has incorrect package name.",
170                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion2.getPackageName());
171
172         assertEquals("TypeUnion2 generated TO has incorrect number of properties", 2,
173             typeUnion2.getProperties().size());
174         containsAttributes(typeUnion2, true, true, true, new NameTypePattern("string", "String"));
175         containsAttributes(typeUnion2, true, true, true, new NameTypePattern("uint64", "Uint64"));
176
177     }
178
179     @Test
180     public void bitAndUnionEnclosingTest() {
181
182         assertNotNull("Parent container object wasn't found.", parentContainer);
183         containsMethods(parentContainer, new NameTypePattern("getLf", "Lf"));
184
185         GeneratedTransferObject bitLeaf = null;
186         GeneratedTransferObject unionLeaf = null;
187         List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
188         for (GeneratedType genType : enclosedTypes) {
189             if (genType instanceof GeneratedTransferObject) {
190                 if (genType.getName().equals("BitLeaf")) {
191                     bitLeaf = (GeneratedTransferObject) genType;
192                 } else if (genType.getName().equals("UnionLeaf")) {
193                     unionLeaf = (GeneratedTransferObject) genType;
194                 }
195             }
196         }
197
198         assertNotNull("BitLeaf TO wasn't found.", bitLeaf);
199         assertNotNull("UnionLeaf TO wasn't found.", unionLeaf);
200
201         assertEquals("BitLeaf has incorrect package name.",
202                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
203                 bitLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
204         assertEquals("UnionLeaf has incorrect package name.",
205                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
206                 unionLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
207
208         List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
209         GeneratedProperty firstBitProperty = null;
210         GeneratedProperty secondBitProperty = null;
211         GeneratedProperty thirdBitProperty = null;
212
213         for (GeneratedProperty genProperty : propertiesBitLeaf) {
214             if (genProperty.getName().equals("firstBit")) {
215                 firstBitProperty = genProperty;
216             } else if (genProperty.getName().equals("secondBit")) {
217                 secondBitProperty = genProperty;
218             } else if (genProperty.getName().equals("thirdBit")) {
219                 thirdBitProperty = genProperty;
220             }
221         }
222
223         assertNotNull("firstBit property wasn't found", firstBitProperty);
224         assertNotNull("secondBit property wasn't found", secondBitProperty);
225         assertNotNull("thirdBit property wasn't found", thirdBitProperty);
226
227         assertEquals("firstBit property has incorrect type", "Boolean", firstBitProperty.getReturnType().getName());
228         assertEquals("secondBit property has incorrect type", "Boolean", secondBitProperty.getReturnType().getName());
229         assertEquals("thirdBit property has incorrect type", "Boolean", thirdBitProperty.getReturnType().getName());
230
231         GeneratedProperty uint32Property = null;
232         GeneratedProperty stringProperty = null;
233         GeneratedProperty uint8Property = null;
234         List<GeneratedProperty> propertiesUnionLeaf = unionLeaf.getProperties();
235         for (GeneratedProperty genProperty : propertiesUnionLeaf) {
236             if (genProperty.getName().equals("int32")) {
237                 uint32Property = genProperty;
238             } else if (genProperty.getName().equals("string")) {
239                 stringProperty = genProperty;
240             } else if (genProperty.getName().equals("uint8")) {
241                 uint8Property = genProperty;
242             }
243         }
244
245         assertNotNull("uint32 property wasn't found", uint32Property);
246         assertNotNull("string property wasn't found", stringProperty);
247         assertNotNull("uint8 property wasn't found", uint8Property);
248
249         assertEquals("uint32 property has incorrect type", "Integer", uint32Property.getReturnType().getName());
250         assertEquals("string property has incorrect type", "String", stringProperty.getReturnType().getName());
251         assertEquals("uint8 property has incorrect type", "Uint8", uint8Property.getReturnType().getName());
252
253     }
254
255 }