1365e560c72a04e425f156d902f9ef76a0f293a5
[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.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
13 import com.google.common.base.Joiner;
14 import java.io.File;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.Arrays;
20 import java.util.Properties;
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 String GLOBAL_SETTINGS_OVERRIDE;
28     private static String USER_SETTINGS_OVERRIDE;
29
30     @BeforeClass
31     public static void setupClass() {
32         // OpenDaylight Jenkins does not have settings at the default path, pick file locations from environment
33         GLOBAL_SETTINGS_OVERRIDE = System.getenv("GLOBAL_SETTINGS_FILE");
34         USER_SETTINGS_OVERRIDE = System.getenv("SETTINGS_FILE");
35     }
36
37     // TODO Test yang files in transitive dependencies
38
39     @Test
40     public void testYangRootNotExist() throws Exception {
41         Verifier v = setUp("test-parent/YangRootNotExist/", false);
42         v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
43     }
44
45     @Test
46     public void testCorrect() throws Exception {
47         Verifier v = setUp("test-parent/Correct/", false);
48         verifyCorrectLog(v);
49     }
50
51     @Test
52     public void testAdditionalConfiguration() throws Exception {
53         Verifier v = setUp("test-parent/AdditionalConfig/", false);
54         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}");
55         v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
56         v.verifyTextInLog(File.separator
57                 + "files marked as resources: META-INF/yang");
58         v.verifyTextInLog(
59                 Joiner.on(File.separator).join(
60                         Arrays.asList("target", "generated-sources", "spi"))
61
62                         + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
63     }
64
65     @Test
66     public void testMissingYangInDep() throws Exception {
67         try {
68             setUp("test-parent/MissingYangInDep/", false);
69         } catch (VerificationException e) {
70             assertVerificationException(
71                     e,
72                     "org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException: Imported module " +
73                     "[ModuleIdentifierImpl{name='unknownDep', namespace=null, revision=2013-02-27}");
74             return;
75         }
76
77         fail("Verification exception should have been thrown");
78     }
79
80     static void verifyCorrectLog(final Verifier v) throws VerificationException {
81         v.verifyErrorFreeLog();
82         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
83         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
84         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
85     }
86
87     @Test
88     public void testNoGenerators() throws Exception {
89         Verifier v = setUp("test-parent/NoGenerators/", false);
90         v.verifyErrorFreeLog();
91         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
92     }
93
94     @Test
95     public void testInvalidVersion() throws Exception {
96         Verifier v = setUp("test-parent/InvalidVersion/", false);
97         v.verifyErrorFreeLog();
98         v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
99     }
100
101     @Test
102     public void testUnknownGenerator() throws Exception {
103         Verifier v = setUp("test-parent/UnknownGenerator/", true);
104         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
105         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
106         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
107         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
108         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
109     }
110
111     @Test
112     public void testNoYangFiles() throws Exception {
113         Verifier v = setUp("test-parent/NoYangFiles/", false);
114         v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
115     }
116
117     static void assertVerificationException(final VerificationException e,
118             final String string) {
119         assertThat(e.getMessage(), containsString(string));
120     }
121
122     static Verifier setUp(final String project, final boolean ignoreF)
123             throws VerificationException, URISyntaxException {
124         final URL path = YangToSourcesPluginTestIT.class.getResource("/"
125                 + project + "pom.xml");
126         File parent = new File(path.toURI());
127         Verifier verifier = new Verifier(parent.getParent());
128         if (ignoreF) {
129             verifier.addCliOption("-fn");
130         }
131         if (GLOBAL_SETTINGS_OVERRIDE != null) {
132             verifier.addCliOption("-gs");
133             verifier.addCliOption(GLOBAL_SETTINGS_OVERRIDE);
134         }
135         if (USER_SETTINGS_OVERRIDE != null) {
136             verifier.addCliOption("-s");
137             verifier.addCliOption(USER_SETTINGS_OVERRIDE);
138         }
139         verifier.setMavenDebug(true);
140         verifier.executeGoal("generate-sources");
141         return verifier;
142     }
143
144     @Test
145     public void testNoOutputDir() throws Exception {
146         Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
147         verifyCorrectLog(v);
148     }
149
150     @Test
151     public void testFindResourceOnCp() throws Exception {
152         Verifier v1 = new Verifier(new File(getClass().getResource(
153                 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
154         v1.executeGoal("clean");
155         v1.executeGoal("package");
156
157         Properties sp = new Properties();
158         try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
159             sp.load(is);
160         }
161         String buildDir = sp.getProperty("target.dir");
162
163         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
164         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
165         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
166
167         Verifier v2 = new Verifier(new File(getClass().getResource(
168                 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
169         v2.executeGoal("clean");
170         v2.executeGoal("package");
171
172         sp = new Properties();
173         try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
174             sp.load(is);
175         }
176         buildDir = sp.getProperty("target.dir");
177
178         v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
179         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
180         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
181         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
182     }
183
184 }