Cleaned up Java Binding code from YANG Tools
[mdsal.git] / binding / mdsal-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.util.List;
17
18 import org.junit.Test;
19 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
24 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature.Parameter;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27
28 public class GeneratedTypesBitsTest {
29
30
31     @Test
32     public void testGeneretedTypesBitsTest() throws Exception {
33         final URI yangTypesPath = getClass().getResource("/simple-bits-demo.yang").toURI();
34
35         final SchemaContext context = SupportTestUtil.resolveSchemaContextFromFiles(yangTypesPath);
36         assertTrue(context != null);
37
38         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
39         final List<Type> genTypes = bindingGen.generateTypes(context);
40         assertTrue(genTypes != null);
41
42         List<MethodSignature> methodSignaturesList = null;
43
44         boolean leafParentFound = false;
45
46         boolean byteTypeFound = false;
47         int classPropertiesNumb = 0;
48         int toStringPropertiesNum = 0;
49         int equalPropertiesNum = 0;
50         int hashPropertiesNum = 0;
51
52         String nameReturnParamType = "";
53         boolean getByteLeafMethodFound = false;
54         boolean setByteLeafMethodFound = false;
55         int setByteLeafMethodParamNum = 0;
56
57         for (final Type type : genTypes) {
58             if (type instanceof GeneratedTransferObject) { // searching for
59                                                            // ByteType
60                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
61                 if (genTO.getName().equals("ByteType")) {
62                     byteTypeFound = true;
63                     List<GeneratedProperty> genProperties = genTO.getProperties();
64                     classPropertiesNumb = genProperties.size();
65
66                     genProperties = null;
67                     genProperties = genTO.getToStringIdentifiers();
68                     toStringPropertiesNum = genProperties.size();
69
70                     genProperties = null;
71                     genProperties = genTO.getEqualsIdentifiers();
72                     equalPropertiesNum = genProperties.size();
73
74                     genProperties = null;
75                     genProperties = genTO.getHashCodeIdentifiers();
76                     hashPropertiesNum = genProperties.size();
77
78                 }
79             } else if (type instanceof GeneratedType) { // searching for
80                                                         // interface
81                                                         // LeafParameterContainer
82                 final GeneratedType genType = (GeneratedType) type;
83                 if (genType.getName().equals("LeafParentContainer")) {
84                     leafParentFound = true;
85                     // check of methods
86                     methodSignaturesList = genType.getMethodDefinitions();
87                     if (methodSignaturesList != null) {
88                         for (MethodSignature methodSignature : methodSignaturesList) { // loop
89                                                                                        // through
90                                                                                        // all
91                                                                                        // methods
92                             if (methodSignature.getName().equals("getByteLeaf")) {
93                                 getByteLeafMethodFound = true;
94
95                                 nameReturnParamType = methodSignature.getReturnType().getName();
96                             } else if (methodSignature.getName().equals("setByteLeaf")) {
97                                 setByteLeafMethodFound = true;
98
99                                 List<Parameter> parameters = methodSignature.getParameters();
100                                 setByteLeafMethodParamNum = parameters.size();
101                             }
102
103                         }
104                     }
105                 }
106             }
107
108         }
109
110         assertTrue(byteTypeFound);
111
112         assertEquals(classPropertiesNumb,8);
113
114         assertEquals(toStringPropertiesNum,8);
115         assertEquals(equalPropertiesNum,8);
116         assertEquals(hashPropertiesNum,8);
117         assertTrue(leafParentFound);
118
119         assertNotNull(methodSignaturesList);
120
121         assertTrue(getByteLeafMethodFound);
122         assertEquals(nameReturnParamType,"ByteType");
123
124         assertFalse(setByteLeafMethodFound);
125         assertEquals(0, setByteLeafMethodParamNum);
126     }
127
128 }