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.InputStream;
17 import java.net.URISyntaxException;
19 import java.util.List;
20 import java.util.Properties;
21 import org.apache.maven.it.VerificationException;
22 import org.apache.maven.it.Verifier;
23 import org.junit.Test;
25 public class YangToSourcesPluginTestIT {
27 // TODO Test yang files in transitive dependencies
30 public void testYangRootNotExist() throws URISyntaxException {
32 setUp("test-parent/YangRootNotExist/", false);
33 } catch (VerificationException e) {
34 assertVerificationException(e,
35 "[ERROR] yang-to-sources: Unable to parse yang files from ");
36 assertVerificationException(
38 "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
42 fail("Verification exception should have been thrown");
46 public void testCorrect() throws Exception {
47 Verifier v = setUp("test-parent/Correct/", false);
52 public void testAdditionalConfiguration() throws Exception {
53 Verifier v = setUp("test-parent/AdditionalConfig/", false);
54 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}");
55 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
56 v.verifyTextInLog(File.separator
57 + "files marked as resources: META-INF/yang");
58 v.verifyTextInLog("target"
60 + "generated-resources marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
64 public void testMissingYangInDep() throws Exception {
66 setUp("test-parent/MissingYangInDep/", false);
67 } catch (VerificationException e) {
68 assertVerificationException(
70 "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
74 fail("Verification exception should have been thrown");
78 public void testNamingConflict() throws Exception {
79 Verifier v = setUp("test-parent/NamingConflict/", false);
80 v.verifyErrorFreeLog();
81 String baseDir = v.getBasedir();
82 String fileName = v.getLogFileName();
83 List<String> lines = v.loadFile(baseDir, fileName, false);
84 for (String s : lines) {
85 if (s.contains("conflict")) {
86 System.err.println(s);
89 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.");
92 static void verifyCorrectLog(Verifier v) throws VerificationException {
93 v.verifyErrorFreeLog();
94 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
95 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
96 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
100 public void testNoGenerators() throws Exception {
101 Verifier v = setUp("test-parent/NoGenerators/", false);
102 v.verifyErrorFreeLog();
103 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
107 public void testInvalidVersion() throws Exception {
108 Verifier v = setUp("test-parent/InvalidVersion/", false);
109 v.verifyErrorFreeLog();
110 v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
114 public void testUnknownGenerator() throws Exception {
115 Verifier v = setUp("test-parent/UnknownGenerator/", true);
116 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
117 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
118 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
119 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
120 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
124 public void testNoYangFiles() throws Exception {
125 Verifier v = setUp("test-parent/NoYangFiles/", false);
126 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
129 static void assertVerificationException(VerificationException e,
131 assertThat(e.getMessage(), containsString(string));
134 static Verifier setUp(String project, boolean ignoreF)
135 throws VerificationException, URISyntaxException {
136 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
137 + project + "pom.xml");
138 File parent = new File(path.toURI());
139 Verifier verifier = new Verifier(parent.getParent());
141 verifier.addCliOption("-fn");
142 verifier.setMavenDebug(true);
143 verifier.executeGoal("generate-sources");
148 public void testNoOutputDir() throws Exception {
149 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
154 public void testFindResourceOnCp() throws Exception {
155 Verifier v1 = new Verifier(new File(getClass().getResource(
156 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
157 v1.executeGoal("clean");
158 v1.executeGoal("package");
160 Properties sp = new Properties();
161 try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
164 String buildDir = sp.getProperty("target.dir");
166 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
167 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
168 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
170 Verifier v2 = new Verifier(new File(getClass().getResource(
171 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
172 v2.executeGoal("clean");
173 v2.executeGoal("package");
175 sp = new Properties();
176 try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
179 buildDir = sp.getProperty("target.dir");
181 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
182 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
183 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
184 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");