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;
18 import java.net.URISyntaxException;
20 import java.util.Properties;
22 import org.apache.maven.it.VerificationException;
23 import org.apache.maven.it.Verifier;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
27 public class YangToSourcesPluginTestIT {
28 private static final String SRC_PROPERTIES = "target/it-project.properties";
29 private static final String VERSION_PROP = "it-project.version";
30 private static Properties props;
32 // TODO Test yang files in transitive dependencies
35 public void testYangRootNotExist() throws URISyntaxException {
37 setUp("YangRootNotExist/", false);
38 } catch (VerificationException e) {
39 assertVerificationException(e,
40 "[ERROR] yang-to-sources: Unable to parse yang files from ");
41 assertVerificationException(
43 "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
47 fail("Verification exception should have been thrown");
51 public void testCorrect() throws Exception {
52 Verifier v = setUp("Correct/", false);
57 public void testAdditionalConfiguration() throws Exception {
58 Verifier v = setUp("AdditionalConfig/", false);
59 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}");
60 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
61 v.verifyTextInLog(File.separator
62 + "files marked as resources: META-INF/yang");
63 v.verifyTextInLog("target"
65 + "generated-resources marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
69 public void testMissingYangInDep() throws Exception {
71 setUp("MissingYangInDep/", false);
72 } catch (VerificationException e) {
73 assertVerificationException(
75 "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
79 fail("Verification exception should have been thrown");
82 static void verifyCorrectLog(Verifier v) throws VerificationException {
83 v.verifyErrorFreeLog();
84 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
85 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
86 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
90 public void testNoGenerators() throws Exception {
91 Verifier v = setUp("NoGenerators/", false);
92 v.verifyErrorFreeLog();
93 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
97 public void testUnknownGenerator() throws Exception {
98 Verifier v = setUp("UnknownGenerator/", true);
99 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
100 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
101 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
102 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
103 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
107 public void testNoYangFiles() throws Exception {
108 Verifier v = setUp("NoYangFiles/", false);
109 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
112 static void assertVerificationException(VerificationException e,
114 assertThat(e.getMessage(), containsString(string));
118 public static void generateProps() throws IOException {
119 final Properties sp = new Properties();
120 try (InputStream is = new FileInputStream(new File(SRC_PROPERTIES))) {
124 props = new Properties(System.getProperties());
125 props.put(VERSION_PROP, sp.getProperty(VERSION_PROP));
128 static Verifier setUp(String project, boolean ignoreF)
129 throws VerificationException, URISyntaxException {
130 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
131 + project + "pom.xml");
132 File parent = new File(path.toURI());
133 Verifier verifier = new Verifier(parent.getParent());
135 verifier.addCliOption("-fn");
136 verifier.setMavenDebug(true);
137 verifier.setSystemProperties(props);
138 verifier.executeGoal("generate-sources");
143 public void testNoOutputDir() throws Exception {
144 Verifier v = YangToSourcesPluginTestIT.setUp("NoOutputDir/", false);
149 public void testFindResourceOnCp() throws Exception {
150 Verifier v1 = new Verifier(new File(getClass().getResource(
151 "/GenerateTest1/pom.xml").toURI()).getParent());
152 v1.setSystemProperties(props);
153 v1.executeGoal("clean");
154 v1.executeGoal("package");
155 v1.assertFilePresent("target/classes/META-INF/yang/testfile1.yang");
156 v1.assertFilePresent("target/classes/META-INF/yang/testfile2.yang");
157 v1.assertFilePresent("target/classes/META-INF/yang/testfile3.yang");
159 Verifier v2 = YangToSourcesPluginTestIT.setUp("GenerateTest2/", false);
160 v2.executeGoal("clean");
161 v2.executeGoal("package");
162 v2.assertFilePresent("target/classes/META-INF/yang/private.yang");
163 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile1.yang");
164 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile2.yang");
165 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile3.yang");