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