Merge "Fixed bug in code generator when handling case defined in grouping as augment...
[yangtools.git] / code-generator / binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / GeneratedTypesBitsTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.List;
18
19 import org.junit.Test;
20 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
25 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature.Parameter;
26 import org.opendaylight.yangtools.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28
29 public class GeneratedTypesBitsTest {
30
31
32     @Test
33     public void testGeneretedTypesBitsTest() throws URISyntaxException {
34         final URI yangTypesPath = getClass().getResource("/simple-bits-demo.yang").toURI();
35
36         final SchemaContext context = SupportTestUtil.resolveSchemaContextFromFiles(yangTypesPath);
37         assertTrue(context != null);
38
39         final BindingGenerator bindingGen = new BindingGeneratorImpl();
40         final List<Type> genTypes = bindingGen.generateTypes(context);
41         assertTrue(genTypes != null);
42
43         List<MethodSignature> methodSignaturesList = null;
44
45         boolean leafParentFound = false;
46
47         boolean byteTypeFound = false;
48         int classPropertiesNumb = 0;
49         int toStringPropertiesNum = 0;
50         int equalPropertiesNum = 0;
51         int hashPropertiesNum = 0;
52
53         String nameReturnParamType = "";
54         String nameMethodeParamType = "";
55         boolean getByteLeafMethodFound = false;
56         boolean setByteLeafMethodFound = false;
57         int setByteLeafMethodParamNum = 0;
58
59         for (final Type type : genTypes) {
60             if (type instanceof GeneratedTransferObject) { // searching for
61                                                            // ByteType
62                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
63                 if (genTO.getName().equals("ByteType")) {
64                     byteTypeFound = true;
65                     List<GeneratedProperty> genProperties = genTO.getProperties();
66                     classPropertiesNumb = genProperties.size();
67
68                     genProperties = null;
69                     genProperties = genTO.getToStringIdentifiers();
70                     toStringPropertiesNum = genProperties.size();
71
72                     genProperties = null;
73                     genProperties = genTO.getEqualsIdentifiers();
74                     equalPropertiesNum = genProperties.size();
75
76                     genProperties = null;
77                     genProperties = genTO.getHashCodeIdentifiers();
78                     hashPropertiesNum = genProperties.size();
79
80                 }
81             } else if (type instanceof GeneratedType) { // searching for
82                                                         // interface
83                                                         // LeafParameterContainer
84                 final GeneratedType genType = (GeneratedType) type;
85                 if (genType.getName().equals("LeafParentContainer")) {
86                     leafParentFound = true;
87                     // check of methods
88                     methodSignaturesList = genType.getMethodDefinitions();
89                     if (methodSignaturesList != null) {
90                         for (MethodSignature methodSignature : methodSignaturesList) { // loop
91                                                                                        // through
92                                                                                        // all
93                                                                                        // methods
94                             if (methodSignature.getName().equals("getByteLeaf")) {
95                                 getByteLeafMethodFound = true;
96
97                                 nameReturnParamType = methodSignature.getReturnType().getName();
98                             } else if (methodSignature.getName().equals("setByteLeaf")) {
99                                 setByteLeafMethodFound = true;
100
101                                 List<Parameter> parameters = methodSignature.getParameters();
102                                 setByteLeafMethodParamNum = parameters.size();
103                                 for (Parameter parameter : parameters) {
104                                     nameMethodeParamType = parameter.getType().getName();
105                                 }
106
107                             }
108
109                         }
110                     }
111                 }
112             }
113
114         }
115
116         assertTrue(byteTypeFound);
117
118         assertEquals(classPropertiesNumb,8);
119
120         assertEquals(toStringPropertiesNum,8);
121         assertEquals(equalPropertiesNum,8);
122         assertEquals(hashPropertiesNum,8);
123         assertTrue(leafParentFound);
124
125         assertNotNull(methodSignaturesList);
126
127         assertTrue(getByteLeafMethodFound);
128         assertEquals(nameReturnParamType,"ByteType");
129
130         assertFalse(setByteLeafMethodFound);
131         assertEquals(0, setByteLeafMethodParamNum);
132     }
133
134 }