2 * Copyright (c) 2016 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;
14 import java.io.InputStream;
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17 import java.util.Collections;
18 import java.util.List;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
23 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
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;
40 * Test class for testing BaseYangTypes class.
42 public class BaseYangTypesTest {
44 private static SchemaContext schemaContext;
46 private static BinaryTypeDefinition binary = null;
47 private static DecimalTypeDefinition decimal64 = null;
48 private static EnumTypeDefinition enumeration = null;
49 private static IntegerTypeDefinition int8 = null;
50 private static IntegerTypeDefinition int16 = null;
51 private static IntegerTypeDefinition int32 = null;
52 private static IntegerTypeDefinition int64 = null;
53 private static StringTypeDefinition string = null;
54 private static UnsignedIntegerTypeDefinition uint8 = null;
55 private static UnsignedIntegerTypeDefinition uint16 = null;
56 private static UnsignedIntegerTypeDefinition uint32 = null;
57 private static UnsignedIntegerTypeDefinition uint64 = null;
58 private static UnionTypeDefinition union = null;
59 private static EmptyTypeDefinition empty = null;
60 private static BooleanTypeDefinition bool = null;
63 public static void setup() throws SourceException, ReactorException {
64 final List<InputStream> modelsToParse = Collections
65 .singletonList(BaseYangTypesTest.class.getResourceAsStream("/base-yang-types.yang"));
66 schemaContext = TestUtils.parseYangStreams(modelsToParse);
67 assertNotNull(schemaContext);
68 initTypeDefinitionsFromSchemaContext();
71 private static void initTypeDefinitionsFromSchemaContext() {
72 Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
73 assertTrue(!typedefs.isEmpty());
75 for (final TypeDefinition<?> typedef : typedefs) {
76 assertNotNull(typedef);
78 final TypeDefinition<?> baseType = typedef.getBaseType();
79 assertNotNull(baseType);
81 if (baseType instanceof BinaryTypeDefinition) {
82 binary = (BinaryTypeDefinition) baseType;
83 } else if (baseType instanceof DecimalTypeDefinition) {
84 decimal64 = (DecimalTypeDefinition) baseType;
85 } else if (baseType instanceof EnumTypeDefinition) {
86 enumeration = (EnumTypeDefinition) baseType;
87 } else if (baseType instanceof IntegerTypeDefinition) {
88 String typeName = baseType.getQName().getLocalName();
91 int8 = (IntegerTypeDefinition) baseType;
94 int16 = (IntegerTypeDefinition) baseType;
97 int32 = (IntegerTypeDefinition) baseType;
100 int64 = (IntegerTypeDefinition) baseType;
103 } else if (baseType instanceof StringTypeDefinition) {
104 string = (StringTypeDefinition) baseType;
105 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
106 String typeName = baseType.getQName().getLocalName();
109 uint8 = (UnsignedIntegerTypeDefinition) baseType;
112 uint16 = (UnsignedIntegerTypeDefinition) baseType;
115 uint32 = (UnsignedIntegerTypeDefinition) baseType;
118 uint64 = (UnsignedIntegerTypeDefinition) baseType;
121 } else if (baseType instanceof UnionTypeDefinition) {
122 union = (UnionTypeDefinition) baseType;
123 } else if (baseType instanceof EmptyTypeDefinition) {
124 empty = (EmptyTypeDefinition) baseType;
125 } else if (baseType instanceof BooleanTypeDefinition) {
126 bool = (BooleanTypeDefinition) baseType;
129 assertNotNull(binary);
130 assertNotNull(decimal64);
131 assertNotNull(enumeration);
133 assertNotNull(int16);
134 assertNotNull(int32);
135 assertNotNull(int64);
136 assertNotNull(string);
137 assertNotNull(uint8);
138 assertNotNull(uint16);
139 assertNotNull(uint32);
140 assertNotNull(uint64);
141 assertNotNull(union);
142 assertNotNull(empty);
147 public void javaTypeForSchemaDefinitionTypeTest() {
148 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
150 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
151 assertNotNull(javaType);
152 assertEquals("byte[]", javaType.getFullyQualifiedName());
154 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
155 assertNotNull(javaType);
156 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
158 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
159 assertNotNull(javaType);
160 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
162 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
163 assertNotNull(javaType);
164 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
166 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
167 assertNotNull(javaType);
168 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
170 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
171 assertNotNull(javaType);
172 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
174 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
175 assertNotNull(javaType);
176 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
178 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
179 assertNotNull(javaType);
180 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
182 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
183 assertNotNull(javaType);
184 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
186 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
187 assertNotNull(javaType);
188 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
190 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
191 assertNotNull(javaType);
192 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
194 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
195 assertNotNull(javaType);
196 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
198 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
199 assertNotNull(javaType);
200 assertEquals("Union", javaType.getFullyQualifiedName());
202 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
203 assertNotNull(javaType);
204 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
206 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
207 assertNotNull(javaType);
208 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
212 public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
213 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
215 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
216 assertNotNull(javaType);
217 assertEquals("byte[]", javaType.getFullyQualifiedName());
219 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
220 assertNotNull(javaType);
221 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
223 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
224 assertNotNull(javaType);
225 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
227 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
228 assertNotNull(javaType);
229 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
231 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
232 assertNotNull(javaType);
233 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
235 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
236 assertNotNull(javaType);
237 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
239 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
240 assertNotNull(javaType);
241 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
243 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
244 assertNotNull(javaType);
245 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
247 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
248 assertNotNull(javaType);
249 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
251 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
252 assertNotNull(javaType);
253 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
255 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
256 assertNotNull(javaType);
257 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
259 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
260 assertNotNull(javaType);
261 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
263 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
264 assertNotNull(javaType);
265 assertEquals("Union", javaType.getFullyQualifiedName());
267 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
268 assertNotNull(javaType);
269 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
271 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
272 assertNotNull(javaType);
273 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());