1 package org.opendaylight.controller.sal.binding.generator.impl;
3 import static org.junit.Assert.assertTrue;
6 import java.util.ArrayList;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
12 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
13 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
14 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
15 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
16 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
17 import org.opendaylight.controller.sal.binding.model.api.Type;
18 import org.opendaylight.controller.yang.model.api.Module;
19 import org.opendaylight.controller.yang.model.api.SchemaContext;
20 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
21 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
23 public class GeneretedTypesBitsTest {
25 private SchemaContext resolveSchemaContextFromFiles(
26 final String... yangFiles) {
27 final YangModelParser parser = new YangParserImpl();
29 final List<File> inputFiles = new ArrayList<File>();
30 for (int i = 0; i < yangFiles.length; ++i) {
31 inputFiles.add(new File(yangFiles[i]));
34 final Set<Module> modules = parser.parseYangModels(inputFiles);
35 return parser.resolveSchemaContext(modules);
39 public void testGeneretedTypesBitsTest() {
40 final String yangTypesPath = getClass().getResource(
41 "/simple-bits-demo.yang").getPath();
43 final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
44 assertTrue(context != null);
46 final BindingGenerator bindingGen = new BindingGeneratorImpl();
47 final List<Type> genTypes = bindingGen.generateTypes(context);
48 assertTrue(genTypes != null);
50 List<MethodSignature> methodSignaturesList = null;
52 boolean leafParentFound = false;
54 boolean byteTypeFound = false;
55 int classPropertiesNumb = 0;
56 int toStringPropertiesNum = 0;
57 int equalPropertiesNum = 0;
58 int hashPropertiesNum = 0;
60 String nameReturnParamType = "";
61 String nameMethodeParamType = "";
62 boolean getByteLeafMethodFound = false;
63 boolean setByteLeafMethodFound = false;
64 int setByteLeafMethodParamNum = 0;
66 for (final Type type : genTypes) {
67 if (type instanceof GeneratedTransferObject) { // searching for
69 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
70 if (genTO.getName().equals("ByteType")) {
72 List<GeneratedProperty> genProperties = genTO
74 classPropertiesNumb = genProperties.size();
77 genProperties = genTO.getToStringIdentifiers();
78 toStringPropertiesNum = genProperties.size();
81 genProperties = genTO.getEqualsIdentifiers();
82 equalPropertiesNum = genProperties.size();
85 genProperties = genTO.getHashCodeIdentifiers();
86 hashPropertiesNum = genProperties.size();
89 } else if (type instanceof GeneratedType) { // searching for
91 // LeafParameterContainer
92 final GeneratedType genType = (GeneratedType) type;
93 if (genType.getName().compareTo("LeafParentContainer") == 0) {
94 leafParentFound = true;
96 methodSignaturesList = genType.getMethodDefinitions();
97 if (methodSignaturesList != null) {
98 for (MethodSignature methodSignature : methodSignaturesList) { // loop
102 if (methodSignature.getName().compareTo(
103 "getByteLeaf") == 0) {
104 getByteLeafMethodFound = true;
106 nameReturnParamType = methodSignature
107 .getReturnType().getName();
108 } else if (methodSignature.getName().compareTo(
109 "setByteLeaf") == 0) {
110 setByteLeafMethodFound = true;
112 List<Parameter> parameters = methodSignature
114 setByteLeafMethodParamNum = parameters.size();
115 for (Parameter parameter : parameters) {
116 nameMethodeParamType = parameter.getType()
129 assertTrue("type >ByteType< wasn't in YANG file", byteTypeFound == true);
131 assertTrue("Incorrect number of bit properties in class",
132 classPropertiesNumb == 8);
134 assertTrue("Incorrect number of properties in toString method",
135 toStringPropertiesNum == 8);
136 assertTrue("Incorrect number of properties in equals method",
137 equalPropertiesNum == 8);
138 assertTrue("Incorrect number of properties in hash method",
139 hashPropertiesNum == 8);
140 assertTrue("LeafParameterContainer container wasn't found",
141 leafParentFound == true);
143 assertTrue("No methods were generated", methodSignaturesList != null);
145 assertTrue("Method getByteLeaf wasn't generated.",
146 getByteLeafMethodFound == true);
147 assertTrue("Wrong returning parameter type of method getByteLeaf",
148 nameReturnParamType.compareTo("ByteType") == 0);
150 assertTrue("Method setByteLeaf wasn't generated.",
151 setByteLeafMethodFound == true);
152 assertTrue("Incorrect number of input parameters for setByteLeaf",
153 setByteLeafMethodParamNum == 1);
154 assertTrue("Wrong input parameter type",
155 nameMethodeParamType.compareTo("ByteType") == 0);