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.io.IOException;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.List;
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;
31 public class BindingGeneratorUtilTest {
33 private static List<File> loadTestResources(String testFile) {
34 final List<File> testModels = new ArrayList<File>();
37 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
38 } catch (URISyntaxException e) {
39 throw new IllegalArgumentException("Failed to load sources from " + testFile);
41 testModels.add(listModelFile);
48 * <li>moduleNamespaceToPackageName</li> - with revision
49 * <li>packageNameForGeneratedType</li>
51 * <li>validateJavaPackage</li>
53 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
54 * - without revision </ul>
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 = "";
63 for (Module m : modules) {
67 assertNotNull("Module can't be null", module);
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);
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());
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);
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);
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);
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;
108 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
109 passedSuccesfully = true;
110 } catch (IllegalArgumentException e) {
112 assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
117 * Test for the method
119 * <li>{@link BindingGeneratorUtil#validateParameterName()
120 * validateParameterName()}</li>
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"));
135 * <li>parseToClassName</li>
137 * <li>parseToCamelCase</li>
139 * <li>replaceWithCamelCase</li>
141 * </ul> <li>parseToValidParamName</li>
143 * <li>parseToCamelCase</li>
145 * <li>replaceWithCamelCase</li>
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 "));
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 "));