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.junit.Assert.assertThat;
11 import static org.junit.Assert.fail;
12 import static org.junit.matchers.JUnitMatchers.containsString;
15 import java.io.FileInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
19 import java.util.Properties;
21 import org.apache.maven.it.VerificationException;
22 import org.apache.maven.it.Verifier;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
26 public class YangToSourcesPluginTestIT {
27 private static final String SRC_PROPERTIES = "target/it-project.properties";
28 private static final String VERSION_PROP = "it-project.version";
29 private static Properties props;
31 // TODO Test yang files in transitive dependencies
34 public void testYangRootNotExist() {
36 setUp("YangRootNotExist/", false);
37 } catch (VerificationException e) {
38 assertVerificationException(e,
39 "[ERROR] yang-to-sources: Unable to parse yang files from ");
40 assertVerificationException(
42 "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
46 fail("Verification exception should have been thrown");
50 public void testCorrect() throws VerificationException {
51 Verifier v = setUp("Correct/", false);
56 public void testAdditionalConfiguration() throws VerificationException {
57 Verifier v = setUp("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");
62 v.verifyTextInLog("target"
64 + "generated-resources marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
68 public void testMissingYangInDep() throws VerificationException {
70 setUp("MissingYangInDep/", false);
71 } catch (VerificationException e) {
72 assertVerificationException(
74 "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
78 fail("Verification exception should have been thrown");
81 static void verifyCorrectLog(Verifier v) throws VerificationException {
82 v.verifyErrorFreeLog();
83 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
84 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
85 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
89 public void testNoGenerators() throws VerificationException {
90 Verifier v = setUp("NoGenerators/", false);
91 v.verifyErrorFreeLog();
92 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
96 public void testUnknownGenerator() throws VerificationException {
97 Verifier v = setUp("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 VerificationException {
107 Verifier v = setUp("NoYangFiles/", false);
108 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
111 static void assertVerificationException(VerificationException e,
113 assertThat(e.getMessage(), containsString(string));
117 public static void generateProps() throws IOException {
118 final Properties sp = new Properties();
119 try (InputStream is = new FileInputStream(new File(SRC_PROPERTIES))) {
123 props = new Properties(System.getProperties());
124 props.put(VERSION_PROP, sp.getProperty(VERSION_PROP));
127 static Verifier setUp(String project, boolean ignoreF)
128 throws VerificationException {
129 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
130 + project + "pom.xml");
131 File parent = new File(path.getPath());
132 Verifier verifier = new Verifier(parent.getParent());
134 verifier.addCliOption("-fn");
135 verifier.setMavenDebug(true);
136 verifier.setSystemProperties(props);
137 verifier.executeGoal("generate-sources");
142 public void testNoOutputDir() throws VerificationException {
143 Verifier v = YangToSourcesPluginTestIT.setUp("NoOutputDir/", false);
148 public void testFindResourceOnCp() throws VerificationException {
149 Verifier v1 = new Verifier(new File(getClass().getResource(
150 "/GenerateTest1/pom.xml").getPath()).getParent());
151 v1.setSystemProperties(props);
152 v1.executeGoal("clean");
153 v1.executeGoal("package");
154 v1.assertFilePresent("target/classes/META-INF/yang/testfile1.yang");
155 v1.assertFilePresent("target/classes/META-INF/yang/testfile2.yang");
156 v1.assertFilePresent("target/classes/META-INF/yang/testfile3.yang");
158 Verifier v2 = YangToSourcesPluginTestIT.setUp("GenerateTest2/", false);
159 v2.executeGoal("clean");
160 v2.executeGoal("package");
161 v2.assertFilePresent("target/classes/META-INF/yang/private.yang");
162 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile1.yang");
163 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile2.yang");
164 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile3.yang");