2 * Copyright (c) 2014 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.binding.generator.util.stmt.parser.retest;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.binding.generator.util.Types;
20 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
22 import com.google.common.base.Optional;
25 import java.io.IOException;
26 import java.io.Serializable;
28 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.List;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
37 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
38 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
39 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
40 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
41 import org.opendaylight.yangtools.yang.binding.BindingMapping;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
50 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
51 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
52 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
53 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
54 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
55 import org.opendaylight.yangtools.yang.model.util.Decimal64;
56 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
57 import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder;
58 import org.opendaylight.yangtools.yang.model.util.Int16;
59 import org.opendaylight.yangtools.yang.model.util.StringType;
60 import org.opendaylight.yangtools.yang.model.util.Uint16;
61 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
63 public class BindingGeneratorUtilTest {
66 public ExpectedException expectedEx = ExpectedException.none();
68 private static List<File> loadTestResources(String testFile) {
69 final List<File> testModels = new ArrayList<File>();
72 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
73 } catch (URISyntaxException e) {
74 throw new IllegalArgumentException("Failed to load sources from " + testFile);
76 testModels.add(listModelFile);
83 * <li>moduleNamespaceToPackageName</li> - with revision
84 * <li>packageNameForGeneratedType</li>
86 * <li>validateJavaPackage</li>
88 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
89 * - without revision </ul>
90 * @throws ReactorException Reactor exception
91 * @throws SourceException Source exception
94 public void testBindingGeneratorUtilMethods() throws IOException, SourceException, ReactorException {
95 List<File> testModels = loadTestResources("/module.yang");
97 final Set<Module> modules = RetestUtils.parseYangSources(testModels).getModules();
98 String packageName = "";
100 for (Module m : modules) {
104 assertNotNull("Module can't be null", module);
106 // test of the method moduleNamespaceToPackageName()
107 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
108 assertEquals("Generated package name is incorrect.",
109 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
111 // test of the method packageNameForGeneratedType()
112 DataNodeIterator it = new DataNodeIterator(module);
113 List<ContainerSchemaNode> schemaContainers = it.allContainers();
114 String subPackageNameForDataNode = "";
115 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
116 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
117 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
118 containerSchemaNode.getPath());
122 assertEquals("The name of the subpackage is incorrect.",
123 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910.cont.outter",
124 subPackageNameForDataNode);
126 // test of the method packageNameForTypeDefinition
127 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
128 String subPackageNameForTypeDefinition = "";
129 TypeDefinition<?> firstTypeDef = null;
131 for (TypeDefinition<?> tpDef : typeDefinitions) {
132 if (tpDef.getQName().getLocalName().equals("tpdf")) {
133 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
134 firstTypeDef = tpDef;
138 assertEquals("The name of the subpackage is incorrect.",
139 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
140 subPackageNameForTypeDefinition);
142 // test method getRestrictions
143 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
144 assertNotNull(restriction);
146 // test method computeDefaultSUID
147 GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
148 genTypeBuilder.addMethod("testMethod");
149 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
150 genTypeBuilder.addEnclosingTransferObject("testObject");
151 genTypeBuilder.addProperty("newProp");
152 GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
153 genTypeBuilder.addImplementsType(genType);
154 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
156 GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
157 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
158 assertNotEquals(computedSUID, computedSUID2);
160 // test of exception part of the method moduleNamespaceToPackageName()
161 ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision", null);
162 moduleBuilder.setSource("");
163 Module moduleWithoutRevision = moduleBuilder.build();
164 boolean passedSuccesfully = false;
166 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
167 passedSuccesfully = true;
168 } catch (IllegalArgumentException e) {
170 assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
175 * Test for the method
177 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
178 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
183 public void testPackageNameForTypeDefinitionNullBasePackageName() {
184 expectedEx.expect(IllegalArgumentException.class);
185 expectedEx.expectMessage("Base Package Name cannot be NULL!");
186 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
190 * Test for the method
192 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
193 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
198 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
199 expectedEx.expect(IllegalArgumentException.class);
200 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
201 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
205 * Test for the method
207 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
208 * packageNameForGeneratedType(String, SchemaPath)}</li>
212 public void testPackageNameForGeneratedTypeNullBasePackageName() {
213 expectedEx.expect(NullPointerException.class);
214 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
218 * Test for the method
220 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
221 * packageNameForGeneratedType(String, SchemaPath)}</li>
225 public void testPackageNameForGeneratedTypeNullSchemaPath() {
226 expectedEx.expect(NullPointerException.class);
227 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
231 * Test for the method
233 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
234 * parseToClassName(String)}</li>
238 public void testParseToClassNameNullValue() {
239 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
240 assertEquals("TestClassName", className);
242 expectedEx.expect(IllegalArgumentException.class);
243 expectedEx.expectMessage("Name can not be null");
244 className = BindingGeneratorUtil.parseToClassName(null);
248 * Test for the method
250 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
251 * parseToClassName(String)}</li>
255 public void testParseToClassNameEmptyValue() {
256 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
257 assertEquals("TestClassName", className);
259 expectedEx.expect(IllegalArgumentException.class);
260 expectedEx.expectMessage("Name can not be empty");
261 className = BindingGeneratorUtil.parseToClassName("");
265 * Test for the method
267 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
268 * resolveJavaReservedWordEquivalency(String)}</li>
272 public void testValidateParameterName() {
273 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
274 assertEquals("Return value is incorrect.", "whatever",
275 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
276 assertEquals("Return value is incorrect.", "_case",
277 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
283 * <li>parseToClassName</li>
285 * <li>parseToCamelCase</li>
287 * <li>replaceWithCamelCase</li>
289 * </ul> <li>parseToValidParamName</li>
291 * <li>parseToCamelCase</li>
293 * <li>replaceWithCamelCase</li>
299 public void testParsingMethods() {
300 // parseToClassName method testing
301 assertEquals("Class name has incorrect format", "SomeTestingClassName",
302 BindingMapping.getClassName(" some-testing_class name "));
303 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
304 BindingMapping.getClassName(" 0 some-testing_class name "));
306 // parseToValidParamName
307 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
308 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
309 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
310 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
314 public void computeDefaultSUIDTest() {
315 GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
317 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
318 method.setAccessModifier(AccessModifier.PUBLIC);
319 generatedTypeBuilder.addProperty("myProperty");
320 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
322 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
327 public void getRestrictionsTest() {
329 Optional<String> absent = Optional.absent();
331 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
332 Int16.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
334 ArrayList<LengthConstraint> lenght = new ArrayList<LengthConstraint>();
335 ArrayList<RangeConstraint> range = new ArrayList<RangeConstraint>();
336 ArrayList<PatternConstraint> pattern = new ArrayList<PatternConstraint>();
338 lenght.add(BaseConstraints.newLengthConstraint(1, 2, absent, absent));
339 range.add(BaseConstraints.newRangeConstraint(1, 2, absent, absent));
340 pattern.add(BaseConstraints.newPatternConstraint(".*", absent, absent));
342 extTypeBuilder.lengths(lenght);
343 extTypeBuilder.ranges(range);
344 extTypeBuilder.patterns(pattern);
346 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extTypeBuilder.build());
348 assertNotNull(restrictions);
350 assertEquals(1, restrictions.getLengthConstraints().size());
351 assertEquals(1, restrictions.getRangeConstraints().size());
352 assertEquals(1, restrictions.getPatternConstraints().size());
354 assertFalse(restrictions.isEmpty());
355 assertTrue(restrictions.getLengthConstraints().contains(
356 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
357 assertTrue(restrictions.getRangeConstraints()
358 .contains(BaseConstraints.newRangeConstraint(1, 2, absent, absent)));
359 assertTrue(restrictions.getPatternConstraints().contains(
360 BaseConstraints.newPatternConstraint(".*", absent, absent)));
364 public void getEmptyRestrictionsTest() {
366 Optional<String> absent = Optional.absent();
368 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
369 StringType.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
371 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extTypeBuilder.build());
373 assertNotNull(restrictions);
374 assertTrue(restrictions.isEmpty());
379 public void getDefaultIntegerRestrictionsTest() {
381 Optional<String> absent = Optional.absent();
383 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
384 Int16.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
386 ExtendedType extType = extTypeBuilder.build();
387 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType);
389 assertNotNull(restrictions);
390 assertFalse(restrictions.isEmpty());
391 assertEquals(((IntegerTypeDefinition) extType.getBaseType()).getRangeConstraints(),
392 restrictions.getRangeConstraints());
393 assertTrue(restrictions.getLengthConstraints().isEmpty());
394 assertTrue(restrictions.getPatternConstraints().isEmpty());
399 public void getDefaultUnsignedIntegerRestrictionsTest() {
401 Optional<String> absent = Optional.absent();
403 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
404 Uint16.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
406 ExtendedType extType = extTypeBuilder.build();
407 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType);
409 assertNotNull(restrictions);
410 assertFalse(restrictions.isEmpty());
411 assertEquals(((UnsignedIntegerTypeDefinition) extType.getBaseType()).getRangeConstraints(),
412 restrictions.getRangeConstraints());
413 assertTrue(restrictions.getLengthConstraints().isEmpty());
414 assertTrue(restrictions.getPatternConstraints().isEmpty());
418 public void getDefaultDecimalRestrictionsTest() {
420 Optional<String> absent = Optional.absent();
421 SchemaPath path = SchemaPath.create(true, QName.create("/root"));
423 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
424 Decimal64.create(path, 10), absent, absent, path);
426 ExtendedType extType = extTypeBuilder.build();
427 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType);
429 assertNotNull(restrictions);
430 assertFalse(restrictions.isEmpty());
431 assertEquals(((DecimalTypeDefinition) extType.getBaseType()).getRangeConstraints(),
432 restrictions.getRangeConstraints());
433 assertTrue(restrictions.getLengthConstraints().isEmpty());
434 assertTrue(restrictions.getPatternConstraints().isEmpty());