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