Use plugin-generator-api in yang-maven-plugin
[yangtools.git] / plugin / 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.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.fail;
13
14 import com.google.common.base.Joiner;
15 import com.google.common.io.Resources;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
20 import java.net.URL;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.Arrays;
24 import java.util.Optional;
25 import java.util.Properties;
26 import org.apache.maven.it.VerificationException;
27 import org.apache.maven.it.Verifier;
28 import org.junit.Test;
29
30 public class YangToSourcesPluginTestIT {
31
32     // TODO Test yang files in transitive dependencies
33
34     @Test
35     public void testYangRootNotExist() throws Exception {
36         setUp("test-parent/YangRootNotExist/", false)
37             .verifyTextInLog("[WARNING] yang-to-sources: YANG source directory");
38     }
39
40     @Test
41     public void testCorrect() throws VerificationException, URISyntaxException, IOException {
42         verifyCorrectLog(setUp("test-parent/Correct/", false));
43     }
44
45     @Test
46     public void testAdditionalConfiguration() throws Exception {
47         Verifier vrf = setUp("test-parent/AdditionalConfig/", false);
48         vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
49                 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: "
50                 + "{nm1=abcd=a.b.c.d, nm2=abcd2=a.b.c.d.2}");
51         vrf.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : "
52                 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
53         vrf.verifyTextInLog("[DEBUG] yang-to-sources: YANG files marked as resources:");
54         vrf.verifyTextInLog(Joiner.on(File.separator).join(Arrays.asList("target", "generated-sources", "spi"))
55                 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi."
56                 + "CodeGeneratorTestImpl");
57     }
58
59     @Test
60     public void testMissingYangInDep() throws Exception {
61         try {
62             setUp("test-parent/MissingYangInDep/", false);
63             fail("Verification exception should have been thrown");
64         } catch (VerificationException e) {
65             assertVerificationException(e, "Imported module [unknownDep] was not found.");
66         }
67     }
68
69     static void verifyCorrectLog(final Verifier vrf) throws VerificationException {
70         vrf.verifyErrorFreeLog();
71         vrf.verifyTextInLog("[INFO] yang-to-sources: Project model files found: ");
72         vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from "
73                 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
74         vrf.verifyTextInLog("[INFO] yang-to-sources: Sources generated by "
75                 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: 0");
76     }
77
78     @Test
79     public void testNoGenerators() throws Exception {
80         Verifier vrf = setUp("test-parent/NoGenerators/", false);
81         vrf.verifyErrorFreeLog();
82         vrf.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
83     }
84
85     @Test
86     public void testInvalidVersion() throws Exception {
87         Verifier vrf = setUp("test-parent/InvalidVersion/", false);
88         vrf.verifyErrorFreeLog();
89         vrf.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
90     }
91
92     @Test
93     public void testUnknownGenerator() throws Exception {
94         Verifier vrf = setUp("test-parent/UnknownGenerator/", true);
95         vrf.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from "
96                 + "org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
97         vrf.verifyTextInLog("on project unknown-generator: Failed to find code generator class unknown");
98         vrf.verifyTextInLog("MojoFailureException: Failed to find code generator class unknown");
99         vrf.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
100     }
101
102     @Test
103     public void testNoYangFiles() throws Exception {
104         setUp("test-parent/NoYangFiles/", false).verifyTextInLog("[INFO] yang-to-sources: No input files found");
105     }
106
107     static void assertVerificationException(final VerificationException ex, final String string) {
108         assertThat(ex.getMessage(), containsString(string));
109     }
110
111     static Verifier setUp(final String project, final boolean ignoreF)
112             throws VerificationException, URISyntaxException, IOException {
113         final URL path = YangToSourcesPluginTestIT.class.getResource("/" + project + "pom.xml");
114         final Verifier verifier = new Verifier(new File(path.toURI()).getParent());
115         if (ignoreF) {
116             verifier.addCliOption("-fn");
117         }
118
119         final Optional<String> maybeSettings = getEffectiveSettingsXML();
120         if (maybeSettings.isPresent()) {
121             verifier.addCliOption("-gs");
122             verifier.addCliOption(maybeSettings.get());
123         }
124         verifier.setMavenDebug(true);
125         verifier.executeGoal("generate-sources");
126         return verifier;
127     }
128
129     @Test
130     public void testNoOutputDir() throws Exception {
131         verifyCorrectLog(YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false));
132     }
133
134     @Test
135     public void testFindResourceOnCp() throws Exception {
136         Verifier v1 = setUp("test-parent/GenerateTest1/", false);
137         v1.executeGoal("clean");
138         v1.executeGoal("package");
139
140         String buildDir = getMavenBuildDirectory(v1);
141         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
142         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
143         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
144
145         Verifier v2 = setUp("test-parent/GenerateTest2/", false);
146         v2.executeGoal("clean");
147         v2.executeGoal("package");
148
149         buildDir = getMavenBuildDirectory(v2);
150         v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private@2013-02-27.yang");
151         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types1@2013-02-27.yang");
152         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types2@2013-02-27.yang");
153         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/types3@2013-02-27.yang");
154     }
155
156     @Test
157     public void testFileGenerator() throws Exception {
158         Verifier v1 = setUp("test-parent/FileGenerator/", false);
159         v1.executeGoal("clean");
160         v1.executeGoal("package");
161
162         String buildDir = getMavenBuildDirectory(v1);
163
164         v1.assertFilePresent(buildDir + "/generated-sources/"
165             + "org.opendaylight.yangtools.yang2sources.spi.TestFileGenerator/fooGenSource.test");
166         v1.assertFilePresent(buildDir + "/generated-resources/"
167             + "org.opendaylight.yangtools.yang2sources.spi.TestFileGenerator/foo-gen-resource");
168         v1.assertFilePresent(buildDir + "/../src/main/java/fooSource.test");
169         v1.assertFilePresent(buildDir + "/../src/main/resources/foo-resource");
170     }
171
172     private static String getMavenBuildDirectory(final Verifier verifier) throws IOException {
173         final Properties sp = new Properties();
174         final Path path = new File(verifier.getBasedir() + "/it-project.properties").toPath();
175         try (InputStream is = Files.newInputStream(path)) {
176             sp.load(is);
177         }
178         return sp.getProperty("target.dir");
179     }
180
181     private static Optional<String> getEffectiveSettingsXML() throws URISyntaxException, VerificationException,
182             IOException {
183         final URL path = Resources.getResource(YangToSourcesPluginTestIT.class, "/test-parent/pom.xml");
184         final File buildDir = new File(path.toURI()).getParentFile().getParentFile().getParentFile();
185         final File effectiveSettingsXML = new File(buildDir, "effective-settings.xml");
186         if (effectiveSettingsXML.exists()) {
187             return Optional.of(effectiveSettingsXML.getAbsolutePath());
188         }
189
190         fail(effectiveSettingsXML.getAbsolutePath());
191         return Optional.empty();
192     }
193 }