Migrate away from DataNodeIterator
[mdsal.git] / binding / mdsal-binding-generator-util / src / test / java / org / opendaylight / mdsal / binding / model / util / BindingGeneratorUtilTest.java
index d37e495ab8b0f091d52e883e490ac2bdc2bf38b4..76240b31266d0f463ed52b20cf1208c2fcd2c0c1 100644 (file)
@@ -13,62 +13,50 @@ import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
-import java.io.File;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Range;
 import java.io.Serializable;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.Restrictions;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
-import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedTypeBuilderImpl;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
+import org.opendaylight.mdsal.binding.model.util.generated.type.builder.CodegenGeneratedTypeBuilder;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
-import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
+import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
 import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
+import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class BindingGeneratorUtilTest {
-    private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("/root"));
+    private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "root"));
 
     @Rule
     public ExpectedException expectedEx = ExpectedException.none();
 
-    private static List<File> loadTestResources(final String testFile) {
-        final List<File> testModels = new ArrayList<>();
-        File listModelFile;
-        try {
-            listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).toURI());
-        } catch (URISyntaxException e) {
-            throw new IllegalArgumentException("Failed to load sources from " + testFile);
-        }
-        testModels.add(listModelFile);
-        return testModels;
-    }
-
-    /**
+    /*
      * Tests methods:
      * &lt;ul&gt;
      * &lt;li&gt;moduleNamespaceToPackageName&lt;/li&gt; - with revision
@@ -80,10 +68,9 @@ public class BindingGeneratorUtilTest {
      * - without revision &lt;/ul&gt;
      */
     @Test
-    public void testBindingGeneratorUtilMethods() throws Exception {
-        List<File> testModels = loadTestResources("/module.yang");
-
-        final Set<Module> modules = YangParserTestUtils.parseYangSources(testModels).getModules();
+    public void testBindingGeneratorUtilMethods() {
+        final Set<Module> modules = YangParserTestUtils.parseYangResources(BindingGeneratorUtilTest.class,
+            "/module.yang").getModules();
         String packageName = "";
         Module module = null;
         for (Module m : modules) {
@@ -93,15 +80,13 @@ public class BindingGeneratorUtilTest {
         assertNotNull("Module can't be null", module);
 
         // test of the method moduleNamespaceToPackageName()
-        packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
+        packageName = BindingMapping.getRootPackageName(module.getQNameModule());
         assertEquals("Generated package name is incorrect.",
                 "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
 
         // test of the method packageNameForGeneratedType()
-        DataNodeIterator it = new DataNodeIterator(module);
-        List<ContainerSchemaNode> schemaContainers = it.allContainers();
         String subPackageNameForDataNode = "";
-        for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
+        for (ContainerSchemaNode containerSchemaNode : SchemaNodeUtils.getAllContainers(module)) {
             if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
                 subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
                         containerSchemaNode.getPath());
@@ -112,81 +97,25 @@ public class BindingGeneratorUtilTest {
                 "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",
                 subPackageNameForDataNode);
 
-        // test of the method packageNameForTypeDefinition
-        Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
-        String subPackageNameForTypeDefinition = "";
-        TypeDefinition<?> firstTypeDef = null;
-
-        for (TypeDefinition<?> tpDef : typeDefinitions) {
-            if (tpDef.getQName().getLocalName().equals("tpdf")) {
-                subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
-                firstTypeDef = tpDef;
-                break;
-            }
-        }
-        assertEquals("The name of the subpackage is incorrect.",
-                "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
-                subPackageNameForTypeDefinition);
-
-        // test method getRestrictions
-        Restrictions restriction = BindingGeneratorUtil.getRestrictions(firstTypeDef);
-        assertNotNull(restriction);
-
         // test method computeDefaultSUID
-        GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
+        GeneratedTypeBuilder genTypeBuilder = new CodegenGeneratedTypeBuilder(
+            JavaTypeName.create("org.opendaylight.yangtools.test", "TestType"));
         genTypeBuilder.addMethod("testMethod");
         genTypeBuilder.addAnnotation("org.opendaylight.yangtools.test.annotation", "AnnotationTest");
         genTypeBuilder.addEnclosingTransferObject("testObject");
         genTypeBuilder.addProperty("newProp");
-        GeneratedTypeBuilder genType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "Type2");
+        GeneratedTypeBuilder genType = new CodegenGeneratedTypeBuilder(
+            JavaTypeName.create("org.opendaylight.yangtools.test", "Type2"));
         genTypeBuilder.addImplementsType(genType);
         long computedSUID = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder);
 
-        GeneratedTypeBuilder genTypeBuilder2 = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test2", "TestType2");
+        GeneratedTypeBuilder genTypeBuilder2 = new CodegenGeneratedTypeBuilder(
+            JavaTypeName.create("org.opendaylight.yangtools.test2", "TestType2"));
         long computedSUID2 = BindingGeneratorUtil.computeDefaultSUID(genTypeBuilder2);
         assertNotEquals(computedSUID, computedSUID2);
-
-        // test of exception part of the method moduleNamespaceToPackageName()
-        Module moduleWithoutRevision = mock(Module.class);
-        doReturn(null).when(moduleWithoutRevision).getQNameModule();
-        try {
-            BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
-            fail("Expected IllegalArgumentException");
-        } catch (IllegalArgumentException e) {
-        }
-    }
-
-    /**
-     * Test for the method
-     * &lt;ul&gt;
-     * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
-     * packageNameForTypeDefinition(String, TypeDefinition)}&lt;/li&gt;
-     * &lt;/ul&gt;
-     */
-    @Test
-    @Deprecated
-    public void testPackageNameForTypeDefinitionNullBasePackageName() {
-        expectedEx.expect(IllegalArgumentException.class);
-        expectedEx.expectMessage("Base Package Name cannot be NULL!");
-        BindingGeneratorUtil.packageNameForTypeDefinition(null, null);
-    }
-
-    /**
-     * Test for the method
-     * &lt;ul&gt;
-     * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForTypeDefinition(String, TypeDefinition)
-     * packageNameForTypeDefinition(String, TypeDefinition)}&lt;/li&gt;
-     * &lt;/ul&gt;
-     */
-    @Test
-    @Deprecated
-    public void testPackageNameForTypeDefinitionNullTypeDefinition() {
-        expectedEx.expect(IllegalArgumentException.class);
-        expectedEx.expectMessage("Type Definition reference cannot be NULL!");
-        BindingGeneratorUtil.packageNameForTypeDefinition("test.package", null);
     }
 
-    /**
+    /*
      * Test for the method
      * &lt;ul&gt;
      * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
@@ -199,7 +128,7 @@ public class BindingGeneratorUtilTest {
         BindingGeneratorUtil.packageNameForGeneratedType(null, null);
     }
 
-    /**
+    /*
      * Test for the method
      * &lt;ul&gt;
      * &lt;li&gt;{@link BindingGeneratorUtil#packageNameForGeneratedType(String, SchemaPath)
@@ -212,41 +141,7 @@ public class BindingGeneratorUtilTest {
         BindingGeneratorUtil.packageNameForGeneratedType("test.package", null);
     }
 
-    /**
-     * Test for the method
-     * &lt;ul&gt;
-     * &lt;li&gt;{@link BindingGeneratorUtil#parseToClassName(String)
-     * parseToClassName(String)}&lt;/li&gt;
-     * &lt;/ul&gt;
-     */
-    @Test
-    public void testParseToClassNameNullValue() {
-        String className = BindingGeneratorUtil.parseToClassName("test-class-name");
-        assertEquals("TestClassName", className);
-
-        expectedEx.expect(IllegalArgumentException.class);
-        expectedEx.expectMessage("Name can not be null");
-        className = BindingGeneratorUtil.parseToClassName(null);
-    }
-
-    /**
-     * Test for the method
-     * &lt;ul&gt;
-     * &lt;li&gt;{@link BindingGeneratorUtil#parseToClassName(String)
-     * parseToClassName(String)}&lt;/li&gt;
-     * &lt;/ul&gt;
-     */
-    @Test
-    public void testParseToClassNameEmptyValue() {
-        String className = BindingGeneratorUtil.parseToClassName("test-class-name");
-        assertEquals("TestClassName", className);
-
-        expectedEx.expect(IllegalArgumentException.class);
-        expectedEx.expectMessage("Name can not be empty");
-        className = BindingGeneratorUtil.parseToClassName("");
-    }
-
-    /**
+    /*
      * Test for the method
      * &lt;ul&gt;
      * &lt;li&gt;{@link BindingGeneratorUtil#resolveJavaReservedWordEquivalency(String)
@@ -262,7 +157,7 @@ public class BindingGeneratorUtilTest {
                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
     }
 
-    /**
+    /*
      * Tests the methods:
      * &lt;ul&gt;
      * &lt;li&gt;parseToClassName&lt;/li&gt;
@@ -282,22 +177,23 @@ public class BindingGeneratorUtilTest {
      */
     @Test
     public void testParsingMethods() {
-        // parseToClassName method testing
+        // getClassName method testing
         assertEquals("Class name has incorrect format", "SomeTestingClassName",
-                BindingMapping.getClassName("  some-testing_class name   "));
+            BindingMapping.getClassName("  some-testing_class name   "));
         assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
-                BindingMapping.getClassName("  0 some-testing_class name   "));
+            BindingMapping.getClassName("  0 some-testing_class name   "));
 
-        // parseToValidParamName
+        // getPropertyName
         assertEquals("Parameter name has incorrect format", "someTestingParameterName",
-                BindingGeneratorUtil.parseToValidParamName("  some-testing_parameter   name   "));
+            BindingMapping.getPropertyName("  some-testing_parameter   name   "));
         assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
-                BindingGeneratorUtil.parseToValidParamName("  0some-testing_parameter   name   "));
+            BindingMapping.getPropertyName("  0some-testing_parameter   name   "));
     }
 
     @Test
     public void computeDefaultSUIDTest() {
-        GeneratedTypeBuilderImpl generatedTypeBuilder = new GeneratedTypeBuilderImpl("my.package", "MyName");
+        CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
+            JavaTypeName.create("my.package", "MyName"));
 
         MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
         method.setAccessModifier(AccessModifier.PUBLIC);
@@ -309,26 +205,23 @@ public class BindingGeneratorUtilTest {
     }
 
     @Test
-    public void getRestrictionsTest() {
-        final Optional<String> absent = Optional.absent();
+    public void getRestrictionsTest() throws InvalidLengthConstraintException {
+        final Optional<String> absent = Optional.empty();
         final StringTypeBuilder builder =
                 RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH);
 
         builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent));
-        builder.setLengthAlternatives(ImmutableList.of(
-            BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
+        builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), ImmutableList.of(ValueRange.of(1, 2)));
 
         Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build());
 
         assertNotNull(restrictions);
-
-        assertEquals(1, restrictions.getLengthConstraints().size());
-        assertEquals(0, restrictions.getRangeConstraints().size());
+        assertEquals(ImmutableSet.of(Range.closed(1, 2)),
+            restrictions.getLengthConstraint().get().getAllowedRanges().asRanges());
+        assertFalse(restrictions.getRangeConstraint().isPresent());
         assertEquals(1, restrictions.getPatternConstraints().size());
 
         assertFalse(restrictions.isEmpty());
-        assertTrue(restrictions.getLengthConstraints().contains(
-                BaseConstraints.newLengthConstraint(1, 2, absent, absent)));
         assertTrue(restrictions.getPatternConstraints().contains(
                 BaseConstraints.newPatternConstraint(".*", absent, absent)));
     }
@@ -349,9 +242,9 @@ public class BindingGeneratorUtilTest {
 
         assertNotNull(restrictions);
         assertFalse(restrictions.isEmpty());
-        assertEquals(((IntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
-                restrictions.getRangeConstraints());
-        assertTrue(restrictions.getLengthConstraints().isEmpty());
+        assertEquals(((Int16TypeDefinition) type.getBaseType()).getRangeConstraint(),
+                restrictions.getRangeConstraint());
+        assertFalse(restrictions.getLengthConstraint().isPresent());
         assertTrue(restrictions.getPatternConstraints().isEmpty());
     }
 
@@ -362,9 +255,9 @@ public class BindingGeneratorUtilTest {
 
         assertNotNull(restrictions);
         assertFalse(restrictions.isEmpty());
-        assertEquals(((UnsignedIntegerTypeDefinition) type.getBaseType()).getRangeConstraints(),
-                restrictions.getRangeConstraints());
-        assertTrue(restrictions.getLengthConstraints().isEmpty());
+        assertEquals(((Uint16TypeDefinition) type.getBaseType()).getRangeConstraint(),
+                restrictions.getRangeConstraint());
+        assertFalse(restrictions.getLengthConstraint().isPresent());
         assertTrue(restrictions.getPatternConstraints().isEmpty());
     }
 
@@ -377,8 +270,16 @@ public class BindingGeneratorUtilTest {
 
         assertNotNull(restrictions);
         assertFalse(restrictions.isEmpty());
-        assertEquals(base.getRangeConstraints(), restrictions.getRangeConstraints());
-        assertTrue(restrictions.getLengthConstraints().isEmpty());
+        assertEquals(base.getRangeConstraint(), restrictions.getRangeConstraint());
+        assertFalse(restrictions.getLengthConstraint().isPresent());
         assertTrue(restrictions.getPatternConstraints().isEmpty());
     }
+
+    @Test
+    public void unicodeCharReplaceTest() {
+        String inputString = "abcu\\uuuuu\\uuua\\u\\\\uabc\\\\uuuu\\\\\\uuuu\\\\\\\\uuuu///uu/u/u/u/u/u/u";
+
+        assertEquals("abcu\\\\uuuuu\\\\uuua\\\\u\\\\uabc\\\\uuuu\\\\uuuu\\\\uuuu///uu/u/u/u/u/u/u",
+            BindingGeneratorUtil.replaceAllIllegalChars(inputString));
+    }
 }