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