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;
53 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
55 public class BindingGeneratorUtilTest {
56 private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("/root"));
59 public ExpectedException expectedEx = ExpectedException.none();
61 private static List<File> loadTestResources(final String testFile) {
62 final List<File> testModels = new ArrayList<>();
65 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
66 } catch (URISyntaxException e) {
67 throw new IllegalArgumentException("Failed to load sources from " + testFile);
69 testModels.add(listModelFile);
76 * <li>moduleNamespaceToPackageName</li> - with revision
77 * <li>packageNameForGeneratedType</li>
79 * <li>validateJavaPackage</li>
81 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
82 * - without revision </ul>
83 * @throws ReactorException Reactor exception
84 * @throws SourceException Source exception
87 public void testBindingGeneratorUtilMethods() throws IOException, SourceException, ReactorException {
88 List<File> testModels = loadTestResources("/module.yang");
90 final Set<Module> modules = YangParserTestUtils.parseYangSources(testModels).getModules();
91 String packageName = "";
93 for (Module m : modules) {
97 assertNotNull("Module can't be null", module);
99 // test of the method moduleNamespaceToPackageName()
100 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
101 assertEquals("Generated package name is incorrect.",
102 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
104 // test of the method packageNameForGeneratedType()
105 DataNodeIterator it = new DataNodeIterator(module);
106 List<ContainerSchemaNode> schemaContainers = it.allContainers();
107 String subPackageNameForDataNode = "";
108 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
109 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
110 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
111 containerSchemaNode.getPath());
115 assertEquals("The name of the subpackage is incorrect.",
116 "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",
117 subPackageNameForDataNode);
119 // test of the method packageNameForTypeDefinition
120 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
121 String subPackageNameForTypeDefinition = "";
122 TypeDefinition<?> firstTypeDef = null;
124 for (TypeDefinition<?> tpDef : typeDefinitions) {
125 if (tpDef.getQName().getLocalName().equals("tpdf")) {
126 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
127 firstTypeDef = tpDef;
131 assertEquals("The name of the subpackage is incorrect.",
132 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
133 subPackageNameForTypeDefinition);
135 // test method getRestrictions
136 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
137 assertNotNull(restriction);
139 // test method computeDefaultSUID
140 GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
141 genTypeBuilder.addMethod("testMethod");
142 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
143 genTypeBuilder.addEnclosingTransferObject("testObject");
144 genTypeBuilder.addProperty("newProp");
145 GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
146 genTypeBuilder.addImplementsType(genType);
147 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
149 GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
150 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
151 assertNotEquals(computedSUID, computedSUID2);
153 // test of exception part of the method moduleNamespaceToPackageName()
154 Module moduleWithoutRevision = mock(Module.class);
155 doReturn(null).when(moduleWithoutRevision).getQNameModule();
157 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
158 fail("Expected IllegalArgumentException");
159 } catch (IllegalArgumentException e) {
164 * Test for the method
166 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
167 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
172 public void testPackageNameForTypeDefinitionNullBasePackageName() {
173 expectedEx.expect(IllegalArgumentException.class);
174 expectedEx.expectMessage("Base Package Name cannot be NULL!");
175 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
179 * Test for the method
181 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
182 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
187 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
188 expectedEx.expect(IllegalArgumentException.class);
189 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
190 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
194 * Test for the method
196 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
197 * packageNameForGeneratedType(String, SchemaPath)}</li>
201 public void testPackageNameForGeneratedTypeNullBasePackageName() {
202 expectedEx.expect(NullPointerException.class);
203 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
207 * Test for the method
209 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
210 * packageNameForGeneratedType(String, SchemaPath)}</li>
214 public void testPackageNameForGeneratedTypeNullSchemaPath() {
215 expectedEx.expect(NullPointerException.class);
216 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
220 * Test for the method
222 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
223 * parseToClassName(String)}</li>
227 public void testParseToClassNameNullValue() {
228 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
229 assertEquals("TestClassName", className);
231 expectedEx.expect(IllegalArgumentException.class);
232 expectedEx.expectMessage("Name can not be null");
233 className = BindingGeneratorUtil.parseToClassName(null);
237 * Test for the method
239 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
240 * parseToClassName(String)}</li>
244 public void testParseToClassNameEmptyValue() {
245 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
246 assertEquals("TestClassName", className);
248 expectedEx.expect(IllegalArgumentException.class);
249 expectedEx.expectMessage("Name can not be empty");
250 className = BindingGeneratorUtil.parseToClassName("");
254 * Test for the method
256 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
257 * resolveJavaReservedWordEquivalency(String)}</li>
261 public void testValidateParameterName() {
262 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
263 assertEquals("Return value is incorrect.", "whatever",
264 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
265 assertEquals("Return value is incorrect.", "_case",
266 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
272 * <li>parseToClassName</li>
274 * <li>parseToCamelCase</li>
276 * <li>replaceWithCamelCase</li>
278 * </ul> <li>parseToValidParamName</li>
280 * <li>parseToCamelCase</li>
282 * <li>replaceWithCamelCase</li>
288 public void testParsingMethods() {
289 // parseToClassName method testing
290 assertEquals("Class name has incorrect format", "SomeTestingClassName",
291 BindingMapping.getClassName(" some-testing_class name "));
292 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
293 BindingMapping.getClassName(" 0 some-testing_class name "));
295 // parseToValidParamName
296 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
297 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
298 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
299 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
303 public void computeDefaultSUIDTest() {
304 GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
306 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
307 method.setAccessModifier(AccessModifier.PUBLIC);
308 generatedTypeBuilder.addProperty("myProperty");
309 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
311 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
316 public void getRestrictionsTest() {
317 final Optional<String> absent = Optional.absent();
318 final StringTypeBuilder builder =
319 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
321 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
322 builder.setLengthAlternatives(ImmutableList.of(
323 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
325 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
327 assertNotNull(restrictions);
329 assertEquals(1, restrictions.getLengthConstraints().size());
330 assertEquals(0, restrictions.getRangeConstraints().size());
331 assertEquals(1, restrictions.getPatternConstraints().size());
333 assertFalse(restrictions.isEmpty());
334 assertTrue(restrictions.getLengthConstraints().contains(
335 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
336 assertTrue(restrictions.getPatternConstraints().contains(
337 BaseConstraints.newPatternConstraint(".*", absent, absent)));
341 public void getEmptyRestrictionsTest() {
342 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
343 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
345 assertNotNull(restrictions);
346 assertTrue(restrictions.isEmpty());
350 public void getDefaultIntegerRestrictionsTest() {
351 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
352 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
354 assertNotNull(restrictions);
355 assertFalse(restrictions.isEmpty());
356 assertEquals(((IntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
357 restrictions.getRangeConstraints());
358 assertTrue(restrictions.getLengthConstraints().isEmpty());
359 assertTrue(restrictions.getPatternConstraints().isEmpty());
363 public void getDefaultUnsignedIntegerRestrictionsTest() {
364 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
365 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
367 assertNotNull(restrictions);
368 assertFalse(restrictions.isEmpty());
369 assertEquals(((UnsignedIntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
370 restrictions.getRangeConstraints());
371 assertTrue(restrictions.getLengthConstraints().isEmpty());
372 assertTrue(restrictions.getPatternConstraints().isEmpty());
376 public void getDefaultDecimalRestrictionsTest() {
377 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
378 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
380 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
382 assertNotNull(restrictions);
383 assertFalse(restrictions.isEmpty());
384 assertEquals(base.getRangeConstraints(), restrictions.getRangeConstraints());
385 assertTrue(restrictions.getLengthConstraints().isEmpty());
386 assertTrue(restrictions.getPatternConstraints().isEmpty());