2 * Copyright (c) 2016 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.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
27 public void testInnerClassCreationForBitsAndUnionsInLeafes() throws Exception {
28 final URI yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").toURI();
30 final SchemaContext context = YangParserTestUtils.parseYangSources(new File(yangTypesPath));
31 assertTrue(context != null);
33 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
34 final List<Type> genTypes = bindingGen.generateTypes(context);
35 assertTrue(genTypes != null);
37 boolean parentContainerFound = false;
38 boolean bitLeafTOFound = false;
39 boolean unionLeafTOFound = false;
41 boolean firstBitPropertyFound = false;
42 boolean secondBitPropertyFound = false;
43 boolean thirdBitPropertyFound = false;
45 boolean firstBitPropertyTypeOK = false;
46 boolean secondBitPropertyTypeOK = false;
47 boolean thirdBitPropertyTypeOK = false;
49 boolean int32UnionPropertyFound = false;
50 boolean int32UnionPropertyTypeOK = false;
51 boolean stringUnionPropertyFound = false;
52 boolean stringUnionPropertyTypeOK = false;
53 boolean uint8UnionPropertyFound = false;
54 boolean uint8UnionPropertyTypeOK = false;
56 for (Type type : genTypes) {
57 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
58 if (type.getName().equals("ParentContainer")) {
59 parentContainerFound = true;
60 GeneratedType parentContainer = (GeneratedType) type;
61 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
62 for (GeneratedType genType : enclosedTypes) {
63 if (genType instanceof GeneratedTransferObject) {
64 if (genType.getName().equals("BitLeaf")) {
65 bitLeafTOFound = true;
66 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
68 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
69 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
70 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
71 if (bitLeafProperty.getName().equals("firstBit")) {
72 firstBitPropertyFound = true;
73 if (bitLeafPropertyType.equals("Boolean")) {
74 firstBitPropertyTypeOK = true;
76 } else if (bitLeafProperty.getName().equals("secondBit")) {
77 secondBitPropertyFound = true;
78 if (bitLeafPropertyType.equals("Boolean")) {
79 secondBitPropertyTypeOK = true;
81 } else if (bitLeafProperty.getName().equals("thirdBit")) {
82 thirdBitPropertyFound = true;
83 if (bitLeafPropertyType.equals("Boolean")) {
84 thirdBitPropertyTypeOK = true;
90 } else if (genType.getName().equals("UnionLeaf")) {
91 unionLeafTOFound = true;
92 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
94 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
95 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
96 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
97 if (unionLeafProperty.getName().equals("int32")) {
98 int32UnionPropertyFound = true;
99 if (unionLeafPropertyType.equals("Integer")) {
100 int32UnionPropertyTypeOK = true;
102 } else if (unionLeafProperty.getName().equals("string")) {
103 stringUnionPropertyFound = true;
104 if (unionLeafPropertyType.equals("String")) {
105 stringUnionPropertyTypeOK = true;
107 } else if (unionLeafProperty.getName().equals("uint8")) {
108 uint8UnionPropertyFound = true;
109 if (unionLeafPropertyType.equals("Short")) {
110 uint8UnionPropertyTypeOK = true;
122 assertTrue(parentContainerFound);
124 assertTrue(bitLeafTOFound);
125 assertTrue(firstBitPropertyFound);
126 assertTrue(secondBitPropertyFound);
127 assertTrue(thirdBitPropertyFound);
129 assertTrue(firstBitPropertyTypeOK);
130 assertTrue(secondBitPropertyTypeOK);
131 assertTrue(thirdBitPropertyTypeOK);
133 assertTrue(unionLeafTOFound);
134 assertTrue(int32UnionPropertyFound);
135 assertTrue(int32UnionPropertyTypeOK);
136 assertTrue(stringUnionPropertyFound);
137 assertTrue(stringUnionPropertyTypeOK);
138 assertTrue(uint8UnionPropertyFound);
139 assertTrue(uint8UnionPropertyTypeOK);