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