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