Cleanup GeneratedTypeBuilder/enclosing mechanics
[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.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.mock;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.collect.Range;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.Optional;
25 import org.junit.Test;
26 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
27 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
28 import org.opendaylight.mdsal.binding.model.api.Restrictions;
29 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
30 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
31 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTOBuilder;
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;
52
53 public class BindingGeneratorUtilTest {
54     private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "root"));
55
56     /*
57      * Tests methods:
58      * <ul>
59      * <li>moduleNamespaceToPackageName</li> - with revision
60      * <li>packageNameForGeneratedType</li>
61      * <ul>
62      * <li>validateJavaPackage</li>
63      * </ul>
64      * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
65      * - without revision </ul>
66      */
67     @Test
68     public void testBindingGeneratorUtilMethods() {
69         final Collection<? extends Module> modules = YangParserTestUtils.parseYangResources(
70             BindingGeneratorUtilTest.class, "/module.yang").getModules();
71         String packageName = "";
72         Module module = null;
73         for (Module m : modules) {
74             module = m;
75             break;
76         }
77         assertNotNull("Module can't be null", module);
78
79         // test of the method moduleNamespaceToPackageName()
80         packageName = BindingMapping.getRootPackageName(module.getQNameModule());
81         assertEquals("Generated package name is incorrect.",
82                 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
83
84         // test of the method packageNameForGeneratedType()
85         String subPackageNameForDataNode = "";
86         for (ContainerSchemaNode containerSchemaNode : SchemaNodeUtils.getAllContainers(module)) {
87             if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
88                 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
89                         containerSchemaNode.getPath());
90                 break;
91             }
92         }
93         assertEquals("The name of the subpackage is incorrect.",
94                 "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",
95                 subPackageNameForDataNode);
96
97         // test method computeDefaultSUID
98         GeneratedTypeBuilder genTypeBuilder = new CodegenGeneratedTypeBuilder(
99             JavaTypeName.create("org.opendaylight.yangtools.test", "TestType"));
100         genTypeBuilder.addMethod("testMethod");
101         genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
102         genTypeBuilder.addEnclosingTransferObject(new CodegenGeneratedTOBuilder(genTypeBuilder.getIdentifier()
103             .createEnclosed("testObject")).build());
104         genTypeBuilder.addProperty("newProp");
105         GeneratedTypeBuilder genType = new CodegenGeneratedTypeBuilder(
106             JavaTypeName.create("org.opendaylight.yangtools.test", "Type2"));
107         genTypeBuilder.addImplementsType(genType);
108         long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
109
110         GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder(
111             JavaTypeName.create("org.opendaylight.yangtools.test2", "TestType2"));
112         long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
113         assertNotEquals(computedSUID, computedSUID2);
114     }
115
116     /*
117      * Test for the method
118      * &lt;ul&gt;
119      * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
120      * packageNameForGeneratedType(String, SchemaPath)}&lt;/li&gt;
121      * &lt;/ul&gt;
122      */
123     @Test
124     public void testPackageNameForGeneratedTypeNullBasePackageName() {
125         assertThrows(NullPointerException.class, () ->  BindingGeneratorUtil.packageNameForGeneratedType(null, null));
126     }
127
128     /*
129      * Test for the method
130      * &lt;ul&gt;
131      * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
132      * packageNameForGeneratedType(String, SchemaPath)}&lt;/li&gt;
133      * &lt;/ul&gt;
134      */
135     @Test
136     public void testPackageNameForGeneratedTypeNullSchemaPath() {
137         assertThrows(NullPointerException.class,
138             () -> BindingGeneratorUtil.packageNameForGeneratedType("test.package", null));
139     }
140
141     /*
142      * Test for the method
143      * &lt;ul&gt;
144      * &lt;li&gt;{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
145      * resolveJavaReservedWordEquivalency(String)}&lt;/li&gt;
146      * &lt;ul&gt;
147      */
148     @Test
149     public void testValidateParameterName() {
150         assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
151         assertEquals("Return value is incorrect.", "whatever",
152                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
153         assertEquals("Return value is incorrect.", "_case",
154                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
155     }
156
157     /*
158      * Tests the methods:
159      * &lt;ul&gt;
160      * &lt;li&gt;parseToClassName&lt;/li&gt;
161      * &lt;ul&gt;
162      * &lt;li&gt;parseToCamelCase&lt;/li&gt;
163      * &lt;ul&gt;
164      * &lt;li&gt;replaceWithCamelCase&lt;/li&gt;
165      * &lt;/ul&gt;
166      * &lt;/ul&gt; &lt;li&gt;parseToValidParamName&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;
173      * &lt;ul&gt;
174      */
175     @Test
176     public void testParsingMethods() {
177         // getClassName method testing
178         assertEquals("Class name has incorrect format", "SomeTestingClassName",
179             BindingMapping.getClassName("  some-testing_class name   "));
180         assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
181             BindingMapping.getClassName("  0 some-testing_class name   "));
182
183         // getPropertyName
184         assertEquals("Parameter name has incorrect format", "someTestingParameterName",
185             BindingMapping.getPropertyName("  some-testing_parameter   name   "));
186         assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
187             BindingMapping.getPropertyName("  0some-testing_parameter   name   "));
188     }
189
190     @Test
191     public void computeDefaultSUIDTest() {
192         CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
193             JavaTypeName.create("my.package", "MyName"));
194
195         MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
196         method.setAccessModifier(AccessModifier.PUBLIC);
197         generatedTypeBuilder.addProperty("myProperty");
198         generatedTypeBuilder.addImplementsType(Types.typeForClass(Serializable.class));
199
200         assertEquals(6788238694991761868L, BindingGeneratorUtil.computeDefaultSUID(generatedTypeBuilder));
201
202     }
203
204     @Test
205     public void getRestrictionsTest() throws InvalidLengthConstraintException {
206         final Optional<String> absent = Optional.empty();
207         final StringTypeBuilder builder =
208                 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
209
210         builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
211         builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
212
213         Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
214
215         assertNotNull(restrictions);
216         assertEquals(ImmutableSet.of(Range.closed(1, 2)),
217             restrictions.getLengthConstraint().get().getAllowedRanges().asRanges());
218         assertFalse(restrictions.getRangeConstraint().isPresent());
219         assertEquals(1, restrictions.getPatternConstraints().size());
220
221         assertFalse(restrictions.isEmpty());
222         assertTrue(restrictions.getPatternConstraints().contains(
223                 BaseConstraints.newPatternConstraint(".*", absent, absent)));
224     }
225
226     @Test
227     public void getEmptyRestrictionsTest() {
228         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build();
229         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
230
231         assertNotNull(restrictions);
232         assertTrue(restrictions.isEmpty());
233     }
234
235     @Test
236     public void getDefaultIntegerRestrictionsTest() {
237         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build();
238         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
239
240         assertNotNull(restrictions);
241         assertFalse(restrictions.isEmpty());
242         assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
243                 restrictions.getRangeConstraint());
244         assertFalse(restrictions.getLengthConstraint().isPresent());
245         assertTrue(restrictions.getPatternConstraints().isEmpty());
246     }
247
248     @Test
249     public void getDefaultUnsignedIntegerRestrictionsTest() {
250         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build();
251         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
252
253         assertNotNull(restrictions);
254         assertFalse(restrictions.isEmpty());
255         assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
256                 restrictions.getRangeConstraint());
257         assertFalse(restrictions.getLengthConstraint().isPresent());
258         assertTrue(restrictions.getPatternConstraints().isEmpty());
259     }
260
261     @Test
262     public void getDefaultDecimalRestrictionsTest() {
263         final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build();
264         final TypeDefinition<?> type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build();
265
266         final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type);
267
268         assertNotNull(restrictions);
269         assertFalse(restrictions.isEmpty());
270         assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
271         assertFalse(restrictions.getLengthConstraint().isPresent());
272         assertTrue(restrictions.getPatternConstraints().isEmpty());
273     }
274
275     @Test
276     public void unicodeCharReplaceTest() {
277         String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
278
279         assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
280             BindingGeneratorUtil.replaceAllIllegalChars(inputString));
281     }
282 }