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.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;
23 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
26 public void testInnerClassCreationForBitsAndUnionsInLeafes() throws Exception {
27 final URI yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").toURI();
29 final SchemaContext context = TestUtils.parseYangSources(new File(yangTypesPath));
30 assertTrue(context != null);
32 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
33 final List<Type> genTypes = bindingGen.generateTypes(context);
34 assertTrue(genTypes != null);
36 boolean parentContainerFound = false;
37 boolean bitLeafTOFound = false;
38 boolean unionLeafTOFound = false;
40 boolean firstBitPropertyFound = false;
41 boolean secondBitPropertyFound = false;
42 boolean thirdBitPropertyFound = false;
44 boolean firstBitPropertyTypeOK = false;
45 boolean secondBitPropertyTypeOK = false;
46 boolean thirdBitPropertyTypeOK = false;
48 boolean int32UnionPropertyFound = false;
49 boolean int32UnionPropertyTypeOK = false;
50 boolean stringUnionPropertyFound = false;
51 boolean stringUnionPropertyTypeOK = false;
52 boolean uint8UnionPropertyFound = false;
53 boolean uint8UnionPropertyTypeOK = false;
55 for (Type type : genTypes) {
56 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
57 if (type.getName().equals("ParentContainer")) {
58 parentContainerFound = true;
59 GeneratedType parentContainer = (GeneratedType) type;
60 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
61 for (GeneratedType genType : enclosedTypes) {
62 if (genType instanceof GeneratedTransferObject) {
63 if (genType.getName().equals("BitLeaf")) {
64 bitLeafTOFound = true;
65 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
67 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
68 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
69 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
70 if (bitLeafProperty.getName().equals("firstBit")) {
71 firstBitPropertyFound = true;
72 if (bitLeafPropertyType.equals("Boolean")) {
73 firstBitPropertyTypeOK = true;
75 } else if (bitLeafProperty.getName().equals("secondBit")) {
76 secondBitPropertyFound = true;
77 if (bitLeafPropertyType.equals("Boolean")) {
78 secondBitPropertyTypeOK = true;
80 } else if (bitLeafProperty.getName().equals("thirdBit")) {
81 thirdBitPropertyFound = true;
82 if (bitLeafPropertyType.equals("Boolean")) {
83 thirdBitPropertyTypeOK = true;
89 } else if (genType.getName().equals("UnionLeaf")) {
90 unionLeafTOFound = true;
91 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
93 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
94 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
95 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
96 if (unionLeafProperty.getName().equals("int32")) {
97 int32UnionPropertyFound = true;
98 if (unionLeafPropertyType.equals("Integer")) {
99 int32UnionPropertyTypeOK = true;
101 } else if (unionLeafProperty.getName().equals("string")) {
102 stringUnionPropertyFound = true;
103 if (unionLeafPropertyType.equals("String")) {
104 stringUnionPropertyTypeOK = true;
106 } else if (unionLeafProperty.getName().equals("uint8")) {
107 uint8UnionPropertyFound = true;
108 if (unionLeafPropertyType.equals("Short")) {
109 uint8UnionPropertyTypeOK = true;
121 assertTrue(parentContainerFound);
123 assertTrue(bitLeafTOFound);
124 assertTrue(firstBitPropertyFound);
125 assertTrue(secondBitPropertyFound);
126 assertTrue(thirdBitPropertyFound);
128 assertTrue(firstBitPropertyTypeOK);
129 assertTrue(secondBitPropertyTypeOK);
130 assertTrue(thirdBitPropertyTypeOK);
132 assertTrue(unionLeafTOFound);
133 assertTrue(int32UnionPropertyFound);
134 assertTrue(int32UnionPropertyTypeOK);
135 assertTrue(stringUnionPropertyFound);
136 assertTrue(stringUnionPropertyTypeOK);
137 assertTrue(uint8UnionPropertyFound);
138 assertTrue(uint8UnionPropertyTypeOK);