Implemented support for generating YangModuleInfo implementation.
[yangtools.git] / code-generator / binding-generator-util / src / test / java / org / opendaylight / yangtools / binding / generator / util / BindingGeneratorUtilTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.binding.generator.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
25 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
26 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28
29 public class BindingGeneratorUtilTest {
30
31     private static List<File> loadTestResources(String testFile) {
32         final List<File> testModels = new ArrayList<File>();
33         final File listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).getPath());
34         testModels.add(listModelFile);
35         return testModels;
36     }
37
38     /**
39      * Tests methods:
40      * <ul>
41      * <li>moduleNamespaceToPackageName</li> - with revision
42      * <li>packageNameForGeneratedType</li>
43      * <ul>
44      * <li>validateJavaPackage</li>
45      * </ul>
46      * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
47      * - without revision </ul>
48      */
49     @Test
50     public void testBindingGeneratorUtilMethods() {
51         List<File> testModels = loadTestResources("/module.yang");
52         final YangModelParser parser = new YangParserImpl();
53         final Set<Module> modules = parser.parseYangModels(testModels);
54         String packageName = "";
55         Module module = null;
56         for (Module m : modules) {
57             module = m;
58             break;
59         }
60         assertNotNull("Module can't be null", module);
61
62         // test of the method moduleNamespaceToPackageName()
63         packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
64         assertEquals("Generated package name is incorrect.",
65                 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
66
67         // test of the method packageNameForGeneratedType()
68         DataNodeIterator it = new DataNodeIterator(module);
69         List<ContainerSchemaNode> schemaContainers = it.allContainers();
70         String subPackageNameForDataNode = "";
71         for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
72             if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
73                 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
74                         containerSchemaNode.getPath());
75                 break;
76             }
77         }
78         assertEquals("The name of the subpackage is incorrect.",
79                 "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",
80                 subPackageNameForDataNode);
81
82         // test of the method packageNameForTypeDefinition
83         Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
84         String subPackageNameForTypeDefinition = "";
85         for (TypeDefinition<?> tpDef : typeDefinitions) {
86             if (tpDef.getQName().getLocalName().equals("tpdf")) {
87                 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
88                 break;
89             }
90         }
91         assertEquals("The name of the subpackage is incorrect.",
92                 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
93                 subPackageNameForTypeDefinition);
94
95         // test of exception part of the method moduleNamespaceToPackageName()
96         ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision", null);
97         Module moduleWithoutRevision = moduleBuilder.build();
98         boolean passedSuccesfully = false;
99         try {
100             BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
101             passedSuccesfully = true;
102         } catch (IllegalArgumentException e) {
103         }
104         assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
105
106     }
107
108     /**
109      * Test for the method
110      * <ul>
111      * <li>{@link BindingGeneratorUtil#validateParameterName()
112      * validateParameterName()}</li>
113      * <ul>
114      */
115     @Test
116     public void testValidateParameterName() {
117         assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
118         assertEquals("Return value is incorrect.", "whatever",
119                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
120         assertEquals("Return value is incorrect.", "_case",
121                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
122     }
123
124     /**
125      * Tests the methods:
126      * <ul>
127      * <li>parseToClassName</li>
128      * <ul>
129      * <li>parseToCamelCase</li>
130      * <ul>
131      * <li>replaceWithCamelCase</li>
132      * </ul>
133      * </ul> <li>parseToValidParamName</li>
134      * <ul>
135      * <li>parseToCamelCase</li>
136      * <ul>
137      * <li>replaceWithCamelCase</li>
138      * </ul>
139      * </ul>
140      * <ul>
141      */
142     @Test
143     public void testParsingMethods() {
144         // parseToClassName method testing
145         assertEquals("Class name has incorrect format", "SomeTestingClassName",
146                 BindingGeneratorUtil.parseToClassName("  some-testing_class name   "));
147         assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
148                 BindingGeneratorUtil.parseToClassName("  0 some-testing_class name   "));
149
150         // parseToValidParamName
151         assertEquals("Parameter name has incorrect format", "someTestingParameterName",
152                 BindingGeneratorUtil.parseToValidParamName("  some-testing_parameter   name   "));
153         assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
154                 BindingGeneratorUtil.parseToValidParamName("  0some-testing_parameter   name   "));
155     }
156
157 }