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.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
14 import com.google.common.base.Joiner;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
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;
29 public class YangToSourcesPluginTestIT {
31 private static String GLOBAL_SETTINGS_OVERRIDE;
32 private static String USER_SETTINGS_OVERRIDE;
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");
41 // TODO Test yang files in transitive dependencies
44 public void testYangRootNotExist() throws Exception {
45 Verifier v = setUp("test-parent/YangRootNotExist/", false);
46 v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
50 public void testCorrect() throws Exception {
51 Verifier v = setUp("test-parent/Correct/", false);
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");
63 Joiner.on(File.separator).join(
64 Arrays.asList("target", "generated-sources", "spi"))
66 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
70 public void testMissingYangInDep() throws Exception {
72 setUp("test-parent/MissingYangInDep/", false);
73 } catch (VerificationException e) {
74 assertVerificationException(
76 "org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException: Imported module " +
77 "[unknownDep] was not found.");
81 fail("Verification exception should have been thrown");
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");
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");
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:");
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}");
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");
121 static void assertVerificationException(final VerificationException e,
122 final String string) {
123 assertThat(e.getMessage(), containsString(string));
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());
133 verifier.addCliOption("-fn");
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());
142 if (USER_SETTINGS_OVERRIDE != null) {
143 verifier.addCliOption("-s");
144 verifier.addCliOption(USER_SETTINGS_OVERRIDE);
146 verifier.setMavenDebug(true);
147 verifier.executeGoal("generate-sources");
152 public void testNoOutputDir() throws Exception {
153 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
158 public void testFindResourceOnCp() throws Exception {
159 Verifier v1 = new Verifier(new File(getClass().getResource(
160 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
161 v1.executeGoal("clean");
162 v1.executeGoal("package");
164 String buildDir = getMavenBuildDirectory(v1);
165 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
166 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
167 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
169 Verifier v2 = new Verifier(new File(getClass().getResource(
170 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
171 v2.executeGoal("clean");
172 v2.executeGoal("package");
174 buildDir = getMavenBuildDirectory(v2);
175 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
176 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
177 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
178 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
181 private static String getMavenBuildDirectory(Verifier verifier) throws IOException {
182 Properties sp = new Properties();
183 try (InputStream is = new FileInputStream(verifier.getBasedir() + "/it-project.properties")) {
186 return sp.getProperty("target.dir");
189 private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException, IOException {
190 final URL path = YangToSourcesPluginTestIT.class.getResource("/test-parent/pom.xml");
191 File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile();
192 File effectiveSettingsXML = new File(buildDir, "effective-settings.xml");
193 if (effectiveSettingsXML.exists()) {
194 return Optional.of(effectiveSettingsXML.getAbsolutePath());
196 fail(effectiveSettingsXML.getAbsolutePath());
197 return Optional.empty();