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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsAttributes;
13 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsMethods;
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.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.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
31 public class BitAndUnionTOEnclosingTest {
33 private final static List<File> testModels = new ArrayList<File>();
34 private static List<Type> genTypes = null;
35 private static GeneratedType parentContainer = null;
37 public static void parseResources() throws IOException {
38 final YangContextParser parser = new YangParserImpl();
39 final SchemaContext context = parser.parseFiles(testModels);
41 assertNotNull(context);
42 final BindingGenerator bindingGen = new BindingGeneratorImpl();
43 genTypes = bindingGen.generateTypes(context);
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;
56 public static void loadTestResources() throws IOException, URISyntaxException {
57 final File listModelFile = new File(ExtendedTypedefTest.class.getResource("/bit_and_union.yang").toURI());
58 testModels.add(listModelFile);
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;
76 } else if (genType.getName().equals("Lf1")) {
77 lf1Leaf = (GeneratedTransferObject) genType;
79 } else if (genType.getName().equals("Lf2")) {
80 lf2Leaf = (GeneratedTransferObject) genType;
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());
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"));
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());
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"));
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());
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"));
124 public void testNestedTypesInTypedef() {
126 GeneratedTransferObject typeUnionTypedef = null;
127 int typeUnionTypedefCounter = 0;
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++;
139 assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
140 assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
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());
147 assertEquals("TypeUnion generated TO has incorrect number of properties", 3, typeUnionTypedef.getProperties()
149 containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
150 containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion1", "TypeUnion1"));
152 List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
153 assertEquals("Incorrect number of nested unions", 2, nestedUnions.size());
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;
164 } else if (genType.getName().equals("TypeUnion2")) {
165 typeUnion2 = (GeneratedTransferObject) genType;
171 assertNotNull("TypeUnion1 TO wasn't found.", typeUnion1);
172 assertEquals("TypeUnion1 TO has incorrect number of occurences.", 1, typeUnion1Counter);
174 assertEquals("TypeUnion1 has incorrect package name.",
175 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion1.getPackageName());
177 assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4, typeUnion1.getProperties().size());
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"));
184 assertNotNull("TypeUnion2 TO wasn't found.", typeUnion2);
185 assertEquals("TypeUnion2 TO has incorrect number of occurences.", 1, typeUnion2Counter);
187 assertEquals("TypeUnion2 has incorrect package name.",
188 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion2.getPackageName());
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"));
197 public void bitAndUnionEnclosingTest() {
199 assertNotNull("Parent container object wasn't found.", parentContainer);
200 containsMethods(parentContainer, new NameTypePattern("getLf", "Lf"));
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;
215 assertNotNull("BitLeaf TO wasn't found.", bitLeaf);
216 assertNotNull("UnionLeaf TO wasn't found.", unionLeaf);
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());
225 List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
226 GeneratedProperty firstBitProperty = null;
227 GeneratedProperty secondBitProperty = null;
228 GeneratedProperty thirdBitProperty = null;
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;
240 assertNotNull("firstBit property wasn't found", firstBitProperty);
241 assertNotNull("secondBit property wasn't found", secondBitProperty);
242 assertNotNull("thirdBit property wasn't found", thirdBitProperty);
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());
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;
262 assertNotNull("uint32 property wasn't found", uint32Property);
263 assertNotNull("string property wasn't found", stringProperty);
264 assertNotNull("uint8 property wasn't found", uint8Property);
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());