d2e21a0277f11cdcb2cedad114c428431ae796bd
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / yang / types / BaseYangTypesTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.yang.types;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.File;
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Set;
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;
37
38 /**
39  * Test class for testing BaseYangTypes class.
40  */
41 public class BaseYangTypesTest {
42
43     private static SchemaContext schemaContext;
44
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;
60
61     @BeforeClass
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();
68     }
69
70     private static void initTypeDefinitionsFromSchemaContext() {
71         Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
72         assertTrue(!typedefs.isEmpty());
73
74         for (final TypeDefinition<?> typedef : typedefs) {
75             assertNotNull(typedef);
76
77             final TypeDefinition<?> baseType = typedef.getBaseType();
78             assertNotNull(baseType);
79
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();
88                 switch (typeName) {
89                 case "int8":
90                     int8 = (IntegerTypeDefinition) baseType;
91                     break;
92                 case "int16":
93                     int16 = (IntegerTypeDefinition) baseType;
94                     break;
95                 case "int32":
96                     int32 = (IntegerTypeDefinition) baseType;
97                     break;
98                 case "int64":
99                     int64 = (IntegerTypeDefinition) baseType;
100                     break;
101                 }
102             } else if (baseType instanceof StringTypeDefinition) {
103                 string = (StringTypeDefinition) baseType;
104             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
105                 String typeName = baseType.getQName().getLocalName();
106                 switch (typeName) {
107                 case "uint8":
108                     uint8 = (UnsignedIntegerTypeDefinition) baseType;
109                     break;
110                 case "uint16":
111                     uint16 = (UnsignedIntegerTypeDefinition) baseType;
112                     break;
113                 case "uint32":
114                     uint32 = (UnsignedIntegerTypeDefinition) baseType;
115                     break;
116                 case "uint64":
117                     uint64 = (UnsignedIntegerTypeDefinition) baseType;
118                     break;
119                 }
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;
126             }
127         }
128         assertNotNull(binary);
129         assertNotNull(decimal64);
130         assertNotNull(enumeration);
131         assertNotNull(int8);
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);
142         assertNotNull(bool);
143     }
144
145     @Test
146     public void javaTypeForSchemaDefinitionTypeTest() {
147         final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
148
149         Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
150         assertNotNull(javaType);
151         assertEquals("byte[]", javaType.getFullyQualifiedName());
152
153         javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
154         assertNotNull(javaType);
155         assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
156
157         javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
158         assertNotNull(javaType);
159         assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
160
161         javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
162         assertNotNull(javaType);
163         assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
164
165         javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
166         assertNotNull(javaType);
167         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
168
169         javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
170         assertNotNull(javaType);
171         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
172
173         javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
174         assertNotNull(javaType);
175         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
176
177         javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
178         assertNotNull(javaType);
179         assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
180
181         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
182         assertNotNull(javaType);
183         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
184
185         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
186         assertNotNull(javaType);
187         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
188
189         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
190         assertNotNull(javaType);
191         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
192
193         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
194         assertNotNull(javaType);
195         assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
196
197         javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
198         assertNotNull(javaType);
199         assertEquals("Union", javaType.getFullyQualifiedName());
200
201         javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
202         assertNotNull(javaType);
203         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
204
205         javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
206         assertNotNull(javaType);
207         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
208     }
209
210     @Test
211     public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
212         final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
213
214         Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
215         assertNotNull(javaType);
216         assertEquals("byte[]", javaType.getFullyQualifiedName());
217
218         javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
219         assertNotNull(javaType);
220         assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
221
222         javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
223         assertNotNull(javaType);
224         assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
225
226         javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
227         assertNotNull(javaType);
228         assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
229
230         javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
231         assertNotNull(javaType);
232         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
233
234         javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
235         assertNotNull(javaType);
236         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
237
238         javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
239         assertNotNull(javaType);
240         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
241
242         javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
243         assertNotNull(javaType);
244         assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
245
246         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
247         assertNotNull(javaType);
248         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
249
250         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
251         assertNotNull(javaType);
252         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
253
254         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
255         assertNotNull(javaType);
256         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
257
258         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
259         assertNotNull(javaType);
260         assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
261
262         javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
263         assertNotNull(javaType);
264         assertEquals("Union", javaType.getFullyQualifiedName());
265
266         javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
267         assertNotNull(javaType);
268         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
269
270         javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
271         assertNotNull(javaType);
272         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
273     }
274 }