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.jupiter.api.Assertions.assertThrows;
13 import static org.junit.jupiter.api.Assertions.fail;
15 import com.google.common.base.Joiner;
16 import com.google.common.io.Resources;
18 import java.nio.file.Files;
19 import java.util.Properties;
20 import org.apache.maven.it.VerificationException;
21 import org.apache.maven.it.Verifier;
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.Disabled;
24 import org.junit.jupiter.api.Test;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 class YangToSourcesPluginTestIT {
29 private static final Logger LOG = LoggerFactory.getLogger(YangToSourcesPluginTestIT.class);
30 private static final String MAVEN_OPTS = "MAVEN_OPTS";
31 private static final String DESTFILE = "destfile=";
33 private static String ARGLINE_PREFIX;
34 private static String ARGLINE_SUFFIX;
37 static void beforeAll() {
38 final var argLine = System.getProperty("itArgPath");
39 if (argLine == null || argLine.isBlank()) {
43 final int destFileIndex = argLine.indexOf(DESTFILE);
44 if (destFileIndex == -1) {
45 LOG.warn("Cannot find \"{} in \"{}\", disabling integration", DESTFILE, argLine);
49 ARGLINE_PREFIX = argLine.substring(0, destFileIndex + DESTFILE.length());
50 final int commaIndex = argLine.indexOf(',', ARGLINE_PREFIX.length());
51 ARGLINE_SUFFIX = commaIndex == -1 ? "" : argLine.substring(commaIndex);
54 // TODO Test yang files in transitive dependencies
57 void testYangRootNotExist() throws Exception {
58 setUp("test-parent/YangRootNotExist/", false)
59 .verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
63 void testCorrect() throws Exception {
64 verifyCorrectLog(setUp("test-parent/Correct/", false));
68 @Disabled // FIXME depends on fix of a bug processing multiple fileGenerator definitions having same identifier
69 void testAdditionalConfiguration() throws Exception {
70 final var vrf = setUp("test-parent/AdditionalConfig/", false);
71 vrf.verifyTextInLog("""
72 [DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.plugin.\
73 generator.api.TestFileGenerator: {nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}""");
74 vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
75 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator: {c1=config}");
76 vrf.verifyTextInLog("[DEBUG] yang-to-sources: YANG files marked as resources:");
77 vrf.verifyTextInLog(Joiner.on(File.separator).join("target", "generated-sources", "spi")
78 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
82 void testMissingYangInDep() throws Exception {
83 final var ex = assertThrows(VerificationException.class, () -> setUp("test-parent/MissingYangInDep/", false));
84 assertThat(ex.getMessage(), containsString("Imported module [unknownDep] was not found."));
87 void verifyCorrectLog(final Verifier vrf) throws VerificationException {
88 vrf.verifyErrorFreeLog();
89 vrf.verifyTextInLog("[INFO] yang-to-sources: Project model files found: ");
90 vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator "
91 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator instantiated");
92 vrf.verifyTextInLog("[INFO] yang-to-sources: Sources generated by "
93 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator: 0");
97 void testNoGenerators() throws Exception {
98 final var vrf = setUp("test-parent/NoGenerators/", false);
99 vrf.verifyErrorFreeLog();
100 vrf.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
104 void testInvalidVersion() throws Exception {
105 final var vrf = setUp("test-parent/InvalidVersion/", false);
106 vrf.verifyErrorFreeLog();
107 vrf.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
111 void testUnknownGenerator() throws Exception {
112 final var vrf = setUp("test-parent/UnknownGenerator/", true);
113 vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator "
114 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator instantiated");
115 vrf.verifyTextInLog("[WARNING] yang-to-sources: No generator found for identifier unknown");
119 void testNoYangFiles() throws Exception {
120 setUp("test-parent/NoYangFiles/", false).verifyTextInLog("[INFO] yang-to-sources: No input files found");
123 static Verifier setUp(final String project, final boolean ignoreF) throws Exception {
124 final var path = YangToSourcesPluginTestIT.class.getResource("/" + project + "pom.xml");
125 final var parent = new File(path.toURI()).getParentFile();
126 final var verifier = new Verifier(parent.toString());
128 verifier.addCliOption("-fn");
131 final var parentPath = Resources.getResource(YangToSourcesPluginTestIT.class, "/test-parent/pom.xml");
132 final var parentBuildDir = new File(parentPath.toURI()).getParentFile().getParentFile().getParentFile();
133 final var parentEffectiveSettingsXML = new File(parentBuildDir, "effective-settings.xml");
134 if (parentEffectiveSettingsXML.exists()) {
135 verifier.addCliOption("-gs");
136 verifier.addCliOption(parentEffectiveSettingsXML.getAbsolutePath());
138 fail(parentEffectiveSettingsXML.getAbsolutePath());
141 verifier.setForkJvm(true);
142 verifier.setMavenDebug(true);
144 if (ARGLINE_PREFIX != null) {
146 ARGLINE_PREFIX + parent.getParent() + "/" + parent.getName() + ".exec" + ARGLINE_SUFFIX;
147 final var mavenOpts = System.getenv(MAVEN_OPTS);
148 final var newMavenOpts = mavenOpts == null ? argLine : mavenOpts + " " + argLine;
149 LOG.debug("Adjusted {} to \"{}\"", MAVEN_OPTS, newMavenOpts);
150 verifier.setEnvironmentVariable(MAVEN_OPTS, newMavenOpts);
153 verifier.executeGoal("generate-sources");
158 void testNoOutputDir() throws Exception {
159 verifyCorrectLog(YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false));
163 void testFindResourceOnCp() throws Exception {
164 final var v1 = setUp("test-parent/GenerateTest1/", false);
165 v1.executeGoal("clean");
166 v1.executeGoal("package");
168 var buildDir = getMavenBuildDirectory(v1);
169 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
170 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
171 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
173 final var v2 = setUp("test-parent/GenerateTest2/", false);
174 v2.executeGoal("clean");
175 v2.executeGoal("package");
177 buildDir = getMavenBuildDirectory(v2);
178 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private@2013-02-27.yang");
179 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
180 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
181 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
185 void testFileGenerator() throws Exception {
186 final var v1 = setUp("test-parent/FileGenerator/", false);
187 v1.executeGoal("clean");
188 v1.executeGoal("package");
190 final var buildDir = getMavenBuildDirectory(v1);
192 v1.assertFilePresent(buildDir + "/generated-sources/"
193 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator/fooGenSource.test");
194 v1.assertFilePresent(buildDir + "/generated-resources/"
195 + "org.opendaylight.yangtools.plugin.generator.api.TestFileGenerator/foo-gen-resource");
196 v1.assertFilePresent(buildDir + "/../src/main/java/fooSource.test");
197 v1.assertFilePresent(buildDir + "/../src/main/resources/foo-resource");
200 private static String getMavenBuildDirectory(final Verifier verifier) throws Exception {
201 final var sp = new Properties();
202 try (var is = Files.newInputStream(new File(verifier.getBasedir() + "/it-project.properties").toPath())) {
205 return sp.getProperty("target.dir");