2 * Copyright (c) 2016 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.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.ImmutableList;
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
32 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
33 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
34 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
35 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
36 import org.opendaylight.yangtools.yang.binding.BindingMapping;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
46 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
47 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
48 import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
49 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
50 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
52 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
54 public class BindingGeneratorUtilTest {
55 private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("/root"));
58 public ExpectedException expectedEx = ExpectedException.none();
60 private static List<File> loadTestResources(final String testFile) {
61 final List<File> testModels = new ArrayList<File>();
64 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
65 } catch (URISyntaxException e) {
66 throw new IllegalArgumentException("Failed to load sources from " + testFile);
68 testModels.add(listModelFile);
75 * <li>moduleNamespaceToPackageName</li> - with revision
76 * <li>packageNameForGeneratedType</li>
78 * <li>validateJavaPackage</li>
80 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
81 * - without revision </ul>
82 * @throws ReactorException Reactor exception
83 * @throws SourceException Source exception
86 public void testBindingGeneratorUtilMethods() throws IOException, SourceException, ReactorException {
87 List<File> testModels = loadTestResources("/module.yang");
89 final Set<Module> modules = TestUtils.parseYangSources(testModels).getModules();
90 String packageName = "";
92 for (Module m : modules) {
96 assertNotNull("Module can't be null", module);
98 // test of the method moduleNamespaceToPackageName()
99 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
100 assertEquals("Generated package name is incorrect.",
101 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
103 // test of the method packageNameForGeneratedType()
104 DataNodeIterator it = new DataNodeIterator(module);
105 List<ContainerSchemaNode> schemaContainers = it.allContainers();
106 String subPackageNameForDataNode = "";
107 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
108 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
109 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
110 containerSchemaNode.getPath());
114 assertEquals("The name of the subpackage is incorrect.",
115 "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",
116 subPackageNameForDataNode);
118 // test of the method packageNameForTypeDefinition
119 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
120 String subPackageNameForTypeDefinition = "";
121 TypeDefinition<?> firstTypeDef = null;
123 for (TypeDefinition<?> tpDef : typeDefinitions) {
124 if (tpDef.getQName().getLocalName().equals("tpdf")) {
125 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
126 firstTypeDef = tpDef;
130 assertEquals("The name of the subpackage is incorrect.",
131 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
132 subPackageNameForTypeDefinition);
134 // test method getRestrictions
135 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
136 assertNotNull(restriction);
138 // test method computeDefaultSUID
139 GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
140 genTypeBuilder.addMethod("testMethod");
141 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
142 genTypeBuilder.addEnclosingTransferObject("testObject");
143 genTypeBuilder.addProperty("newProp");
144 GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
145 genTypeBuilder.addImplementsType(genType);
146 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
148 GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
149 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
150 assertNotEquals(computedSUID, computedSUID2);
152 // test of exception part of the method moduleNamespaceToPackageName()
153 Module moduleWithoutRevision = mock(Module.class);
154 doReturn(null).when(moduleWithoutRevision).getQNameModule();
156 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
157 fail("Expected IllegalArgumentException");
158 } catch (IllegalArgumentException e) {
163 * Test for the method
165 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
166 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
171 public void testPackageNameForTypeDefinitionNullBasePackageName() {
172 expectedEx.expect(IllegalArgumentException.class);
173 expectedEx.expectMessage("Base Package Name cannot be NULL!");
174 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
178 * Test for the method
180 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
181 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
186 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
187 expectedEx.expect(IllegalArgumentException.class);
188 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
189 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
193 * Test for the method
195 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
196 * packageNameForGeneratedType(String, SchemaPath)}</li>
200 public void testPackageNameForGeneratedTypeNullBasePackageName() {
201 expectedEx.expect(NullPointerException.class);
202 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
206 * Test for the method
208 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
209 * packageNameForGeneratedType(String, SchemaPath)}</li>
213 public void testPackageNameForGeneratedTypeNullSchemaPath() {
214 expectedEx.expect(NullPointerException.class);
215 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
219 * Test for the method
221 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
222 * parseToClassName(String)}</li>
226 public void testParseToClassNameNullValue() {
227 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
228 assertEquals("TestClassName", className);
230 expectedEx.expect(IllegalArgumentException.class);
231 expectedEx.expectMessage("Name can not be null");
232 className = BindingGeneratorUtil.parseToClassName(null);
236 * Test for the method
238 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
239 * parseToClassName(String)}</li>
243 public void testParseToClassNameEmptyValue() {
244 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
245 assertEquals("TestClassName", className);
247 expectedEx.expect(IllegalArgumentException.class);
248 expectedEx.expectMessage("Name can not be empty");
249 className = BindingGeneratorUtil.parseToClassName("");
253 * Test for the method
255 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
256 * resolveJavaReservedWordEquivalency(String)}</li>
260 public void testValidateParameterName() {
261 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
262 assertEquals("Return value is incorrect.", "whatever",
263 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
264 assertEquals("Return value is incorrect.", "_case",
265 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
271 * <li>parseToClassName</li>
273 * <li>parseToCamelCase</li>
275 * <li>replaceWithCamelCase</li>
277 * </ul> <li>parseToValidParamName</li>
279 * <li>parseToCamelCase</li>
281 * <li>replaceWithCamelCase</li>
287 public void testParsingMethods() {
288 // parseToClassName method testing
289 assertEquals("Class name has incorrect format", "SomeTestingClassName",
290 BindingMapping.getClassName(" some-testing_class name "));
291 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
292 BindingMapping.getClassName(" 0 some-testing_class name "));
294 // parseToValidParamName
295 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
296 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
297 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
298 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
302 public void computeDefaultSUIDTest() {
303 GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
305 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
306 method.setAccessModifier(AccessModifier.PUBLIC);
307 generatedTypeBuilder.addProperty("myProperty");
308 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
310 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
315 public void getRestrictionsTest() {
316 final Optional<String> absent = Optional.absent();
317 final StringTypeBuilder builder =
318 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
320 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
321 builder.setLengthAlternatives(ImmutableList.of(
322 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
324 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
326 assertNotNull(restrictions);
328 assertEquals(1, restrictions.getLengthConstraints().size());
329 assertEquals(0, restrictions.getRangeConstraints().size());
330 assertEquals(1, restrictions.getPatternConstraints().size());
332 assertFalse(restrictions.isEmpty());
333 assertTrue(restrictions.getLengthConstraints().contains(
334 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
335 assertTrue(restrictions.getPatternConstraints().contains(
336 BaseConstraints.newPatternConstraint(".*", absent, absent)));
340 public void getEmptyRestrictionsTest() {
341 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
342 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
344 assertNotNull(restrictions);
345 assertTrue(restrictions.isEmpty());
349 public void getDefaultIntegerRestrictionsTest() {
350 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
351 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
353 assertNotNull(restrictions);
354 assertFalse(restrictions.isEmpty());
355 assertEquals(((IntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
356 restrictions.getRangeConstraints());
357 assertTrue(restrictions.getLengthConstraints().isEmpty());
358 assertTrue(restrictions.getPatternConstraints().isEmpty());
362 public void getDefaultUnsignedIntegerRestrictionsTest() {
363 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
364 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
366 assertNotNull(restrictions);
367 assertFalse(restrictions.isEmpty());
368 assertEquals(((UnsignedIntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
369 restrictions.getRangeConstraints());
370 assertTrue(restrictions.getLengthConstraints().isEmpty());
371 assertTrue(restrictions.getPatternConstraints().isEmpty());
375 public void getDefaultDecimalRestrictionsTest() {
376 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
377 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
379 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
381 assertNotNull(restrictions);
382 assertFalse(restrictions.isEmpty());
383 assertEquals(base.getRangeConstraints(), restrictions.getRangeConstraints());
384 assertTrue(restrictions.getLengthConstraints().isEmpty());
385 assertTrue(restrictions.getPatternConstraints().isEmpty());