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.controller.sal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
14 import java.util.ArrayList;
15 import java.util.List;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.controller.sal.binding.model.api.Type;
25 import org.opendaylight.controller.yang.model.api.Module;
26 import org.opendaylight.controller.yang.model.api.SchemaContext;
27 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
30 public class BitAndUnionTOEnclosingTest {
32 private final static List<File> testModels = new ArrayList<File>();
35 public static void loadTestResources() {
36 final File listModelFile = new File(ExtendedTypedefTest.class.getResource("/bit_and_union_in_leaf.yang")
38 testModels.add(listModelFile);
42 public void bitAndUnionEnclosingTest() {
43 final YangModelParser parser = new YangParserImpl();
44 final Set<Module> modules = parser.parseYangModels(testModels);
45 final SchemaContext context = parser.resolveSchemaContext(modules);
47 assertNotNull(context);
48 final BindingGenerator bindingGen = new BindingGeneratorImpl();
49 final List<Type> genTypes = bindingGen.generateTypes(context);
51 GeneratedType parentContainer = null;
53 for (Type type : genTypes) {
54 if ((type instanceof GeneratedType) && !(type instanceof GeneratedTransferObject)) {
55 GeneratedType genTO = (GeneratedType) type;
56 if (genTO.getName().equals("ParentContainer")) {
57 parentContainer = genTO;
63 assertNotNull("Parent container object wasn't found.", parentContainer);
65 GeneratedTransferObject bitLeaf = null;
66 GeneratedTransferObject unionLeaf = null;
67 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
68 for (GeneratedType genType : enclosedTypes) {
69 if (genType instanceof GeneratedTransferObject) {
70 if (genType.getName().equals("BitLeaf")) {
71 bitLeaf = (GeneratedTransferObject) genType;
72 } else if (genType.getName().equals("UnionLeaf")) {
73 unionLeaf = (GeneratedTransferObject) genType;
78 assertNotNull("BitLeaf TO builder wasn't found.", bitLeaf);
79 assertNotNull("UnionLeaf TO builder wasn't found.", unionLeaf);
81 assertEquals("BitLeaf has incorrect package name.",
82 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev2013626.ParentContainer",
83 bitLeaf.getPackageName());
84 assertEquals("UnionLeaf has incorrect package name.",
85 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev2013626.ParentContainer",
86 bitLeaf.getPackageName());
88 List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
89 GeneratedProperty firstBitProperty = null;
90 GeneratedProperty secondBitProperty = null;
91 GeneratedProperty thirdBitProperty = null;
92 for (GeneratedProperty genProperty : propertiesBitLeaf) {
93 if (genProperty.getName().equals("firstBit")) {
94 firstBitProperty = genProperty;
95 } else if (genProperty.getName().equals("secondBit")) {
96 secondBitProperty = genProperty;
97 } else if (genProperty.getName().equals("thirdBit")) {
98 thirdBitProperty = genProperty;
102 assertNotNull("firstBit property wasn't found", firstBitProperty);
103 assertNotNull("secondBit property wasn't found", secondBitProperty);
104 assertNotNull("thirdBit property wasn't found", thirdBitProperty);
106 assertEquals("firstBit property has incorrect type", "Boolean", firstBitProperty.getReturnType().getName());
107 assertEquals("secondBit property has incorrect type", "Boolean", secondBitProperty.getReturnType().getName());
108 assertEquals("thirdBit property has incorrect type", "Boolean", thirdBitProperty.getReturnType().getName());
110 GeneratedProperty uint32Property = null;
111 GeneratedProperty stringProperty = null;
112 GeneratedProperty uint8Property = null;
113 List<GeneratedProperty> propertiesUnionLeaf = unionLeaf.getProperties();
114 for (GeneratedProperty genProperty : propertiesUnionLeaf) {
115 if (genProperty.getName().equals("int32")) {
116 uint32Property = genProperty;
117 } else if (genProperty.getName().equals("string")) {
118 stringProperty = genProperty;
119 } else if (genProperty.getName().equals("uint8")) {
120 uint8Property = genProperty;
124 assertNotNull("uint32 property wasn't found", uint32Property);
125 assertNotNull("string property wasn't found", stringProperty);
126 assertNotNull("uint8 property wasn't found", uint8Property);
128 assertEquals("uint32 property has incorrect type", "Integer", uint32Property.getReturnType().getName());
129 assertEquals("string property has incorrect type", "String", stringProperty.getReturnType().getName());
130 assertEquals("uint8 property has incorrect type", "Short", uint8Property.getReturnType().getName());