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;
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.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil;
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.test.util.YangParserTestUtils;
39 * Test class for testing BaseYangTypes class.
41 public class BaseYangTypesTest {
43 private static SchemaContext schemaContext;
45 private static BinaryTypeDefinition binary = null;
46 private static DecimalTypeDefinition decimal64 = null;
47 private static EnumTypeDefinition enumeration = null;
48 private static IntegerTypeDefinition int8 = null;
49 private static IntegerTypeDefinition int16 = null;
50 private static IntegerTypeDefinition int32 = null;
51 private static IntegerTypeDefinition int64 = null;
52 private static StringTypeDefinition string = null;
53 private static UnsignedIntegerTypeDefinition uint8 = null;
54 private static UnsignedIntegerTypeDefinition uint16 = null;
55 private static UnsignedIntegerTypeDefinition uint32 = null;
56 private static UnsignedIntegerTypeDefinition uint64 = null;
57 private static UnionTypeDefinition union = null;
58 private static EmptyTypeDefinition empty = null;
59 private static BooleanTypeDefinition bool = null;
62 public static void setup() throws Exception {
63 final List<File> modelsToParse = Collections
64 .singletonList(new File(BaseYangTypesTest.class.getResource("/base-yang-types.yang").toURI()));
65 schemaContext = YangParserTestUtils.parseYangFiles(modelsToParse);
66 assertNotNull(schemaContext);
67 initTypeDefinitionsFromSchemaContext();
70 private static void initTypeDefinitionsFromSchemaContext() {
71 Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
72 assertTrue(!typedefs.isEmpty());
74 for (final TypeDefinition<?> typedef : typedefs) {
75 assertNotNull(typedef);
77 final TypeDefinition<?> baseType = typedef.getBaseType();
78 assertNotNull(baseType);
80 if (baseType instanceof BinaryTypeDefinition) {
81 binary = (BinaryTypeDefinition) baseType;
82 } else if (baseType instanceof DecimalTypeDefinition) {
83 decimal64 = (DecimalTypeDefinition) baseType;
84 } else if (baseType instanceof EnumTypeDefinition) {
85 enumeration = (EnumTypeDefinition) baseType;
86 } else if (baseType instanceof IntegerTypeDefinition) {
87 String typeName = baseType.getQName().getLocalName();
90 int8 = (IntegerTypeDefinition) baseType;
93 int16 = (IntegerTypeDefinition) baseType;
96 int32 = (IntegerTypeDefinition) baseType;
99 int64 = (IntegerTypeDefinition) baseType;
102 } else if (baseType instanceof StringTypeDefinition) {
103 string = (StringTypeDefinition) baseType;
104 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
105 String typeName = baseType.getQName().getLocalName();
108 uint8 = (UnsignedIntegerTypeDefinition) baseType;
111 uint16 = (UnsignedIntegerTypeDefinition) baseType;
114 uint32 = (UnsignedIntegerTypeDefinition) baseType;
117 uint64 = (UnsignedIntegerTypeDefinition) baseType;
120 } else if (baseType instanceof UnionTypeDefinition) {
121 union = (UnionTypeDefinition) baseType;
122 } else if (baseType instanceof EmptyTypeDefinition) {
123 empty = (EmptyTypeDefinition) baseType;
124 } else if (baseType instanceof BooleanTypeDefinition) {
125 bool = (BooleanTypeDefinition) baseType;
128 assertNotNull(binary);
129 assertNotNull(decimal64);
130 assertNotNull(enumeration);
132 assertNotNull(int16);
133 assertNotNull(int32);
134 assertNotNull(int64);
135 assertNotNull(string);
136 assertNotNull(uint8);
137 assertNotNull(uint16);
138 assertNotNull(uint32);
139 assertNotNull(uint64);
140 assertNotNull(union);
141 assertNotNull(empty);
146 public void javaTypeForSchemaDefinitionTypeTest() {
147 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
149 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
150 assertNotNull(javaType);
151 assertEquals("byte[]", javaType.getFullyQualifiedName());
153 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
154 assertNotNull(javaType);
155 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
157 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
158 assertNotNull(javaType);
159 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
161 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
162 assertNotNull(javaType);
163 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
165 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
166 assertNotNull(javaType);
167 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
169 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
170 assertNotNull(javaType);
171 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
173 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
174 assertNotNull(javaType);
175 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
177 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
178 assertNotNull(javaType);
179 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
181 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
182 assertNotNull(javaType);
183 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
185 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
186 assertNotNull(javaType);
187 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
189 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
190 assertNotNull(javaType);
191 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
193 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
194 assertNotNull(javaType);
195 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
197 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
198 assertNotNull(javaType);
199 assertEquals("Union", javaType.getFullyQualifiedName());
201 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
202 assertNotNull(javaType);
203 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
205 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
206 assertNotNull(javaType);
207 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
211 public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
212 final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
214 Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
215 assertNotNull(javaType);
216 assertEquals("byte[]", javaType.getFullyQualifiedName());
218 javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
219 assertNotNull(javaType);
220 assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
222 javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
223 assertNotNull(javaType);
224 assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
226 javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
227 assertNotNull(javaType);
228 assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
230 javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
231 assertNotNull(javaType);
232 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
234 javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
235 assertNotNull(javaType);
236 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
238 javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
239 assertNotNull(javaType);
240 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
242 javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
243 assertNotNull(javaType);
244 assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
246 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
247 assertNotNull(javaType);
248 assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
250 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
251 assertNotNull(javaType);
252 assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
254 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
255 assertNotNull(javaType);
256 assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
258 javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
259 assertNotNull(javaType);
260 assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
262 javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
263 assertNotNull(javaType);
264 assertEquals("Union", javaType.getFullyQualifiedName());
266 javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
267 assertNotNull(javaType);
268 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
270 javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
271 assertNotNull(javaType);
272 assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());