2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
10 import static org.junit.Assert.assertTrue;
12 import com.google.common.collect.ImmutableSet;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
17 import java.net.URLClassLoader;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
28 public void testAugmentToUsesInAugment() throws IOException, URISyntaxException {
29 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-uses-to-augment");
30 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-uses-to-augment");
31 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles("/compilation/augment-uses-to-augment");
32 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
33 final List<Type> types = bindingGenerator.generateTypes(context);
34 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
35 generator.generateToFile(sourcesOutputDir);
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());
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());
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());
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());
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());
74 // Test if sources are compilable
75 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
77 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
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);
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");
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);
97 Class<?> linkAttributesClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.baz.rev131008.LinkAttributes",
99 CompilationTestUtils.assertImplementsIfc(igpLinkAttributes1Class, linkAttributesClass);
100 } catch (ClassNotFoundException e) {
101 throw new AssertionError("Class for augment wasn't generated");
104 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);