2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.binding.yang.types;
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;
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;
41 * Test class for testing BaseYangTypes class.
43 * @author Lukas Sedlak <lsedlak@cisco.com>
45 public class BaseYangTypesTest {
47 private static SchemaContext schemaContext;
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;
65 @SuppressWarnings("deprecation")
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();
78 private static void initTypeDefinitionsFromSchemaContext() {
79 Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
80 assertTrue(!typedefs.isEmpty());
82 for (final TypeDefinition<?> typedef : typedefs) {
83 assertNotNull(typedef);
84 assertTrue(typedef instanceof ExtendedType);
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();
97 int8 = (IntegerTypeDefinition) baseType;
100 int16 = (IntegerTypeDefinition) baseType;
103 int32 = (IntegerTypeDefinition) baseType;
106 int64 = (IntegerTypeDefinition) baseType;
109 } else if (baseType instanceof StringTypeDefinition) {
110 string = (StringTypeDefinition) baseType;
111 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
112 String typeName = baseType.getQName().getLocalName();
115 uint8 = (UnsignedIntegerTypeDefinition) baseType;
118 uint16 = (UnsignedIntegerTypeDefinition) baseType;
121 uint32 = (UnsignedIntegerTypeDefinition) baseType;
124 uint64 = (UnsignedIntegerTypeDefinition) baseType;
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;
135 assertNotNull(binary);
136 assertNotNull(decimal64);
137 assertNotNull(enumeration);
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);
153 public void javaTypeForSchemaDefinitionTypeTest() {
154 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
156 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
157 assertNotNull(javaType);
158 assertEquals("byte[]", javaType.getFullyQualifiedName());
160 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
161 assertNotNull(javaType);
162 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
164 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
165 assertNotNull(javaType);
166 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
168 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
169 assertNotNull(javaType);
170 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
172 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
173 assertNotNull(javaType);
174 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
176 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
177 assertNotNull(javaType);
178 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
180 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
181 assertNotNull(javaType);
182 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
184 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
185 assertNotNull(javaType);
186 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
188 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
189 assertNotNull(javaType);
190 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
192 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
193 assertNotNull(javaType);
194 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
196 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
197 assertNotNull(javaType);
198 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
200 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
201 assertNotNull(javaType);
202 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
204 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
205 assertNotNull(javaType);
206 assertEquals("Union", javaType.getFullyQualifiedName());
208 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
209 assertNotNull(javaType);
210 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
212 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
213 assertNotNull(javaType);
214 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
218 public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
219 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
221 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
222 assertNotNull(javaType);
223 assertEquals("byte[]", javaType.getFullyQualifiedName());
225 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
226 assertNotNull(javaType);
227 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
229 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
230 assertNotNull(javaType);
231 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
233 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
234 assertNotNull(javaType);
235 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
237 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
238 assertNotNull(javaType);
239 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
241 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
242 assertNotNull(javaType);
243 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
245 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
246 assertNotNull(javaType);
247 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
249 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
250 assertNotNull(javaType);
251 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
253 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
254 assertNotNull(javaType);
255 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
257 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
258 assertNotNull(javaType);
259 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
261 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
262 assertNotNull(javaType);
263 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
265 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
266 assertNotNull(javaType);
267 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
269 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
270 assertNotNull(javaType);
271 assertEquals("Union", javaType.getFullyQualifiedName());
273 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
274 assertNotNull(javaType);
275 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
277 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
278 assertNotNull(javaType);
279 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());