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