Rehost BindingMapping in yang.binding.contract.Naming
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / UnionWithMultipleIdentityrefsTest.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech s.r.o. 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
12 import java.io.File;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Method;
15 import java.net.URL;
16 import java.net.URLClassLoader;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.binding.contract.Naming;
19
20 public class UnionWithMultipleIdentityrefsTest extends BaseCompilationTest {
21     @Test
22     public void test() throws Exception {
23         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-multiple-identityrefs");
24         final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-multiple-identityrefs");
25         generateTestSources("/compilation/union-with-multiple-identityrefs", sourcesOutputDir);
26
27         // Test if sources are compilable
28         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
29
30         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
31         Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG
32                 + ".urn.opendaylight.yang.union.test.rev220428.IdentOne", true, loader);
33         Class<?> identTwoClass = Class.forName(CompilationTestUtils.BASE_PKG
34                 + ".urn.opendaylight.yang.union.test.rev220428.IdentTwo", true, loader);
35         Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG
36                 + ".urn.opendaylight.yang.union.test.rev220428.UnionType", true, loader);
37
38         Object identOneValue = identOneClass.getDeclaredField(Naming.VALUE_STATIC_FIELD_NAME).get(null);
39         Object identTwoValue = identTwoClass.getDeclaredField(Naming.VALUE_STATIC_FIELD_NAME).get(null);
40
41         Constructor<?> unionTypeIdentOneConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
42                 identOneClass);
43         Constructor<?> unionTypeIdentTwoConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
44                 identTwoClass);
45         Object unionTypeOne = unionTypeIdentOneConstructor.newInstance(identOneValue);
46         Object unionTypeTwo = unionTypeIdentTwoConstructor.newInstance(identTwoValue);
47
48         Method getIdentityOne = unionTypeClass.getDeclaredMethod("getIdentOne");
49         Object actualIdentityOne = getIdentityOne.invoke(unionTypeOne);
50         assertEquals(identOneValue, actualIdentityOne);
51
52         Method getIdentityTwo = unionTypeClass.getDeclaredMethod("getIdentTwo");
53         Object actualIdentityTwo = getIdentityTwo.invoke(unionTypeTwo);
54         assertEquals(identTwoValue, actualIdentityTwo);
55
56         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
57     }
58 }