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