2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.binding.generator.util;
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;
16 import java.util.ArrayList;
17 import java.util.List;
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;
29 public class BindingGeneratorUtilTest {
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);
41 * <li>moduleNamespaceToPackageName</li> - with revision
42 * <li>packageNameForGeneratedType</li>
44 * <li>validateJavaPackage</li>
46 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
47 * - without revision </ul>
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 = "";
56 for (Module m : modules) {
60 assertNotNull("Module can't be null", module);
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);
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());
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);
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);
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);
95 // test of exception part of the method moduleNamespaceToPackageName()
96 ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision");
97 Module moduleWithoutRevision = moduleBuilder.build();
98 boolean passedSuccesfully = false;
100 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
101 passedSuccesfully = true;
102 } catch (IllegalArgumentException e) {
104 assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
109 * Test for the method
111 * <li>{@link BindingGeneratorUtil#validateParameterName()
112 * validateParameterName()}</li>
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"));
127 * <li>parseToClassName</li>
129 * <li>parseToCamelCase</li>
131 * <li>replaceWithCamelCase</li>
133 * </ul> <li>parseToValidParamName</li>
135 * <li>parseToCamelCase</li>
137 * <li>replaceWithCamelCase</li>
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 "));
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 "));