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