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.ArrayList;
17 import java.util.List;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public class TypeProviderIntegrationTest {
31 private final String PKG = "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.";
32 private static SchemaContext context;
33 private TypeProviderImpl provider;
37 public static void setup() throws ParseException {
38 final String path1 = TypeProviderIntegrationTest.class.getResource("/type-provider/test.yang").getPath();
39 final String path2 = TypeProviderIntegrationTest.class.getResource(
40 "/type-provider/ietf-inet-types@2010-09-24.yang").getPath();
41 context = resolveSchemaContextFromFiles(path1, path2);
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("new " + PKG + "MyEnumeration(" + 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);
246 private static SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
247 final YangModelParser parser = new YangParserImpl();
249 final List<File> inputFiles = new ArrayList<File>();
250 for (int i = 0; i < yangFiles.length; ++i) {
251 inputFiles.add(new File(yangFiles[i]));
254 final Set<Module> modules = parser.parseYangModels(inputFiles);
255 return parser.resolveSchemaContext(modules);