Introduce top-level pom file.
[mdsal.git] / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / test / AugmentToUsesInAugmentCompilationTest.java
1 /*
2  * Copyright (c) 2013 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.yangtools.sal.java.api.generator.test;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.BASE_PKG;
12 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
13 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.FS;
14 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
15 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_BAR;
16 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_BAZ;
17 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_FOO;
18 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertFilesCount;
19 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
20 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
21 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
22 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testAugmentation;
23 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
24 import com.google.common.collect.ImmutableSet;
25 import java.io.File;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.util.List;
29 import org.junit.Test;
30 import org.opendaylight.yangtools.sal.binding.model.api.Type;
31 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
35
36     @Test
37     public void testAugmentToUsesInAugment() throws Exception {
38         final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "augment-uses-to-augment");
39         assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
40         final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "augment-uses-to-augment");
41         assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
42
43         final List<File> sourceFiles = getSourceFiles("/compilation/augment-uses-to-augment");
44         final SchemaContext context = parser.parseFiles(sourceFiles);
45         final List<Type> types = bindingGenerator.generateTypes(context);
46         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
47         generator.generateToFile(sourcesOutputDir);
48
49         // Test if all sources are generated from 'module foo'
50         File fooParent = new File(sourcesOutputDir, NS_FOO);
51         assertFilesCount(fooParent, 4);
52         assertTrue(new File(fooParent, "IgpLinkAttributes.java").exists());
53         assertTrue(new File(fooParent, "Link1.java").exists());
54         assertTrue(new File(fooParent, "Link1Builder.java").exists());
55
56         // Test if all sources are generated from 'module bar'
57         File barParent = new File(sourcesOutputDir, NS_BAR);
58         assertFilesCount(barParent, 7);
59         assertTrue(new File(barParent, "BarData.java").exists());
60         assertTrue(new File(barParent, "NetworkTopology.java").exists());
61         assertTrue(new File(barParent, "NetworkTopologyBuilder.java").exists());
62         assertTrue(new File(barParent, "Link.java").exists());
63         assertTrue(new File(barParent, "LinkAttributes.java").exists());
64
65         File networkParent = new File(barParent, "network");
66         assertFilesCount(networkParent, 1);
67         File topologyParent = new File(networkParent, "topology");
68         assertFilesCount(topologyParent, 3);
69         assertTrue(new File(topologyParent, "Topology.java").exists());
70         assertTrue(new File(topologyParent, "TopologyBuilder.java").exists());
71         assertTrue(new File(topologyParent, "TopologyKey.java").exists());
72
73         File linkParent = new File(barParent, "link");
74         assertFilesCount(linkParent, 3);
75         assertTrue(new File(linkParent, "Link.java").exists());
76         assertTrue(new File(linkParent, "LinkBuilder.java").exists());
77         assertTrue(new File(linkParent, "LinkKey.java").exists());
78
79         // Test if all sources are generated from 'module baz'
80         File bazParent = new File(sourcesOutputDir, NS_BAZ);
81         assertFilesCount(bazParent, 4);
82         assertTrue(new File(bazParent, "IgpLinkAttributes1.java").exists());
83         assertTrue(new File(bazParent, "IgpLinkAttributes1Builder.java").exists());
84         assertTrue(new File(bazParent, "LinkAttributes.java").exists());
85
86         // Test if sources are compilable
87         testCompilation(sourcesOutputDir, compiledOutputDir);
88
89         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
90
91         try {
92             Class<?> link1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Link1", true, loader);
93             String augmentableNode = BASE_PKG + ".urn.opendaylight.bar.rev131008.link.Link";
94             testAugmentation(link1Class, augmentableNode);
95
96             Class<?> igpLinkAttributesClass = Class.forName(BASE_PKG
97                     + ".urn.opendaylight.foo.rev131008.IgpLinkAttributes", true, loader);
98             assertImplementsIfc(link1Class, igpLinkAttributesClass);
99         } catch (ClassNotFoundException e) {
100             throw new AssertionError("Class for augment wasn't generated");
101         }
102
103         try {
104             Class<?> igpLinkAttributes1Class = Class.forName(BASE_PKG
105                     + ".urn.opendaylight.baz.rev131008.IgpLinkAttributes1", true, loader);
106             String augmentableNode = BASE_PKG + ".urn.opendaylight.foo.rev131008.igp.link.attributes.IgpLinkAttributes";
107             testAugmentation(igpLinkAttributes1Class, augmentableNode);
108
109             Class<?> linkAttributesClass = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.LinkAttributes",
110                     true, loader);
111             assertImplementsIfc(igpLinkAttributes1Class, linkAttributesClass);
112         } catch (ClassNotFoundException e) {
113             throw new AssertionError("Class for augment wasn't generated");
114         }
115
116         cleanUp(sourcesOutputDir, compiledOutputDir);
117     }
118
119 }