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.mdsal.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.mdsal.binding.generator.spi.TypeProvider;
23 import org.opendaylight.mdsal.binding.generator.util.BindingGeneratorUtil;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.mdsal.binding.yang.types.BaseYangTypes;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 * Test class for testing BaseYangTypes class.
44 public class BaseYangTypesTest {
46 private static SchemaContext schemaContext;
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;
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 = YangParserTestUtils.parseYangStreams(modelsToParse);
69 assertNotNull(schemaContext);
70 initTypeDefinitionsFromSchemaContext();
73 private static void initTypeDefinitionsFromSchemaContext() {
74 Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
75 assertTrue(!typedefs.isEmpty());
77 for (final TypeDefinition<?> typedef : typedefs) {
78 assertNotNull(typedef);
80 final TypeDefinition<?> baseType = typedef.getBaseType();
81 assertNotNull(baseType);
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();
93 int8 = (IntegerTypeDefinition) baseType;
96 int16 = (IntegerTypeDefinition) baseType;
99 int32 = (IntegerTypeDefinition) baseType;
102 int64 = (IntegerTypeDefinition) baseType;
105 } else if (baseType instanceof StringTypeDefinition) {
106 string = (StringTypeDefinition) baseType;
107 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
108 String typeName = baseType.getQName().getLocalName();
111 uint8 = (UnsignedIntegerTypeDefinition) baseType;
114 uint16 = (UnsignedIntegerTypeDefinition) baseType;
117 uint32 = (UnsignedIntegerTypeDefinition) baseType;
120 uint64 = (UnsignedIntegerTypeDefinition) baseType;
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;
131 assertNotNull(binary);
132 assertNotNull(decimal64);
133 assertNotNull(enumeration);
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);
149 public void javaTypeForSchemaDefinitionTypeTest() {
150 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
152 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
153 assertNotNull(javaType);
154 assertEquals("byte[]", javaType.getFullyQualifiedName());
156 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
157 assertNotNull(javaType);
158 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
160 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
161 assertNotNull(javaType);
162 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
164 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
165 assertNotNull(javaType);
166 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
168 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
169 assertNotNull(javaType);
170 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
172 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
173 assertNotNull(javaType);
174 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
176 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
177 assertNotNull(javaType);
178 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
180 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
181 assertNotNull(javaType);
182 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
184 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
185 assertNotNull(javaType);
186 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
188 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
189 assertNotNull(javaType);
190 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
192 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
193 assertNotNull(javaType);
194 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
196 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
197 assertNotNull(javaType);
198 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
200 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
201 assertNotNull(javaType);
202 assertEquals("Union", javaType.getFullyQualifiedName());
204 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
205 assertNotNull(javaType);
206 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
208 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
209 assertNotNull(javaType);
210 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
214 public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
215 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
217 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
218 assertNotNull(javaType);
219 assertEquals("byte[]", javaType.getFullyQualifiedName());
221 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
222 assertNotNull(javaType);
223 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
225 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
226 assertNotNull(javaType);
227 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
229 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
230 assertNotNull(javaType);
231 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
233 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
234 assertNotNull(javaType);
235 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
237 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
238 assertNotNull(javaType);
239 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
241 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
242 assertNotNull(javaType);
243 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
245 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
246 assertNotNull(javaType);
247 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
249 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
250 assertNotNull(javaType);
251 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
253 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
254 assertNotNull(javaType);
255 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
257 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
258 assertNotNull(javaType);
259 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
261 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
262 assertNotNull(javaType);
263 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
265 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
266 assertNotNull(javaType);
267 assertEquals("Union", javaType.getFullyQualifiedName());
269 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
270 assertNotNull(javaType);
271 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
273 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
274 assertNotNull(javaType);
275 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());