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