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.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.Serializable;
23 import java.net.URISyntaxException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.opendaylight.mdsal.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
31 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
32 import org.opendaylight.mdsal.binding.model.api.Restrictions;
33 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
34 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
35 import org.opendaylight.yangtools.yang.binding.BindingMapping;
36 import org.opendaylight.yangtools.yang.common.QName;
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.type.DecimalTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
45 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
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.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("/root"));
56 public ExpectedException expectedEx = ExpectedException.none();
58 private static List<File> loadTestResources(final String testFile) {
59 final List<File> testModels = new ArrayList<>();
62 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
63 } catch (URISyntaxException e) {
64 throw new IllegalArgumentException("Failed to load sources from " + testFile);
66 testModels.add(listModelFile);
73 * <li>moduleNamespaceToPackageName</li> - with revision
74 * <li>packageNameForGeneratedType</li>
76 * <li>validateJavaPackage</li>
78 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
79 * - without revision </ul>
82 public void testBindingGeneratorUtilMethods() throws Exception {
83 List<File> testModels = loadTestResources("/module.yang");
85 final Set<Module> modules = YangParserTestUtils.parseYangSources(testModels).getModules();
86 String packageName = "";
88 for (Module m : modules) {
92 assertNotNull("Module can't be null", module);
94 // test of the method moduleNamespaceToPackageName()
95 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
96 assertEquals("Generated package name 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", packageName);
99 // test of the method packageNameForGeneratedType()
100 DataNodeIterator it = new DataNodeIterator(module);
101 List<ContainerSchemaNode> schemaContainers = it.allContainers();
102 String subPackageNameForDataNode = "";
103 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
104 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
105 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
106 containerSchemaNode.getPath());
110 assertEquals("The name of the subpackage is incorrect.",
111 "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",
112 subPackageNameForDataNode);
114 // test of the method packageNameForTypeDefinition
115 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
116 String subPackageNameForTypeDefinition = "";
117 TypeDefinition<?> firstTypeDef = null;
119 for (TypeDefinition<?> tpDef : typeDefinitions) {
120 if (tpDef.getQName().getLocalName().equals("tpdf")) {
121 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
122 firstTypeDef = tpDef;
126 assertEquals("The name of the subpackage is incorrect.",
127 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
128 subPackageNameForTypeDefinition);
130 // test method getRestrictions
131 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
132 assertNotNull(restriction);
134 // test method computeDefaultSUID
135 GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
136 genTypeBuilder.addMethod("testMethod");
137 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
138 genTypeBuilder.addEnclosingTransferObject("testObject");
139 genTypeBuilder.addProperty("newProp");
140 GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
141 genTypeBuilder.addImplementsType(genType);
142 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
144 GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
145 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
146 assertNotEquals(computedSUID, computedSUID2);
148 // test of exception part of the method moduleNamespaceToPackageName()
149 Module moduleWithoutRevision = mock(Module.class);
150 doReturn(null).when(moduleWithoutRevision).getQNameModule();
152 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
153 fail("Expected IllegalArgumentException");
154 } catch (IllegalArgumentException e) {
159 * Test for the method
161 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
162 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
167 public void testPackageNameForTypeDefinitionNullBasePackageName() {
168 expectedEx.expect(IllegalArgumentException.class);
169 expectedEx.expectMessage("Base Package Name cannot be NULL!");
170 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
174 * Test for the method
176 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
177 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
182 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
183 expectedEx.expect(IllegalArgumentException.class);
184 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
185 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
189 * Test for the method
191 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
192 * packageNameForGeneratedType(String, SchemaPath)}</li>
196 public void testPackageNameForGeneratedTypeNullBasePackageName() {
197 expectedEx.expect(NullPointerException.class);
198 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
202 * Test for the method
204 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
205 * packageNameForGeneratedType(String, SchemaPath)}</li>
209 public void testPackageNameForGeneratedTypeNullSchemaPath() {
210 expectedEx.expect(NullPointerException.class);
211 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
215 * Test for the method
217 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
218 * parseToClassName(String)}</li>
222 public void testParseToClassNameNullValue() {
223 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
224 assertEquals("TestClassName", className);
226 expectedEx.expect(IllegalArgumentException.class);
227 expectedEx.expectMessage("Name can not be null");
228 className = BindingGeneratorUtil.parseToClassName(null);
232 * Test for the method
234 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
235 * parseToClassName(String)}</li>
239 public void testParseToClassNameEmptyValue() {
240 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
241 assertEquals("TestClassName", className);
243 expectedEx.expect(IllegalArgumentException.class);
244 expectedEx.expectMessage("Name can not be empty");
245 className = BindingGeneratorUtil.parseToClassName("");
249 * Test for the method
251 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
252 * resolveJavaReservedWordEquivalency(String)}</li>
256 public void testValidateParameterName() {
257 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
258 assertEquals("Return value is incorrect.", "whatever",
259 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
260 assertEquals("Return value is incorrect.", "_case",
261 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
267 * <li>parseToClassName</li>
269 * <li>parseToCamelCase</li>
271 * <li>replaceWithCamelCase</li>
273 * </ul> <li>parseToValidParamName</li>
275 * <li>parseToCamelCase</li>
277 * <li>replaceWithCamelCase</li>
283 public void testParsingMethods() {
284 // parseToClassName method testing
285 assertEquals("Class name has incorrect format", "SomeTestingClassName",
286 BindingMapping.getClassName(" some-testing_class name "));
287 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
288 BindingMapping.getClassName(" 0 some-testing_class name "));
290 // parseToValidParamName
291 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
292 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
293 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
294 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
298 public void computeDefaultSUIDTest() {
299 GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
301 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
302 method.setAccessModifier(AccessModifier.PUBLIC);
303 generatedTypeBuilder.addProperty("myProperty");
304 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
306 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
311 public void getRestrictionsTest() {
312 final Optional<String> absent = Optional.absent();
313 final StringTypeBuilder builder =
314 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
316 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
317 builder.setLengthAlternatives(ImmutableList.of(
318 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
320 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
322 assertNotNull(restrictions);
324 assertEquals(1, restrictions.getLengthConstraints().size());
325 assertEquals(0, restrictions.getRangeConstraints().size());
326 assertEquals(1, restrictions.getPatternConstraints().size());
328 assertFalse(restrictions.isEmpty());
329 assertTrue(restrictions.getLengthConstraints().contains(
330 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
331 assertTrue(restrictions.getPatternConstraints().contains(
332 BaseConstraints.newPatternConstraint(".*", absent, absent)));
336 public void getEmptyRestrictionsTest() {
337 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
338 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
340 assertNotNull(restrictions);
341 assertTrue(restrictions.isEmpty());
345 public void getDefaultIntegerRestrictionsTest() {
346 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
347 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
349 assertNotNull(restrictions);
350 assertFalse(restrictions.isEmpty());
351 assertEquals(((IntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
352 restrictions.getRangeConstraints());
353 assertTrue(restrictions.getLengthConstraints().isEmpty());
354 assertTrue(restrictions.getPatternConstraints().isEmpty());
358 public void getDefaultUnsignedIntegerRestrictionsTest() {
359 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
360 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
362 assertNotNull(restrictions);
363 assertFalse(restrictions.isEmpty());
364 assertEquals(((UnsignedIntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
365 restrictions.getRangeConstraints());
366 assertTrue(restrictions.getLengthConstraints().isEmpty());
367 assertTrue(restrictions.getPatternConstraints().isEmpty());
371 public void getDefaultDecimalRestrictionsTest() {
372 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
373 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
375 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
377 assertNotNull(restrictions);
378 assertFalse(restrictions.isEmpty());
379 assertEquals(base.getRangeConstraints(), restrictions.getRangeConstraints());
380 assertTrue(restrictions.getLengthConstraints().isEmpty());
381 assertTrue(restrictions.getPatternConstraints().isEmpty());