Bug 5883 - no constructor for indentityref in union 22/38822/8
authorIsaku Yamahata <isaku.yamahata@intel.com>
Tue, 10 May 2016 19:55:54 +0000 (12:55 -0700)
committerRobert Varga <nite@hq.sk>
Sat, 28 May 2016 21:13:19 +0000 (21:13 +0000)
The generated java code for union with identityref doens't include a
constructor for identityref.

module union-with-identityref {
    yang-version 1;
    namespace "urn:opendaylight:yang:union:test";
    prefix "uniontest";

    description "test union with identityref";

    revision "2016-05-09";

    identity ident-base;
    identity ident-one {
        base ident-base;
    }
    identity ident-two {
        base ident-base;
    }

    typedef union-type {
        type union {
            type uint8;
            type identityref {
                base ident-base;
            }
        }
    }
}

generated java code:
public class UnionType
 implements Serializable {
    private static final long serialVersionUID = 4724108168179933454L;
    private final java.lang.Short _uint8;
    private final char[] _value;

    public UnionType(java.lang.Short _uint8) {
        super();
        this._uint8 = _uint8;
        this._value = _uint8.toString().toCharArray();
    }

Change-Id: Idf8f7df54afd0934e47b19c131ea41d046a7b5df
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionWithIdentityrefTest.java [new file with mode: 0644]
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/union-with-identityref/union-with-identityref.yang [new file with mode: 0644]

index bffb1c1f3ce6b16c33d743733a37562820e37520..50f04381b44ebdb659b844d114a8756738905cb9 100644 (file)
@@ -779,8 +779,7 @@ public final class TypeProviderImpl implements TypeProvider {
                     makeSerializable((GeneratedTOBuilderImpl) genTOBuilder);
                     returnType = genTOBuilder.toInstance();
                 } else {
-                    final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
-                            innerTypeDefinition, typedef);
+                    final Type javaType = javaTypeForSchemaDefinitionType(innerTypeDefinition, typedef);
                     returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
                 }
                 if (returnType != null) {
@@ -929,8 +928,7 @@ public final class TypeProviderImpl implements TypeProvider {
                         unionTypeName, unionGenTOBuilder);
                 updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
             } else {
-                final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(unionType,
-                        parentNode);
+                final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
                 updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
             }
         }
@@ -1697,4 +1695,4 @@ public final class TypeProviderImpl implements TypeProvider {
         return BindingMapping.getPropertyName(type.getQName().getLocalName());
     }
 
-}
\ No newline at end of file
+}
diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionWithIdentityrefTest.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionWithIdentityrefTest.java
new file mode 100644 (file)
index 0000000..0c30ac0
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2016 Intel corporation and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.java.api.generator.test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.collect.ImmutableSet;
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * union constructor with indentityref
+ * previously identityref was ignored so that there is no constructor
+ * for indentityref
+ *
+ */
+public class UnionWithIdentityrefTest extends BaseCompilationTest {
+
+    @Test
+    public void test() throws Exception {
+        final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "union-with-identityref");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "union-with-identityref");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
+
+        // Test if sources are compilable
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+
+        ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
+        Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
+        Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
+        Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
+
+        // test UnionType with IdentOne argument
+        Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass, Class.class);
+        Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneClass);
+
+        Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
+        Object actualUint8 = getUint8.invoke(unionType);
+        assertNull(actualUint8);
+
+        Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
+        Object actualIdentityref = getIdentityref.invoke(unionType);
+        assertEquals(identOneClass, actualIdentityref);
+
+        Method getValue = unionTypeClass.getDeclaredMethod("getValue");
+        Object actualValue = getValue.invoke(unionType);
+        assertArrayEquals(identOneClass.toString().toCharArray(), (char[])actualValue);
+
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
+        final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
+        final SchemaContext context = RetestUtils.parseYangSources(sourceFiles);
+        final List<Type> types = bindingGenerator.generateTypes(context);
+        final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
+        generator.generateToFile(sourcesOutputDir);
+    }
+}
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/union-with-identityref/union-with-identityref.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/union-with-identityref/union-with-identityref.yang
new file mode 100644 (file)
index 0000000..95a4dee
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2016 Intel Corporation and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+module union-with-identityref {
+    yang-version 1;
+    namespace "urn:opendaylight:yang:union:test";
+    prefix "uniontest";
+
+    description "test union with identityref";
+
+    revision "2016-05-09";
+
+    identity ident-base;
+    identity ident-one {
+        base ident-base;
+    }
+    identity ident-two {
+        base ident-base;
+    }
+
+    typedef union-type {
+        type union {
+            type uint8;
+            type identityref {
+                base ident-base;
+            }
+        }
+    }
+}
\ No newline at end of file