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