BUG-1902: reuse ConcreteTypeImpl instances
[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.io.IOException;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
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.YangContextParser;
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() throws IOException {
58         List<File> testModels = loadTestResources("/module.yang");
59         final YangContextParser parser = new YangParserImpl();
60         final Set<Module> modules = parser.parseFiles(testModels).getModules();
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         moduleBuilder.setSource("");
105         Module moduleWithoutRevision = moduleBuilder.build();
106         boolean passedSuccesfully = false;
107         try {
108             BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
109             passedSuccesfully = true;
110         } catch (IllegalArgumentException e) {
111         }
112         assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
113
114     }
115
116     /**
117      * Test for the method
118      * <ul>
119      * <li>{@link BindingGeneratorUtil#validateParameterName()
120      * validateParameterName()}</li>
121      * <ul>
122      */
123     @Test
124     public void testValidateParameterName() {
125         assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
126         assertEquals("Return value is incorrect.", "whatever",
127                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
128         assertEquals("Return value is incorrect.", "_case",
129                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
130     }
131
132     /**
133      * Tests the methods:
134      * <ul>
135      * <li>parseToClassName</li>
136      * <ul>
137      * <li>parseToCamelCase</li>
138      * <ul>
139      * <li>replaceWithCamelCase</li>
140      * </ul>
141      * </ul> <li>parseToValidParamName</li>
142      * <ul>
143      * <li>parseToCamelCase</li>
144      * <ul>
145      * <li>replaceWithCamelCase</li>
146      * </ul>
147      * </ul>
148      * <ul>
149      */
150     @Test
151     public void testParsingMethods() {
152         // parseToClassName method testing
153         assertEquals("Class name has incorrect format", "SomeTestingClassName",
154                 BindingMapping.getClassName("  some-testing_class name   "));
155         assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
156                 BindingMapping.getClassName("  0 some-testing_class name   "));
157
158         // parseToValidParamName
159         assertEquals("Parameter name has incorrect format", "someTestingParameterName",
160                 BindingGeneratorUtil.parseToValidParamName("  some-testing_parameter   name   "));
161         assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
162                 BindingGeneratorUtil.parseToValidParamName("  0some-testing_parameter   name   "));
163     }
164
165 }