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