2 * Copyright (c) 2013 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.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.*;
12 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsMethods;
13 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsAttributes;
16 import java.util.ArrayList;
17 import java.util.List;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
24 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
25 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
26 import org.opendaylight.yangtools.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
30 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
32 public class BitAndUnionTOEnclosingTest {
34 private final static List<File> testModels = new ArrayList<File>();
35 private static List<Type> genTypes = null;
36 private static GeneratedType parentContainer = null;
38 public static void parseResources() {
39 final YangModelParser parser = new YangParserImpl();
40 final Set<Module> modules = parser.parseYangModels(testModels);
41 final SchemaContext context = parser.resolveSchemaContext(modules);
43 assertNotNull(context);
44 final BindingGenerator bindingGen = new BindingGeneratorImpl();
45 genTypes = bindingGen.generateTypes(context);
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;
58 public static void loadTestResources() {
59 final File listModelFile = new File(ExtendedTypedefTest.class.getResource("/bit_and_union.yang").getPath());
60 testModels.add(listModelFile);
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;
78 } else if (genType.getName().equals("Lf1")) {
79 lf1Leaf = (GeneratedTransferObject) genType;
81 } else if (genType.getName().equals("Lf2")) {
82 lf2Leaf = (GeneratedTransferObject) genType;
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.rev2013626.ParentContainer",
94 lfLeaf.getPackageName());
96 assertEquals("Lf generated TO has incorrect number of properties", 2, lfLeaf.getProperties().size());
97 containsAttributes(lfLeaf, true, true, true, new NameTypePattern("string", "String"));
98 containsAttributes(lfLeaf, true, false, true, new NameTypePattern("lf1", "Lf1"));
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.rev2013626.ParentContainer",
105 lf1Leaf.getPackageName());
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"));
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.rev2013626.ParentContainer",
118 lf2Leaf.getPackageName());
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"));
126 public void testNestedTypesInTypedef() {
128 GeneratedTransferObject typeUnionTypedef = null;
129 int typeUnionTypedefCounter = 0;
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++;
141 assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
142 assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
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.rev2013626", typeUnionTypedef.getPackageName());
149 assertEquals("TypeUnion generated TO has incorrect number of properties", 2, typeUnionTypedef.getProperties()
151 containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
152 containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion1", "TypeUnion1"));
154 List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
155 assertEquals("Incorrect number of nested unions", 2, nestedUnions.size());
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;
166 } else if (genType.getName().equals("TypeUnion2")) {
167 typeUnion2 = (GeneratedTransferObject) genType;
173 assertNotNull("TypeUnion1 TO wasn't found.", typeUnion1);
174 assertEquals("TypeUnion1 TO has incorrect number of occurences.", 1, typeUnion1Counter);
176 assertEquals("TypeUnion1 has incorrect package name.",
177 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev2013626", typeUnion1.getPackageName());
179 assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4, typeUnion1.getProperties().size());
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"));
186 assertNotNull("TypeUnion2 TO wasn't found.", typeUnion2);
187 assertEquals("TypeUnion2 TO has incorrect number of occurences.", 1, typeUnion2Counter);
189 assertEquals("TypeUnion2 has incorrect package name.",
190 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev2013626", typeUnion2.getPackageName());
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"));
199 public void bitAndUnionEnclosingTest() {
201 assertNotNull("Parent container object wasn't found.", parentContainer);
202 containsMethods(parentContainer, new NameTypePattern("getLf", "Lf"));
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;
217 assertNotNull("BitLeaf TO wasn't found.", bitLeaf);
218 assertNotNull("UnionLeaf TO wasn't found.", unionLeaf);
220 assertEquals("BitLeaf has incorrect package name.",
221 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev2013626.ParentContainer",
222 bitLeaf.getPackageName());
223 assertEquals("UnionLeaf has incorrect package name.",
224 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev2013626.ParentContainer",
225 bitLeaf.getPackageName());
227 List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
228 GeneratedProperty firstBitProperty = null;
229 GeneratedProperty secondBitProperty = null;
230 GeneratedProperty thirdBitProperty = null;
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;
242 assertNotNull("firstBit property wasn't found", firstBitProperty);
243 assertNotNull("secondBit property wasn't found", secondBitProperty);
244 assertNotNull("thirdBit property wasn't found", thirdBitProperty);
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());
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;
264 assertNotNull("uint32 property wasn't found", uint32Property);
265 assertNotNull("string property wasn't found", stringProperty);
266 assertNotNull("uint8 property wasn't found", uint8Property);
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());