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;
14 import com.google.common.base.Joiner;
16 import java.io.FileInputStream;
17 import java.io.InputStream;
18 import java.net.URISyntaxException;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Properties;
23 import org.apache.maven.it.VerificationException;
24 import org.apache.maven.it.Verifier;
25 import org.junit.Test;
27 public class YangToSourcesPluginTestIT {
29 // TODO Test yang files in transitive dependencies
32 public void testYangRootNotExist() throws URISyntaxException {
34 setUp("test-parent/YangRootNotExist/", false);
35 } catch (VerificationException e) {
36 assertVerificationException(e,
37 "[ERROR] yang-to-sources: Unable to parse yang files from ");
38 assertVerificationException(
40 "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
44 fail("Verification exception should have been thrown");
48 public void testCorrect() throws Exception {
49 Verifier v = setUp("test-parent/Correct/", false);
54 public void testAdditionalConfiguration() throws Exception {
55 Verifier v = setUp("test-parent/AdditionalConfig/", false);
56 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}");
57 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
58 v.verifyTextInLog(File.separator
59 + "files marked as resources: META-INF/yang");
61 Joiner.on(File.separator).join(
62 Arrays.asList("target", "generated-sources", "spi"))
64 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
68 public void testMissingYangInDep() throws Exception {
70 setUp("test-parent/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");
82 public void testNamingConflict() throws Exception {
83 Verifier v = setUp("test-parent/NamingConflict/", false);
84 v.verifyErrorFreeLog();
85 String baseDir = v.getBasedir();
86 String fileName = v.getLogFileName();
87 List<String> lines = v.loadFile(baseDir, fileName, false);
88 for (String s : lines) {
89 if (s.contains("conflict")) {
90 System.err.println(s);
93 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.");
96 static void verifyCorrectLog(Verifier v) throws VerificationException {
97 v.verifyErrorFreeLog();
98 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
99 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
100 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
104 public void testNoGenerators() throws Exception {
105 Verifier v = setUp("test-parent/NoGenerators/", false);
106 v.verifyErrorFreeLog();
107 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
111 public void testInvalidVersion() throws Exception {
112 Verifier v = setUp("test-parent/InvalidVersion/", false);
113 v.verifyErrorFreeLog();
114 v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
118 public void testUnknownGenerator() throws Exception {
119 Verifier v = setUp("test-parent/UnknownGenerator/", true);
120 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
121 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
122 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
123 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
124 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
128 public void testNoYangFiles() throws Exception {
129 Verifier v = setUp("test-parent/NoYangFiles/", false);
130 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
133 static void assertVerificationException(VerificationException e,
135 assertThat(e.getMessage(), containsString(string));
138 static Verifier setUp(String project, boolean ignoreF)
139 throws VerificationException, URISyntaxException {
140 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
141 + project + "pom.xml");
142 File parent = new File(path.toURI());
143 Verifier verifier = new Verifier(parent.getParent());
145 verifier.addCliOption("-fn");
146 verifier.setMavenDebug(true);
147 verifier.executeGoal("generate-sources");
152 public void testNoOutputDir() throws Exception {
153 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
158 public void testFindResourceOnCp() throws Exception {
159 Verifier v1 = new Verifier(new File(getClass().getResource(
160 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
161 v1.executeGoal("clean");
162 v1.executeGoal("package");
164 Properties sp = new Properties();
165 try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
168 String buildDir = sp.getProperty("target.dir");
170 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
171 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
172 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
174 Verifier v2 = new Verifier(new File(getClass().getResource(
175 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
176 v2.executeGoal("clean");
177 v2.executeGoal("package");
179 sp = new Properties();
180 try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
183 buildDir = sp.getProperty("target.dir");
185 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
186 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
187 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
188 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");