Bug 6859 #2 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / GeneratedTypesBitsTest.java
1 /*
2  * Copyright (c) 2016 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.io.File;
16 import java.net.URI;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.mdsal.binding.model.api.MethodSignature.Parameter;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class GeneratedTypesBitsTest {
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 = YangParserTestUtils.parseYangSources(new File(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 = genTO.getToStringIdentifiers();
67                     toStringPropertiesNum = genProperties.size();
68
69                     genProperties = genTO.getEqualsIdentifiers();
70                     equalPropertiesNum = genProperties.size();
71
72                     genProperties = genTO.getHashCodeIdentifiers();
73                     hashPropertiesNum = genProperties.size();
74
75                 }
76             } else if (type instanceof GeneratedType) { // searching for
77                                                         // interface
78                                                         // LeafParameterContainer
79                 final GeneratedType genType = (GeneratedType) type;
80                 if (genType.getName().equals("LeafParentContainer")) {
81                     leafParentFound = true;
82                     // check of methods
83                     methodSignaturesList = genType.getMethodDefinitions();
84                     if (methodSignaturesList != null) {
85                         for (MethodSignature methodSignature : methodSignaturesList) { // loop
86                                                                                        // through
87                                                                                        // all
88                                                                                        // methods
89                             if (methodSignature.getName().equals("getByteLeaf")) {
90                                 getByteLeafMethodFound = true;
91
92                                 nameReturnParamType = methodSignature.getReturnType().getName();
93                             } else if (methodSignature.getName().equals("setByteLeaf")) {
94                                 setByteLeafMethodFound = true;
95
96                                 List<Parameter> parameters = methodSignature.getParameters();
97                                 setByteLeafMethodParamNum = parameters.size();
98                             }
99
100                         }
101                     }
102                 }
103             }
104
105         }
106
107         assertTrue(byteTypeFound);
108
109         assertEquals(8, classPropertiesNumb);
110
111         assertEquals(8, toStringPropertiesNum);
112         assertEquals(8, equalPropertiesNum);
113         assertEquals(8, hashPropertiesNum);
114         assertTrue(leafParentFound);
115
116         assertNotNull(methodSignaturesList);
117
118         assertTrue(getByteLeafMethodFound);
119         assertEquals("ByteType", nameReturnParamType);
120
121         assertFalse(setByteLeafMethodFound);
122         assertEquals(0, setByteLeafMethodParamNum);
123     }
124
125 }