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.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.mock;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.collect.Range;
22 import java.io.Serializable;
23 import java.util.Optional;
25 import org.junit.Test;
26 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
27 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
28 import org.opendaylight.mdsal.binding.model.api.Restrictions;
29 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
30 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
31 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
32 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
40 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
44 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
45 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
46 import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
47 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
48 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
49 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
50 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
52 public class BindingGeneratorUtilTest {
53 private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "root"));
58 * <li>moduleNamespaceToPackageName</li> - with revision
59 * <li>packageNameForGeneratedType</li>
61 * <li>validateJavaPackage</li>
63 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
64 * - without revision </ul>
67 public void testBindingGeneratorUtilMethods() {
68 final Set<Module> modules = YangParserTestUtils.parseYangResources(BindingGeneratorUtilTest.class,
69 "/module.yang").getModules();
70 String packageName = "";
72 for (Module m : modules) {
76 assertNotNull("Module can't be null", module);
78 // test of the method moduleNamespaceToPackageName()
79 packageName = BindingMapping.getRootPackageName(module.getQNameModule());
80 assertEquals("Generated package name is incorrect.",
81 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
83 // test of the method packageNameForGeneratedType()
84 String subPackageNameForDataNode = "";
85 for (ContainerSchemaNode containerSchemaNode : SchemaNodeUtils.getAllContainers(module)) {
86 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
87 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
88 containerSchemaNode.getPath());
92 assertEquals("The name of the subpackage is incorrect.",
93 "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",
94 subPackageNameForDataNode);
96 // test method computeDefaultSUID
97 GeneratedTypeBuilder genTypeBuilder = new CodegenGeneratedTypeBuilder(
98 JavaTypeName.create("org.opendaylight.yangtools.test", "TestType"));
99 genTypeBuilder.addMethod("testMethod");
100 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
101 genTypeBuilder.addEnclosingTransferObject("testObject");
102 genTypeBuilder.addProperty("newProp");
103 GeneratedTypeBuilder genType = new CodegenGeneratedTypeBuilder(
104 JavaTypeName.create("org.opendaylight.yangtools.test", "Type2"));
105 genTypeBuilder.addImplementsType(genType);
106 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
108 GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder(
109 JavaTypeName.create("org.opendaylight.yangtools.test2", "TestType2"));
110 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
111 assertNotEquals(computedSUID, computedSUID2);
115 * Test for the method
117 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
118 * packageNameForGeneratedType(String, SchemaPath)}</li>
122 public void testPackageNameForGeneratedTypeNullBasePackageName() {
123 assertThrows(NullPointerException.class, () -> BindingGeneratorUtil.packageNameForGeneratedType(null, null));
127 * Test for the method
129 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
130 * packageNameForGeneratedType(String, SchemaPath)}</li>
134 public void testPackageNameForGeneratedTypeNullSchemaPath() {
135 assertThrows(NullPointerException.class,
136 () -> BindingGeneratorUtil.packageNameForGeneratedType("test.package", null));
140 * Test for the method
142 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
143 * resolveJavaReservedWordEquivalency(String)}</li>
147 public void testValidateParameterName() {
148 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
149 assertEquals("Return value is incorrect.", "whatever",
150 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
151 assertEquals("Return value is incorrect.", "_case",
152 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
158 * <li>parseToClassName</li>
160 * <li>parseToCamelCase</li>
162 * <li>replaceWithCamelCase</li>
164 * </ul> <li>parseToValidParamName</li>
166 * <li>parseToCamelCase</li>
168 * <li>replaceWithCamelCase</li>
174 public void testParsingMethods() {
175 // getClassName method testing
176 assertEquals("Class name has incorrect format", "SomeTestingClassName",
177 BindingMapping.getClassName(" some-testing_class name "));
178 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
179 BindingMapping.getClassName(" 0 some-testing_class name "));
182 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
183 BindingMapping.getPropertyName(" some-testing_parameter name "));
184 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
185 BindingMapping.getPropertyName(" 0some-testing_parameter name "));
189 public void computeDefaultSUIDTest() {
190 CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
191 JavaTypeName.create("my.package", "MyName"));
193 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
194 method.setAccessModifier(AccessModifier.PUBLIC);
195 generatedTypeBuilder.addProperty("myProperty");
196 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
198 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
203 public void getRestrictionsTest() throws InvalidLengthConstraintException {
204 final Optional<String> absent = Optional.empty();
205 final StringTypeBuilder builder =
206 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
208 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
209 builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
211 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
213 assertNotNull(restrictions);
214 assertEquals(ImmutableSet.of(Range.closed(1, 2)),
215 restrictions.getLengthConstraint().get().getAllowedRanges().asRanges());
216 assertFalse(restrictions.getRangeConstraint().isPresent());
217 assertEquals(1, restrictions.getPatternConstraints().size());
219 assertFalse(restrictions.isEmpty());
220 assertTrue(restrictions.getPatternConstraints().contains(
221 BaseConstraints.newPatternConstraint(".*", absent, absent)));
225 public void getEmptyRestrictionsTest() {
226 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
227 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
229 assertNotNull(restrictions);
230 assertTrue(restrictions.isEmpty());
234 public void getDefaultIntegerRestrictionsTest() {
235 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
236 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
238 assertNotNull(restrictions);
239 assertFalse(restrictions.isEmpty());
240 assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
241 restrictions.getRangeConstraint());
242 assertFalse(restrictions.getLengthConstraint().isPresent());
243 assertTrue(restrictions.getPatternConstraints().isEmpty());
247 public void getDefaultUnsignedIntegerRestrictionsTest() {
248 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
249 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
251 assertNotNull(restrictions);
252 assertFalse(restrictions.isEmpty());
253 assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
254 restrictions.getRangeConstraint());
255 assertFalse(restrictions.getLengthConstraint().isPresent());
256 assertTrue(restrictions.getPatternConstraints().isEmpty());
260 public void getDefaultDecimalRestrictionsTest() {
261 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
262 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
264 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
266 assertNotNull(restrictions);
267 assertFalse(restrictions.isEmpty());
268 assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
269 assertFalse(restrictions.getLengthConstraint().isPresent());
270 assertTrue(restrictions.getPatternConstraints().isEmpty());
274 public void unicodeCharReplaceTest() {
275 String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
277 assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
278 BindingGeneratorUtil.replaceAllIllegalChars(inputString));