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