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;
13 import com.google.common.base.Joiner;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17 import java.net.URISyntaxException;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Properties;
22 import org.apache.maven.it.VerificationException;
23 import org.apache.maven.it.Verifier;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
27 public class YangToSourcesPluginTestIT {
28 private static String GLOBAL_SETTINGS_OVERRIDE;
29 private static String USER_SETTINGS_OVERRIDE;
32 public static void setupClass() {
33 // OpenDaylight Jenkins does not have settings at the default path, pick file locations from environment
34 GLOBAL_SETTINGS_OVERRIDE = System.getenv("GLOBAL_SETTINGS_FILE");
35 USER_SETTINGS_OVERRIDE = System.getenv("SETTINGS_FILE");
38 // TODO Test yang files in transitive dependencies
41 public void testYangRootNotExist() throws Exception {
42 Verifier v = setUp("test-parent/YangRootNotExist/", false);
43 v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
47 public void testCorrect() throws Exception {
48 Verifier v = setUp("test-parent/Correct/", false);
53 public void testAdditionalConfiguration() throws Exception {
54 Verifier v = setUp("test-parent/AdditionalConfig/", false);
55 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}");
56 v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
57 v.verifyTextInLog(File.separator
58 + "files marked as resources: META-INF/yang");
60 Joiner.on(File.separator).join(
61 Arrays.asList("target", "generated-sources", "spi"))
63 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
67 public void testMissingYangInDep() throws Exception {
69 setUp("test-parent/MissingYangInDep/", false);
70 } catch (VerificationException e) {
71 assertVerificationException(
73 "org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException: Imported module " +
74 "[ModuleIdentifierImpl{name='unknownDep', namespace=null, revision=2013-02-27}");
78 fail("Verification exception should have been thrown");
81 static void verifyCorrectLog(final Verifier v) throws VerificationException {
82 v.verifyErrorFreeLog();
83 v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
84 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
85 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
89 public void testNoGenerators() throws Exception {
90 Verifier v = setUp("test-parent/NoGenerators/", false);
91 v.verifyErrorFreeLog();
92 v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
96 public void testInvalidVersion() throws Exception {
97 Verifier v = setUp("test-parent/InvalidVersion/", false);
98 v.verifyErrorFreeLog();
99 v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
103 public void testUnknownGenerator() throws Exception {
104 Verifier v = setUp("test-parent/UnknownGenerator/", true);
105 v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
106 v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
107 v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
108 v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
109 v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
113 public void testNoYangFiles() throws Exception {
114 Verifier v = setUp("test-parent/NoYangFiles/", false);
115 v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
118 static void assertVerificationException(final VerificationException e,
119 final String string) {
120 assertThat(e.getMessage(), containsString(string));
123 static Verifier setUp(final String project, final boolean ignoreF)
124 throws VerificationException, URISyntaxException {
125 final URL path = YangToSourcesPluginTestIT.class.getResource("/"
126 + project + "pom.xml");
127 File parent = new File(path.toURI());
128 Verifier verifier = new Verifier(parent.getParent());
130 verifier.addCliOption("-fn");
132 if (GLOBAL_SETTINGS_OVERRIDE != null) {
133 verifier.addCliOption("-gs");
134 verifier.addCliOption(GLOBAL_SETTINGS_OVERRIDE);
136 if (USER_SETTINGS_OVERRIDE != null) {
137 verifier.addCliOption("-s");
138 verifier.addCliOption(USER_SETTINGS_OVERRIDE);
140 verifier.setMavenDebug(true);
141 verifier.executeGoal("generate-sources");
146 public void testNoOutputDir() throws Exception {
147 Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
152 public void testFindResourceOnCp() throws Exception {
153 Verifier v1 = setUp("test-parent/GenerateTest1/", false);
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 = setUp("test-parent/GenerateTest2/", false);
168 v2.executeGoal("clean");
169 v2.executeGoal("package");
171 sp = new Properties();
172 try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
175 buildDir = sp.getProperty("target.dir");
177 v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
178 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
179 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
180 v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");