Merge "Added StatusCode to Response.Status Mapping"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / generator / impl / GeneratedTypesBitsTest.java
1 package org.opendaylight.controller.sal.binding.generator.impl;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.File;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Set;
11
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
14 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
15 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
16 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
17 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
18 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
19 import org.opendaylight.controller.sal.binding.model.api.Type;
20 import org.opendaylight.controller.yang.model.api.Module;
21 import org.opendaylight.controller.yang.model.api.SchemaContext;
22 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
23 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
24
25 public class GeneratedTypesBitsTest {
26
27     private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
28         final YangModelParser parser = new YangParserImpl();
29
30         final List<File> inputFiles = new ArrayList<File>();
31         for (int i = 0; i < yangFiles.length; ++i) {
32             inputFiles.add(new File(yangFiles[i]));
33         }
34
35         final Set<Module> modules = parser.parseYangModels(inputFiles);
36         return parser.resolveSchemaContext(modules);
37     }
38
39     @Test
40     public void testGeneretedTypesBitsTest() {
41         final String yangTypesPath = getClass().getResource("/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.getProperties();
73                     classPropertiesNumb = genProperties.size();
74
75                     genProperties = null;
76                     genProperties = genTO.getToStringIdentifiers();
77                     toStringPropertiesNum = genProperties.size();
78
79                     genProperties = null;
80                     genProperties = genTO.getEqualsIdentifiers();
81                     equalPropertiesNum = genProperties.size();
82
83                     genProperties = null;
84                     genProperties = genTO.getHashCodeIdentifiers();
85                     hashPropertiesNum = genProperties.size();
86
87                 }
88             } else if (type instanceof GeneratedType) { // searching for
89                                                         // interface
90                                                         // LeafParameterContainer
91                 final GeneratedType genType = (GeneratedType) type;
92                 if (genType.getName().equals("LeafParentContainer")) {
93                     leafParentFound = true;
94                     // check of methods
95                     methodSignaturesList = genType.getMethodDefinitions();
96                     if (methodSignaturesList != null) {
97                         for (MethodSignature methodSignature : methodSignaturesList) { // loop
98                                                                                        // through
99                                                                                        // all
100                                                                                        // methods
101                             if (methodSignature.getName().equals("getByteLeaf")) {
102                                 getByteLeafMethodFound = true;
103
104                                 nameReturnParamType = methodSignature.getReturnType().getName();
105                             } else if (methodSignature.getName().equals("setByteLeaf")) {
106                                 setByteLeafMethodFound = true;
107
108                                 List<Parameter> parameters = methodSignature.getParameters();
109                                 setByteLeafMethodParamNum = parameters.size();
110                                 for (Parameter parameter : parameters) {
111                                     nameMethodeParamType = parameter.getType().getName();
112                                 }
113
114                             }
115
116                         }
117                     }
118                 }
119             }
120
121         }
122
123         assertTrue(byteTypeFound);
124
125         assertEquals(classPropertiesNumb,8);
126
127         assertEquals(toStringPropertiesNum,8);
128         assertEquals(equalPropertiesNum,8);
129         assertEquals(hashPropertiesNum,8);
130         assertTrue(leafParentFound);
131
132         assertNotNull(methodSignaturesList);
133
134         assertTrue(getByteLeafMethodFound);
135         assertEquals(nameReturnParamType,"ByteType");
136
137         assertTrue(setByteLeafMethodFound);
138         assertEquals(setByteLeafMethodParamNum,1);
139         assertEquals(nameMethodeParamType,"ByteType");
140
141     }
142
143 }