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 Exception {
35 Verifier v = setUp("test-parent/YangRootNotExist/", false);
36 v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
40 public void testCorrect() throws Exception {
41 Verifier v = setUp("test-parent/Correct/", false);
46 public void testAdditionalConfiguration() throws Exception {
47 Verifier v = setUp("test-parent/AdditionalConfig/", false);
48 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}");
49 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
50 v.verifyTextInLog(File.separator
51 + "files marked as resources: META-INF/yang");
53 Joiner.on(File.separator).join(
54 Arrays.asList("target", "generated-sources", "spi"))
56 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
60 public void testMissingYangInDep() throws Exception {
62 setUp("test-parent/MissingYangInDep/", false);
63 } catch (VerificationException e) {
64 assertVerificationException(
66 "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
70 fail("Verification exception should have been thrown");
74 public void testNamingConflict() throws Exception {
75 Verifier v = setUp("test-parent/NamingConflict/", false);
76 v.verifyErrorFreeLog();
77 String baseDir = v.getBasedir();
78 String fileName = v.getLogFileName();
79 List<String> lines = v.loadFile(baseDir, fileName, false);
80 for (String s : lines) {
81 if (s.contains("conflict")) {
82 System.err.println(s);
85 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.");
88 static void verifyCorrectLog(final Verifier v) throws VerificationException {
89 v.verifyErrorFreeLog();
90 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
91 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
92 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
96 public void testNoGenerators() throws Exception {
97 Verifier v = setUp("test-parent/NoGenerators/", false);
98 v.verifyErrorFreeLog();
99 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
103 public void testInvalidVersion() throws Exception {
104 Verifier v = setUp("test-parent/InvalidVersion/", false);
105 v.verifyErrorFreeLog();
106 v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
110 public void testUnknownGenerator() throws Exception {
111 Verifier v = setUp("test-parent/UnknownGenerator/", true);
112 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
113 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
114 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
115 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
116 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
120 public void testNoYangFiles() throws Exception {
121 Verifier v = setUp("test-parent/NoYangFiles/", false);
122 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
125 static void assertVerificationException(final VerificationException e,
126 final String string) {
127 assertThat(e.getMessage(), containsString(string));
130 static Verifier setUp(final String project, final boolean ignoreF)
131 throws VerificationException, URISyntaxException {
132 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
133 + project + "pom.xml");
134 File parent = new File(path.toURI());
135 Verifier verifier = new Verifier(parent.getParent());
137 verifier.addCliOption("-fn");
139 verifier.setMavenDebug(true);
140 verifier.executeGoal("generate-sources");
145 public void testNoOutputDir() throws Exception {
146 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
151 public void testFindResourceOnCp() throws Exception {
152 Verifier v1 = new Verifier(new File(getClass().getResource(
153 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
154 v1.executeGoal("clean");
155 v1.executeGoal("package");
157 Properties sp = new Properties();
158 try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
161 String buildDir = sp.getProperty("target.dir");
163 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
164 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
165 v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
167 Verifier v2 = new Verifier(new File(getClass().getResource(
168 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
169 v2.executeGoal("clean");
170 v2.executeGoal("package");
172 sp = new Properties();
173 try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
176 buildDir = sp.getProperty("target.dir");
178 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
179 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
180 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
181 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");