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