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.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.List;
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;
30 public class BindingGeneratorUtilTest {
32 private static List<File> loadTestResources(String testFile) {
33 final List<File> testModels = new ArrayList<File>();
36 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
37 } catch (URISyntaxException e) {
38 throw new IllegalArgumentException("Failed to load sources from " + testFile);
40 testModels.add(listModelFile);
47 * <li>moduleNamespaceToPackageName</li> - with revision
48 * <li>packageNameForGeneratedType</li>
50 * <li>validateJavaPackage</li>
52 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
53 * - without revision </ul>
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 = "";
62 for (Module m : modules) {
66 assertNotNull("Module can't be null", module);
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);
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());
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);
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);
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);
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;
106 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
107 passedSuccesfully = true;
108 } catch (IllegalArgumentException e) {
110 assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
115 * Test for the method
117 * <li>{@link BindingGeneratorUtil#validateParameterName()
118 * validateParameterName()}</li>
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"));
133 * <li>parseToClassName</li>
135 * <li>parseToCamelCase</li>
137 * <li>replaceWithCamelCase</li>
139 * </ul> <li>parseToValidParamName</li>
141 * <li>parseToCamelCase</li>
143 * <li>replaceWithCamelCase</li>
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 "));
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 "));