d34f7c5278b5bbbe78791ab5d9bc73d08041e291
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / TypeProviderIntegrationTest.java
1 /*
2  * Copyright (c) 2013 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.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.io.File;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.Arrays;
17 import java.util.Set;
18 import org.junit.Before;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28
29 public class TypeProviderIntegrationTest {
30     private final String PKG = "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.";
31     private static SchemaContext context;
32     private TypeProviderImpl provider;
33     private Module m;
34
35     @BeforeClass
36     public static void setup() throws Exception {
37         File abstractTopology = new File(TypeProviderIntegrationTest.class.getResource("/type-provider/test.yang")
38                 .toURI());
39         File ietfInetTypes = new File(TypeProviderIntegrationTest.class.getResource("/ietf/ietf-inet-types.yang")
40                 .toURI());
41         context = new YangParserImpl().parseFiles(Arrays.asList(abstractTopology, ietfInetTypes));
42         assertNotNull(context);
43     }
44
45     @Before
46     public void init() throws ParseException {
47         provider = new TypeProviderImpl(context);
48         m = context.findModuleByName("test", new SimpleDateFormat("yyyy-MM-dd").parse("2013-10-08"));
49     }
50
51     @Test
52     public void testGetTypeDefaultConstructionBinary() throws ParseException {
53         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-binary");
54         String actual = provider.getTypeDefaultConstruction(leaf);
55         assertEquals("new byte[] {77, 97, 110}", actual);
56
57         leaf = (LeafSchemaNode) m.getDataChildByName("ext-binary");
58         actual = provider.getTypeDefaultConstruction(leaf);
59         assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
60     }
61
62     @Test
63     public void testGetTypeDefaultConstructionBits() throws ParseException {
64         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-bits");
65         String actual = provider.getTypeDefaultConstruction(leaf);
66         assertEquals("new " + PKG + "TestData.LeafBits(false, false, true)", actual);
67
68         leaf = (LeafSchemaNode) m.getDataChildByName("ext-bits");
69         actual = provider.getTypeDefaultConstruction(leaf);
70         assertEquals("new " + PKG + "MyBits(false, false, true)", actual);
71     }
72
73     @Test
74     public void testGetTypeDefaultConstructionBoolean() throws ParseException {
75         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-boolean");
76         String actual = provider.getTypeDefaultConstruction(leaf);
77         assertEquals("new java.lang.Boolean(\"true\")", actual);
78
79         leaf = (LeafSchemaNode) m.getDataChildByName("ext-boolean");
80         actual = provider.getTypeDefaultConstruction(leaf);
81         assertEquals("new " + PKG + "MyBoolean(new java.lang.Boolean(\"true\"))", actual);
82     }
83
84     @Test
85     public void testGetTypeDefaultConstructionDecimal() throws ParseException {
86         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-decimal64");
87         String actual = provider.getTypeDefaultConstruction(leaf);
88         assertEquals("new java.math.BigDecimal(\"3.14\")", actual);
89
90         leaf = (LeafSchemaNode) m.getDataChildByName("ext-decimal64");
91         actual = provider.getTypeDefaultConstruction(leaf);
92         assertEquals("new " + PKG + "MyDecimal64(new java.math.BigDecimal(\"3.14\"))", actual);
93     }
94
95     @Test
96     public void testGetTypeDefaultConstructionEmpty() throws ParseException {
97         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-empty");
98         String actual = provider.getTypeDefaultConstruction(leaf);
99         assertEquals("new java.lang.Boolean(\"false\")", actual);
100
101         leaf = (LeafSchemaNode) m.getDataChildByName("ext-empty");
102         actual = provider.getTypeDefaultConstruction(leaf);
103         assertEquals("new " + PKG + "MyEmpty(new java.lang.Boolean(\"false\"))", actual);
104     }
105
106     @Test
107     public void testGetTypeDefaultConstructionEnumeration() throws ParseException {
108         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-enumeration");
109         String actual = provider.getTypeDefaultConstruction(leaf);
110         assertEquals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.LeafEnumeration.Seven", actual);
111
112         leaf = (LeafSchemaNode) m.getDataChildByName("ext-enumeration");
113         actual = provider.getTypeDefaultConstruction(leaf);
114         assertEquals(PKG + "MyEnumeration.Seven", actual);
115     }
116
117     @Test
118     public void testGetTypeDefaultConstructionInt8() throws ParseException {
119         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-int8");
120         String actual = provider.getTypeDefaultConstruction(leaf);
121         assertEquals("new java.lang.Byte(\"11\")", actual);
122
123         leaf = (LeafSchemaNode) m.getDataChildByName("ext-int8");
124         actual = provider.getTypeDefaultConstruction(leaf);
125         assertEquals("new " + PKG + "MyInt8(new java.lang.Byte(\"11\"))", actual);
126     }
127
128     @Test
129     public void testGetTypeDefaultConstructionInt16() throws ParseException {
130         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-int16");
131         String actual = provider.getTypeDefaultConstruction(leaf);
132         assertEquals("new java.lang.Short(\"111\")", actual);
133
134         leaf = (LeafSchemaNode) m.getDataChildByName("ext-int16");
135         actual = provider.getTypeDefaultConstruction(leaf);
136         assertEquals("new " + PKG + "MyInt16(new java.lang.Short(\"111\"))", actual);
137     }
138
139     @Test
140     public void testGetTypeDefaultConstructionInt32() throws ParseException {
141         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-int32");
142         String actual = provider.getTypeDefaultConstruction(leaf);
143         assertEquals("new java.lang.Integer(\"1111\")", actual);
144
145         leaf = (LeafSchemaNode) m.getDataChildByName("ext-int32");
146         actual = provider.getTypeDefaultConstruction(leaf);
147         assertEquals("new " + PKG + "MyInt32(new java.lang.Integer(\"1111\"))", actual);
148     }
149
150     @Test
151     public void testGetTypeDefaultConstructionInt64() throws ParseException {
152         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-int64");
153         String actual = provider.getTypeDefaultConstruction(leaf);
154         assertEquals("new java.lang.Long(\"11111\")", actual);
155
156         leaf = (LeafSchemaNode) m.getDataChildByName("ext-int64");
157         actual = provider.getTypeDefaultConstruction(leaf);
158         assertEquals("new " + PKG + "MyInt64(new java.lang.Long(\"11111\"))", actual);
159     }
160
161     @Test
162     public void testGetTypeDefaultConstructionLeafref1() throws ParseException {
163         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-leafref");
164         String actual = provider.getTypeDefaultConstruction(leaf);
165         assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
166
167         leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref");
168         actual = provider.getTypeDefaultConstruction(leaf);
169         assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
170     }
171
172     @Test
173     public void testGetTypeDefaultConstructionLeafref2() throws ParseException {
174         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-leafref1");
175         String actual = provider.getTypeDefaultConstruction(leaf);
176         assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
177
178         leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref1");
179         actual = provider.getTypeDefaultConstruction(leaf);
180         assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
181     }
182
183     @Test
184     public void testGetTypeDefaultConstructionString() throws ParseException {
185         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-string");
186         String actual = provider.getTypeDefaultConstruction(leaf);
187         assertEquals("\"name\"", actual);
188
189         leaf = (LeafSchemaNode) m.getDataChildByName("ext-string");
190         actual = provider.getTypeDefaultConstruction(leaf);
191         assertEquals("new " + PKG + "MyString(\"name\")", actual);
192     }
193
194     @Test
195     public void testGetTypeDefaultConstructionUint8() throws ParseException {
196         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-uint8");
197         String actual = provider.getTypeDefaultConstruction(leaf);
198         assertEquals("new java.lang.Short(\"11\")", actual);
199
200         leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint8");
201         actual = provider.getTypeDefaultConstruction(leaf);
202         assertEquals("new " + PKG + "MyUint8(new java.lang.Short(\"11\"))", actual);
203     }
204
205     @Test
206     public void testGetTypeDefaultConstructionUint16() throws ParseException {
207         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-uint16");
208         String actual = provider.getTypeDefaultConstruction(leaf);
209         assertEquals("new java.lang.Integer(\"111\")", actual);
210
211         leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint16");
212         actual = provider.getTypeDefaultConstruction(leaf);
213         assertEquals("new " + PKG + "MyUint16(new java.lang.Integer(\"111\"))", actual);
214     }
215
216     @Test
217     public void testGetTypeDefaultConstructionUint32() throws ParseException {
218         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-uint32");
219         String actual = provider.getTypeDefaultConstruction(leaf);
220         assertEquals("new java.lang.Long(\"1111\")", actual);
221
222         leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint32");
223         actual = provider.getTypeDefaultConstruction(leaf);
224         assertEquals("new " + PKG + "MyUint32(new java.lang.Long(\"1111\"))", actual);
225     }
226
227     @Test
228     public void testGetTypeDefaultConstructionUint64() throws ParseException {
229         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-uint64");
230         String actual = provider.getTypeDefaultConstruction(leaf);
231         assertEquals("new java.math.BigInteger(\"11111\")", actual);
232
233         leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint64");
234         actual = provider.getTypeDefaultConstruction(leaf);
235         assertEquals("new " + PKG + "MyUint64(new java.math.BigInteger(\"11111\"))", actual);
236     }
237
238     @Test
239     public void testGetTypeDefaultConstruction() throws ParseException {
240         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("ip-leaf");
241         String actual = provider.getTypeDefaultConstruction(leaf);
242         String exp = "new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address(\"0.0.0.1\")";
243         assertEquals(exp, actual);
244     }
245
246     @Test
247     public void testGetTypeDefaultConstructionUnion() throws ParseException {
248         LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-union");
249         String actual = provider.getTypeDefaultConstruction(leaf);
250         String expected = "new " + PKG + "TestData.LeafUnion(\"111\".toCharArray())";
251         assertEquals(expected, actual);
252
253         leaf = (LeafSchemaNode) m.getDataChildByName("ext-union");
254         actual = provider.getTypeDefaultConstruction(leaf);
255         expected = "new " + PKG + "MyUnion(\"111\".toCharArray())";
256         assertEquals(expected, actual);
257     }
258
259     @Test
260     public void testGetTypeDefaultConstructionUnionNested() throws ParseException {
261         ContainerSchemaNode c1 = (ContainerSchemaNode) m.getDataChildByName("c1");
262         ContainerSchemaNode c2 = (ContainerSchemaNode) c1.getDataChildByName("c2");
263         ContainerSchemaNode c3 = (ContainerSchemaNode) c2.getDataChildByName("c3");
264         LeafSchemaNode leaf = (LeafSchemaNode) c3.getDataChildByName("id");
265
266         String actual = provider.getTypeDefaultConstruction(leaf);
267         String expected = "new " + PKG + "NestedUnion(\"111\".toCharArray())";
268         assertEquals(expected, actual);
269     }
270
271     @Test
272     public void testGetParamNameFromType() throws ParseException {
273         m = context.findModuleByName("ietf-inet-types", new SimpleDateFormat("yyyy-MM-dd").parse("2010-09-24"));
274         Set<TypeDefinition<?>> types = m.getTypeDefinitions();
275         TypeDefinition<?> ipv4 = null;
276         TypeDefinition<?> ipv6 = null;
277         TypeDefinition<?> ipv4Pref = null;
278         TypeDefinition<?> ipv6Pref = null;
279         for (TypeDefinition<?> type : types) {
280             if ("ipv4-address".equals(type.getQName().getLocalName())) {
281                 ipv4 = type;
282             } else if ("ipv6-address".equals(type.getQName().getLocalName())) {
283                 ipv6 = type;
284             } else if ("ipv4-prefix".equals(type.getQName().getLocalName())) {
285                 ipv4Pref = type;
286             } else if ("ipv6-prefix".equals(type.getQName().getLocalName())) {
287                 ipv6Pref = type;
288             }
289         }
290
291         assertNotNull(ipv4);
292         assertNotNull(ipv6);
293         assertNotNull(ipv4Pref);
294         assertNotNull(ipv6Pref);
295         assertEquals("ipv4Address", provider.getParamNameFromType(ipv4));
296         assertEquals("ipv6Address", provider.getParamNameFromType(ipv6));
297         assertEquals("ipv4Prefix", provider.getParamNameFromType(ipv4Pref));
298         assertEquals("ipv6Prefix", provider.getParamNameFromType(ipv6Pref));
299     }
300 }