Improve stored map memory efficiency
[yangtools.git] / code-generator / binding-type-provider / src / test / java / org / opendaylight / yangtools / sal / binding / yang / types / BaseYangTypesTest.java
1 /*
2  * Copyright (c) 2013 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.yang.types;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import java.io.InputStream;
14 import java.math.BigDecimal;
15 import java.math.BigInteger;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Set;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
22 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
37 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
38 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
39
40 /**
41  * Test class for testing BaseYangTypes class.
42  *
43  * @author Lukas Sedlak <lsedlak@cisco.com>
44  */
45 public class BaseYangTypesTest {
46
47     private static SchemaContext schemaContext;
48
49     private static BinaryTypeDefinition binary = null;
50     private static DecimalTypeDefinition decimal64 = null;
51     private static EnumTypeDefinition enumeration = null;
52     private static IntegerTypeDefinition int8 = null;
53     private static IntegerTypeDefinition int16 = null;
54     private static IntegerTypeDefinition int32 = null;
55     private static IntegerTypeDefinition int64 = null;
56     private static StringTypeDefinition string = null;
57     private static UnsignedIntegerTypeDefinition uint8 = null;
58     private static UnsignedIntegerTypeDefinition uint16 = null;
59     private static UnsignedIntegerTypeDefinition uint32 = null;
60     private static UnsignedIntegerTypeDefinition uint64 = null;
61     private static UnionTypeDefinition union = null;
62     private static EmptyTypeDefinition empty = null;
63     private static BooleanTypeDefinition bool = null;
64
65     @SuppressWarnings("deprecation")
66     @BeforeClass
67     public static void setup() {
68         final List<InputStream> modelsToParse = Collections
69             .singletonList(BaseYangTypesTest.class.getResourceAsStream("/base-yang-types.yang"));
70         final YangContextParser parser = new YangParserImpl();
71         final Set<Module> modules = parser.parseYangModelsFromStreams(modelsToParse);
72         assertTrue(!modules.isEmpty());
73         schemaContext = parser.resolveSchemaContext(modules);
74         assertNotNull(schemaContext);
75         initTypeDefinitionsFromSchemaContext();
76     }
77
78     private static void initTypeDefinitionsFromSchemaContext() {
79         Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
80         assertTrue(!typedefs.isEmpty());
81
82         for (final TypeDefinition<?> typedef : typedefs) {
83             assertNotNull(typedef);
84             assertTrue(typedef instanceof ExtendedType);
85
86             final TypeDefinition baseType = typedef.getBaseType();
87             if (baseType instanceof BinaryTypeDefinition) {
88                 binary = (BinaryTypeDefinition) baseType;
89             } else if (baseType instanceof DecimalTypeDefinition) {
90                 decimal64 = (DecimalTypeDefinition) baseType;
91             } else if (baseType instanceof EnumTypeDefinition) {
92                 enumeration = (EnumTypeDefinition) baseType;
93             } else if (baseType instanceof IntegerTypeDefinition) {
94                 String typeName = baseType.getQName().getLocalName();
95                 switch (typeName) {
96                 case "int8":
97                     int8 = (IntegerTypeDefinition) baseType;
98                     break;
99                 case "int16":
100                     int16 = (IntegerTypeDefinition) baseType;
101                     break;
102                 case "int32":
103                     int32 = (IntegerTypeDefinition) baseType;
104                     break;
105                 case "int64":
106                     int64 = (IntegerTypeDefinition) baseType;
107                     break;
108                 }
109             } else if (baseType instanceof StringTypeDefinition) {
110                 string = (StringTypeDefinition) baseType;
111             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
112                 String typeName = baseType.getQName().getLocalName();
113                 switch (typeName) {
114                 case "uint8":
115                     uint8 = (UnsignedIntegerTypeDefinition) baseType;
116                     break;
117                 case "uint16":
118                     uint16 = (UnsignedIntegerTypeDefinition) baseType;
119                     break;
120                 case "uint32":
121                     uint32 = (UnsignedIntegerTypeDefinition) baseType;
122                     break;
123                 case "uint64":
124                     uint64 = (UnsignedIntegerTypeDefinition) baseType;
125                     break;
126                 }
127             } else if (baseType instanceof UnionTypeDefinition) {
128                 union = (UnionTypeDefinition) baseType;
129             } else if (baseType instanceof EmptyTypeDefinition) {
130                 empty = (EmptyTypeDefinition) baseType;
131             } else if (baseType instanceof BooleanTypeDefinition) {
132                 bool = (BooleanTypeDefinition) baseType;
133             }
134         }
135         assertNotNull(binary);
136         assertNotNull(decimal64);
137         assertNotNull(enumeration);
138         assertNotNull(int8);
139         assertNotNull(int16);
140         assertNotNull(int32);
141         assertNotNull(int64);
142         assertNotNull(string);
143         assertNotNull(uint8);
144         assertNotNull(uint16);
145         assertNotNull(uint32);
146         assertNotNull(uint64);
147         assertNotNull(union);
148         assertNotNull(empty);
149         assertNotNull(bool);
150     }
151
152     @Test
153     public void javaTypeForSchemaDefinitionTypeTest() {
154         final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
155
156         Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
157         assertNotNull(javaType);
158         assertEquals("byte[]", javaType.getFullyQualifiedName());
159
160         javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
161         assertNotNull(javaType);
162         assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
163
164         javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
165         assertNotNull(javaType);
166         assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
167
168         javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
169         assertNotNull(javaType);
170         assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
171
172         javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
173         assertNotNull(javaType);
174         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
175
176         javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
177         assertNotNull(javaType);
178         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
179
180         javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
181         assertNotNull(javaType);
182         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
183
184         javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
185         assertNotNull(javaType);
186         assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
187
188         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
189         assertNotNull(javaType);
190         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
191
192         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
193         assertNotNull(javaType);
194         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
195
196         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
197         assertNotNull(javaType);
198         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
199
200         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
201         assertNotNull(javaType);
202         assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
203
204         javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
205         assertNotNull(javaType);
206         assertEquals("Union", javaType.getFullyQualifiedName());
207
208         javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
209         assertNotNull(javaType);
210         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
211
212         javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
213         assertNotNull(javaType);
214         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
215     }
216
217     @Test
218     public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
219         final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
220
221         Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
222         assertNotNull(javaType);
223         assertEquals("byte[]", javaType.getFullyQualifiedName());
224
225         javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
226         assertNotNull(javaType);
227         assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
228
229         javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
230         assertNotNull(javaType);
231         assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
232
233         javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
234         assertNotNull(javaType);
235         assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
236
237         javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
238         assertNotNull(javaType);
239         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
240
241         javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
242         assertNotNull(javaType);
243         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
244
245         javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
246         assertNotNull(javaType);
247         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
248
249         javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
250         assertNotNull(javaType);
251         assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
252
253         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
254         assertNotNull(javaType);
255         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
256
257         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
258         assertNotNull(javaType);
259         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
260
261         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
262         assertNotNull(javaType);
263         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
264
265         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
266         assertNotNull(javaType);
267         assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
268
269         javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
270         assertNotNull(javaType);
271         assertEquals("Union", javaType.getFullyQualifiedName());
272
273         javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
274         assertNotNull(javaType);
275         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
276
277         javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
278         assertNotNull(javaType);
279         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
280     }
281 }