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.mdsal.binding.model.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;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Range;
23 import java.io.Serializable;
24 import java.util.List;
25 import java.util.Optional;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
31 import org.opendaylight.mdsal.binding.model.api.Restrictions;
32 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
33 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
34 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
35 import org.opendaylight.yangtools.yang.binding.BindingMapping;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
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.stmt.ValueRange;
43 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
47 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
48 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
49 import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
50 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
51 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
52 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
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("test", "/root"));
59 public ExpectedException expectedEx = ExpectedException.none();
64 * <li>moduleNamespaceToPackageName</li> - with revision
65 * <li>packageNameForGeneratedType</li>
67 * <li>validateJavaPackage</li>
69 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
70 * - without revision </ul>
73 public void testBindingGeneratorUtilMethods() {
74 final Set<Module> modules = YangParserTestUtils.parseYangResources(BindingGeneratorUtilTest.class,
75 "/module.yang").getModules();
76 String packageName = "";
78 for (Module m : modules) {
82 assertNotNull("Module can't be null", module);
84 // test of the method moduleNamespaceToPackageName()
85 packageName = BindingMapping.getRootPackageName(module.getQNameModule());
86 assertEquals("Generated package name is incorrect.",
87 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
89 // test of the method packageNameForGeneratedType()
90 DataNodeIterator it = new DataNodeIterator(module);
91 List<ContainerSchemaNode> schemaContainers = it.allContainers();
92 String subPackageNameForDataNode = "";
93 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
94 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
95 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
96 containerSchemaNode.getPath());
100 assertEquals("The name of the subpackage 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.cont.outter",
102 subPackageNameForDataNode);
104 // test of the method packageNameForTypeDefinition
105 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
106 String subPackageNameForTypeDefinition = "";
107 TypeDefinition<?> firstTypeDef = null;
109 for (TypeDefinition<?> tpDef : typeDefinitions) {
110 if (tpDef.getQName().getLocalName().equals("tpdf")) {
111 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
112 firstTypeDef = tpDef;
116 assertEquals("The name of the subpackage is incorrect.",
117 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
118 subPackageNameForTypeDefinition);
120 // test method getRestrictions
121 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
122 assertNotNull(restriction);
124 // test method computeDefaultSUID
125 GeneratedTypeBuilder genTypeBuilder = new CodegenGeneratedTypeBuilder("org.opendaylight.yangtools.test", "TestType");
126 genTypeBuilder.addMethod("testMethod");
127 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
128 genTypeBuilder.addEnclosingTransferObject("testObject");
129 genTypeBuilder.addProperty("newProp");
130 GeneratedTypeBuilder genType = new CodegenGeneratedTypeBuilder("org.opendaylight.yangtools.test", "Type2");
131 genTypeBuilder.addImplementsType(genType);
132 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
134 GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder("org.opendaylight.yangtools.test2", "TestType2");
135 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
136 assertNotEquals(computedSUID, computedSUID2);
138 // test of exception part of the method moduleNamespaceToPackageName()
139 Module moduleWithoutRevision = mock(Module.class);
140 doReturn(null).when(moduleWithoutRevision).getQNameModule();
142 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
143 fail("Expected IllegalArgumentException");
144 } catch (IllegalArgumentException e) {
149 * Test for the method
151 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
152 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
157 public void testPackageNameForTypeDefinitionNullBasePackageName() {
158 expectedEx.expect(IllegalArgumentException.class);
159 expectedEx.expectMessage("Base Package Name cannot be NULL!");
160 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
164 * Test for the method
166 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
167 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
172 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
173 expectedEx.expect(IllegalArgumentException.class);
174 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
175 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
179 * Test for the method
181 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
182 * packageNameForGeneratedType(String, SchemaPath)}</li>
186 public void testPackageNameForGeneratedTypeNullBasePackageName() {
187 expectedEx.expect(NullPointerException.class);
188 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
192 * Test for the method
194 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
195 * packageNameForGeneratedType(String, SchemaPath)}</li>
199 public void testPackageNameForGeneratedTypeNullSchemaPath() {
200 expectedEx.expect(NullPointerException.class);
201 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
205 * Test for the method
207 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
208 * parseToClassName(String)}</li>
212 public void testParseToClassNameNullValue() {
213 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
214 assertEquals("TestClassName", className);
216 expectedEx.expect(IllegalArgumentException.class);
217 expectedEx.expectMessage("Name can not be null");
218 className = BindingGeneratorUtil.parseToClassName(null);
222 * Test for the method
224 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
225 * parseToClassName(String)}</li>
229 public void testParseToClassNameEmptyValue() {
230 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
231 assertEquals("TestClassName", className);
233 expectedEx.expect(IllegalArgumentException.class);
234 expectedEx.expectMessage("Name can not be empty");
235 className = BindingGeneratorUtil.parseToClassName("");
239 * Test for the method
241 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
242 * resolveJavaReservedWordEquivalency(String)}</li>
246 public void testValidateParameterName() {
247 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
248 assertEquals("Return value is incorrect.", "whatever",
249 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
250 assertEquals("Return value is incorrect.", "_case",
251 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
257 * <li>parseToClassName</li>
259 * <li>parseToCamelCase</li>
261 * <li>replaceWithCamelCase</li>
263 * </ul> <li>parseToValidParamName</li>
265 * <li>parseToCamelCase</li>
267 * <li>replaceWithCamelCase</li>
273 public void testParsingMethods() {
274 // parseToClassName method testing
275 assertEquals("Class name has incorrect format", "SomeTestingClassName",
276 BindingMapping.getClassName(" some-testing_class name "));
277 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
278 BindingMapping.getClassName(" 0 some-testing_class name "));
280 // parseToValidParamName
281 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
282 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
283 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
284 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
288 public void computeDefaultSUIDTest() {
289 CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder("my.package", "MyName");
291 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
292 method.setAccessModifier(AccessModifier.PUBLIC);
293 generatedTypeBuilder.addProperty("myProperty");
294 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
296 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
301 public void getRestrictionsTest() throws InvalidLengthConstraintException {
302 final Optional<String> absent = Optional.empty();
303 final StringTypeBuilder builder =
304 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
306 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
307 builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
309 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
311 assertNotNull(restrictions);
312 assertEquals(ImmutableSet.of(Range.closed(1, 2)),
313 restrictions.getLengthConstraint().get().getAllowedRanges().asRanges());
314 assertFalse(restrictions.getRangeConstraint().isPresent());
315 assertEquals(1, restrictions.getPatternConstraints().size());
317 assertFalse(restrictions.isEmpty());
318 assertTrue(restrictions.getPatternConstraints().contains(
319 BaseConstraints.newPatternConstraint(".*", absent, absent)));
323 public void getEmptyRestrictionsTest() {
324 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
325 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
327 assertNotNull(restrictions);
328 assertTrue(restrictions.isEmpty());
332 public void getDefaultIntegerRestrictionsTest() {
333 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
334 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
336 assertNotNull(restrictions);
337 assertFalse(restrictions.isEmpty());
338 assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
339 restrictions.getRangeConstraint());
340 assertFalse(restrictions.getLengthConstraint().isPresent());
341 assertTrue(restrictions.getPatternConstraints().isEmpty());
345 public void getDefaultUnsignedIntegerRestrictionsTest() {
346 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
347 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
349 assertNotNull(restrictions);
350 assertFalse(restrictions.isEmpty());
351 assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
352 restrictions.getRangeConstraint());
353 assertFalse(restrictions.getLengthConstraint().isPresent());
354 assertTrue(restrictions.getPatternConstraints().isEmpty());
358 public void getDefaultDecimalRestrictionsTest() {
359 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
360 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
362 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
364 assertNotNull(restrictions);
365 assertFalse(restrictions.isEmpty());
366 assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
367 assertFalse(restrictions.getLengthConstraint().isPresent());
368 assertTrue(restrictions.getPatternConstraints().isEmpty());
372 public void unicodeCharReplaceTest() {
373 String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
375 assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
376 BindingGeneratorUtil.replaceAllIllegalChars(inputString));