2 * Copyright (c) 2016 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 File abstractTopology = new File(TypeProviderIntegrationTest.class.getResource("/type-provider/test.yang")
37 File ietfInetTypes = new File(TypeProviderIntegrationTest.class.getResource("/ietf/ietf-inet-types.yang")
39 context = TestUtils.parseYangSources(abstractTopology, ietfInetTypes);
40 assertNotNull(context);
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"));
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);
55 leaf = (LeafSchemaNode) m.getDataChildByName("ext-binary");
56 actual = provider.getTypeDefaultConstruction(leaf);
57 assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
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);
66 leaf = (LeafSchemaNode) m.getDataChildByName("ext-bits");
67 actual = provider.getTypeDefaultConstruction(leaf);
68 assertEquals("new " + PKG + "MyBits(false, false, true)", actual);
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);
77 leaf = (LeafSchemaNode) m.getDataChildByName("ext-boolean");
78 actual = provider.getTypeDefaultConstruction(leaf);
79 assertEquals("new " + PKG + "MyBoolean(new java.lang.Boolean(\"true\"))", actual);
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);
88 leaf = (LeafSchemaNode) m.getDataChildByName("ext-decimal64");
89 actual = provider.getTypeDefaultConstruction(leaf);
90 assertEquals("new " + PKG + "MyDecimal64(new java.math.BigDecimal(\"3.14\"))", actual);
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);
99 leaf = (LeafSchemaNode) m.getDataChildByName("ext-empty");
100 actual = provider.getTypeDefaultConstruction(leaf);
101 assertEquals("new " + PKG + "MyEmpty(new java.lang.Boolean(\"false\"))", actual);
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);
110 leaf = (LeafSchemaNode) m.getDataChildByName("ext-enumeration");
111 actual = provider.getTypeDefaultConstruction(leaf);
112 assertEquals(PKG + "MyEnumeration.Seven", actual);
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);
121 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int8");
122 actual = provider.getTypeDefaultConstruction(leaf);
123 assertEquals("new " + PKG + "MyInt8(new java.lang.Byte(\"11\"))", actual);
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);
132 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int16");
133 actual = provider.getTypeDefaultConstruction(leaf);
134 assertEquals("new " + PKG + "MyInt16(new java.lang.Short(\"111\"))", actual);
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);
143 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int32");
144 actual = provider.getTypeDefaultConstruction(leaf);
145 assertEquals("new " + PKG + "MyInt32(new java.lang.Integer(\"1111\"))", actual);
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);
154 leaf = (LeafSchemaNode) m.getDataChildByName("ext-int64");
155 actual = provider.getTypeDefaultConstruction(leaf);
156 assertEquals("new " + PKG + "MyInt64(new java.lang.Long(\"11111\"))", actual);
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);
165 leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref");
166 actual = provider.getTypeDefaultConstruction(leaf);
167 assertEquals("new java.math.BigDecimal(\"1.234\")", actual);
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);
176 leaf = (LeafSchemaNode) m.getDataChildByName("ext-leafref1");
177 actual = provider.getTypeDefaultConstruction(leaf);
178 assertEquals("new " + PKG + "MyBinary(new byte[] {77, 97, 110})", actual);
182 public void testGetTypeDefaultConstructionString() throws ParseException {
183 LeafSchemaNode leaf = (LeafSchemaNode) m.getDataChildByName("leaf-string");
184 String actual = provider.getTypeDefaultConstruction(leaf);
185 assertEquals("\"name\"", actual);
187 leaf = (LeafSchemaNode) m.getDataChildByName("ext-string");
188 actual = provider.getTypeDefaultConstruction(leaf);
189 assertEquals("new " + PKG + "MyString(\"name\")", actual);
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);
198 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint8");
199 actual = provider.getTypeDefaultConstruction(leaf);
200 assertEquals("new " + PKG + "MyUint8(new java.lang.Short(\"11\"))", actual);
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);
209 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint16");
210 actual = provider.getTypeDefaultConstruction(leaf);
211 assertEquals("new " + PKG + "MyUint16(new java.lang.Integer(\"111\"))", actual);
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);
220 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint32");
221 actual = provider.getTypeDefaultConstruction(leaf);
222 assertEquals("new " + PKG + "MyUint32(new java.lang.Long(\"1111\"))", actual);
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);
231 leaf = (LeafSchemaNode) m.getDataChildByName("ext-uint64");
232 actual = provider.getTypeDefaultConstruction(leaf);
233 assertEquals("new " + PKG + "MyUint64(new java.math.BigInteger(\"11111\"))", actual);
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);
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);
251 leaf = (LeafSchemaNode) m.getDataChildByName("ext-union");
252 actual = provider.getTypeDefaultConstruction(leaf);
253 expected = "new " + PKG + "MyUnion(\"111\".toCharArray())";
254 assertEquals(expected, actual);
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");
264 String actual = provider.getTypeDefaultConstruction(leaf);
265 String expected = "new " + PKG + "NestedUnion(\"111\".toCharArray())";
266 assertEquals(expected, actual);
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())) {
280 } else if ("ipv6-address".equals(type.getQName().getLocalName())) {
282 } else if ("ipv4-prefix".equals(type.getQName().getLocalName())) {
284 } else if ("ipv6-prefix".equals(type.getQName().getLocalName())) {
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));