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.base.Optional;
21 import com.google.common.collect.ImmutableList;
23 import java.io.Serializable;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
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.mdsal.binding.model.util.generated.type.builder.GeneratedTypeBuilderImpl;
36 import org.opendaylight.yangtools.yang.binding.BindingMapping;
37 import org.opendaylight.yangtools.yang.common.QName;
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.type.DecimalTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
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.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("/root"));
57 public ExpectedException expectedEx = ExpectedException.none();
59 private static List<File> loadTestResources(final String testFile) {
60 final List<File> testModels = new ArrayList<>();
63 listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
64 } catch (URISyntaxException e) {
65 throw new IllegalArgumentException("Failed to load sources from " + testFile);
67 testModels.add(listModelFile);
74 * <li>moduleNamespaceToPackageName</li> - with revision
75 * <li>packageNameForGeneratedType</li>
77 * <li>validateJavaPackage</li>
79 * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
80 * - without revision </ul>
83 public void testBindingGeneratorUtilMethods() throws Exception {
84 List<File> testModels = loadTestResources("/module.yang");
86 final Set<Module> modules = YangParserTestUtils.parseYangSources(testModels).getModules();
87 String packageName = "";
89 for (Module m : modules) {
93 assertNotNull("Module can't be null", module);
95 // test of the method moduleNamespaceToPackageName()
96 packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
97 assertEquals("Generated package name is incorrect.",
98 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
100 // test of the method packageNameForGeneratedType()
101 DataNodeIterator it = new DataNodeIterator(module);
102 List<ContainerSchemaNode> schemaContainers = it.allContainers();
103 String subPackageNameForDataNode = "";
104 for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
105 if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
106 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
107 containerSchemaNode.getPath());
111 assertEquals("The name of the subpackage is incorrect.",
112 "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",
113 subPackageNameForDataNode);
115 // test of the method packageNameForTypeDefinition
116 Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
117 String subPackageNameForTypeDefinition = "";
118 TypeDefinition<?> firstTypeDef = null;
120 for (TypeDefinition<?> tpDef : typeDefinitions) {
121 if (tpDef.getQName().getLocalName().equals("tpdf")) {
122 subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
123 firstTypeDef = tpDef;
127 assertEquals("The name of the subpackage is incorrect.",
128 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
129 subPackageNameForTypeDefinition);
131 // test method getRestrictions
132 Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
133 assertNotNull(restriction);
135 // test method computeDefaultSUID
136 GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
137 genTypeBuilder.addMethod("testMethod");
138 genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
139 genTypeBuilder.addEnclosingTransferObject("testObject");
140 genTypeBuilder.addProperty("newProp");
141 GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
142 genTypeBuilder.addImplementsType(genType);
143 long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
145 GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
146 long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
147 assertNotEquals(computedSUID, computedSUID2);
149 // test of exception part of the method moduleNamespaceToPackageName()
150 Module moduleWithoutRevision = mock(Module.class);
151 doReturn(null).when(moduleWithoutRevision).getQNameModule();
153 BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
154 fail("Expected IllegalArgumentException");
155 } catch (IllegalArgumentException e) {
160 * Test for the method
162 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
163 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
168 public void testPackageNameForTypeDefinitionNullBasePackageName() {
169 expectedEx.expect(IllegalArgumentException.class);
170 expectedEx.expectMessage("Base Package Name cannot be NULL!");
171 BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
175 * Test for the method
177 * <li>{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
178 * packageNameForTypeDefinition(String, TypeDefinition)}</li>
183 public void testPackageNameForTypeDefinitionNullTypeDefinition() {
184 expectedEx.expect(IllegalArgumentException.class);
185 expectedEx.expectMessage("Type Definition reference cannot be NULL!");
186 BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
190 * Test for the method
192 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
193 * packageNameForGeneratedType(String, SchemaPath)}</li>
197 public void testPackageNameForGeneratedTypeNullBasePackageName() {
198 expectedEx.expect(NullPointerException.class);
199 BindingGeneratorUtil.packageNameForGeneratedType(null, null);
203 * Test for the method
205 * <li>{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
206 * packageNameForGeneratedType(String, SchemaPath)}</li>
210 public void testPackageNameForGeneratedTypeNullSchemaPath() {
211 expectedEx.expect(NullPointerException.class);
212 BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
216 * Test for the method
218 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
219 * parseToClassName(String)}</li>
223 public void testParseToClassNameNullValue() {
224 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
225 assertEquals("TestClassName", className);
227 expectedEx.expect(IllegalArgumentException.class);
228 expectedEx.expectMessage("Name can not be null");
229 className = BindingGeneratorUtil.parseToClassName(null);
233 * Test for the method
235 * <li>{@link BindingGeneratorUtil#parseToClassName(String)
236 * parseToClassName(String)}</li>
240 public void testParseToClassNameEmptyValue() {
241 String className = BindingGeneratorUtil.parseToClassName("test-class-name");
242 assertEquals("TestClassName", className);
244 expectedEx.expect(IllegalArgumentException.class);
245 expectedEx.expectMessage("Name can not be empty");
246 className = BindingGeneratorUtil.parseToClassName("");
250 * Test for the method
252 * <li>{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
253 * resolveJavaReservedWordEquivalency(String)}</li>
257 public void testValidateParameterName() {
258 assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
259 assertEquals("Return value is incorrect.", "whatever",
260 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
261 assertEquals("Return value is incorrect.", "_case",
262 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
268 * <li>parseToClassName</li>
270 * <li>parseToCamelCase</li>
272 * <li>replaceWithCamelCase</li>
274 * </ul> <li>parseToValidParamName</li>
276 * <li>parseToCamelCase</li>
278 * <li>replaceWithCamelCase</li>
284 public void testParsingMethods() {
285 // parseToClassName method testing
286 assertEquals("Class name has incorrect format", "SomeTestingClassName",
287 BindingMapping.getClassName(" some-testing_class name "));
288 assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
289 BindingMapping.getClassName(" 0 some-testing_class name "));
291 // parseToValidParamName
292 assertEquals("Parameter name has incorrect format", "someTestingParameterName",
293 BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
294 assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
295 BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
299 public void computeDefaultSUIDTest() {
300 GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
302 MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
303 method.setAccessModifier(AccessModifier.PUBLIC);
304 generatedTypeBuilder.addProperty("myProperty");
305 generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
307 assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
312 public void getRestrictionsTest() {
313 final Optional<String> absent = Optional.absent();
314 final StringTypeBuilder builder =
315 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
317 builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
318 builder.setLengthAlternatives(ImmutableList.of(
319 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
321 Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
323 assertNotNull(restrictions);
325 assertEquals(1, restrictions.getLengthConstraints().size());
326 assertEquals(0, restrictions.getRangeConstraints().size());
327 assertEquals(1, restrictions.getPatternConstraints().size());
329 assertFalse(restrictions.isEmpty());
330 assertTrue(restrictions.getLengthConstraints().contains(
331 BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
332 assertTrue(restrictions.getPatternConstraints().contains(
333 BaseConstraints.newPatternConstraint(".*", absent, absent)));
337 public void getEmptyRestrictionsTest() {
338 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
339 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
341 assertNotNull(restrictions);
342 assertTrue(restrictions.isEmpty());
346 public void getDefaultIntegerRestrictionsTest() {
347 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
348 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
350 assertNotNull(restrictions);
351 assertFalse(restrictions.isEmpty());
352 assertEquals(((IntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
353 restrictions.getRangeConstraints());
354 assertTrue(restrictions.getLengthConstraints().isEmpty());
355 assertTrue(restrictions.getPatternConstraints().isEmpty());
359 public void getDefaultUnsignedIntegerRestrictionsTest() {
360 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
361 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
363 assertNotNull(restrictions);
364 assertFalse(restrictions.isEmpty());
365 assertEquals(((UnsignedIntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
366 restrictions.getRangeConstraints());
367 assertTrue(restrictions.getLengthConstraints().isEmpty());
368 assertTrue(restrictions.getPatternConstraints().isEmpty());
372 public void getDefaultDecimalRestrictionsTest() {
373 final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
374 final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
376 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
378 assertNotNull(restrictions);
379 assertFalse(restrictions.isEmpty());
380 assertEquals(base.getRangeConstraints(), restrictions.getRangeConstraints());
381 assertTrue(restrictions.getLengthConstraints().isEmpty());
382 assertTrue(restrictions.getPatternConstraints().isEmpty());