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;
17 import java.io.FileInputStream;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Properties;
25 import org.apache.maven.it.VerificationException;
26 import org.apache.maven.it.Verifier;
27 import org.junit.Test;
29 public class YangToSourcesPluginTestIT {
31 // TODO Test yang files in transitive dependencies
34 public void testYangRootNotExist() throws URISyntaxException {
36 setUp("test-parent/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 Exception {
51 Verifier v = setUp("test-parent/Correct/", false);
56 public void testAdditionalConfiguration() throws Exception {
57 Verifier v = setUp("test-parent/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");
63 Joiner.on(File.separator).join(
64 Arrays.asList("target", "generated-sources", "spi"))
66 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
70 public void testMissingYangInDep() throws Exception {
72 setUp("test-parent/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("test-parent/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(final 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("test-parent/NoGenerators/", false);
108 v.verifyErrorFreeLog();
109 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
113 public void testInvalidVersion() throws Exception {
114 Verifier v = setUp("test-parent/InvalidVersion/", false);
115 v.verifyErrorFreeLog();
116 v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
120 public void testUnknownGenerator() throws Exception {
121 Verifier v = setUp("test-parent/UnknownGenerator/", true);
122 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
123 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
124 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
125 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
126 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
130 public void testNoYangFiles() throws Exception {
131 Verifier v = setUp("test-parent/NoYangFiles/", false);
132 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
135 static void assertVerificationException(final VerificationException e,
136 final String string) {
137 assertThat(e.getMessage(), containsString(string));
140 static Verifier setUp(final String project, final boolean ignoreF)
141 throws VerificationException, URISyntaxException {
142 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
143 + project + "pom.xml");
144 File parent = new File(path.toURI());
145 Verifier verifier = new Verifier(parent.getParent());
147 verifier.addCliOption("-fn");
149 verifier.setMavenDebug(true);
150 verifier.executeGoal("generate-sources");
155 public void testNoOutputDir() throws Exception {
156 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
161 public void testFindResourceOnCp() throws Exception {
162 Verifier v1 = new Verifier(new File(getClass().getResource(
163 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
164 v1.executeGoal("clean");
165 v1.executeGoal("package");
167 Properties sp = new Properties();
168 try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
171 String buildDir = sp.getProperty("target.dir");
173 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
174 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
175 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
177 Verifier v2 = new Verifier(new File(getClass().getResource(
178 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
179 v2.executeGoal("clean");
180 v2.executeGoal("package");
182 sp = new Properties();
183 try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
186 buildDir = sp.getProperty("target.dir");
188 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
189 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
190 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
191 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");