Remove accidental tabs
[yangtools.git] / yang / yang-maven-plugin-it / src / test / java / org / opendaylight / yangtools / yang2sources / plugin / it / YangToSourcesPluginTestIT.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin.it;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
13
14 import com.google.common.base.Joiner;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
20 import java.net.URL;
21 import java.util.Arrays;
22 import java.util.Optional;
23 import java.util.Properties;
24 import org.apache.maven.it.VerificationException;
25 import org.apache.maven.it.Verifier;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28
29 public class YangToSourcesPluginTestIT {
30
31     // TODO Test yang files in transitive dependencies
32
33     @Test
34     public void testYangRootNotExist() throws Exception {
35         Verifier v = setUp("test-parent/YangRootNotExist/", false);
36         v.verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
37     }
38
39     @Test
40     public void testCorrect() throws Exception {
41         Verifier v = setUp("test-parent/Correct/", false);
42         verifyCorrectLog(v);
43     }
44
45     @Test
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");
52         v.verifyTextInLog(
53                 Joiner.on(File.separator).join(
54                         Arrays.asList("target", "generated-sources", "spi"))
55
56                         + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
57     }
58
59     @Test
60     public void testMissingYangInDep() throws Exception {
61         try {
62             setUp("test-parent/MissingYangInDep/", false);
63         } catch (VerificationException e) {
64             assertVerificationException(
65                     e,
66                     "org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException: Imported module " +
67                     "[unknownDep] was not found.");
68             return;
69         }
70
71         fail("Verification exception should have been thrown");
72     }
73
74     static void verifyCorrectLog(final Verifier v) throws VerificationException {
75         v.verifyErrorFreeLog();
76         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
77         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
78         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
79     }
80
81     @Test
82     public void testNoGenerators() throws Exception {
83         Verifier v = setUp("test-parent/NoGenerators/", false);
84         v.verifyErrorFreeLog();
85         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
86     }
87
88     @Test
89     public void testInvalidVersion() throws Exception {
90         Verifier v = setUp("test-parent/InvalidVersion/", false);
91         v.verifyErrorFreeLog();
92         v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
93     }
94
95     @Test
96     public void testUnknownGenerator() throws Exception {
97         Verifier v = setUp("test-parent/UnknownGenerator/", true);
98         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
99         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
100         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
101         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
102         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
103     }
104
105     @Test
106     public void testNoYangFiles() throws Exception {
107         Verifier v = setUp("test-parent/NoYangFiles/", false);
108         v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
109     }
110
111     static void assertVerificationException(final VerificationException e,
112             final String string) {
113         assertThat(e.getMessage(), containsString(string));
114     }
115
116     static Verifier setUp(final String project, final boolean ignoreF)
117             throws VerificationException, URISyntaxException, IOException {
118         final URL path = YangToSourcesPluginTestIT.class.getResource("/"
119                 + project + "pom.xml");
120         File parent = new File(path.toURI());
121         Verifier verifier = new Verifier(parent.getParent());
122         if (ignoreF) {
123             verifier.addCliOption("-fn");
124         }
125
126         final Optional<String> maybeSettings = getEffectiveSettingsXML();
127         if (maybeSettings.isPresent()) {
128             verifier.addCliOption("-gs");
129             verifier.addCliOption(maybeSettings.get());
130         }
131         verifier.setMavenDebug(true);
132         verifier.executeGoal("generate-sources");
133         return verifier;
134     }
135
136     @Test
137     public void testNoOutputDir() throws Exception {
138         Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
139         verifyCorrectLog(v);
140     }
141
142     @Test
143     public void testFindResourceOnCp() throws Exception {
144         Verifier v1 = setUp("test-parent/GenerateTest1/", false);
145         v1.executeGoal("clean");
146         v1.executeGoal("package");
147
148         String buildDir = getMavenBuildDirectory(v1);
149         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
150         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
151         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
152
153         Verifier v2 = setUp("test-parent/GenerateTest2/", false);
154         v2.executeGoal("clean");
155         v2.executeGoal("package");
156
157         buildDir = getMavenBuildDirectory(v2);
158         v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
159         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
160         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
161         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
162     }
163
164     private static String getMavenBuildDirectory(Verifier verifier) throws IOException {
165         Properties sp = new Properties();
166         try (InputStream is = new FileInputStream(verifier.getBasedir() + "/it-project.properties")) {
167             sp.load(is);
168         }
169         return sp.getProperty("target.dir");
170     }
171
172     private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException, IOException {
173         final URL path = YangToSourcesPluginTestIT.class.getResource("/test-parent/pom.xml");
174         File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile();
175         File effectiveSettingsXML = new File(buildDir, "effective-settings.xml");
176         if (effectiveSettingsXML.exists()) {
177             return Optional.of(effectiveSettingsXML.getAbsolutePath());
178         } else {
179             fail(effectiveSettingsXML.getAbsolutePath());
180             return Optional.empty();
181         }
182     }
183
184 }