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;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
17 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
23 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
25 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
27 private static SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) throws Exception {
28 final YangContextParser parser = new YangParserImpl();
30 final List<File> inputFiles = new ArrayList<File>();
31 for (URI yangFile : yangFiles) {
32 inputFiles.add(new File(yangFile));
35 return parser.parseFiles(inputFiles);
39 public void testInnerClassCreationForBitsAndUnionsInLeafes() throws Exception {
40 final URI yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").toURI();
42 final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
43 assertTrue(context != null);
45 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
46 final List<Type> genTypes = bindingGen.generateTypes(context);
47 assertTrue(genTypes != null);
49 boolean parentContainerFound = false;
50 boolean bitLeafTOFound = false;
51 boolean unionLeafTOFound = false;
53 boolean firstBitPropertyFound = false;
54 boolean secondBitPropertyFound = false;
55 boolean thirdBitPropertyFound = false;
57 boolean firstBitPropertyTypeOK = false;
58 boolean secondBitPropertyTypeOK = false;
59 boolean thirdBitPropertyTypeOK = false;
61 boolean int32UnionPropertyFound = false;
62 boolean int32UnionPropertyTypeOK = false;
63 boolean stringUnionPropertyFound = false;
64 boolean stringUnionPropertyTypeOK = false;
65 boolean uint8UnionPropertyFound = false;
66 boolean uint8UnionPropertyTypeOK = false;
68 for (Type type : genTypes) {
69 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
70 if (type.getName().equals("ParentContainer")) {
71 parentContainerFound = true;
72 GeneratedType parentContainer = (GeneratedType) type;
73 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
74 for (GeneratedType genType : enclosedTypes) {
75 if (genType instanceof GeneratedTransferObject) {
76 if (genType.getName().equals("BitLeaf")) {
77 bitLeafTOFound = true;
78 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
80 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
81 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
82 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
83 if (bitLeafProperty.getName().equals("firstBit")) {
84 firstBitPropertyFound = true;
85 if (bitLeafPropertyType.equals("Boolean")) {
86 firstBitPropertyTypeOK = true;
88 } else if (bitLeafProperty.getName().equals("secondBit")) {
89 secondBitPropertyFound = true;
90 if (bitLeafPropertyType.equals("Boolean")) {
91 secondBitPropertyTypeOK = true;
93 } else if (bitLeafProperty.getName().equals("thirdBit")) {
94 thirdBitPropertyFound = true;
95 if (bitLeafPropertyType.equals("Boolean")) {
96 thirdBitPropertyTypeOK = true;
102 } else if (genType.getName().equals("UnionLeaf")) {
103 unionLeafTOFound = true;
104 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
106 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
107 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
108 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
109 if (unionLeafProperty.getName().equals("int32")) {
110 int32UnionPropertyFound = true;
111 if (unionLeafPropertyType.equals("Integer")) {
112 int32UnionPropertyTypeOK = true;
114 } else if (unionLeafProperty.getName().equals("string")) {
115 stringUnionPropertyFound = true;
116 if (unionLeafPropertyType.equals("String")) {
117 stringUnionPropertyTypeOK = true;
119 } else if (unionLeafProperty.getName().equals("uint8")) {
120 uint8UnionPropertyFound = true;
121 if (unionLeafPropertyType.equals("Short")) {
122 uint8UnionPropertyTypeOK = true;
134 assertTrue(parentContainerFound);
136 assertTrue(bitLeafTOFound);
137 assertTrue(firstBitPropertyFound);
138 assertTrue(secondBitPropertyFound);
139 assertTrue(thirdBitPropertyFound);
141 assertTrue(firstBitPropertyTypeOK);
142 assertTrue(secondBitPropertyTypeOK);
143 assertTrue(thirdBitPropertyTypeOK);
145 assertTrue(unionLeafTOFound);
146 assertTrue(int32UnionPropertyFound);
147 assertTrue(int32UnionPropertyTypeOK);
148 assertTrue(stringUnionPropertyFound);
149 assertTrue(stringUnionPropertyTypeOK);
150 assertTrue(uint8UnionPropertyFound);
151 assertTrue(uint8UnionPropertyTypeOK);