2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
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;
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;
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);
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"));
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);
54 leaf = (LeafSchemaNode) m.getDataChildByName("ext-binary");
55 actual = provider.getTypeDefaultConstruction(leaf);
56 assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
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);
65 leaf = (LeafSchemaNode) m.getDataChildByName("ext-bits");
66 actual = provider.getTypeDefaultConstruction(leaf);
67 assertEquals("new " + PKG + "MyBits(false, false, true)", actual);
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);
76 leaf = (LeafSchemaNode) m.getDataChildByName("ext-boolean");
77 actual = provider.getTypeDefaultConstruction(leaf);
78 assertEquals("new " + PKG + "MyBoolean(new java.lang.Boolean(\"true\"))", actual);
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);
87 leaf = (LeafSchemaNode) m.getDataChildByName("ext-decimal64");
88 actual = provider.getTypeDefaultConstruction(leaf);
89 assertEquals("new " + PKG + "MyDecimal64(new java.math.BigDecimal(\"3.14\"))", actual);
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);
98 leaf = (LeafSchemaNode) m.getDataChildByName("ext-empty");
99 actual = provider.getTypeDefaultConstruction(leaf);
100 assertEquals("new " + PKG + "MyEmpty(new java.lang.Boolean(\"false\"))", actual);
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);
109 leaf = (LeafSchemaNode) m.getDataChildByName("ext-enumeration");
110 actual = provider.getTypeDefaultConstruction(leaf);
111 assertEquals(PKG + "MyEnumeration.Seven", actual);
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);
120 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int8");
121 actual = provider.getTypeDefaultConstruction(leaf);
122 assertEquals("new " + PKG + "MyInt8(new java.lang.Byte(\"11\"))", actual);
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);
131 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int16");
132 actual = provider.getTypeDefaultConstruction(leaf);
133 assertEquals("new " + PKG + "MyInt16(new java.lang.Short(\"111\"))", actual);
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);
142 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int32");
143 actual = provider.getTypeDefaultConstruction(leaf);
144 assertEquals("new " + PKG + "MyInt32(new java.lang.Integer(\"1111\"))", actual);
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);
153 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int64");
154 actual = provider.getTypeDefaultConstruction(leaf);
155 assertEquals("new " + PKG + "MyInt64(new java.lang.Long(\"11111\"))", actual);
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);
164 leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref");
165 actual = provider.getTypeDefaultConstruction(leaf);
166 assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
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);
175 leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref1");
176 actual = provider.getTypeDefaultConstruction(leaf);
177 assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
181 public void testGetTypeDefaultConstructionString() throws ParseException {
182 LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-string");
183 String actual = provider.getTypeDefaultConstruction(leaf);
184 assertEquals("\"name\"", actual);
186 leaf = (LeafSchemaNode) m.getDataChildByName("ext-string");
187 actual = provider.getTypeDefaultConstruction(leaf);
188 assertEquals("new " + PKG + "MyString(\"name\")", actual);
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);
197 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint8");
198 actual = provider.getTypeDefaultConstruction(leaf);
199 assertEquals("new " + PKG + "MyUint8(new java.lang.Short(\"11\"))", actual);
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);
208 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint16");
209 actual = provider.getTypeDefaultConstruction(leaf);
210 assertEquals("new " + PKG + "MyUint16(new java.lang.Integer(\"111\"))", actual);
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);
219 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint32");
220 actual = provider.getTypeDefaultConstruction(leaf);
221 assertEquals("new " + PKG + "MyUint32(new java.lang.Long(\"1111\"))", actual);
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);
230 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint64");
231 actual = provider.getTypeDefaultConstruction(leaf);
232 assertEquals("new " + PKG + "MyUint64(new java.math.BigInteger(\"11111\"))", actual);
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);
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);
250 leaf = (LeafSchemaNode) m.getDataChildByName("ext-union");
251 actual = provider.getTypeDefaultConstruction(leaf);
252 expected = "new " + PKG + "MyUnion(\"111\".toCharArray())";
253 assertEquals(expected, actual);
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");
263 String actual = provider.getTypeDefaultConstruction(leaf);
264 String expected = "new " + PKG + "NestedUnion(\"111\".toCharArray())";
265 assertEquals(expected, actual);
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())) {
279 } else if ("ipv6-address".equals(type.getQName().getLocalName())) {
281 } else if ("ipv4-prefix".equals(type.getQName().getLocalName())) {
283 } else if ("ipv6-prefix".equals(type.getQName().getLocalName())) {
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));