2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang2sources.plugin.it;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.fail;
14 import com.google.common.base.Joiner;
15 import com.google.common.io.Resources;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
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.Test;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class YangToSourcesPluginTestIT {
34 private static final Logger LOG = LoggerFactory.getLogger(YangToSourcesPluginTestIT.class);
35 private static final String MAVEN_OPTS = "MAVEN_OPTS";
36 private static final String DESTFILE = "destfile=";
38 private static String ARGLINE_PREFIX;
39 private static String ARGLINE_SUFFIX;
42 public static void beforeClass() {
43 final String argLine = System.getProperty("itArgPath");
44 if (argLine == null || argLine.isBlank()) {
48 final int destFileIndex = argLine.indexOf(DESTFILE);
49 if (destFileIndex == -1) {
50 LOG.warn("Cannot find \"{} in \"{}\", disabling integration", DESTFILE, argLine);
54 ARGLINE_PREFIX = argLine.substring(0, destFileIndex + DESTFILE.length());
55 final int commaIndex = argLine.indexOf(',', ARGLINE_PREFIX.length());
56 ARGLINE_SUFFIX = commaIndex == -1 ? "" : argLine.substring(commaIndex);
59 // TODO Test yang files in transitive dependencies
62 public void testYangRootNotExist() throws Exception {
63 setUp("test-parent/YangRootNotExist/", false)
64 .verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
68 public void testCorrect() throws VerificationException, URISyntaxException, IOException {
69 verifyCorrectLog(setUp("test-parent/Correct/", false));
73 public void testAdditionalConfiguration() throws Exception {
74 Verifier vrf = setUp("test-parent/AdditionalConfig/", false);
75 vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
76 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: "
77 + "{nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}");
78 vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
79 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
80 vrf.verifyTextInLog("[DEBUG] yang-to-sources: YANG files marked as resources:");
81 vrf.verifyTextInLog(Joiner.on(File.separator).join(Arrays.asList("target", "generated-sources", "spi"))
82 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi."
83 + "CodeGeneratorTestImpl");
87 public void testMissingYangInDep() throws Exception {
89 setUp("test-parent/MissingYangInDep/", false);
90 fail("Verification exception should have been thrown");
91 } catch (VerificationException e) {
92 assertVerificationException(e, "Imported module [unknownDep] was not found.");
96 static void verifyCorrectLog(final Verifier vrf) throws VerificationException {
97 vrf.verifyErrorFreeLog();
98 vrf.verifyTextInLog("[INFO] yang-to-sources: Project model files found: ");
99 vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from "
100 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
101 vrf.verifyTextInLog("[INFO] yang-to-sources: Sources generated by "
102 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: 0");
106 public void testNoGenerators() throws Exception {
107 Verifier vrf = setUp("test-parent/NoGenerators/", false);
108 vrf.verifyErrorFreeLog();
109 vrf.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
113 public void testInvalidVersion() throws Exception {
114 Verifier vrf = setUp("test-parent/InvalidVersion/", false);
115 vrf.verifyErrorFreeLog();
116 vrf.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
120 public void testUnknownGenerator() throws Exception {
121 Verifier vrf = setUp("test-parent/UnknownGenerator/", true);
122 vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from "
123 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
124 vrf.verifyTextInLog("on project unknown-generator: Failed to find code generator class unknown");
125 vrf.verifyTextInLog("MojoFailureException: Failed to find code generator class unknown");
126 vrf.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
130 public void testNoYangFiles() throws Exception {
131 setUp("test-parent/NoYangFiles/", false).verifyTextInLog("[INFO] yang-to-sources: No input files found");
134 static void assertVerificationException(final VerificationException ex, final String string) {
135 assertThat(ex.getMessage(), containsString(string));
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());
144 verifier.addCliOption("-fn");
147 getEffectiveSettingsXML().ifPresent(settings -> {
148 verifier.addCliOption("-gs");
149 verifier.addCliOption(settings);
152 verifier.setForkJvm(true);
153 verifier.setMavenDebug(true);
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);
164 verifier.executeGoal("generate-sources");
169 public void testNoOutputDir() throws Exception {
170 verifyCorrectLog(YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false));
174 public void testFindResourceOnCp() throws Exception {
175 Verifier v1 = setUp("test-parent/GenerateTest1/", false);
176 v1.executeGoal("clean");
177 v1.executeGoal("package");
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");
184 Verifier v2 = setUp("test-parent/GenerateTest2/", false);
185 v2.executeGoal("clean");
186 v2.executeGoal("package");
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");
196 public void testFileGenerator() throws Exception {
197 Verifier v1 = setUp("test-parent/FileGenerator/", false);
198 v1.executeGoal("clean");
199 v1.executeGoal("package");
201 String buildDir = getMavenBuildDirectory(v1);
203 v1.assertFilePresent(buildDir + "/generated-sources/"
204 + "org.opendaylight.yangtools.yang2sources.spi.TestFileGenerator/fooGenSource.test");
205 v1.assertFilePresent(buildDir + "/generated-resources/"
206 + "org.opendaylight.yangtools.yang2sources.spi.TestFileGenerator/foo-gen-resource");
207 v1.assertFilePresent(buildDir + "/../src/main/java/fooSource.test");
208 v1.assertFilePresent(buildDir + "/../src/main/resources/foo-resource");
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)) {
217 return sp.getProperty("target.dir");
220 private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException,
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());
229 fail(effectiveSettingsXML.getAbsolutePath());
230 return Optional.empty();