Fixed getTypeDefaultConstruction method in TypeProvider.
[yangtools.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.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29
30 public class TypeProviderIntegrationTest {
31     private final String PKG = "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.";
32     private static SchemaContext context;
33     private TypeProviderImpl provider;
34     private Module m;
35
36     @BeforeClass
37     public static void setup() throws ParseException {
38         final String path1 = TypeProviderIntegrationTest.class.getResource("/type-provider/test.yang").getPath();
39         final String path2 = TypeProviderIntegrationTest.class.getResource(
40                 "/type-provider/ietf-inet-types@2010-09-24.yang").getPath();
41         context = resolveSchemaContextFromFiles(path1, path2);
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("new " + PKG + "MyEnumeration(" + 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     private static SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
247         final YangModelParser parser = new YangParserImpl();
248
249         final List<File> inputFiles = new ArrayList<File>();
250         for (int i = 0; i < yangFiles.length; ++i) {
251             inputFiles.add(new File(yangFiles[i]));
252         }
253
254         final Set<Module> modules = parser.parseYangModels(inputFiles);
255         return parser.resolveSchemaContext(modules);
256     }
257
258 }