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