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