Revert "Fix broken tests according to yangtools...
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / 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.test;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.ImmutableSet;
16 import java.io.File;
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.Method;
19 import java.net.URL;
20 import java.net.URLClassLoader;
21 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 /**
29  * union constructor with indentityref
30  * previously identityref was ignored so that there is no constructor
31  * for indentityref
32  *
33  */
34 public class UnionWithIdentityrefTest extends BaseCompilationTest {
35
36     @Test
37     public void test() throws Exception {
38         final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "union-with-identityref");
39         assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
40         final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "union-with-identityref");
41         assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
42
43         generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
44
45         // Test if sources are compilable
46         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
47
48         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
49         Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
50         Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
51         Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
52
53         // test UnionType with IdentOne argument
54         Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass, Class.class);
55         Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneClass);
56
57         Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
58         Object actualUint8 = getUint8.invoke(unionType);
59         assertNull(actualUint8);
60
61         Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
62         Object actualIdentityref = getIdentityref.invoke(unionType);
63         assertEquals(identOneClass, actualIdentityref);
64
65         Method getValue = unionTypeClass.getDeclaredMethod("getValue");
66         Object actualValue = getValue.invoke(unionType);
67         assertArrayEquals(identOneClass.toString().toCharArray(), (char[])actualValue);
68
69         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
70     }
71
72     private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
73         final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
74         final SchemaContext context = YangParserTestUtils.parseYangSources(sourceFiles);
75         final List<Type> types = bindingGenerator.generateTypes(context);
76         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
77         generator.generateToFile(sourcesOutputDir);
78     }
79 }