Merge "HostTracker Bundle Separation"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin-it / src / test / java / org / opendaylight / controller / yang2sources / plugin / it / YangToSourcesPluginTestIT.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.controller.yang2sources.plugin.it;
9
10 import static org.junit.Assert.*;
11 import static org.junit.matchers.JUnitMatchers.*;
12
13 import java.io.File;
14 import java.net.URL;
15
16 import org.apache.maven.it.VerificationException;
17 import org.apache.maven.it.Verifier;
18 import org.junit.Test;
19
20 public class YangToSourcesPluginTestIT {
21
22     // TODO Test yang files in transitive dependencies
23
24     @Test
25     public void testYangRootNotExist() {
26         try {
27             setUp("YangRootNotExist/", false);
28         } catch (VerificationException e) {
29             assertVerificationException(e,
30                     "[ERROR] yang-to-sources: Unable to parse yang files from ");
31             assertVerificationException(
32                     e,
33                     "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
34             return;
35         }
36
37         fail("Verification exception should have been thrown");
38     }
39
40     @Test
41     public void testCorrect() throws VerificationException {
42         Verifier v = setUp("Correct/", false);
43         verifyCorrectLog(v);
44     }
45
46     @Test
47     public void testAdditionalConfiguration() throws VerificationException {
48         Verifier v = setUp("AdditionalConfig/", false);
49         v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: {nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}");
50         v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
51         v.verifyTextInLog(File.separator
52                 + "files marked as resources: META-INF" + File.separator
53                 + "yang");
54         v.verifyTextInLog("target"
55                 + File.separator
56                 + "generated-resources marked as resources for generator: org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
57     }
58
59     @Test
60     public void testMissingYangInDep() throws VerificationException {
61         try {
62             setUp("MissingYangInDep/", false);
63         } catch (VerificationException e) {
64             assertVerificationException(
65                     e,
66                     "org.opendaylight.controller.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
67             return;
68         }
69
70         fail("Verification exception should have been thrown");
71     }
72
73     static void verifyCorrectLog(Verifier v) throws VerificationException {
74         v.verifyErrorFreeLog();
75         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
76         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
77         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
78     }
79
80     @Test
81     public void testNoGenerators() throws VerificationException {
82         Verifier v = setUp("NoGenerators/", false);
83         v.verifyErrorFreeLog();
84         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
85     }
86
87     @Test
88     public void testUnknownGenerator() throws VerificationException {
89         Verifier v = setUp("UnknownGenerator/", true);
90         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
91         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
92         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
93         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
94         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
95     }
96
97     @Test
98     public void testNoYangFiles() throws VerificationException {
99         Verifier v = setUp("NoYangFiles/", false);
100         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from []");
101         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
102         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
103     }
104
105     static void assertVerificationException(VerificationException e,
106             String string) {
107         assertThat(e.getMessage(), containsString(string));
108     }
109
110     static Verifier setUp(String project, boolean ignoreF)
111             throws VerificationException {
112         final URL path = YangToSourcesPluginTestIT.class.getResource("/"
113                 + project + "pom.xml");
114         File parent = new File(path.getPath());
115         Verifier verifier = new Verifier(parent.getParent());
116         if (ignoreF)
117             verifier.addCliOption("-fn");
118         verifier.setMavenDebug(true);
119         verifier.executeGoal("generate-sources");
120         return verifier;
121     }
122
123     @Test
124     public void testNoOutputDir() throws VerificationException {
125         Verifier v = YangToSourcesPluginTestIT.setUp("NoOutputDir/", false);
126         verifyCorrectLog(v);
127     }
128
129     @Test
130     public void testFindResourceOnCp() throws VerificationException {
131         Verifier v1 = new Verifier(new File(getClass().getResource(
132                 "/GenerateTest1/pom.xml").getPath()).getParent());
133         v1.executeGoal("clean");
134         v1.executeGoal("package");
135         v1.assertFilePresent("target/classes/META-INF/yang/testfile1.yang");
136         v1.assertFilePresent("target/classes/META-INF/yang/testfile2.yang");
137         v1.assertFilePresent("target/classes/META-INF/yang/testfile3.yang");
138
139         Verifier v2 = YangToSourcesPluginTestIT.setUp("GenerateTest2/", false);
140         v2.executeGoal("clean");
141         v2.executeGoal("package");
142         v2.assertFilePresent("target/classes/META-INF/yang/private.yang");
143         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile1.yang");
144         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile2.yang");
145         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile3.yang");
146     }
147 }