Revert "Fix broken tests according to yangtools...
[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.InputStream;
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.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 /**
41  * Test class for testing BaseYangTypes class.
42  */
43 public class BaseYangTypesTest {
44
45     private static SchemaContext schemaContext;
46
47     private static BinaryTypeDefinition binary = null;
48     private static DecimalTypeDefinition decimal64 = null;
49     private static EnumTypeDefinition enumeration = null;
50     private static IntegerTypeDefinition int8 = null;
51     private static IntegerTypeDefinition int16 = null;
52     private static IntegerTypeDefinition int32 = null;
53     private static IntegerTypeDefinition int64 = null;
54     private static StringTypeDefinition string = null;
55     private static UnsignedIntegerTypeDefinition uint8 = null;
56     private static UnsignedIntegerTypeDefinition uint16 = null;
57     private static UnsignedIntegerTypeDefinition uint32 = null;
58     private static UnsignedIntegerTypeDefinition uint64 = null;
59     private static UnionTypeDefinition union = null;
60     private static EmptyTypeDefinition empty = null;
61     private static BooleanTypeDefinition bool = null;
62
63     @BeforeClass
64     public static void setup() throws SourceException, ReactorException {
65         final List<InputStream> modelsToParse = Collections
66             .singletonList(BaseYangTypesTest.class.getResourceAsStream("/base-yang-types.yang"));
67         schemaContext = YangParserTestUtils.parseYangStreams(modelsToParse);
68         assertNotNull(schemaContext);
69         initTypeDefinitionsFromSchemaContext();
70     }
71
72     private static void initTypeDefinitionsFromSchemaContext() {
73         Set<TypeDefinition<?>> typedefs = schemaContext.getTypeDefinitions();
74         assertTrue(!typedefs.isEmpty());
75
76         for (final TypeDefinition<?> typedef : typedefs) {
77             assertNotNull(typedef);
78
79             final TypeDefinition<?> baseType = typedef.getBaseType();
80             assertNotNull(baseType);
81
82             if (baseType instanceof BinaryTypeDefinition) {
83                 binary = (BinaryTypeDefinition) baseType;
84             } else if (baseType instanceof DecimalTypeDefinition) {
85                 decimal64 = (DecimalTypeDefinition) baseType;
86             } else if (baseType instanceof EnumTypeDefinition) {
87                 enumeration = (EnumTypeDefinition) baseType;
88             } else if (baseType instanceof IntegerTypeDefinition) {
89                 String typeName = baseType.getQName().getLocalName();
90                 switch (typeName) {
91                 case "int8":
92                     int8 = (IntegerTypeDefinition) baseType;
93                     break;
94                 case "int16":
95                     int16 = (IntegerTypeDefinition) baseType;
96                     break;
97                 case "int32":
98                     int32 = (IntegerTypeDefinition) baseType;
99                     break;
100                 case "int64":
101                     int64 = (IntegerTypeDefinition) baseType;
102                     break;
103                 }
104             } else if (baseType instanceof StringTypeDefinition) {
105                 string = (StringTypeDefinition) baseType;
106             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
107                 String typeName = baseType.getQName().getLocalName();
108                 switch (typeName) {
109                 case "uint8":
110                     uint8 = (UnsignedIntegerTypeDefinition) baseType;
111                     break;
112                 case "uint16":
113                     uint16 = (UnsignedIntegerTypeDefinition) baseType;
114                     break;
115                 case "uint32":
116                     uint32 = (UnsignedIntegerTypeDefinition) baseType;
117                     break;
118                 case "uint64":
119                     uint64 = (UnsignedIntegerTypeDefinition) baseType;
120                     break;
121                 }
122             } else if (baseType instanceof UnionTypeDefinition) {
123                 union = (UnionTypeDefinition) baseType;
124             } else if (baseType instanceof EmptyTypeDefinition) {
125                 empty = (EmptyTypeDefinition) baseType;
126             } else if (baseType instanceof BooleanTypeDefinition) {
127                 bool = (BooleanTypeDefinition) baseType;
128             }
129         }
130         assertNotNull(binary);
131         assertNotNull(decimal64);
132         assertNotNull(enumeration);
133         assertNotNull(int8);
134         assertNotNull(int16);
135         assertNotNull(int32);
136         assertNotNull(int64);
137         assertNotNull(string);
138         assertNotNull(uint8);
139         assertNotNull(uint16);
140         assertNotNull(uint32);
141         assertNotNull(uint64);
142         assertNotNull(union);
143         assertNotNull(empty);
144         assertNotNull(bool);
145     }
146
147     @Test
148     public void javaTypeForSchemaDefinitionTypeTest() {
149         final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
150
151         Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary);
152         assertNotNull(javaType);
153         assertEquals("byte[]", javaType.getFullyQualifiedName());
154
155         javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64);
156         assertNotNull(javaType);
157         assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
158
159         javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration);
160         assertNotNull(javaType);
161         assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
162
163         javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8);
164         assertNotNull(javaType);
165         assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
166
167         javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16);
168         assertNotNull(javaType);
169         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
170
171         javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32);
172         assertNotNull(javaType);
173         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
174
175         javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64);
176         assertNotNull(javaType);
177         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
178
179         javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string);
180         assertNotNull(javaType);
181         assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
182
183         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8);
184         assertNotNull(javaType);
185         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
186
187         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16);
188         assertNotNull(javaType);
189         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
190
191         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32);
192         assertNotNull(javaType);
193         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
194
195         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64);
196         assertNotNull(javaType);
197         assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
198
199         javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union);
200         assertNotNull(javaType);
201         assertEquals("Union", javaType.getFullyQualifiedName());
202
203         javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty);
204         assertNotNull(javaType);
205         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
206
207         javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool);
208         assertNotNull(javaType);
209         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
210     }
211
212     @Test
213     public void javaTypeForRestrictedSchemaDefinitionTypeTest() {
214         final TypeProvider typeProvider = BaseYangTypes.BASE_YANG_TYPES_PROVIDER;
215
216         Type javaType = typeProvider.javaTypeForSchemaDefinitionType(binary, binary, BindingGeneratorUtil.getRestrictions(binary));
217         assertNotNull(javaType);
218         assertEquals("byte[]", javaType.getFullyQualifiedName());
219
220         javaType = typeProvider.javaTypeForSchemaDefinitionType(decimal64, decimal64, BindingGeneratorUtil.getRestrictions(decimal64));
221         assertNotNull(javaType);
222         assertEquals(BigDecimal.class.getCanonicalName(), javaType.getFullyQualifiedName());
223
224         javaType = typeProvider.javaTypeForSchemaDefinitionType(enumeration, enumeration, BindingGeneratorUtil.getRestrictions(enumeration));
225         assertNotNull(javaType);
226         assertEquals(Enum.class.getCanonicalName(), javaType.getFullyQualifiedName());
227
228         javaType = typeProvider.javaTypeForSchemaDefinitionType(int8, int8, BindingGeneratorUtil.getRestrictions(int8));
229         assertNotNull(javaType);
230         assertEquals(Byte.class.getCanonicalName(), javaType.getFullyQualifiedName());
231
232         javaType = typeProvider.javaTypeForSchemaDefinitionType(int16, int16, BindingGeneratorUtil.getRestrictions(int16));
233         assertNotNull(javaType);
234         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
235
236         javaType = typeProvider.javaTypeForSchemaDefinitionType(int32, int32, BindingGeneratorUtil.getRestrictions(int32));
237         assertNotNull(javaType);
238         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
239
240         javaType = typeProvider.javaTypeForSchemaDefinitionType(int64, int64, BindingGeneratorUtil.getRestrictions(int64));
241         assertNotNull(javaType);
242         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
243
244         javaType = typeProvider.javaTypeForSchemaDefinitionType(string, string, BindingGeneratorUtil.getRestrictions(string));
245         assertNotNull(javaType);
246         assertEquals(String.class.getCanonicalName(), javaType.getFullyQualifiedName());
247
248         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint8, uint8, BindingGeneratorUtil.getRestrictions(uint8));
249         assertNotNull(javaType);
250         assertEquals(Short.class.getCanonicalName(), javaType.getFullyQualifiedName());
251
252         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint16, uint16, BindingGeneratorUtil.getRestrictions(uint16));
253         assertNotNull(javaType);
254         assertEquals(Integer.class.getCanonicalName(), javaType.getFullyQualifiedName());
255
256         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint32, uint32, BindingGeneratorUtil.getRestrictions(uint32));
257         assertNotNull(javaType);
258         assertEquals(Long.class.getCanonicalName(), javaType.getFullyQualifiedName());
259
260         javaType = typeProvider.javaTypeForSchemaDefinitionType(uint64, uint64, BindingGeneratorUtil.getRestrictions(uint64));
261         assertNotNull(javaType);
262         assertEquals(BigInteger.class.getCanonicalName(), javaType.getFullyQualifiedName());
263
264         javaType = typeProvider.javaTypeForSchemaDefinitionType(union, union, BindingGeneratorUtil.getRestrictions(union));
265         assertNotNull(javaType);
266         assertEquals("Union", javaType.getFullyQualifiedName());
267
268         javaType = typeProvider.javaTypeForSchemaDefinitionType(empty, empty, BindingGeneratorUtil.getRestrictions(empty));
269         assertNotNull(javaType);
270         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
271
272         javaType = typeProvider.javaTypeForSchemaDefinitionType(bool, bool, BindingGeneratorUtil.getRestrictions(bool));
273         assertNotNull(javaType);
274         assertEquals(Boolean.class.getCanonicalName(), javaType.getFullyQualifiedName());
275     }
276 }