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;
16 import java.util.Arrays;
18 import org.junit.Before;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 public class TypeProviderIntegrationTest {
30 private final String PKG = "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.";
31 private static SchemaContext context;
32 private TypeProviderImpl provider;
36 public static void setup() throws Exception {
37 File abstractTopology = new File(TypeProviderIntegrationTest.class.getResource("/type-provider/test.yang")
39 File ietfInetTypes = new File(TypeProviderIntegrationTest.class.getResource("/ietf/ietf-inet-types.yang")
41 context = new YangParserImpl().parseFiles(Arrays.asList(abstractTopology, ietfInetTypes));
42 assertNotNull(context);
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"));
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);
57 leaf = (LeafSchemaNode) m.getDataChildByName("ext-binary");
58 actual = provider.getTypeDefaultConstruction(leaf);
59 assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
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);
68 leaf = (LeafSchemaNode) m.getDataChildByName("ext-bits");
69 actual = provider.getTypeDefaultConstruction(leaf);
70 assertEquals("new " + PKG + "MyBits(false, false, true)", actual);
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);
79 leaf = (LeafSchemaNode) m.getDataChildByName("ext-boolean");
80 actual = provider.getTypeDefaultConstruction(leaf);
81 assertEquals("new " + PKG + "MyBoolean(new java.lang.Boolean(\"true\"))", actual);
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);
90 leaf = (LeafSchemaNode) m.getDataChildByName("ext-decimal64");
91 actual = provider.getTypeDefaultConstruction(leaf);
92 assertEquals("new " + PKG + "MyDecimal64(new java.math.BigDecimal(\"3.14\"))", actual);
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);
101 leaf = (LeafSchemaNode) m.getDataChildByName("ext-empty");
102 actual = provider.getTypeDefaultConstruction(leaf);
103 assertEquals("new " + PKG + "MyEmpty(new java.lang.Boolean(\"false\"))", actual);
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);
112 leaf = (LeafSchemaNode) m.getDataChildByName("ext-enumeration");
113 actual = provider.getTypeDefaultConstruction(leaf);
114 assertEquals(PKG + "MyEnumeration.Seven", actual);
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);
123 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int8");
124 actual = provider.getTypeDefaultConstruction(leaf);
125 assertEquals("new " + PKG + "MyInt8(new java.lang.Byte(\"11\"))", actual);
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);
134 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int16");
135 actual = provider.getTypeDefaultConstruction(leaf);
136 assertEquals("new " + PKG + "MyInt16(new java.lang.Short(\"111\"))", actual);
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);
145 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int32");
146 actual = provider.getTypeDefaultConstruction(leaf);
147 assertEquals("new " + PKG + "MyInt32(new java.lang.Integer(\"1111\"))", actual);
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);
156 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int64");
157 actual = provider.getTypeDefaultConstruction(leaf);
158 assertEquals("new " + PKG + "MyInt64(new java.lang.Long(\"11111\"))", actual);
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);
167 leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref");
168 actual = provider.getTypeDefaultConstruction(leaf);
169 assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
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);
178 leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref1");
179 actual = provider.getTypeDefaultConstruction(leaf);
180 assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
184 public void testGetTypeDefaultConstructionString() throws ParseException {
185 LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-string");
186 String actual = provider.getTypeDefaultConstruction(leaf);
187 assertEquals("\"name\"", actual);
189 leaf = (LeafSchemaNode) m.getDataChildByName("ext-string");
190 actual = provider.getTypeDefaultConstruction(leaf);
191 assertEquals("new " + PKG + "MyString(\"name\")", actual);
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);
200 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint8");
201 actual = provider.getTypeDefaultConstruction(leaf);
202 assertEquals("new " + PKG + "MyUint8(new java.lang.Short(\"11\"))", actual);
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);
211 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint16");
212 actual = provider.getTypeDefaultConstruction(leaf);
213 assertEquals("new " + PKG + "MyUint16(new java.lang.Integer(\"111\"))", actual);
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);
222 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint32");
223 actual = provider.getTypeDefaultConstruction(leaf);
224 assertEquals("new " + PKG + "MyUint32(new java.lang.Long(\"1111\"))", actual);
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);
233 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint64");
234 actual = provider.getTypeDefaultConstruction(leaf);
235 assertEquals("new " + PKG + "MyUint64(new java.math.BigInteger(\"11111\"))", actual);
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);
247 public void testGetTypeDefaultConstructionUnion() throws ParseException {
248 LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-union");
249 String actual = provider.getTypeDefaultConstruction(leaf);
250 String expected = "new " + PKG + "TestData.LeafUnion(\"111\".toCharArray())";
251 assertEquals(expected, actual);
253 leaf = (LeafSchemaNode) m.getDataChildByName("ext-union");
254 actual = provider.getTypeDefaultConstruction(leaf);
255 expected = "new " + PKG + "MyUnion(\"111\".toCharArray())";
256 assertEquals(expected, actual);
260 public void testGetTypeDefaultConstructionUnionNested() throws ParseException {
261 ContainerSchemaNode c1 = (ContainerSchemaNode) m.getDataChildByName("c1");
262 ContainerSchemaNode c2 = (ContainerSchemaNode) c1.getDataChildByName("c2");
263 ContainerSchemaNode c3 = (ContainerSchemaNode) c2.getDataChildByName("c3");
264 LeafSchemaNode leaf = (LeafSchemaNode) c3.getDataChildByName("id");
266 String actual = provider.getTypeDefaultConstruction(leaf);
267 String expected = "new " + PKG + "NestedUnion(\"111\".toCharArray())";
268 assertEquals(expected, actual);
272 public void testGetParamNameFromType() throws ParseException {
273 m = context.findModuleByName("ietf-inet-types", new SimpleDateFormat("yyyy-MM-dd").parse("2010-09-24"));
274 Set<TypeDefinition<?>> types = m.getTypeDefinitions();
275 TypeDefinition<?> ipv4 = null;
276 TypeDefinition<?> ipv6 = null;
277 TypeDefinition<?> ipv4Pref = null;
278 TypeDefinition<?> ipv6Pref = null;
279 for (TypeDefinition<?> type : types) {
280 if ("ipv4-address".equals(type.getQName().getLocalName())) {
282 } else if ("ipv6-address".equals(type.getQName().getLocalName())) {
284 } else if ("ipv4-prefix".equals(type.getQName().getLocalName())) {
286 } else if ("ipv6-prefix".equals(type.getQName().getLocalName())) {
293 assertNotNull(ipv4Pref);
294 assertNotNull(ipv6Pref);
295 assertEquals("ipv4Address", provider.getParamNameFromType(ipv4));
296 assertEquals("ipv6Address", provider.getParamNameFromType(ipv6));
297 assertEquals("ipv4Prefix", provider.getParamNameFromType(ipv4Pref));
298 assertEquals("ipv6Prefix", provider.getParamNameFromType(ipv6Pref));