1 package org.opendaylight.yangtools.binding.generator.util;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
9 import java.util.ArrayList;
10 import java.util.List;
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
18 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
19 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
20 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
22 public class BindingGeneratorUtilTest {
24 private static List<File> loadTestResources(String testFile) {
25 final List<File> testModels = new ArrayList<File>();
26 final File listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).getPath());
27 testModels.add(listModelFile);
34 * <li>moduleNamespaceToPackageName</li> - with revision
35 * <li>packageNameForGeneratedType</li>
37 * <li>validateJavaPackage</li>
39 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
40 * - without revision </ul>
43 public void testBindingGeneratorUtilMethods() {
44 List<File> testModels = loadTestResources("/module.yang");
45 final YangModelParser parser = new YangParserImpl();
46 final Set<Module> modules = parser.parseYangModels(testModels);
47 String packageName = "";
49 for (Module m : modules) {
53 assertNotNull("Module can't be null", module);
55 // test of the method moduleNamespaceToPackageName()
56 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
57 assertEquals("Generated package name is incorrect.",
58 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
60 // test of the method packageNameForGeneratedType()
61 DataNodeIterator it = new DataNodeIterator(module);
62 List<ContainerSchemaNode> schemaContainers = it.allContainers();
63 String subPackageNameForDataNode = "";
64 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
65 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
66 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
67 containerSchemaNode.getPath());
71 assertEquals("The name of the subpackage is incorrect.",
72 "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",
73 subPackageNameForDataNode);
75 // test of the method packageNameForTypeDefinition
76 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
77 String subPackageNameForTypeDefinition = "";
78 for (TypeDefinition<?> tpDef : typeDefinitions) {
79 if (tpDef.getQName().getLocalName().equals("tpdf")) {
80 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
84 assertEquals("The name of the subpackage is incorrect.",
85 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
86 subPackageNameForTypeDefinition);
88 // test of exception part of the method moduleNamespaceToPackageName()
89 ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision");
90 Module moduleWithoutRevision = moduleBuilder.build();
91 boolean passedSuccesfully = false;
93 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
94 passedSuccesfully = true;
95 } catch (IllegalArgumentException e) {
97 assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
102 * Test for the method
104 * <li>{@link BindingGeneratorUtil#validateParameterName()
105 * validateParameterName()}</li>
109 public void testValidateParameterName() {
110 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
111 assertEquals("Return value is incorrect.", "whatever",
112 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
113 assertEquals("Return value is incorrect.", "_case",
114 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
120 * <li>parseToClassName</li>
122 * <li>parseToCamelCase</li>
124 * <li>replaceWithCamelCase</li>
126 * </ul> <li>parseToValidParamName</li>
128 * <li>parseToCamelCase</li>
130 * <li>replaceWithCamelCase</li>
136 public void testParsingMethods() {
137 // parseToClassName method testing
138 assertEquals("Class name has incorrect format", "SomeTestingClassName",
139 BindingGeneratorUtil.parseToClassName(" some-testing_class name "));
140 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
141 BindingGeneratorUtil.parseToClassName(" 0 some-testing_class name "));
143 // parseToValidParamName
144 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
145 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
146 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
147 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));