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