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