2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl;
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;
15 import java.util.List;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class BitAndUnionTOEnclosingTest {
25 private static List<GeneratedType> genTypes = null;
26 private static GeneratedType parentContainer = null;
29 public static void loadTestResources() {
30 genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource("/bit_and_union.yang"));
32 for (GeneratedType genType : genTypes) {
33 if (genType.getName().equals("ParentContainer") && !(genType instanceof GeneratedTransferObject)) {
34 parentContainer = genType;
40 public void testNestedTypesInLeaf() {
41 GeneratedTransferObject lfLeaf = null;
42 int lfLeafCounter = 0;
43 GeneratedTransferObject lf1Leaf = null;
44 int lf1LeafCounter = 0;
45 GeneratedTransferObject lf2Leaf = null;
46 int lf2LeafCounter = 0;
47 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
48 assertEquals(5, enclosedTypes.size());
50 for (GeneratedType genType : enclosedTypes) {
51 if (genType instanceof GeneratedTransferObject) {
52 if (genType.getName().equals("Lf")) {
53 lfLeaf = (GeneratedTransferObject) genType;
55 } else if (genType.getName().equals("Lf$1")) {
56 lf1Leaf = (GeneratedTransferObject) genType;
58 } else if (genType.getName().equals("Lf$2")) {
59 lf2Leaf = (GeneratedTransferObject) genType;
66 // nested types in leaf, contains Lf?
67 assertNotNull("Lf TO wasn't found.", lfLeaf);
68 assertEquals("Lf TO has incorrect number of occurences.", 1, lfLeafCounter);
69 assertEquals("Lf has incorrect package name.",
70 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
71 lfLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
73 assertEquals("Lf generated TO has incorrect number of properties", 2, lfLeaf.getProperties().size());
74 containsAttributes(lfLeaf, true, true, true, new NameTypePattern("string", "String"));
75 containsAttributes(lfLeaf, true, false, true, new NameTypePattern("lf$1", "Lf$1"));
77 // nested types in leaf, contains Lf1?
78 assertNotNull("Lf$1 TO wasn't found.", lf1Leaf);
79 assertEquals("Lf$1 TO has incorrect number of occurences.", 1, lf1LeafCounter);
80 assertEquals("Lf$1 has incorrect package name.",
81 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
82 lf1Leaf.getIdentifier().immediatelyEnclosingClass().get().toString());
84 assertEquals("Lf generated TO has incorrect number of properties", 4, lf1Leaf.getProperties().size());
85 containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("uint32", "Uint32"));
86 containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("int8", "Byte"));
87 containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("string", "String"));
88 containsAttributes(lf1Leaf, true, false, true, new NameTypePattern("lf$2", "Lf$2"));
90 // nested types in leaf, contains Lf2?
91 assertNotNull("Lf$2 TO wasn't found.", lf2Leaf);
92 assertEquals("Lf$2 TO has incorrect number of occurences.", 1, lf2LeafCounter);
93 assertEquals("Lf$2 has incorrect package name.",
94 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
95 lf2Leaf.getIdentifier().immediatelyEnclosingClass().get().toString());
97 assertEquals("Lf generated TO has incorrect number of properties", 2, lf2Leaf.getProperties().size());
98 containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("string", "String"));
99 containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("uint64", "Uint64"));
103 public void testNestedTypesInTypedef() {
105 GeneratedTransferObject typeUnionTypedef = null;
106 int typeUnionTypedefCounter = 0;
108 for (GeneratedType genType : genTypes) {
109 if (genType.getName().equals("TypeUnion") && genType instanceof GeneratedTransferObject) {
110 typeUnionTypedef = (GeneratedTransferObject) genType;
111 typeUnionTypedefCounter++;
115 assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
116 assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
118 assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
119 assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
120 assertEquals("TypeUnion has incorrect package name.",
121 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnionTypedef.getPackageName());
123 assertEquals("TypeUnion generated TO has incorrect number of properties", 2, typeUnionTypedef.getProperties()
125 containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
126 containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion$1", "TypeUnion$1"));
128 List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
129 assertEquals("Incorrect number of nested unions", 2, nestedUnions.size());
131 GeneratedTransferObject typeUnion1 = null;
132 int typeUnion1Counter = 0;
133 GeneratedTransferObject typeUnion2 = null;
134 int typeUnion2Counter = 0;
135 for (GeneratedType genType : nestedUnions) {
136 if (genType instanceof GeneratedTransferObject) {
137 if (genType.getName().equals("TypeUnion$1")) {
138 typeUnion1 = (GeneratedTransferObject) genType;
140 } else if (genType.getName().equals("TypeUnion$2")) {
141 typeUnion2 = (GeneratedTransferObject) genType;
147 assertNotNull("TypeUnion$1 TO wasn't found.", typeUnion1);
148 assertEquals("TypeUnion$1 TO has incorrect number of occurences.", 1, typeUnion1Counter);
150 assertEquals("TypeUnion$1 has incorrect package name.",
151 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion1.getPackageName());
153 assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4,
154 typeUnion1.getProperties().size());
156 containsAttributes(typeUnion1, true, true, true, new NameTypePattern("uint32", "Uint32"));
157 containsAttributes(typeUnion1, true, true, true, new NameTypePattern("int8", "Byte"));
158 containsAttributes(typeUnion1, true, true, true, new NameTypePattern("string", "String"));
159 containsAttributes(typeUnion1, true, false, true, new NameTypePattern("typeUnion$2", "TypeUnion$2"));
161 assertNotNull("TypeUnion$2 TO wasn't found.", typeUnion2);
162 assertEquals("TypeUnion$2 TO has incorrect number of occurences.", 1, typeUnion2Counter);
164 assertEquals("TypeUnion$2 has incorrect package name.",
165 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion2.getPackageName());
167 assertEquals("TypeUnion2 generated TO has incorrect number of properties", 2,
168 typeUnion2.getProperties().size());
169 containsAttributes(typeUnion2, true, true, true, new NameTypePattern("string", "String"));
170 containsAttributes(typeUnion2, true, true, true, new NameTypePattern("uint64", "Uint64"));
175 public void bitAndUnionEnclosingTest() {
177 assertNotNull("Parent container object wasn't found.", parentContainer);
178 containsMethods(parentContainer, new NameTypePattern("getLf", "Lf"));
180 GeneratedTransferObject bitLeaf = null;
181 GeneratedTransferObject unionLeaf = null;
182 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
183 for (GeneratedType genType : enclosedTypes) {
184 if (genType instanceof GeneratedTransferObject) {
185 if (genType.getName().equals("BitLeaf")) {
186 bitLeaf = (GeneratedTransferObject) genType;
187 } else if (genType.getName().equals("UnionLeaf")) {
188 unionLeaf = (GeneratedTransferObject) genType;
193 assertNotNull("BitLeaf TO wasn't found.", bitLeaf);
194 assertNotNull("UnionLeaf TO wasn't found.", unionLeaf);
196 assertEquals("BitLeaf has incorrect package name.",
197 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
198 bitLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
199 assertEquals("UnionLeaf has incorrect package name.",
200 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
201 unionLeaf.getIdentifier().immediatelyEnclosingClass().get().toString());
203 List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
204 GeneratedProperty firstBitProperty = null;
205 GeneratedProperty secondBitProperty = null;
206 GeneratedProperty thirdBitProperty = null;
208 for (GeneratedProperty genProperty : propertiesBitLeaf) {
209 if (genProperty.getName().equals("firstBit")) {
210 firstBitProperty = genProperty;
211 } else if (genProperty.getName().equals("secondBit")) {
212 secondBitProperty = genProperty;
213 } else if (genProperty.getName().equals("thirdBit")) {
214 thirdBitProperty = genProperty;
218 assertNotNull("firstBit property wasn't found", firstBitProperty);
219 assertNotNull("secondBit property wasn't found", secondBitProperty);
220 assertNotNull("thirdBit property wasn't found", thirdBitProperty);
222 assertEquals("firstBit property has incorrect type", "Boolean", firstBitProperty.getReturnType().getName());
223 assertEquals("secondBit property has incorrect type", "Boolean", secondBitProperty.getReturnType().getName());
224 assertEquals("thirdBit property has incorrect type", "Boolean", thirdBitProperty.getReturnType().getName());
226 GeneratedProperty uint32Property = null;
227 GeneratedProperty stringProperty = null;
228 GeneratedProperty uint8Property = null;
229 List<GeneratedProperty> propertiesUnionLeaf = unionLeaf.getProperties();
230 for (GeneratedProperty genProperty : propertiesUnionLeaf) {
231 if (genProperty.getName().equals("int32")) {
232 uint32Property = genProperty;
233 } else if (genProperty.getName().equals("string")) {
234 stringProperty = genProperty;
235 } else if (genProperty.getName().equals("uint8")) {
236 uint8Property = genProperty;
240 assertNotNull("uint32 property wasn't found", uint32Property);
241 assertNotNull("string property wasn't found", stringProperty);
242 assertNotNull("uint8 property wasn't found", uint8Property);
244 assertEquals("uint32 property has incorrect type", "Integer", uint32Property.getReturnType().getName());
245 assertEquals("string property has incorrect type", "String", stringProperty.getReturnType().getName());
246 assertEquals("uint8 property has incorrect type", "Uint8", uint8Property.getReturnType().getName());