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