bb1969633de3379e299913ef2e9ea86f135ae87d
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GenerateInnerClassForBitsAndUnionInLeavesTest.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.assertTrue;
11
12 import java.util.List;
13 import org.junit.Test;
14 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
17 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
18
19 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
20     @Test
21     public void testInnerClassCreationForBitsAndUnionsInLeafes() {
22         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
23             "/bit_and_union_in_leaf.yang"));
24         assertTrue(genTypes != null);
25
26         boolean parentContainerFound = false;
27         boolean bitLeafTOFound = false;
28         boolean unionLeafTOFound = false;
29
30         boolean firstBitPropertyFound = false;
31         boolean secondBitPropertyFound = false;
32         boolean thirdBitPropertyFound = false;
33
34         boolean firstBitPropertyTypeOK = false;
35         boolean secondBitPropertyTypeOK = false;
36         boolean thirdBitPropertyTypeOK = false;
37
38         boolean int32UnionPropertyFound = false;
39         boolean int32UnionPropertyTypeOK = false;
40         boolean stringUnionPropertyFound = false;
41         boolean stringUnionPropertyTypeOK = false;
42         boolean uint8UnionPropertyFound = false;
43         boolean uint8UnionPropertyTypeOK = false;
44
45         for (GeneratedType type : genTypes) {
46             if (!(type instanceof GeneratedTransferObject)) {
47                 if (type.getName().equals("ParentContainer")) {
48                     parentContainerFound = true;
49                     GeneratedType parentContainer = type;
50                     List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
51                     for (GeneratedType genType : enclosedTypes) {
52                         if (genType instanceof GeneratedTransferObject) {
53                             if (genType.getName().equals("BitLeaf")) {
54                                 bitLeafTOFound = true;
55                                 GeneratedTransferObject bitLeafTO = (GeneratedTransferObject) genType;
56
57                                 List<GeneratedProperty> bitLeafProperties = bitLeafTO.getProperties();
58                                 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
59                                     String bitLeafPropertyType = bitLeafProperty.getReturnType().getName();
60                                     if (bitLeafProperty.getName().equals("firstBit")) {
61                                         firstBitPropertyFound = true;
62                                         if (bitLeafPropertyType.equals("Boolean")) {
63                                             firstBitPropertyTypeOK = true;
64                                         }
65                                     } else if (bitLeafProperty.getName().equals("secondBit")) {
66                                         secondBitPropertyFound = true;
67                                         if (bitLeafPropertyType.equals("Boolean")) {
68                                             secondBitPropertyTypeOK = true;
69                                         }
70                                     } else if (bitLeafProperty.getName().equals("thirdBit")) {
71                                         thirdBitPropertyFound = true;
72                                         if (bitLeafPropertyType.equals("Boolean")) {
73                                             thirdBitPropertyTypeOK = true;
74                                         }
75                                     }
76
77                                 }
78
79                             } else if (genType.getName().equals("UnionLeaf")) {
80                                 unionLeafTOFound = true;
81                                 GeneratedTransferObject unionLeafTO = (GeneratedTransferObject) genType;
82
83                                 List<GeneratedProperty> unionLeafProperties = unionLeafTO.getProperties();
84                                 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
85                                     String unionLeafPropertyType = unionLeafProperty.getReturnType().getName();
86                                     if (unionLeafProperty.getName().equals("int32")) {
87                                         int32UnionPropertyFound = true;
88                                         if (unionLeafPropertyType.equals("Integer")) {
89                                             int32UnionPropertyTypeOK = true;
90                                         }
91                                     } else if (unionLeafProperty.getName().equals("string")) {
92                                         stringUnionPropertyFound = true;
93                                         if (unionLeafPropertyType.equals("String")) {
94                                             stringUnionPropertyTypeOK = true;
95                                         }
96                                     } else if (unionLeafProperty.getName().equals("uint8")) {
97                                         uint8UnionPropertyFound = true;
98                                         if (unionLeafPropertyType.equals("Uint8")) {
99                                             uint8UnionPropertyTypeOK = true;
100                                         }
101                                     }
102
103                                 }
104
105                             }
106                         }
107                     }
108                 }
109             }
110         }
111         assertTrue(parentContainerFound);
112
113         assertTrue(bitLeafTOFound);
114         assertTrue(firstBitPropertyFound);
115         assertTrue(secondBitPropertyFound);
116         assertTrue(thirdBitPropertyFound);
117
118         assertTrue(firstBitPropertyTypeOK);
119         assertTrue(secondBitPropertyTypeOK);
120         assertTrue(thirdBitPropertyTypeOK);
121
122         assertTrue(unionLeafTOFound);
123         assertTrue(int32UnionPropertyFound);
124         assertTrue(int32UnionPropertyTypeOK);
125         assertTrue(stringUnionPropertyFound);
126         assertTrue(stringUnionPropertyTypeOK);
127         assertTrue(uint8UnionPropertyFound);
128         assertTrue(uint8UnionPropertyTypeOK);
129
130     }
131 }