319197c988bd3307bea338c20dfcdc74ea5be1e4
[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.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class BitAndUnionTOEnclosingTest {
26
27     private static List<Type> genTypes = null;
28     private static GeneratedType parentContainer = null;
29
30     @BeforeClass
31     public static void loadTestResources() {
32         final SchemaContext context = YangParserTestUtils.parseYangResource("/bit_and_union.yang");
33
34         assertNotNull(context);
35         genTypes = DefaultBindingGenerator.generateFor(context);
36
37         for (Type type : genTypes) {
38             if (type instanceof GeneratedType) {
39                 GeneratedType genType = (GeneratedType) type;
40                 if (genType.getName().equals("ParentContainer") && !(genType instanceof GeneratedTransferObject)) {
41                     parentContainer = genType;
42                 }
43             }
44         }
45     }
46
47     @Test
48     public void testNestedTypesInLeaf() {
49         GeneratedTransferObject lfLeaf = null;
50         int lfLeafCounter = 0;
51         GeneratedTransferObject lf1Leaf = null;
52         int lf1LeafCounter = 0;
53         GeneratedTransferObject lf2Leaf = null;
54         int lf2LeafCounter = 0;
55         List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
56         for (GeneratedType genType : enclosedTypes) {
57             if (genType instanceof GeneratedTransferObject) {
58                 if (genType.getName().equals("Lf")) {
59                     lfLeaf = (GeneratedTransferObject) genType;
60                     lfLeafCounter++;
61                 } else if (genType.getName().equals("Lf$1")) {
62                     lf1Leaf = (GeneratedTransferObject) genType;
63                     lf1LeafCounter++;
64                 } else if (genType.getName().equals("Lf$2")) {
65                     lf2Leaf = (GeneratedTransferObject) genType;
66                     lf2LeafCounter++;
67                 }
68
69             }
70         }
71
72         // nested types in leaf, contains Lf?
73         assertNotNull("Lf TO wasn't found.", lfLeaf);
74         assertEquals("Lf TO has incorrect number of occurences.", 1, lfLeafCounter);
75         assertEquals("Lf has incorrect package name.",
76                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
77                 lfLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
78
79         assertEquals("Lf generated TO has incorrect number of properties", 2, lfLeaf.getProperties().size());
80         containsAttributes(lfLeaf, true, true, true, new NameTypePattern("string", "String"));
81         containsAttributes(lfLeaf, true, false, true, new NameTypePattern("lf$1", "Lf$1"));
82
83         // nested types in leaf, contains Lf1?
84         assertNotNull("Lf$1 TO wasn't found.", lf1Leaf);
85         assertEquals("Lf$1 TO has incorrect number of occurences.", 1, lf1LeafCounter);
86         assertEquals("Lf$1 has incorrect package name.",
87                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
88                 lf1Leaf.getIdentifier().immediatelyEnclosingClass().get().toString());
89
90         assertEquals("Lf generated TO has incorrect number of properties", 4, lf1Leaf.getProperties().size());
91         containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("uint32", "Uint32"));
92         containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("int8", "Byte"));
93         containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("string", "String"));
94         containsAttributes(lf1Leaf, true, false, true, new NameTypePattern("lf$2", "Lf$2"));
95
96         // nested types in leaf, contains Lf2?
97         assertNotNull("Lf$2 TO wasn't found.", lf2Leaf);
98         assertEquals("Lf$2 TO has incorrect number of occurences.", 1, lf2LeafCounter);
99         assertEquals("Lf$2 has incorrect package name.",
100                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
101                 lf2Leaf.getIdentifier().immediatelyEnclosingClass().get().toString());
102
103         assertEquals("Lf generated TO has incorrect number of properties", 2, lf2Leaf.getProperties().size());
104         containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("string", "String"));
105         containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("uint64", "Uint64"));
106     }
107
108     @Test
109     public void testNestedTypesInTypedef() {
110
111         GeneratedTransferObject typeUnionTypedef = null;
112         int typeUnionTypedefCounter = 0;
113
114         for (Type type : genTypes) {
115             if (type instanceof GeneratedType) {
116                 GeneratedType genType = (GeneratedType) type;
117                 if (genType.getName().equals("TypeUnion") && genType instanceof GeneratedTransferObject) {
118                     typeUnionTypedef = (GeneratedTransferObject) genType;
119                     typeUnionTypedefCounter++;
120                 }
121             }
122         }
123
124         assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
125         assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
126
127         assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
128         assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
129         assertEquals("TypeUnion has incorrect package name.",
130                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnionTypedef.getPackageName());
131
132         assertEquals("TypeUnion generated TO has incorrect number of properties", 2, typeUnionTypedef.getProperties()
133                 .size());
134         containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
135         containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion$1", "TypeUnion$1"));
136
137         List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
138         assertEquals("Incorrect number of nested unions", 2, nestedUnions.size());
139
140         GeneratedTransferObject typeUnion1 = null;
141         int typeUnion1Counter = 0;
142         GeneratedTransferObject typeUnion2 = null;
143         int typeUnion2Counter = 0;
144         for (GeneratedType genType : nestedUnions) {
145             if (genType instanceof GeneratedTransferObject) {
146                 if (genType.getName().equals("TypeUnion$1")) {
147                     typeUnion1 = (GeneratedTransferObject) genType;
148                     typeUnion1Counter++;
149                 } else if (genType.getName().equals("TypeUnion$2")) {
150                     typeUnion2 = (GeneratedTransferObject) genType;
151                     typeUnion2Counter++;
152                 }
153             }
154         }
155
156         assertNotNull("TypeUnion$1 TO wasn't found.", typeUnion1);
157         assertEquals("TypeUnion$1 TO has incorrect number of occurences.", 1, typeUnion1Counter);
158
159         assertEquals("TypeUnion$1 has incorrect package name.",
160                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion1.getPackageName());
161
162         assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4,
163             typeUnion1.getProperties().size());
164
165         containsAttributes(typeUnion1, true, true, true, new NameTypePattern("uint32", "Uint32"));
166         containsAttributes(typeUnion1, true, true, true, new NameTypePattern("int8", "Byte"));
167         containsAttributes(typeUnion1, true, true, true, new NameTypePattern("string", "String"));
168         containsAttributes(typeUnion1, true, false, true, new NameTypePattern("typeUnion$2", "TypeUnion$2"));
169
170         assertNotNull("TypeUnion$2 TO wasn't found.", typeUnion2);
171         assertEquals("TypeUnion$2 TO has incorrect number of occurences.", 1, typeUnion2Counter);
172
173         assertEquals("TypeUnion$2 has incorrect package name.",
174                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion2.getPackageName());
175
176         assertEquals("TypeUnion2 generated TO has incorrect number of properties", 2,
177             typeUnion2.getProperties().size());
178         containsAttributes(typeUnion2, true, true, true, new NameTypePattern("string", "String"));
179         containsAttributes(typeUnion2, true, true, true, new NameTypePattern("uint64", "Uint64"));
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.getIdentifier().immediatelyEnclosingClass().get().toString());
208         assertEquals("UnionLeaf has incorrect package name.",
209                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
210                 unionLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
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", "Uint8", uint8Property.getReturnType().getName());
256
257     }
258
259 }