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.List;
21 import java.util.Properties;
23 import org.apache.maven.it.VerificationException;
24 import org.apache.maven.it.Verifier;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
28 public class YangToSourcesPluginTestIT {
29 private static final String SRC_PROPERTIES = "target/it-project.properties";
30 private static final String VERSION_PROP = "it-project.version";
31 private static Properties props;
33 // TODO Test yang files in transitive dependencies
36 public void testYangRootNotExist() throws URISyntaxException {
38 setUp("YangRootNotExist/", false);
39 } catch (VerificationException e) {
40 assertVerificationException(e,
41 "[ERROR] yang-to-sources: Unable to parse yang files from ");
42 assertVerificationException(
44 "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
48 fail("Verification exception should have been thrown");
52 public void testCorrect() throws Exception {
53 Verifier v = setUp("Correct/", false);
58 public void testAdditionalConfiguration() throws Exception {
59 Verifier v = setUp("AdditionalConfig/", false);
60 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}");
61 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
62 v.verifyTextInLog(File.separator
63 + "files marked as resources: META-INF/yang");
64 v.verifyTextInLog("target"
66 + "generated-resources marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
70 public void testMissingYangInDep() throws Exception {
72 setUp("MissingYangInDep/", false);
73 } catch (VerificationException e) {
74 assertVerificationException(
76 "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
80 fail("Verification exception should have been thrown");
84 public void testNamingConflict() throws Exception {
85 Verifier v = setUp("NamingConflict/", false);
86 v.verifyErrorFreeLog();
87 String baseDir = v.getBasedir();
88 String fileName = v.getLogFileName();
89 List<String> lines = v.loadFile(baseDir, fileName, false);
90 for (String s : lines) {
91 if (s.contains("conflict")) {
92 System.err.println(s);
95 v.verifyTextInLog("[WARNING] Naming conflict for type 'org.opendaylight.yang.gen.v1.urn.yang.test.rev140303.NetworkTopologyRef': file with same name already exists and will not be generated.");
98 static void verifyCorrectLog(Verifier v) throws VerificationException {
99 v.verifyErrorFreeLog();
100 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
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");
106 public void testNoGenerators() throws Exception {
107 Verifier v = setUp("NoGenerators/", false);
108 v.verifyErrorFreeLog();
109 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
113 public void testUnknownGenerator() throws Exception {
114 Verifier v = setUp("UnknownGenerator/", true);
115 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
116 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
117 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
118 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
119 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
123 public void testNoYangFiles() throws Exception {
124 Verifier v = setUp("NoYangFiles/", false);
125 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
128 static void assertVerificationException(VerificationException e,
130 assertThat(e.getMessage(), containsString(string));
134 public static void generateProps() throws IOException {
135 final Properties sp = new Properties();
136 try (InputStream is = new FileInputStream(new File(SRC_PROPERTIES))) {
140 props = new Properties(System.getProperties());
141 props.put(VERSION_PROP, sp.getProperty(VERSION_PROP));
144 static Verifier setUp(String project, boolean ignoreF)
145 throws VerificationException, URISyntaxException {
146 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
147 + project + "pom.xml");
148 File parent = new File(path.toURI());
149 Verifier verifier = new Verifier(parent.getParent());
151 verifier.addCliOption("-fn");
152 verifier.setMavenDebug(true);
153 verifier.setSystemProperties(props);
154 verifier.executeGoal("generate-sources");
159 public void testNoOutputDir() throws Exception {
160 Verifier v = YangToSourcesPluginTestIT.setUp("NoOutputDir/", false);
165 public void testFindResourceOnCp() throws Exception {
166 Verifier v1 = new Verifier(new File(getClass().getResource(
167 "/GenerateTest1/pom.xml").toURI()).getParent());
168 v1.setSystemProperties(props);
169 v1.executeGoal("clean");
170 v1.executeGoal("package");
171 v1.assertFilePresent("target/classes/META-INF/yang/testfile1.yang");
172 v1.assertFilePresent("target/classes/META-INF/yang/testfile2.yang");
173 v1.assertFilePresent("target/classes/META-INF/yang/testfile3.yang");
175 Verifier v2 = YangToSourcesPluginTestIT.setUp("GenerateTest2/", false);
176 v2.executeGoal("clean");
177 v2.executeGoal("package");
178 v2.assertFilePresent("target/classes/META-INF/yang/private.yang");
179 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile1.yang");
180 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile2.yang");
181 v2.assertFileNotPresent("target/classes/META-INF/yang/testfile3.yang");