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.java.api.generator.test;
10 import static org.junit.Assert.assertTrue;
14 import java.net.URISyntaxException;
15 import java.util.ArrayList;
16 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
31 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
33 private SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) {
34 final YangModelParser parser = new YangParserImpl();
36 final List<File> inputFiles = new ArrayList<File>();
37 for (int i = 0; i < yangFiles.length; ++i) {
38 inputFiles.add(new File(yangFiles[i]));
41 final Set<Module> modules = parser.parseYangModels(inputFiles);
42 return parser.resolveSchemaContext(modules);
46 public void testInnerClassCreationForBitsAndUnionsInLeafes() throws URISyntaxException {
47 final URI yangTypesPath = getClass().getResource("/bit_and_union_in_leaf.yang").toURI();
49 final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
50 assertTrue(context != null);
52 final BindingGenerator bindingGen = new BindingGeneratorImpl();
53 final List<Type> genTypes = bindingGen.generateTypes(context);
54 assertTrue(genTypes != null);
56 boolean parentContainerFound = false;
57 boolean bitLeafTOFound = false;
58 boolean unionLeafTOFound = false;
60 boolean firstBitPropertyFound = false;
61 boolean secondBitPropertyFound = false;
62 boolean thirdBitPropertyFound = false;
64 boolean firstBitPropertyTypeOK = false;
65 boolean secondBitPropertyTypeOK = false;
66 boolean thirdBitPropertyTypeOK = false;
68 boolean int32UnionPropertyFound = false;
69 boolean int32UnionPropertyTypeOK = false;
70 boolean stringUnionPropertyFound = false;
71 boolean stringUnionPropertyTypeOK = false;
72 boolean uint8UnionPropertyFound = false;
73 boolean uint8UnionPropertyTypeOK = false;
75 for (Type type : genTypes) {
76 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
77 if (type.getName().equals("ParentContainer")) {
78 parentContainerFound = true;
79 GeneratedType parentContainer = (GeneratedType) type;
80 List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
81 for (GeneratedType genType : enclosedTypes) {
82 if (genType instanceof GeneratedTransferObject) {
83 if (genType.getName().equals("BitLeaf")) {
84 bitLeafTOFound = true;
85 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
87 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
88 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
89 String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
90 if (bitLeafProperty.getName().equals("firstBit")) {
91 firstBitPropertyFound = true;
92 if (bitLeafPropertyType.equals("Boolean")) {
93 firstBitPropertyTypeOK = true;
95 } else if (bitLeafProperty.getName().equals("secondBit")) {
96 secondBitPropertyFound = true;
97 if (bitLeafPropertyType.equals("Boolean")) {
98 secondBitPropertyTypeOK = true;
100 } else if (bitLeafProperty.getName().equals("thirdBit")) {
101 thirdBitPropertyFound = true;
102 if (bitLeafPropertyType.equals("Boolean")) {
103 thirdBitPropertyTypeOK = true;
109 } else if (genType.getName().equals("UnionLeaf")) {
110 unionLeafTOFound = true;
111 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
113 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
114 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
115 String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
116 if (unionLeafProperty.getName().equals("int32")) {
117 int32UnionPropertyFound = true;
118 if (unionLeafPropertyType.equals("Integer")) {
119 int32UnionPropertyTypeOK = true;
121 } else if (unionLeafProperty.getName().equals("string")) {
122 stringUnionPropertyFound = true;
123 if (unionLeafPropertyType.equals("String")) {
124 stringUnionPropertyTypeOK = true;
126 } else if (unionLeafProperty.getName().equals("uint8")) {
127 uint8UnionPropertyFound = true;
128 if (unionLeafPropertyType.equals("Short")) {
129 uint8UnionPropertyTypeOK = true;
141 assertTrue(parentContainerFound);
143 assertTrue(bitLeafTOFound);
144 assertTrue(firstBitPropertyFound);
145 assertTrue(secondBitPropertyFound);
146 assertTrue(thirdBitPropertyFound);
148 assertTrue(firstBitPropertyTypeOK);
149 assertTrue(secondBitPropertyTypeOK);
150 assertTrue(thirdBitPropertyTypeOK);
152 assertTrue(unionLeafTOFound);
153 assertTrue(int32UnionPropertyFound);
154 assertTrue(int32UnionPropertyTypeOK);
155 assertTrue(stringUnionPropertyFound);
156 assertTrue(stringUnionPropertyTypeOK);
157 assertTrue(uint8UnionPropertyFound);
158 assertTrue(uint8UnionPropertyTypeOK);