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;
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.Test;
30 public class YangToSourcesPluginTestIT {
32 // TODO Test yang files in transitive dependencies
35 public void testYangRootNotExist() throws Exception {
36 setUp("test-parent/YangRootNotExist/", false)
37 .verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
41 public void testCorrect() throws VerificationException, URISyntaxException, IOException {
42 verifyCorrectLog(setUp("test-parent/Correct/", false));
46 public void testAdditionalConfiguration() throws Exception {
47 Verifier vrf = setUp("test-parent/AdditionalConfig/", false);
48 vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
49 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: "
50 + "{nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}");
51 vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
52 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
53 vrf.verifyTextInLog("[DEBUG] yang-to-sources: YANG files marked as resources:");
54 vrf.verifyTextInLog(Joiner.on(File.separator).join(Arrays.asList("target", "generated-sources", "spi"))
55 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi."
56 + "CodeGeneratorTestImpl");
60 public void testMissingYangInDep() throws Exception {
62 setUp("test-parent/MissingYangInDep/", false);
63 fail("Verification exception should have been thrown");
64 } catch (VerificationException e) {
65 assertVerificationException(e,
66 "SchemaResolutionException{unsatisfiedImports={RevisionSourceIdentifier"
67 + " [name=private@2013-02-27]=[ModuleImportImpl"
68 + " [name=unknownDep, revision=2013-02-27, semanticVersion=0.0.0]]}");
73 static void verifyCorrectLog(final Verifier vrf) throws VerificationException {
74 vrf.verifyErrorFreeLog();
75 vrf.verifyTextInLog("[INFO] yang-to-sources: Project model files parsed: ");
76 vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from "
77 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
78 vrf.verifyTextInLog("[INFO] yang-to-sources: Sources generated by "
79 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
83 public void testNoGenerators() throws Exception {
84 Verifier vrf = setUp("test-parent/NoGenerators/", false);
85 vrf.verifyErrorFreeLog();
86 vrf.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
90 public void testInvalidVersion() throws Exception {
91 Verifier vrf = setUp("test-parent/InvalidVersion/", false);
92 vrf.verifyErrorFreeLog();
93 vrf.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
97 public void testUnknownGenerator() throws Exception {
98 Verifier vrf = setUp("test-parent/UnknownGenerator/", true);
99 vrf.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
100 vrf.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
101 vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from "
102 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
103 vrf.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi."
104 + "CodeGeneratorTestImpl: null");
105 vrf.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list"
106 + "(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
110 public void testNoYangFiles() throws Exception {
111 setUp("test-parent/NoYangFiles/", false).verifyTextInLog("[INFO] yang-to-sources: No input files found");
114 static void assertVerificationException(final VerificationException ex, final String string) {
115 assertThat(ex.getMessage(), containsString(string));
118 static Verifier setUp(final String project, final boolean ignoreF)
119 throws VerificationException, URISyntaxException, IOException {
120 final URL path = YangToSourcesPluginTestIT.class.getResource("/" + project + "pom.xml");
121 final Verifier verifier = new Verifier(new File(path.toURI()).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 verifyCorrectLog(YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false));
142 public void testFindResourceOnCp() throws Exception {
143 Verifier v1 = setUp("test-parent/GenerateTest1/", false);
144 v1.executeGoal("clean");
145 v1.executeGoal("package");
147 String buildDir = getMavenBuildDirectory(v1);
148 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
149 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
150 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
152 Verifier v2 = setUp("test-parent/GenerateTest2/", false);
153 v2.executeGoal("clean");
154 v2.executeGoal("package");
156 buildDir = getMavenBuildDirectory(v2);
157 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private@2013-02-27.yang");
158 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
159 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
160 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
163 private static String getMavenBuildDirectory(final Verifier verifier) throws IOException {
164 final Properties sp = new Properties();
165 final Path path = new File(verifier.getBasedir() + "/it-project.properties").toPath();
166 try (InputStream is = Files.newInputStream(path)) {
169 return sp.getProperty("target.dir");
172 private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException,
174 final URL path = Resources.getResource(YangToSourcesPluginTestIT.class, "/test-parent/pom.xml");
175 final File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile();
176 final File effectiveSettingsXML = new File(buildDir, "effective-settings.xml");
177 if (effectiveSettingsXML.exists()) {
178 return Optional.of(effectiveSettingsXML.getAbsolutePath());
181 fail(effectiveSettingsXML.getAbsolutePath());
182 return Optional.empty();