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