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.List;
23 import java.util.Optional;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
29 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
30 import org.opendaylight.mdsal.binding.model.api.Restrictions;
31 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
32 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
33 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
34 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
42 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
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.InvalidLengthConstraintException;
50 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
51 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
54 public class BindingGeneratorUtilTest {
55 private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "/root"));
58 public ExpectedException expectedEx = ExpectedException.none();
63 * <li>moduleNamespaceToPackageName</li> - with revision
64 * <li>packageNameForGeneratedType</li>
66 * <li>validateJavaPackage</li>
68 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
69 * - without revision </ul>
72 public void testBindingGeneratorUtilMethods() {
73 final Set<Module> modules = YangParserTestUtils.parseYangResources(BindingGeneratorUtilTest.class,
74 "/module.yang").getModules();
75 String packageName = "";
77 for (Module m : modules) {
81 assertNotNull("Module can't be null", module);
83 // test of the method moduleNamespaceToPackageName()
84 packageName = BindingMapping.getRootPackageName(module.getQNameModule());
85 assertEquals("Generated package name is incorrect.",
86 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
88 // test of the method packageNameForGeneratedType()
89 DataNodeIterator it = new DataNodeIterator(module);
90 List<ContainerSchemaNode> schemaContainers = it.allContainers();
91 String subPackageNameForDataNode = "";
92 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
93 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
94 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
95 containerSchemaNode.getPath());
99 assertEquals("The name of the subpackage is incorrect.",
100 "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",
101 subPackageNameForDataNode);
103 // test method computeDefaultSUID
104 GeneratedTypeBuilder genTypeBuilder = new CodegenGeneratedTypeBuilder(
105 JavaTypeName.create("org.opendaylight.yangtools.test", "TestType"));
106 genTypeBuilder.addMethod("testMethod");
107 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
108 genTypeBuilder.addEnclosingTransferObject("testObject");
109 genTypeBuilder.addProperty("newProp");
110 GeneratedTypeBuilder genType = new CodegenGeneratedTypeBuilder(
111 JavaTypeName.create("org.opendaylight.yangtools.test", "Type2"));
112 genTypeBuilder.addImplementsType(genType);
113 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
115 GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder(
116 JavaTypeName.create("org.opendaylight.yangtools.test2", "TestType2"));
117 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
118 assertNotEquals(computedSUID, computedSUID2);
122 * Test for the method
124 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
125 * packageNameForGeneratedType(String, SchemaPath)}</li>
129 public void testPackageNameForGeneratedTypeNullBasePackageName() {
130 expectedEx.expect(NullPointerException.class);
131 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
135 * Test for the method
137 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
138 * packageNameForGeneratedType(String, SchemaPath)}</li>
142 public void testPackageNameForGeneratedTypeNullSchemaPath() {
143 expectedEx.expect(NullPointerException.class);
144 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
148 * Test for the method
150 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
151 * resolveJavaReservedWordEquivalency(String)}</li>
155 public void testValidateParameterName() {
156 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
157 assertEquals("Return value is incorrect.", "whatever",
158 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
159 assertEquals("Return value is incorrect.", "_case",
160 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
166 * <li>parseToClassName</li>
168 * <li>parseToCamelCase</li>
170 * <li>replaceWithCamelCase</li>
172 * </ul> <li>parseToValidParamName</li>
174 * <li>parseToCamelCase</li>
176 * <li>replaceWithCamelCase</li>
182 public void testParsingMethods() {
183 // getClassName method testing
184 assertEquals("Class name has incorrect format", "SomeTestingClassName",
185 BindingMapping.getClassName(" some-testing_class name "));
186 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
187 BindingMapping.getClassName(" 0 some-testing_class name "));
190 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
191 BindingMapping.getPropertyName(" some-testing_parameter name "));
192 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
193 BindingMapping.getPropertyName(" 0some-testing_parameter name "));
197 public void computeDefaultSUIDTest() {
198 CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
199 JavaTypeName.create("my.package", "MyName"));
201 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
202 method.setAccessModifier(AccessModifier.PUBLIC);
203 generatedTypeBuilder.addProperty("myProperty");
204 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
206 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
211 public void getRestrictionsTest() throws InvalidLengthConstraintException {
212 final Optional<String> absent = Optional.empty();
213 final StringTypeBuilder builder =
214 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
216 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
217 builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
219 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
221 assertNotNull(restrictions);
222 assertEquals(ImmutableSet.of(Range.closed(1, 2)),
223 restrictions.getLengthConstraint().get().getAllowedRanges().asRanges());
224 assertFalse(restrictions.getRangeConstraint().isPresent());
225 assertEquals(1, restrictions.getPatternConstraints().size());
227 assertFalse(restrictions.isEmpty());
228 assertTrue(restrictions.getPatternConstraints().contains(
229 BaseConstraints.newPatternConstraint(".*", absent, absent)));
233 public void getEmptyRestrictionsTest() {
234 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
235 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
237 assertNotNull(restrictions);
238 assertTrue(restrictions.isEmpty());
242 public void getDefaultIntegerRestrictionsTest() {
243 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
244 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
246 assertNotNull(restrictions);
247 assertFalse(restrictions.isEmpty());
248 assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
249 restrictions.getRangeConstraint());
250 assertFalse(restrictions.getLengthConstraint().isPresent());
251 assertTrue(restrictions.getPatternConstraints().isEmpty());
255 public void getDefaultUnsignedIntegerRestrictionsTest() {
256 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
257 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
259 assertNotNull(restrictions);
260 assertFalse(restrictions.isEmpty());
261 assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
262 restrictions.getRangeConstraint());
263 assertFalse(restrictions.getLengthConstraint().isPresent());
264 assertTrue(restrictions.getPatternConstraints().isEmpty());
268 public void getDefaultDecimalRestrictionsTest() {
269 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
270 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
272 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
274 assertNotNull(restrictions);
275 assertFalse(restrictions.isEmpty());
276 assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
277 assertFalse(restrictions.getLengthConstraint().isPresent());
278 assertTrue(restrictions.getPatternConstraints().isEmpty());
282 public void unicodeCharReplaceTest() {
283 String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
285 assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
286 BindingGeneratorUtil.replaceAllIllegalChars(inputString));