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