0d2f426d34e617f0c3217062783e3ed620d2312c
[mdsal.git] / binding / mdsal-binding-generator-util / src / test / java / org / opendaylight / mdsal / binding / model / util / BindingGeneratorUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.model.util;
9
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;
17
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;
24 import java.util.Set;
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;
53
54 public class BindingGeneratorUtilTest {
55     private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "root"));
56
57     @Rule
58     public ExpectedException expectedEx = ExpectedException.none();
59
60     /*
61      * Tests methods:
62      * <ul>
63      * <li>moduleNamespaceToPackageName</li> - with revision
64      * <li>packageNameForGeneratedType</li>
65      * <ul>
66      * <li>validateJavaPackage</li>
67      * </ul>
68      * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
69      * - without revision </ul>
70      */
71     @Test
72     public void testBindingGeneratorUtilMethods() {
73         final Set<Module> modules = YangParserTestUtils.parseYangResources(BindingGeneratorUtilTest.class,
74             "/module.yang").getModules();
75         String packageName = "";
76         Module module = null;
77         for (Module m : modules) {
78             module = m;
79             break;
80         }
81         assertNotNull("Module can't be null", module);
82
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);
87
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());
96                 break;
97             }
98         }
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);
102
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);
114
115         GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder(
116             JavaTypeName.create("org.opendaylight.yangtools.test2", "TestType2"));
117         long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
118         assertNotEquals(computedSUID, computedSUID2);
119     }
120
121     /*
122      * Test for the method
123      * &lt;ul&gt;
124      * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
125      * packageNameForGeneratedType(String, SchemaPath)}&lt;/li&gt;
126      * &lt;/ul&gt;
127      */
128     @Test
129     public void testPackageNameForGeneratedTypeNullBasePackageName() {
130         expectedEx.expect(NullPointerException.class);
131         BindingGeneratorUtil.packageNameForGeneratedType(null, null);
132     }
133
134     /*
135      * Test for the method
136      * &lt;ul&gt;
137      * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
138      * packageNameForGeneratedType(String, SchemaPath)}&lt;/li&gt;
139      * &lt;/ul&gt;
140      */
141     @Test
142     public void testPackageNameForGeneratedTypeNullSchemaPath() {
143         expectedEx.expect(NullPointerException.class);
144         BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
145     }
146
147     /*
148      * Test for the method
149      * &lt;ul&gt;
150      * &lt;li&gt;{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
151      * resolveJavaReservedWordEquivalency(String)}&lt;/li&gt;
152      * &lt;ul&gt;
153      */
154     @Test
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"));
161     }
162
163     /*
164      * Tests the methods:
165      * &lt;ul&gt;
166      * &lt;li&gt;parseToClassName&lt;/li&gt;
167      * &lt;ul&gt;
168      * &lt;li&gt;parseToCamelCase&lt;/li&gt;
169      * &lt;ul&gt;
170      * &lt;li&gt;replaceWithCamelCase&lt;/li&gt;
171      * &lt;/ul&gt;
172      * &lt;/ul&gt; &lt;li&gt;parseToValidParamName&lt;/li&gt;
173      * &lt;ul&gt;
174      * &lt;li&gt;parseToCamelCase&lt;/li&gt;
175      * &lt;ul&gt;
176      * &lt;li&gt;replaceWithCamelCase&lt;/li&gt;
177      * &lt;/ul&gt;
178      * &lt;/ul&gt;
179      * &lt;ul&gt;
180      */
181     @Test
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   "));
188
189         // getPropertyName
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   "));
194     }
195
196     @Test
197     public void computeDefaultSUIDTest() {
198         CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
199             JavaTypeName.create("my.package", "MyName"));
200
201         MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
202         method.setAccessModifier(AccessModifier.PUBLIC);
203         generatedTypeBuilder.addProperty("myProperty");
204         generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
205
206         assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
207
208     }
209
210     @Test
211     public void getRestrictionsTest() throws InvalidLengthConstraintException {
212         final Optional<String> absent = Optional.empty();
213         final StringTypeBuilder builder =
214                 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
215
216         builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
217         builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
218
219         Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
220
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());
226
227         assertFalse(restrictions.isEmpty());
228         assertTrue(restrictions.getPatternConstraints().contains(
229                 BaseConstraints.newPatternConstraint(".*", absent, absent)));
230     }
231
232     @Test
233     public void getEmptyRestrictionsTest() {
234         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
235         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
236
237         assertNotNull(restrictions);
238         assertTrue(restrictions.isEmpty());
239     }
240
241     @Test
242     public void getDefaultIntegerRestrictionsTest() {
243         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
244         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
245
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());
252     }
253
254     @Test
255     public void getDefaultUnsignedIntegerRestrictionsTest() {
256         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
257         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
258
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());
265     }
266
267     @Test
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();
271
272         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
273
274         assertNotNull(restrictions);
275         assertFalse(restrictions.isEmpty());
276         assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
277         assertFalse(restrictions.getLengthConstraint().isPresent());
278         assertTrue(restrictions.getPatternConstraints().isEmpty());
279     }
280
281     @Test
282     public void unicodeCharReplaceTest() {
283         String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
284
285         assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
286             BindingGeneratorUtil.replaceAllIllegalChars(inputString));
287     }
288 }