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