646efd1a78c58734ce846bb50703a885a803142c
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / generator / impl / GeneretedTypesBitsTest.java
1 package org.opendaylight.controller.sal.binding.generator.impl;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.io.File;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Set;
9
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;
22
23 public class GeneretedTypesBitsTest {
24
25     private SchemaContext resolveSchemaContextFromFiles(
26             final String... yangFiles) {
27         final YangModelParser parser = new YangParserImpl();
28
29         final List<File> inputFiles = new ArrayList<File>();
30         for (int i = 0; i < yangFiles.length; ++i) {
31             inputFiles.add(new File(yangFiles[i]));
32         }
33
34         final Set<Module> modules = parser.parseYangModels(inputFiles);
35         return parser.resolveSchemaContext(modules);
36     }
37
38     @Test
39     public void testGeneretedTypesBitsTest() {
40         final String yangTypesPath = getClass().getResource(
41                 "/simple-bits-demo.yang").getPath();
42
43         final SchemaContext context = resolveSchemaContextFromFiles(yangTypesPath);
44         assertTrue(context != null);
45
46         final BindingGenerator bindingGen = new BindingGeneratorImpl();
47         final List<Type> genTypes = bindingGen.generateTypes(context);
48         assertTrue(genTypes != null);
49
50         List<MethodSignature> methodSignaturesList = null;
51
52         boolean leafParentFound = false;
53
54         boolean byteTypeFound = false;
55         int classPropertiesNumb = 0;
56         int toStringPropertiesNum = 0;
57         int equalPropertiesNum = 0;
58         int hashPropertiesNum = 0;
59
60         String nameReturnParamType = "";
61         String nameMethodeParamType = "";
62         boolean getByteLeafMethodFound = false;
63         boolean setByteLeafMethodFound = false;
64         int setByteLeafMethodParamNum = 0;
65
66         for (final Type type : genTypes) {
67             if (type instanceof GeneratedTransferObject) { // searching for
68                                                             // ByteType
69                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
70                 if (genTO.getName().equals("ByteType")) {
71                     byteTypeFound = true;
72                     List<GeneratedProperty> genProperties = genTO
73                             .getProperties();
74                     classPropertiesNumb = genProperties.size();
75
76                     genProperties = null;
77                     genProperties = genTO.getToStringIdentifiers();
78                     toStringPropertiesNum = genProperties.size();
79
80                     genProperties = null;
81                     genProperties = genTO.getEqualsIdentifiers();
82                     equalPropertiesNum = genProperties.size();
83
84                     genProperties = null;
85                     genProperties = genTO.getHashCodeIdentifiers();
86                     hashPropertiesNum = genProperties.size();
87
88                 }
89             } else if (type instanceof GeneratedType) { // searching for
90                                                         // interface
91                                                         // LeafParameterContainer
92                 final GeneratedType genType = (GeneratedType) type;
93                 if (genType.getName().compareTo("LeafParentContainer") == 0) {
94                     leafParentFound = true;
95                     // check of methods
96                     methodSignaturesList = genType.getMethodDefinitions();
97                     if (methodSignaturesList != null) {
98                         for (MethodSignature methodSignature : methodSignaturesList) { // loop
99                                                                                         // through
100                                                                                         // all
101                                                                                         // methods
102                             if (methodSignature.getName().compareTo(
103                                     "getByteLeaf") == 0) {
104                                 getByteLeafMethodFound = true;
105
106                                 nameReturnParamType = methodSignature
107                                         .getReturnType().getName();
108                             } else if (methodSignature.getName().compareTo(
109                                     "setByteLeaf") == 0) {
110                                 setByteLeafMethodFound = true;
111
112                                 List<Parameter> parameters = methodSignature
113                                         .getParameters();
114                                 setByteLeafMethodParamNum = parameters.size();
115                                 for (Parameter parameter : parameters) {
116                                     nameMethodeParamType = parameter.getType()
117                                             .getName();
118                                 }
119
120                             }
121
122                         }
123                     }
124                 }
125             }
126
127         }
128
129         assertTrue("type >ByteType< wasn't in YANG file", byteTypeFound == true);
130
131         assertTrue("Incorrect number of bit properties in class",
132                 classPropertiesNumb == 8);
133
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);
142
143         assertTrue("No methods were generated", methodSignaturesList != null);
144
145         assertTrue("Method getByteLeaf wasn't generated.",
146                 getByteLeafMethodFound == true);
147         assertTrue("Wrong returning parameter type of method getByteLeaf",
148                 nameReturnParamType.compareTo("ByteType") == 0);
149
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);
156
157     }
158
159 }