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