Remove yang-maven-plugin interfaces
[yangtools.git] / plugin / 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.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.fail;
13
14 import com.google.common.base.Joiner;
15 import com.google.common.io.Resources;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
20 import java.net.URL;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.Arrays;
24 import java.util.Optional;
25 import java.util.Properties;
26 import org.apache.maven.it.VerificationException;
27 import org.apache.maven.it.Verifier;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class YangToSourcesPluginTestIT {
35     private static final Logger LOG = LoggerFactory.getLogger(YangToSourcesPluginTestIT.class);
36     private static final String MAVEN_OPTS = "MAVEN_OPTS";
37     private static final String DESTFILE = "destfile=";
38
39     private static String ARGLINE_PREFIX;
40     private static String ARGLINE_SUFFIX;
41
42     @BeforeClass
43     public static void beforeClass() {
44         final String argLine = System.getProperty("itArgPath");
45         if (argLine == null || argLine.isBlank()) {
46             return;
47         }
48
49         final int destFileIndex = argLine.indexOf(DESTFILE);
50         if (destFileIndex == -1) {
51             LOG.warn("Cannot find \"{} in \"{}\", disabling integration", DESTFILE, argLine);
52             return;
53         }
54
55         ARGLINE_PREFIX = argLine.substring(0, destFileIndex + DESTFILE.length());
56         final int commaIndex = argLine.indexOf(',', ARGLINE_PREFIX.length());
57         ARGLINE_SUFFIX = commaIndex == -1 ? "" : argLine.substring(commaIndex);
58     }
59
60     // TODO Test yang files in transitive dependencies
61
62     @Test
63     public void testYangRootNotExist() throws Exception {
64         setUp("test-parent/YangRootNotExist/", false)
65             .verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
66     }
67
68     @Test
69     public void testCorrect() throws VerificationException, URISyntaxException, IOException {
70         verifyCorrectLog(setUp("test-parent/Correct/", false));
71     }
72
73     @Test
74     @Ignore // FIXME depends on fix of a bug processing multiple fileGenerator definitions having same identifier
75     public void testAdditionalConfiguration() throws Exception {
76         Verifier vrf = setUp("test-parent/AdditionalConfig/", false);
77         vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
78                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator: "
79                 + "{nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}");
80         vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
81                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator: {c1=config}");
82         vrf.verifyTextInLog("[DEBUG] yang-to-sources: YANG files marked as resources:");
83         vrf.verifyTextInLog(Joiner.on(File.separator).join(Arrays.asList("target", "generated-sources", "spi"))
84                 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi."
85                 + "CodeGeneratorTestImpl");
86     }
87
88     @Test
89     public void testMissingYangInDep() throws Exception {
90         try {
91             setUp("test-parent/MissingYangInDep/", false);
92             fail("Verification exception should have been thrown");
93         } catch (VerificationException e) {
94             assertVerificationException(e, "Imported module [unknownDep] was not found.");
95         }
96     }
97
98     static void verifyCorrectLog(final Verifier vrf) throws VerificationException {
99         vrf.verifyErrorFreeLog();
100         vrf.verifyTextInLog("[INFO] yang-to-sources: Project model files found: ");
101         vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator "
102                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator instantiated");
103         vrf.verifyTextInLog("[INFO] yang-to-sources: Sources generated by "
104                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator: 0");
105     }
106
107     @Test
108     public void testNoGenerators() throws Exception {
109         Verifier vrf = setUp("test-parent/NoGenerators/", false);
110         vrf.verifyErrorFreeLog();
111         vrf.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
112     }
113
114     @Test
115     public void testInvalidVersion() throws Exception {
116         Verifier vrf = setUp("test-parent/InvalidVersion/", false);
117         vrf.verifyErrorFreeLog();
118         vrf.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
119     }
120
121     @Test
122     public void testUnknownGenerator() throws Exception {
123         Verifier vrf = setUp("test-parent/UnknownGenerator/", true);
124         vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator "
125                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator instantiated");
126         vrf.verifyTextInLog("[WARNING] yang-to-sources: No generator found for identifier unknown");
127     }
128
129     @Test
130     public void testNoYangFiles() throws Exception {
131         setUp("test-parent/NoYangFiles/", false).verifyTextInLog("[INFO] yang-to-sources: No input files found");
132     }
133
134     static void assertVerificationException(final VerificationException ex, final String string) {
135         assertThat(ex.getMessage(), containsString(string));
136     }
137
138     static Verifier setUp(final String project, final boolean ignoreF)
139             throws VerificationException, URISyntaxException, IOException {
140         final URL path = YangToSourcesPluginTestIT.class.getResource("/" + project + "pom.xml");
141         final File parent = new File(path.toURI()).getParentFile();
142         final Verifier verifier = new Verifier(parent.toString());
143         if (ignoreF) {
144             verifier.addCliOption("-fn");
145         }
146
147         getEffectiveSettingsXML().ifPresent(settings -> {
148             verifier.addCliOption("-gs");
149             verifier.addCliOption(settings);
150         });
151
152         verifier.setForkJvm(true);
153         verifier.setMavenDebug(true);
154
155         if (ARGLINE_PREFIX != null) {
156             final String argLine =
157                 ARGLINE_PREFIX + parent.getParent() + "/" + parent.getName() + ".exec" + ARGLINE_SUFFIX;
158             final String mavenOpts = System.getenv(MAVEN_OPTS);
159             final String newMavenOpts = mavenOpts == null ? argLine : mavenOpts + " " + argLine;
160             LOG.debug("Adjusted {} to \"{}\"", MAVEN_OPTS, newMavenOpts);
161             verifier.setEnvironmentVariable(MAVEN_OPTS, newMavenOpts);
162         }
163
164         verifier.executeGoal("generate-sources");
165         return verifier;
166     }
167
168     @Test
169     public void testNoOutputDir() throws Exception {
170         verifyCorrectLog(YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false));
171     }
172
173     @Test
174     public void testFindResourceOnCp() throws Exception {
175         Verifier v1 = setUp("test-parent/GenerateTest1/", false);
176         v1.executeGoal("clean");
177         v1.executeGoal("package");
178
179         String buildDir = getMavenBuildDirectory(v1);
180         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
181         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
182         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
183
184         Verifier v2 = setUp("test-parent/GenerateTest2/", false);
185         v2.executeGoal("clean");
186         v2.executeGoal("package");
187
188         buildDir = getMavenBuildDirectory(v2);
189         v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private@2013-02-27.yang");
190         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
191         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
192         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
193     }
194
195     @Test
196     public void testFileGenerator() throws Exception {
197         Verifier v1 = setUp("test-parent/FileGenerator/", false);
198         v1.executeGoal("clean");
199         v1.executeGoal("package");
200
201         String buildDir = getMavenBuildDirectory(v1);
202
203         v1.assertFilePresent(buildDir + "/generated-sources/"
204                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator/fooGenSource.test");
205         v1.assertFilePresent(buildDir + "/generated-resources/"
206                 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator/foo-gen-resource");
207         v1.assertFilePresent(buildDir + "/../src/main/java/fooSource.test");
208         v1.assertFilePresent(buildDir + "/../src/main/resources/foo-resource");
209     }
210
211     private static String getMavenBuildDirectory(final Verifier verifier) throws IOException {
212         final Properties sp = new Properties();
213         final Path path = new File(verifier.getBasedir() + "/it-project.properties").toPath();
214         try (InputStream is = Files.newInputStream(path)) {
215             sp.load(is);
216         }
217         return sp.getProperty("target.dir");
218     }
219
220     private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException,
221             IOException {
222         final URL path = Resources.getResource(YangToSourcesPluginTestIT.class, "/test-parent/pom.xml");
223         final File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile();
224         final File effectiveSettingsXML = new File(buildDir, "effective-settings.xml");
225         if (effectiveSettingsXML.exists()) {
226             return Optional.of(effectiveSettingsXML.getAbsolutePath());
227         }
228
229         fail(effectiveSettingsXML.getAbsolutePath());
230         return Optional.empty();
231     }
232 }