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