Adjust to yangtools-2.0.0 changes
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / CascadeUsesCompilationTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.BASE_PKG;
15 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
16 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.FS;
17 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
18 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_BAR;
19 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_BAZ;
20 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_FOO;
21 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertFilesCount;
22 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
23 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.cleanUp;
24 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.getSourceFiles;
25 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
26
27 import com.google.common.collect.ImmutableSet;
28 import java.io.File;
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Method;
31 import java.net.URL;
32 import java.net.URLClassLoader;
33 import java.util.List;
34 import org.junit.Test;
35 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
36 import org.opendaylight.mdsal.binding.model.api.Type;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 public class CascadeUsesCompilationTest extends BaseCompilationTest {
41
42     @Test
43     public void testCascadeUsesCompilation() throws Exception {
44         final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "cascade-uses");
45         assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
46         final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "cascade-uses");
47         assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
48
49         final List<File> sourceFiles = getSourceFiles("/compilation/cascade-uses");
50         final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
51         final List<Type> types = bindingGenerator.generateTypes(context);
52         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
53         generator.generateToFile(sourcesOutputDir);
54
55         // Test if all sources are generated from module foo
56         File parent = new File(sourcesOutputDir, NS_FOO);
57         assertFilesCount(parent, 5);
58         File fooData = new File(parent, "FooData.java");
59         File foo_gr1 = new File(parent, "FooGr1.java");
60         File nodes = new File(parent, "Nodes.java");
61         File nodesBuilder = new File(parent, "NodesBuilder.java");
62         assertTrue(fooData.exists());
63         assertTrue(foo_gr1.exists());
64         assertTrue(nodes.exists());
65         assertTrue(nodesBuilder.exists());
66
67         // Test if all sources are generated from module bar
68         parent = new File(sourcesOutputDir, NS_BAR);
69         assertFilesCount(parent, 2);
70         File barGr1 = new File(parent, "BarGr1.java");
71         File barGr2 = new File(parent, "BarGr2.java");
72         assertTrue(barGr1.exists());
73         assertTrue(barGr2.exists());
74
75         // Test if all sources are generated from module baz
76         parent = new File(sourcesOutputDir, NS_BAZ);
77         assertFilesCount(parent, 1);
78         File bazGr1 = new File(parent, "BazGr1.java");
79         assertTrue(bazGr1.exists());
80
81         // Test if sources are compilable
82         testCompilation(sourcesOutputDir, compiledOutputDir);
83
84         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
85         Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
86                 loader);
87         Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
88                 loader);
89         Class<?> fooGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.FooGr1", true, loader);
90         Class<?> barGr2Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr2", true, loader);
91         Class<?> barGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr1", true, loader);
92         Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
93
94         // test generated interface from 'container nodes'
95         assertImplementsIfc(nodesClass, fooGr1Class);
96         assertImplementsIfc(nodesClass, barGr2Class);
97
98         // test generated builder for 'container nodes'
99         assertFalse(nodesBuilderClass.isInterface());
100         Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
101         assertEquals(6, nodesBuilderConstructors.length);
102
103         // test generation of builder constructors from uses in 'container nodes'
104         Constructor<?> defaultConstructor = null;
105         Constructor<?> usesFooGr1 = null;
106         Constructor<?> usesBarGr2 = null;
107         Constructor<?> usesBarGr1 = null;
108         Constructor<?> usesBazGr1 = null;
109         for (Constructor<?> c : nodesBuilderConstructors) {
110             Class<?>[] params = c.getParameterTypes();
111             if (params.length == 0) {
112                 defaultConstructor = c;
113             } else {
114                 assertEquals(1, params.length);
115                 if (params[0].equals(fooGr1Class)) {
116                     usesFooGr1 = c;
117                 } else if (params[0].equals(barGr2Class)) {
118                     usesBarGr2 = c;
119                 } else if (params[0].equals(barGr1Class)) {
120                     usesBarGr1 = c;
121                 } else if (params[0].equals(bazGr1Class)) {
122                     usesBazGr1 = c;
123                 }
124             }
125         }
126         assertNotNull(defaultConstructor);
127         assertNotNull(usesFooGr1);
128         assertNotNull(usesBarGr2);
129         assertNotNull(usesBarGr1);
130         assertNotNull(usesBazGr1);
131
132         Method fieldsFromMethod = null;
133         for (Method m : nodesBuilderClass.getDeclaredMethods()) {
134             String methodName = m.getName();
135             if ("fieldsFrom".equals(methodName)) {
136                 fieldsFromMethod = m;
137             }
138         }
139         assertNotNull(fieldsFromMethod);
140         assertEquals(1, fieldsFromMethod.getParameterTypes().length);
141
142         cleanUp(sourcesOutputDir, compiledOutputDir);
143     }
144
145 }