Tests for class BindingGeneratorUtil
[yangtools.git] / code-generator / binding-generator-util / src / test / java / org / opendaylight / yangtools / binding / generator / util / BindingGeneratorUtilTest.java
1 package org.opendaylight.yangtools.binding.generator.util;
2
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;
7
8 import java.io.File;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Set;
12
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;
21
22 public class BindingGeneratorUtilTest {
23
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);
28         return testModels;
29     }
30
31     /**
32      * Tests methods:
33      * <ul>
34      * <li>moduleNamespaceToPackageName</li> - with revision
35      * <li>packageNameForGeneratedType</li>
36      * <ul>
37      * <li>validateJavaPackage</li>
38      * </ul>
39      * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
40      * - without revision </ul>
41      */
42     @Test
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 = "";
48         Module module = null;
49         for (Module m : modules) {
50             module = m;
51             break;
52         }
53         assertNotNull("Module can't be null", module);
54
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);
59
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());
68                 break;
69             }
70         }
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);
74
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);
81                 break;
82             }
83         }
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);
87
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;
92         try {
93             BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
94             passedSuccesfully = true;
95         } catch (IllegalArgumentException e) {
96         }
97         assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
98
99     }
100
101     /**
102      * Test for the method
103      * <ul>
104      * <li>{@link BindingGeneratorUtil#validateParameterName()
105      * validateParameterName()}</li>
106      * <ul>
107      */
108     @Test
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"));
115     }
116
117     /**
118      * Tests the methods:
119      * <ul>
120      * <li>parseToClassName</li>
121      * <ul>
122      * <li>parseToCamelCase</li>
123      * <ul>
124      * <li>replaceWithCamelCase</li>
125      * </ul>
126      * </ul> <li>parseToValidParamName</li>
127      * <ul>
128      * <li>parseToCamelCase</li>
129      * <ul>
130      * <li>replaceWithCamelCase</li>
131      * </ul>
132      * </ul>
133      * <ul>
134      */
135     @Test
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   "));
142
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   "));
148     }
149
150 }