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