Map identities to proper objects
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / UnionWithIdentityrefTest.java
1 /*
2  * Copyright (c) 2016 Intel corporation 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.java.api.generator;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import java.io.File;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.Method;
16 import java.net.URL;
17 import java.net.URLClassLoader;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
20
21 /**
22  * Union constructor with indentityref. Previously identityref was ignored so that there is no constructor for
23  * identityref.
24  */
25 public class UnionWithIdentityrefTest extends BaseCompilationTest {
26
27     @Test
28     public void test() throws Exception {
29         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-identityref");
30         final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-identityref");
31         generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
32
33         // Test if sources are compilable
34         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
35
36         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
37         Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG
38             + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
39         Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG
40             + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
41         Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG
42             + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
43
44         Object identOneValue = identOneClass.getDeclaredField(BindingMapping.VALUE_STATIC_FIELD_NAME).get(null);
45
46         // test UnionType with IdentOne argument
47         Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
48             identBaseClass);
49         Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneValue);
50
51         Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
52         Object actualUint8 = getUint8.invoke(unionType);
53         assertNull(actualUint8);
54
55         Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
56         Object actualIdentityref = getIdentityref.invoke(unionType);
57         assertEquals(identOneValue, actualIdentityref);
58
59         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
60     }
61 }