Added @ConstructorProperties annotation to constructors of classes generated from...
[mdsal.git] / code-generator / 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.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
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.parser.api.YangModelParser;
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
32     private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
33         final YangModelParser parser = new YangParserImpl();
34
35         final List<File> inputFiles = new ArrayList<File>();
36         for (int i = 0; i < yangFiles.length; ++i) {
37             inputFiles.add(new File(yangFiles[i]));
38         }
39
40         final Set<Module> modules = parser.parseYangModels(inputFiles);
41         return parser.resolveSchemaContext(modules);
42     }
43
44     @Test
45     public void testGetTypeDefaultConstruction1() throws ParseException {
46         final String path1 = getClass().getResource("/type-provider/test.yang").getPath();
47         final String path2 = getClass().getResource("/type-provider/ietf-inet-types@2010-09-24.yang").getPath();
48         final SchemaContext context = resolveSchemaContextFromFiles(path1, path2);
49         assertNotNull(context);
50         TypeProviderImpl provider = new TypeProviderImpl(context);
51         Module m = context.findModuleByName("test", new SimpleDateFormat("yyyy-MM-dd").parse("2013-10-08"));
52
53         LeafSchemaNode leaf = (LeafSchemaNode)m.getDataChildByName("id-binary");
54         String actual = provider.getTypeDefaultConstruction(leaf);
55         assertEquals("new byte[] {77, 97, 110}", actual);
56
57         leaf = (LeafSchemaNode)m.getDataChildByName("id-bits");
58         actual = provider.getTypeDefaultConstruction(leaf);
59         assertEquals("new " + PKG + "TestData.IdBits(false, false, true)", actual);
60
61         leaf = (LeafSchemaNode)m.getDataChildByName("id-boolean");
62         actual = provider.getTypeDefaultConstruction(leaf);
63         assertEquals("new java.lang.Boolean(\"true\")", actual);
64
65         leaf = (LeafSchemaNode)m.getDataChildByName("id-decimal64");
66         actual = provider.getTypeDefaultConstruction(leaf);
67         assertEquals("new java.math.BigDecimal(\"3.14\")", actual);
68
69         leaf = (LeafSchemaNode)m.getDataChildByName("id-empty");
70         actual = provider.getTypeDefaultConstruction(leaf);
71         assertEquals("new java.lang.Boolean(\"false\")", actual);
72
73         leaf = (LeafSchemaNode)m.getDataChildByName("id-enumeration");
74         actual = provider.getTypeDefaultConstruction(leaf);
75         //assertEquals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.IdEnumeration.Seven", actual);
76
77         leaf = (LeafSchemaNode)m.getDataChildByName("id-8");
78         actual = provider.getTypeDefaultConstruction(leaf);
79         assertEquals("new java.lang.Byte(\"11\")", actual);
80
81         leaf = (LeafSchemaNode)m.getDataChildByName("id-16");
82         actual = provider.getTypeDefaultConstruction(leaf);
83         assertEquals("new java.lang.Short(\"111\")", actual);
84
85         leaf = (LeafSchemaNode)m.getDataChildByName("id-32");
86         actual = provider.getTypeDefaultConstruction(leaf);
87         assertEquals("new java.lang.Integer(\"1111\")", actual);
88
89         leaf = (LeafSchemaNode)m.getDataChildByName("id-64");
90         actual = provider.getTypeDefaultConstruction(leaf);
91         assertEquals("new java.lang.Long(\"11111\")", actual);
92
93         leaf = (LeafSchemaNode)m.getDataChildByName("id-leafref");
94         actual = provider.getTypeDefaultConstruction(leaf);
95         assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
96
97         leaf = (LeafSchemaNode)m.getDataChildByName("id-string");
98         actual = provider.getTypeDefaultConstruction(leaf);
99         assertEquals("\"name\"", actual);
100
101         leaf = (LeafSchemaNode)m.getDataChildByName("id-u8");
102         actual = provider.getTypeDefaultConstruction(leaf);
103         assertEquals("new java.lang.Short(\"11\")", actual);
104
105         leaf = (LeafSchemaNode)m.getDataChildByName("id-u16");
106         actual = provider.getTypeDefaultConstruction(leaf);
107         assertEquals("new java.lang.Integer(\"111\")", actual);
108
109         leaf = (LeafSchemaNode)m.getDataChildByName("id-u32");
110         actual = provider.getTypeDefaultConstruction(leaf);
111         assertEquals("new java.lang.Long(\"1111\")", actual);
112
113         leaf = (LeafSchemaNode)m.getDataChildByName("id-u64");
114         actual = provider.getTypeDefaultConstruction(leaf);
115         assertEquals("new java.math.BigInteger(\"11111\")", actual);
116     }
117
118     @Ignore
119     @Test
120     public void testGetTypeDefaultConstruction2() throws ParseException {
121         final String path1 = getClass().getResource("/type-provider/test.yang").getPath();
122         final String path2 = getClass().getResource("/type-provider/ietf-inet-types@2010-09-24.yang").getPath();
123         final SchemaContext context = resolveSchemaContextFromFiles(path1, path2);
124         assertNotNull(context);
125         TypeProviderImpl provider = new TypeProviderImpl(context);
126         Module m = context.findModuleByName("test", new SimpleDateFormat("yyyy-MM-dd").parse("2013-10-08"));
127
128         LeafSchemaNode leaf = (LeafSchemaNode)m.getDataChildByName("ext-binary");
129         String actual = provider.getTypeDefaultConstruction(leaf);
130         assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
131
132         leaf = (LeafSchemaNode)m.getDataChildByName("ext-bits");
133         actual = provider.getTypeDefaultConstruction(leaf);
134         //assertEquals("new " + PKG + "MyBits(false, false, true)", actual);
135
136         leaf = (LeafSchemaNode)m.getDataChildByName("ext-boolean");
137         actual = provider.getTypeDefaultConstruction(leaf);
138         assertEquals("new " + PKG + "MyBoolean(new java.lang.Boolean(\"true\"))", actual);
139
140         leaf = (LeafSchemaNode)m.getDataChildByName("ext-decimal64");
141         actual = provider.getTypeDefaultConstruction(leaf);
142         assertEquals("new " + PKG + "MyDecimal64(new java.math.BigDecimal(\"3.14\"))", actual);
143
144         leaf = (LeafSchemaNode)m.getDataChildByName("ext-empty");
145         actual = provider.getTypeDefaultConstruction(leaf);
146         assertEquals("new " + PKG + "MyEmpty(new java.lang.Boolean(\"false\"))", actual);
147
148         leaf = (LeafSchemaNode)m.getDataChildByName("ext-enumeration");
149         actual = provider.getTypeDefaultConstruction(leaf);
150         assertEquals("new " + PKG + "MyEnumeration(" + PKG + "MyEnumeration.Seven)", actual);
151
152         leaf = (LeafSchemaNode)m.getDataChildByName("ext-8");
153         actual = provider.getTypeDefaultConstruction(leaf);
154         assertEquals("new " + PKG + "My8(new java.lang.Byte(\"11\"))", actual);
155
156         leaf = (LeafSchemaNode)m.getDataChildByName("ext-16");
157         actual = provider.getTypeDefaultConstruction(leaf);
158         assertEquals("new " + PKG + "My16(new java.lang.Short(\"111\"))", actual);
159
160         leaf = (LeafSchemaNode)m.getDataChildByName("ext-32");
161         actual = provider.getTypeDefaultConstruction(leaf);
162         assertEquals("new " + PKG + "My32(new java.lang.Integer(\"1111\"))", actual);
163
164         leaf = (LeafSchemaNode)m.getDataChildByName("ext-64");
165         actual = provider.getTypeDefaultConstruction(leaf);
166         assertEquals("new " + PKG + "My64(new java.lang.Long(\"11111\"))", actual);
167
168         leaf = (LeafSchemaNode)m.getDataChildByName("ext-leafref");
169         actual = provider.getTypeDefaultConstruction(leaf);
170         assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
171
172         leaf = (LeafSchemaNode)m.getDataChildByName("ext-string");
173         actual = provider.getTypeDefaultConstruction(leaf);
174         assertEquals("new " + PKG + "MyString(\"name\")", actual);
175
176         leaf = (LeafSchemaNode)m.getDataChildByName("ext-u8");
177         actual = provider.getTypeDefaultConstruction(leaf);
178         assertEquals("new " + PKG + "MyU8(new java.lang.Short(\"11\"))", actual);
179
180         leaf = (LeafSchemaNode)m.getDataChildByName("ext-u16");
181         actual = provider.getTypeDefaultConstruction(leaf);
182         assertEquals("new " + PKG + "MyU16(new java.lang.Integer(\"111\"))", actual);
183
184         leaf = (LeafSchemaNode)m.getDataChildByName("ext-u32");
185         actual = provider.getTypeDefaultConstruction(leaf);
186         assertEquals("new " + PKG + "MyU32(new java.lang.Long(\"1111\"))", actual);
187
188         leaf = (LeafSchemaNode)m.getDataChildByName("ext-u64");
189         actual = provider.getTypeDefaultConstruction(leaf);
190         assertEquals("new " + PKG + "MyU64(new java.math.BigInteger(\"11111\"))", actual);
191
192         leaf = (LeafSchemaNode)m.getDataChildByName("ip-leaf");
193         actual = provider.getTypeDefaultConstruction(leaf);
194         String exp = "new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.Ipv4Address(\"0.0.0.1\")";
195         assertEquals(exp, actual);
196     }
197
198 }