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