From 4692da9978ca3ec59c0acf49c10b55111ecff790 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Tue, 10 May 2016 12:55:54 -0700 Subject: [PATCH] Bug 5883 - no constructor for indentityref in union 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 --- .../binding/yang/types/TypeProviderImpl.java | 8 +- .../test/UnionWithIdentityrefTest.java | 78 +++++++++++++++++++ .../union-with-identityref.yang | 33 ++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionWithIdentityrefTest.java create mode 100644 binding/mdsal-binding-java-api-generator/src/test/resources/compilation/union-with-identityref/union-with-identityref.yang diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java index bffb1c1f3c..50f04381b4 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java @@ -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 index 0000000000..0c30ac0abf --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionWithIdentityrefTest.java @@ -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 sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath); + final SchemaContext context = RetestUtils.parseYangSources(sourceFiles); + final List 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 index 0000000000..95a4dee032 --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/union-with-identityref/union-with-identityref.yang @@ -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 -- 2.36.6