2 * Copyright (c) 2014 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.assertTrue;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
18 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.yangtools.sal.binding.model.api.Type;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
25 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
29 private SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) throws Exception {
30 final YangContextParser parser = new YangParserImpl();
32 final List<File> inputFiles = new ArrayList<File>();
33 for (int i = 0; i < yangFiles.length; ++i) {
34 inputFiles.add(new File(yangFiles[i]));
37 return parser.parseFiles(inputFiles);
41 public void testInnerClassCreationForBitsAndUnionsInLeafes() throws Exception {
42 final URI yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").toURI();
44 final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
45 assertTrue(context != null);
47 final BindingGenerator bindingGen = new BindingGeneratorImpl();
48 final List<Type> genTypes = bindingGen.generateTypes(context);
49 assertTrue(genTypes != null);
51 boolean parentContainerFound = false;
52 boolean bitLeafTOFound = false;
53 boolean unionLeafTOFound = false;
55 boolean firstBitPropertyFound = false;
56 boolean secondBitPropertyFound = false;
57 boolean thirdBitPropertyFound = false;
59 boolean firstBitPropertyTypeOK = false;
60 boolean secondBitPropertyTypeOK = false;
61 boolean thirdBitPropertyTypeOK = false;
63 boolean int32UnionPropertyFound = false;
64 boolean int32UnionPropertyTypeOK = false;
65 boolean stringUnionPropertyFound = false;
66 boolean stringUnionPropertyTypeOK = false;
67 boolean uint8UnionPropertyFound = false;
68 boolean uint8UnionPropertyTypeOK = false;
70 for (Type type : genTypes) {
71 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
72 if (type.getName().equals("ParentContainer")) {
73 parentContainerFound = true;
74 GeneratedType parentContainer = (GeneratedType) type;
75 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
76 for (GeneratedType genType : enclosedTypes) {
77 if (genType instanceof GeneratedTransferObject) {
78 if (genType.getName().equals("BitLeaf")) {
79 bitLeafTOFound = true;
80 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
82 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
83 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
84 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
85 if (bitLeafProperty.getName().equals("firstBit")) {
86 firstBitPropertyFound = true;
87 if (bitLeafPropertyType.equals("Boolean")) {
88 firstBitPropertyTypeOK = true;
90 } else if (bitLeafProperty.getName().equals("secondBit")) {
91 secondBitPropertyFound = true;
92 if (bitLeafPropertyType.equals("Boolean")) {
93 secondBitPropertyTypeOK = true;
95 } else if (bitLeafProperty.getName().equals("thirdBit")) {
96 thirdBitPropertyFound = true;
97 if (bitLeafPropertyType.equals("Boolean")) {
98 thirdBitPropertyTypeOK = true;
104 } else if (genType.getName().equals("UnionLeaf")) {
105 unionLeafTOFound = true;
106 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
108 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
109 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
110 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
111 if (unionLeafProperty.getName().equals("int32")) {
112 int32UnionPropertyFound = true;
113 if (unionLeafPropertyType.equals("Integer")) {
114 int32UnionPropertyTypeOK = true;
116 } else if (unionLeafProperty.getName().equals("string")) {
117 stringUnionPropertyFound = true;
118 if (unionLeafPropertyType.equals("String")) {
119 stringUnionPropertyTypeOK = true;
121 } else if (unionLeafProperty.getName().equals("uint8")) {
122 uint8UnionPropertyFound = true;
123 if (unionLeafPropertyType.equals("Short")) {
124 uint8UnionPropertyTypeOK = true;
136 assertTrue(parentContainerFound);
138 assertTrue(bitLeafTOFound);
139 assertTrue(firstBitPropertyFound);
140 assertTrue(secondBitPropertyFound);
141 assertTrue(thirdBitPropertyFound);
143 assertTrue(firstBitPropertyTypeOK);
144 assertTrue(secondBitPropertyTypeOK);
145 assertTrue(thirdBitPropertyTypeOK);
147 assertTrue(unionLeafTOFound);
148 assertTrue(int32UnionPropertyFound);
149 assertTrue(int32UnionPropertyTypeOK);
150 assertTrue(stringUnionPropertyFound);
151 assertTrue(stringUnionPropertyTypeOK);
152 assertTrue(uint8UnionPropertyFound);
153 assertTrue(uint8UnionPropertyTypeOK);