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