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