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 // TODO Test yang files in transitive dependencies
34 public void testYangRootNotExist() throws Exception {
35 Verifier v = setUp("test-parent/YangRootNotExist/", false);
36 v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
40 public void testCorrect() throws Exception {
41 Verifier v = setUp("test-parent/Correct/", false);
46 public void testAdditionalConfiguration() throws Exception {
47 Verifier v = setUp("test-parent/AdditionalConfig/", false);
48 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}");
49 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
50 v.verifyTextInLog(File.separator
51 + "files marked as resources: META-INF/yang");
53 Joiner.on(File.separator).join(
54 Arrays.asList("target", "generated-sources", "spi"))
56 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
60 public void testMissingYangInDep() throws Exception {
62 setUp("test-parent/MissingYangInDep/", false);
63 } catch (VerificationException e) {
64 assertVerificationException(
66 "org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException: Imported module " +
67 "[unknownDep] was not found.");
71 fail("Verification exception should have been thrown");
74 static void verifyCorrectLog(final Verifier v) throws VerificationException {
75 v.verifyErrorFreeLog();
76 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
77 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
78 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
82 public void testNoGenerators() throws Exception {
83 Verifier v = setUp("test-parent/NoGenerators/", false);
84 v.verifyErrorFreeLog();
85 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
89 public void testInvalidVersion() throws Exception {
90 Verifier v = setUp("test-parent/InvalidVersion/", false);
91 v.verifyErrorFreeLog();
92 v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
96 public void testUnknownGenerator() throws Exception {
97 Verifier v = setUp("test-parent/UnknownGenerator/", true);
98 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
99 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
100 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
101 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
102 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
106 public void testNoYangFiles() throws Exception {
107 Verifier v = setUp("test-parent/NoYangFiles/", false);
108 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
111 static void assertVerificationException(final VerificationException e,
112 final String string) {
113 assertThat(e.getMessage(), containsString(string));
116 static Verifier setUp(final String project, final boolean ignoreF)
117 throws VerificationException, URISyntaxException, IOException {
118 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
119 + project + "pom.xml");
120 File parent = new File(path.toURI());
121 Verifier verifier = new Verifier(parent.getParent());
123 verifier.addCliOption("-fn");
126 final Optional<String> maybeSettings = getEffectiveSettingsXML();
127 if (maybeSettings.isPresent()) {
128 verifier.addCliOption("-gs");
129 verifier.addCliOption(maybeSettings.get());
131 verifier.setMavenDebug(true);
132 verifier.executeGoal("generate-sources");
137 public void testNoOutputDir() throws Exception {
138 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
143 public void testFindResourceOnCp() throws Exception {
144 Verifier v1 = setUp("test-parent/GenerateTest1/", false);
145 v1.executeGoal("clean");
146 v1.executeGoal("package");
148 String buildDir = getMavenBuildDirectory(v1);
149 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
150 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
151 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
153 Verifier v2 = setUp("test-parent/GenerateTest2/", false);
154 v2.executeGoal("clean");
155 v2.executeGoal("package");
157 buildDir = getMavenBuildDirectory(v2);
158 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
159 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
160 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
161 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
164 private static String getMavenBuildDirectory(Verifier verifier) throws IOException {
165 Properties sp = new Properties();
166 try (InputStream is = new FileInputStream(verifier.getBasedir() + "/it-project.properties")) {
169 return sp.getProperty("target.dir");
172 private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException, IOException {
173 final URL path = YangToSourcesPluginTestIT.class.getResource("/test-parent/pom.xml");
174 File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile();
175 File effectiveSettingsXML = new File(buildDir, "effective-settings.xml");
176 if (effectiveSettingsXML.exists()) {
177 return Optional.of(effectiveSettingsXML.getAbsolutePath());
179 fail(effectiveSettingsXML.getAbsolutePath());
180 return Optional.empty();