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