dfe572e05cd4c60b146331325705e8b82f9ce997
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / NestedGroupingCompilationTest.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.assertTrue;
11 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.BASE_PKG;
12 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_TEST;
13 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertFilesCount;
14 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
15 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.cleanUp;
16 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.getSourceFiles;
17 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
18
19 import com.google.common.collect.ImmutableSet;
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.net.URLClassLoader;
25 import java.util.List;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
28 import org.opendaylight.mdsal.binding.model.api.Type;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31
32 /**
33  * Test correct code generation.
34  *
35  */
36 public class NestedGroupingCompilationTest extends BaseCompilationTest {
37
38     @Test
39     public void testListGeneration() throws Exception {
40         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("nested-grouping");
41         final File compiledOutputDir = CompilationTestUtils.compilerOutput("nested-grouping");
42         generateTestSources("/compilation/nested-grouping", sourcesOutputDir);
43
44         // Test if all sources are generated
45         File parent = new File(sourcesOutputDir, NS_TEST);
46         File foo = new File(parent, "Foo.java");
47         File fooBuilder = new File(parent, "FooBuilder.java");
48         File testData = new File(parent, "TestData.java");
49         File fooDir = new File(parent, "foo");
50         assertTrue(foo.exists());
51         assertTrue(fooBuilder.exists());
52         assertTrue(testData.exists());
53         assertTrue(fooDir.exists());
54         assertFilesCount(parent, 4);
55
56         parent = new File(parent, "foo");
57         File bar = new File(parent, "Bar.java");
58         assertTrue(bar.exists());
59         assertFilesCount(parent, 1);
60
61         // Test if sources are compilable
62         testCompilation(sourcesOutputDir, compiledOutputDir);
63
64         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
65         Class<?> fooClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Foo", true, loader);
66         Class<?> barClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.foo.Bar", true, loader);
67
68         // Test generated 'foo'
69         assertTrue(fooClass.isInterface());
70         assertImplementsIfc(fooClass, barClass);
71
72         cleanUp(sourcesOutputDir, compiledOutputDir);
73     }
74
75     private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
76             throws IOException, URISyntaxException {
77         final List<File> sourceFiles = getSourceFiles(resourceDirPath);
78         final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
79         final List<Type> types = bindingGenerator.generateTypes(context);
80         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
81         generator.generateToFile(sourcesOutputDir);
82     }
83
84 }