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