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