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.stmt.parser.retest;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.yangtools.sal.binding.generator.impl.stmt.parser.retest.SupportTestUtil.containsMethods;
13 import static org.opendaylight.yangtools.sal.binding.generator.impl.stmt.parser.retest.SupportTestUtil.containsAttributes;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
16 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
19 import java.io.IOException;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
26 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
27 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
28 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
29 import org.opendaylight.yangtools.sal.binding.model.api.Type;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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() throws IOException, SourceException, ReactorException {
40 final SchemaContext context = RetestUtils.parseYangSources(testModels);
42 assertNotNull(context);
43 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
44 genTypes = bindingGen.generateTypes(context);
46 for (Type type : genTypes) {
47 if (type instanceof GeneratedType) {
48 GeneratedType genType = (GeneratedType) type;
49 if (genType.getName().equals("ParentContainer") && !(genType instanceof GeneratedTransferObject)) {
50 parentContainer = genType;
57 public static void loadTestResources() throws IOException, URISyntaxException, SourceException, ReactorException {
58 final File listModelFile = new File(ExtendedTypedefTest.class.getResource("/bit_and_union.yang").toURI());
59 testModels.add(listModelFile);
64 public void testNestedTypesInLeaf() {
65 GeneratedTransferObject lfLeaf = null;
66 int lfLeafCounter = 0;
67 GeneratedTransferObject lf1Leaf = null;
68 int lf1LeafCounter = 0;
69 GeneratedTransferObject lf2Leaf = null;
70 int lf2LeafCounter = 0;
71 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
72 for (GeneratedType genType : enclosedTypes) {
73 if (genType instanceof GeneratedTransferObject) {
74 if (genType.getName().equals("Lf")) {
75 lfLeaf = (GeneratedTransferObject) genType;
77 } else if (genType.getName().equals("Lf1")) {
78 lf1Leaf = (GeneratedTransferObject) genType;
80 } else if (genType.getName().equals("Lf2")) {
81 lf2Leaf = (GeneratedTransferObject) genType;
88 // nested types in leaf, contains Lf?
89 assertNotNull("Lf TO wasn't found.", lfLeaf);
90 assertEquals("Lf TO has incorrect number of occurences.", 1, lfLeafCounter);
91 assertEquals("Lf has incorrect package name.",
92 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
93 lfLeaf.getPackageName());
95 assertEquals("Lf generated TO has incorrect number of properties", 3, lfLeaf.getProperties().size());
96 containsAttributes(lfLeaf, true, true, true, new NameTypePattern("string", "String"));
97 containsAttributes(lfLeaf, true, false, true, new NameTypePattern("lf1", "Lf1"));
99 // nested types in leaf, contains Lf1?
100 assertNotNull("Lf1 TO wasn't found.", lf1Leaf);
101 assertEquals("Lf1 TO has incorrect number of occurences.", 1, lf1LeafCounter);
102 assertEquals("Lf1 has incorrect package name.",
103 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
104 lf1Leaf.getPackageName());
106 assertEquals("Lf generated TO has incorrect number of properties", 4, lf1Leaf.getProperties().size());
107 containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("uint32", "Long"));
108 containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("int8", "Byte"));
109 containsAttributes(lf1Leaf, true, true, true, new NameTypePattern("string", "String"));
110 containsAttributes(lf1Leaf, true, false, true, new NameTypePattern("lf2", "Lf2"));
112 // nested types in leaf, contains Lf2?
113 assertNotNull("Lf2 TO wasn't found.", lf2Leaf);
114 assertEquals("Lf2 TO has incorrect number of occurences.", 1, lf2LeafCounter);
115 assertEquals("Lf2 has incorrect package name.",
116 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
117 lf2Leaf.getPackageName());
119 assertEquals("Lf generated TO has incorrect number of properties", 2, lf2Leaf.getProperties().size());
120 containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("string", "String"));
121 containsAttributes(lf2Leaf, true, true, true, new NameTypePattern("uint64", "BigInteger"));
125 public void testNestedTypesInTypedef() {
127 GeneratedTransferObject typeUnionTypedef = null;
128 int typeUnionTypedefCounter = 0;
130 for (Type type : genTypes) {
131 if (type instanceof GeneratedType) {
132 GeneratedType genType = (GeneratedType) type;
133 if (genType.getName().equals("TypeUnion") && (genType instanceof GeneratedTransferObject)) {
134 typeUnionTypedef = (GeneratedTransferObject) genType;
135 typeUnionTypedefCounter++;
140 assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
141 assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
143 assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
144 assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
145 assertEquals("TypeUnion has incorrect package name.",
146 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnionTypedef.getPackageName());
148 assertEquals("TypeUnion generated TO has incorrect number of properties", 3, typeUnionTypedef.getProperties()
150 containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
151 containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion1", "TypeUnion1"));
153 List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
154 assertEquals("Incorrect number of nested unions", 2, nestedUnions.size());
156 GeneratedTransferObject typeUnion1 = null;
157 int typeUnion1Counter = 0;
158 GeneratedTransferObject typeUnion2 = null;
159 int typeUnion2Counter = 0;
160 for (GeneratedType genType : nestedUnions) {
161 if (genType instanceof GeneratedTransferObject) {
162 if (genType.getName().equals("TypeUnion1")) {
163 typeUnion1 = (GeneratedTransferObject) genType;
165 } else if (genType.getName().equals("TypeUnion2")) {
166 typeUnion2 = (GeneratedTransferObject) genType;
172 assertNotNull("TypeUnion1 TO wasn't found.", typeUnion1);
173 assertEquals("TypeUnion1 TO has incorrect number of occurences.", 1, typeUnion1Counter);
175 assertEquals("TypeUnion1 has incorrect package name.",
176 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion1.getPackageName());
178 assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4, typeUnion1.getProperties().size());
180 containsAttributes(typeUnion1, true, true, true, new NameTypePattern("uint32", "Long"));
181 containsAttributes(typeUnion1, true, true, true, new NameTypePattern("int8", "Byte"));
182 containsAttributes(typeUnion1, true, true, true, new NameTypePattern("string", "String"));
183 containsAttributes(typeUnion1, true, false, true, new NameTypePattern("typeUnion2", "TypeUnion2"));
185 assertNotNull("TypeUnion2 TO wasn't found.", typeUnion2);
186 assertEquals("TypeUnion2 TO has incorrect number of occurences.", 1, typeUnion2Counter);
188 assertEquals("TypeUnion2 has incorrect package name.",
189 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion2.getPackageName());
191 assertEquals("TypeUnion2 generated TO has incorrect number of properties", 2, typeUnion2.getProperties().size());
192 containsAttributes(typeUnion2, true, true, true, new NameTypePattern("string", "String"));
193 containsAttributes(typeUnion2, true, true, true, new NameTypePattern("uint64", "BigInteger"));
198 public void bitAndUnionEnclosingTest() {
200 assertNotNull("Parent container object wasn't found.", parentContainer);
201 containsMethods(parentContainer, new NameTypePattern("getLf", "Lf"));
203 GeneratedTransferObject bitLeaf = null;
204 GeneratedTransferObject unionLeaf = null;
205 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
206 for (GeneratedType genType : enclosedTypes) {
207 if (genType instanceof GeneratedTransferObject) {
208 if (genType.getName().equals("BitLeaf")) {
209 bitLeaf = (GeneratedTransferObject) genType;
210 } else if (genType.getName().equals("UnionLeaf")) {
211 unionLeaf = (GeneratedTransferObject) genType;
216 assertNotNull("BitLeaf TO wasn't found.", bitLeaf);
217 assertNotNull("UnionLeaf TO wasn't found.", unionLeaf);
219 assertEquals("BitLeaf has incorrect package name.",
220 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
221 bitLeaf.getPackageName());
222 assertEquals("UnionLeaf has incorrect package name.",
223 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
224 bitLeaf.getPackageName());
226 List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
227 GeneratedProperty firstBitProperty = null;
228 GeneratedProperty secondBitProperty = null;
229 GeneratedProperty thirdBitProperty = null;
231 for (GeneratedProperty genProperty : propertiesBitLeaf) {
232 if (genProperty.getName().equals("firstBit")) {
233 firstBitProperty = genProperty;
234 } else if (genProperty.getName().equals("secondBit")) {
235 secondBitProperty = genProperty;
236 } else if (genProperty.getName().equals("thirdBit")) {
237 thirdBitProperty = genProperty;
241 assertNotNull("firstBit property wasn't found", firstBitProperty);
242 assertNotNull("secondBit property wasn't found", secondBitProperty);
243 assertNotNull("thirdBit property wasn't found", thirdBitProperty);
245 assertEquals("firstBit property has incorrect type", "Boolean", firstBitProperty.getReturnType().getName());
246 assertEquals("secondBit property has incorrect type", "Boolean", secondBitProperty.getReturnType().getName());
247 assertEquals("thirdBit property has incorrect type", "Boolean", thirdBitProperty.getReturnType().getName());
249 GeneratedProperty uint32Property = null;
250 GeneratedProperty stringProperty = null;
251 GeneratedProperty uint8Property = null;
252 List<GeneratedProperty> propertiesUnionLeaf = unionLeaf.getProperties();
253 for (GeneratedProperty genProperty : propertiesUnionLeaf) {
254 if (genProperty.getName().equals("int32")) {
255 uint32Property = genProperty;
256 } else if (genProperty.getName().equals("string")) {
257 stringProperty = genProperty;
258 } else if (genProperty.getName().equals("uint8")) {
259 uint8Property = genProperty;
263 assertNotNull("uint32 property wasn't found", uint32Property);
264 assertNotNull("string property wasn't found", stringProperty);
265 assertNotNull("uint8 property wasn't found", uint8Property);
267 assertEquals("uint32 property has incorrect type", "Integer", uint32Property.getReturnType().getName());
268 assertEquals("string property has incorrect type", "String", stringProperty.getReturnType().getName());
269 assertEquals("uint8 property has incorrect type", "Short", uint8Property.getReturnType().getName());