Merge "Updated example with OF model done during HackFest."
[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("[INFO] 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("[INFO] yang-to-sources: Additional configuration picked up for : org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
50     }
51
52     @Test
53     public void testMissingYangInDep() throws VerificationException {
54         try {
55             setUp("MissingYangInDep/", false);
56         } catch (VerificationException e) {
57             assertVerificationException(
58                     e,
59                     "org.opendaylight.controller.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
60             return;
61         }
62
63         fail("Verification exception should have been thrown");
64     }
65
66     static void verifyCorrectLog(Verifier v) throws VerificationException {
67         v.verifyErrorFreeLog();
68         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
69         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
70         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
71     }
72
73     @Test
74     public void testNoGenerators() throws VerificationException {
75         Verifier v = setUp("NoGenerators/", false);
76         v.verifyErrorFreeLog();
77         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
78     }
79
80     @Test
81     public void testUnknownGenerator() throws VerificationException {
82         Verifier v = setUp("UnknownGenerator/", true);
83         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
84         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
85         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
86         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
87         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
88     }
89
90     @Test
91     public void testNoYangFiles() throws VerificationException {
92         Verifier v = setUp("NoYangFiles/", false);
93         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from []");
94         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
95         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
96     }
97
98     static void assertVerificationException(VerificationException e,
99             String string) {
100         assertThat(e.getMessage(), containsString(string));
101     }
102
103     static Verifier setUp(String project, boolean ignoreF)
104             throws VerificationException {
105         Verifier verifier = new Verifier(new File("src/test/resources/"
106                 + project).getAbsolutePath());
107         if (ignoreF)
108             verifier.addCliOption("-fn");
109         verifier.executeGoal("generate-sources");
110         return verifier;
111     }
112
113     @Test
114     public void testNoOutputDir() throws VerificationException {
115         Verifier v = YangToSourcesPluginTestIT.setUp("NoOutputDir/", false);
116         verifyCorrectLog(v);
117     }
118
119     @Test
120     public void testFindResourceOnCp() throws VerificationException {
121         Verifier v1 = new Verifier(
122                 new File("src/test/resources/GenerateTest1/").getAbsolutePath());
123         v1.executeGoal("clean");
124         v1.executeGoal("package");
125         v1.assertFilePresent("target/classes/META-INF/yang/testfile1.yang");
126         v1.assertFilePresent("target/classes/META-INF/yang/testfile2.yang");
127         v1.assertFilePresent("target/classes/META-INF/yang/testfile3.yang");
128
129         Verifier v2 = YangToSourcesPluginTestIT.setUp("GenerateTest2/", false);
130         v2.executeGoal("clean");
131         v2.executeGoal("package");
132         v2.assertFilePresent("target/classes/META-INF/yang/private.yang");
133         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile1.yang");
134         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile2.yang");
135         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile3.yang");
136     }
137
138 }