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.mockito.Mockito.mock;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Range;
21 import java.io.Serializable;
22 import java.util.Optional;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
28 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
29 import org.opendaylight.mdsal.binding.model.api.Restrictions;
30 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
31 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
32 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
33 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
41 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
45 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
46 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
47 import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
48 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
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.test.util.YangParserTestUtils;
53 public class BindingGeneratorUtilTest {
54 private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "root"));
57 public ExpectedException expectedEx = ExpectedException.none();
62 * <li>moduleNamespaceToPackageName</li> - with revision
63 * <li>packageNameForGeneratedType</li>
65 * <li>validateJavaPackage</li>
67 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
68 * - without revision </ul>
71 public void testBindingGeneratorUtilMethods() {
72 final Set<Module> modules = YangParserTestUtils.parseYangResources(BindingGeneratorUtilTest.class,
73 "/module.yang").getModules();
74 String packageName = "";
76 for (Module m : modules) {
80 assertNotNull("Module can't be null", module);
82 // test of the method moduleNamespaceToPackageName()
83 packageName = BindingMapping.getRootPackageName(module.getQNameModule());
84 assertEquals("Generated package name is incorrect.",
85 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
87 // test of the method packageNameForGeneratedType()
88 String subPackageNameForDataNode = "";
89 for (ContainerSchemaNode containerSchemaNode : SchemaNodeUtils.getAllContainers(module)) {
90 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
91 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
92 containerSchemaNode.getPath());
96 assertEquals("The name of the subpackage is incorrect.",
97 "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",
98 subPackageNameForDataNode);
100 // test method computeDefaultSUID
101 GeneratedTypeBuilder genTypeBuilder = new CodegenGeneratedTypeBuilder(
102 JavaTypeName.create("org.opendaylight.yangtools.test", "TestType"));
103 genTypeBuilder.addMethod("testMethod");
104 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
105 genTypeBuilder.addEnclosingTransferObject("testObject");
106 genTypeBuilder.addProperty("newProp");
107 GeneratedTypeBuilder genType = new CodegenGeneratedTypeBuilder(
108 JavaTypeName.create("org.opendaylight.yangtools.test", "Type2"));
109 genTypeBuilder.addImplementsType(genType);
110 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
112 GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder(
113 JavaTypeName.create("org.opendaylight.yangtools.test2", "TestType2"));
114 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
115 assertNotEquals(computedSUID, computedSUID2);
119 * Test for the method
121 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
122 * packageNameForGeneratedType(String, SchemaPath)}</li>
126 public void testPackageNameForGeneratedTypeNullBasePackageName() {
127 expectedEx.expect(NullPointerException.class);
128 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
132 * Test for the method
134 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
135 * packageNameForGeneratedType(String, SchemaPath)}</li>
139 public void testPackageNameForGeneratedTypeNullSchemaPath() {
140 expectedEx.expect(NullPointerException.class);
141 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
145 * Test for the method
147 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
148 * resolveJavaReservedWordEquivalency(String)}</li>
152 public void testValidateParameterName() {
153 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
154 assertEquals("Return value is incorrect.", "whatever",
155 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
156 assertEquals("Return value is incorrect.", "_case",
157 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
163 * <li>parseToClassName</li>
165 * <li>parseToCamelCase</li>
167 * <li>replaceWithCamelCase</li>
169 * </ul> <li>parseToValidParamName</li>
171 * <li>parseToCamelCase</li>
173 * <li>replaceWithCamelCase</li>
179 public void testParsingMethods() {
180 // getClassName method testing
181 assertEquals("Class name has incorrect format", "SomeTestingClassName",
182 BindingMapping.getClassName(" some-testing_class name "));
183 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
184 BindingMapping.getClassName(" 0 some-testing_class name "));
187 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
188 BindingMapping.getPropertyName(" some-testing_parameter name "));
189 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
190 BindingMapping.getPropertyName(" 0some-testing_parameter name "));
194 public void computeDefaultSUIDTest() {
195 CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
196 JavaTypeName.create("my.package", "MyName"));
198 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
199 method.setAccessModifier(AccessModifier.PUBLIC);
200 generatedTypeBuilder.addProperty("myProperty");
201 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
203 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
208 public void getRestrictionsTest() throws InvalidLengthConstraintException {
209 final Optional<String> absent = Optional.empty();
210 final StringTypeBuilder builder =
211 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
213 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
214 builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
216 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
218 assertNotNull(restrictions);
219 assertEquals(ImmutableSet.of(Range.closed(1, 2)),
220 restrictions.getLengthConstraint().get().getAllowedRanges().asRanges());
221 assertFalse(restrictions.getRangeConstraint().isPresent());
222 assertEquals(1, restrictions.getPatternConstraints().size());
224 assertFalse(restrictions.isEmpty());
225 assertTrue(restrictions.getPatternConstraints().contains(
226 BaseConstraints.newPatternConstraint(".*", absent, absent)));
230 public void getEmptyRestrictionsTest() {
231 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
232 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
234 assertNotNull(restrictions);
235 assertTrue(restrictions.isEmpty());
239 public void getDefaultIntegerRestrictionsTest() {
240 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
241 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
243 assertNotNull(restrictions);
244 assertFalse(restrictions.isEmpty());
245 assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
246 restrictions.getRangeConstraint());
247 assertFalse(restrictions.getLengthConstraint().isPresent());
248 assertTrue(restrictions.getPatternConstraints().isEmpty());
252 public void getDefaultUnsignedIntegerRestrictionsTest() {
253 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
254 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
256 assertNotNull(restrictions);
257 assertFalse(restrictions.isEmpty());
258 assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
259 restrictions.getRangeConstraint());
260 assertFalse(restrictions.getLengthConstraint().isPresent());
261 assertTrue(restrictions.getPatternConstraints().isEmpty());
265 public void getDefaultDecimalRestrictionsTest() {
266 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
267 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
269 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
271 assertNotNull(restrictions);
272 assertFalse(restrictions.isEmpty());
273 assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
274 assertFalse(restrictions.getLengthConstraint().isPresent());
275 assertTrue(restrictions.getPatternConstraints().isEmpty());
279 public void unicodeCharReplaceTest() {
280 String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
282 assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
283 BindingGeneratorUtil.replaceAllIllegalChars(inputString));