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