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;
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;
16 import com.google.common.base.Optional;
18 import java.io.IOException;
19 import java.io.Serializable;
21 import java.net.URISyntaxException;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
29 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
30 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
31 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
32 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
33 import org.opendaylight.yangtools.yang.binding.BindingMapping;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
42 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
43 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
44 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
46 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
47 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
48 import org.opendaylight.yangtools.yang.model.util.Decimal64;
49 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
50 import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder;
51 import org.opendaylight.yangtools.yang.model.util.Int16;
52 import org.opendaylight.yangtools.yang.model.util.StringType;
53 import org.opendaylight.yangtools.yang.model.util.Uint16;
54 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
55 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
57 public class BindingGeneratorUtilTest {
60 public ExpectedException expectedEx = ExpectedException.none();
62 private static List<File> loadTestResources(final String testFile) {
63 final List<File> testModels = new ArrayList<File>();
66 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
67 } catch (URISyntaxException e) {
68 throw new IllegalArgumentException("Failed to load sources from " + testFile);
70 testModels.add(listModelFile);
77 * <li>moduleNamespaceToPackageName</li> - with revision
78 * <li>packageNameForGeneratedType</li>
80 * <li>validateJavaPackage</li>
82 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
83 * - without revision </ul>
85 * @throws IOException IOException
88 public void testBindingGeneratorUtilMethods() throws IOException {
89 List<File> testModels = loadTestResources("/module.yang");
90 final YangContextParser parser = new YangParserImpl();
91 final Set<Module> modules = parser.parseFiles(testModels).getModules();
92 String packageName = "";
94 for (Module m : modules) {
98 assertNotNull("Module can't be null", module);
100 // test of the method moduleNamespaceToPackageName()
101 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
102 assertEquals("Generated package name is incorrect.",
103 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
105 // test of the method packageNameForGeneratedType()
106 DataNodeIterator it = new DataNodeIterator(module);
107 List<ContainerSchemaNode> schemaContainers = it.allContainers();
108 String subPackageNameForDataNode = "";
109 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
110 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
111 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
112 containerSchemaNode.getPath());
116 assertEquals("The name of the subpackage is incorrect.",
117 "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",
118 subPackageNameForDataNode);
120 // test of the method packageNameForTypeDefinition
121 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
122 String subPackageNameForTypeDefinition = "";
123 TypeDefinition<?> firstTypeDef = null;
125 for (TypeDefinition<?> tpDef : typeDefinitions) {
126 if (tpDef.getQName().getLocalName().equals("tpdf")) {
127 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
128 firstTypeDef = tpDef;
132 assertEquals("The name of the subpackage is incorrect.",
133 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
134 subPackageNameForTypeDefinition);
136 // test method getRestrictions
137 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
138 assertNotNull(restriction);
140 // test method computeDefaultSUID
141 GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
142 genTypeBuilder.addMethod("testMethod");
143 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
144 genTypeBuilder.addEnclosingTransferObject("testObject");
145 genTypeBuilder.addProperty("newProp");
146 GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
147 genTypeBuilder.addImplementsType(genType);
148 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
150 GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
151 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
152 assertNotEquals(computedSUID, computedSUID2);
154 // test of exception part of the method moduleNamespaceToPackageName()
155 ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision", null);
156 moduleBuilder.setSource("");
157 Module moduleWithoutRevision = moduleBuilder.build();
158 boolean passedSuccesfully = false;
160 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
161 passedSuccesfully = true;
162 } catch (IllegalArgumentException e) {
164 assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
169 * Test for the method
171 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
172 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
177 public void testPackageNameForTypeDefinitionNullBasePackageName() {
178 expectedEx.expect(IllegalArgumentException.class);
179 expectedEx.expectMessage("Base Package Name cannot be NULL!");
180 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
184 * Test for the method
186 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
187 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
192 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
193 expectedEx.expect(IllegalArgumentException.class);
194 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
195 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
199 * Test for the method
201 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
202 * packageNameForGeneratedType(String, SchemaPath)}</li>
206 public void testPackageNameForGeneratedTypeNullBasePackageName() {
207 expectedEx.expect(NullPointerException.class);
208 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
212 * Test for the method
214 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
215 * packageNameForGeneratedType(String, SchemaPath)}</li>
219 public void testPackageNameForGeneratedTypeNullSchemaPath() {
220 expectedEx.expect(NullPointerException.class);
221 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
225 * Test for the method
227 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
228 * parseToClassName(String)}</li>
232 public void testParseToClassNameNullValue() {
233 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
234 assertEquals("TestClassName", className);
236 expectedEx.expect(IllegalArgumentException.class);
237 expectedEx.expectMessage("Name can not be null");
238 className = BindingGeneratorUtil.parseToClassName(null);
242 * Test for the method
244 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
245 * parseToClassName(String)}</li>
249 public void testParseToClassNameEmptyValue() {
250 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
251 assertEquals("TestClassName", className);
253 expectedEx.expect(IllegalArgumentException.class);
254 expectedEx.expectMessage("Name can not be empty");
255 className = BindingGeneratorUtil.parseToClassName("");
259 * Test for the method
261 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
262 * resolveJavaReservedWordEquivalency(String)}</li>
266 public void testValidateParameterName() {
267 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
268 assertEquals("Return value is incorrect.", "whatever",
269 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
270 assertEquals("Return value is incorrect.", "_case",
271 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
277 * <li>parseToClassName</li>
279 * <li>parseToCamelCase</li>
281 * <li>replaceWithCamelCase</li>
283 * </ul> <li>parseToValidParamName</li>
285 * <li>parseToCamelCase</li>
287 * <li>replaceWithCamelCase</li>
293 public void testParsingMethods() {
294 // parseToClassName method testing
295 assertEquals("Class name has incorrect format", "SomeTestingClassName",
296 BindingMapping.getClassName(" some-testing_class name "));
297 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
298 BindingMapping.getClassName(" 0 some-testing_class name "));
300 // parseToValidParamName
301 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
302 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
303 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
304 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
308 public void computeDefaultSUIDTest() {
309 GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
311 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
312 method.setAccessModifier(AccessModifier.PUBLIC);
313 generatedTypeBuilder.addProperty("myProperty");
314 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
316 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
321 public void getRestrictionsTest() {
323 Optional<String> absent = Optional.absent();
325 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
326 Int16.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
328 ArrayList<LengthConstraint> lenght = new ArrayList<LengthConstraint>();
329 ArrayList<RangeConstraint> range = new ArrayList<RangeConstraint>();
330 ArrayList<PatternConstraint> pattern = new ArrayList<PatternConstraint>();
332 lenght.add(BaseConstraints.newLengthConstraint(1, 2, absent, absent));
333 range.add(BaseConstraints.newRangeConstraint(1, 2, absent, absent));
334 pattern.add(BaseConstraints.newPatternConstraint(".*", absent, absent));
336 extTypeBuilder.lengths(lenght);
337 extTypeBuilder.ranges(range);
338 extTypeBuilder.patterns(pattern);
340 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extTypeBuilder.build());
342 assertNotNull(restrictions);
344 assertEquals(1, restrictions.getLengthConstraints().size());
345 assertEquals(1, restrictions.getRangeConstraints().size());
346 assertEquals(1, restrictions.getPatternConstraints().size());
348 assertFalse(restrictions.isEmpty());
349 assertTrue(restrictions.getLengthConstraints().contains(
350 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
351 assertTrue(restrictions.getRangeConstraints()
352 .contains(BaseConstraints.newRangeConstraint(1, 2, absent, absent)));
353 assertTrue(restrictions.getPatternConstraints().contains(
354 BaseConstraints.newPatternConstraint(".*", absent, absent)));
358 public void getEmptyRestrictionsTest() {
360 Optional<String> absent = Optional.absent();
362 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
363 StringType.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
365 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extTypeBuilder.build());
367 assertNotNull(restrictions);
368 assertTrue(restrictions.isEmpty());
373 public void getDefaultIntegerRestrictionsTest() {
375 Optional<String> absent = Optional.absent();
377 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
378 Int16.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
380 ExtendedType extType = extTypeBuilder.build();
381 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType);
383 assertNotNull(restrictions);
384 assertFalse(restrictions.isEmpty());
385 assertEquals(((IntegerTypeDefinition) extType.getBaseType()).getRangeConstraints(),
386 restrictions.getRangeConstraints());
387 assertTrue(restrictions.getLengthConstraints().isEmpty());
388 assertTrue(restrictions.getPatternConstraints().isEmpty());
393 public void getDefaultUnsignedIntegerRestrictionsTest() {
395 Optional<String> absent = Optional.absent();
397 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
398 Uint16.getInstance(), absent, absent, SchemaPath.create(true, QName.create("/root")));
400 ExtendedType extType = extTypeBuilder.build();
401 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType);
403 assertNotNull(restrictions);
404 assertFalse(restrictions.isEmpty());
405 assertEquals(((UnsignedIntegerTypeDefinition) extType.getBaseType()).getRangeConstraints(),
406 restrictions.getRangeConstraints());
407 assertTrue(restrictions.getLengthConstraints().isEmpty());
408 assertTrue(restrictions.getPatternConstraints().isEmpty());
412 public void getDefaultDecimalRestrictionsTest() {
414 Optional<String> absent = Optional.absent();
415 SchemaPath path = SchemaPath.create(true, QName.create("/root"));
417 Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"),
418 Decimal64.create(path, 10), absent, absent, path);
420 ExtendedType extType = extTypeBuilder.build();
421 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType);
423 assertNotNull(restrictions);
424 assertFalse(restrictions.isEmpty());
425 assertEquals(((DecimalTypeDefinition) extType.getBaseType()).getRangeConstraints(),
426 restrictions.getRangeConstraints());
427 assertTrue(restrictions.getLengthConstraints().isEmpty());
428 assertTrue(restrictions.getPatternConstraints().isEmpty());