Migrate away from DataNodeIterator
[mdsal.git] / binding / mdsal-binding-generator-util / src / test / java / org / opendaylight / mdsal / binding / model / util / BindingGeneratorUtilTest.java
index 0112eb0bb143137804cbccf95a7e54edc97c2f2a..76240b31266d0f463ed52b20cf1208c2fcd2c0c1 100644 (file)
@@ -13,26 +13,24 @@ 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.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Range;
 import java.io.Serializable;
-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.CodegenGeneratedTypeBuilder;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
+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;
@@ -44,7 +42,7 @@ import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
 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;
@@ -53,12 +51,12 @@ 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("test", "/root"));
+    private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("test", "root"));
 
     @Rule
     public ExpectedException expectedEx = ExpectedException.none();
 
-    /**
+    /*
      * Tests methods:
      * <ul>
      * <li>moduleNamespaceToPackageName</li> - with revision
@@ -87,10 +85,8 @@ 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", 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());
@@ -101,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 CodegenGeneratedTypeBuilder("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 CodegenGeneratedTypeBuilder("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 CodegenGeneratedTypeBuilder("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)
@@ -188,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)
@@ -201,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)
@@ -251,7 +157,7 @@ public class BindingGeneratorUtilTest {
                 BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
     }
 
-    /**
+    /*
      * Tests the methods:
      * &lt;ul&gt;
      * &lt;li&gt;parseToClassName&lt;/li&gt;
@@ -271,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() {
-        CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder("my.package", "MyName");
+        CodegenGeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(
+            JavaTypeName.create("my.package", "MyName"));
 
         MethodSignatureBuilder method = generatedTypeBuilder.addMethod("myMethodName");
         method.setAccessModifier(AccessModifier.PUBLIC);