45fe983d0aa180af63a9cf8b834f8267cea4498a
[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.junit.Assert.assertThat;
11 import static org.junit.Assert.fail;
12 import static org.junit.matchers.JUnitMatchers.containsString;
13
14 import com.google.common.base.Joiner;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.InputStream;
18 import java.net.URISyntaxException;
19 import java.net.URL;
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;
26
27 public class YangToSourcesPluginTestIT {
28
29     // TODO Test yang files in transitive dependencies
30
31     @Test
32     public void testYangRootNotExist() throws URISyntaxException {
33         try {
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(
39                     e,
40                     "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
41             return;
42         }
43
44         fail("Verification exception should have been thrown");
45     }
46
47     @Test
48     public void testCorrect() throws Exception {
49         Verifier v = setUp("test-parent/Correct/", false);
50         verifyCorrectLog(v);
51     }
52
53     @Test
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");
60         v.verifyTextInLog(
61                 Joiner.on(File.separator).join(
62                 Arrays.asList("target", "generated-sources", "spi"))
63
64                 + " marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
65     }
66
67     @Test
68     public void testMissingYangInDep() throws Exception {
69         try {
70             setUp("test-parent/MissingYangInDep/", false);
71         } catch (VerificationException e) {
72             assertVerificationException(
73                     e,
74                     "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
75             return;
76         }
77
78         fail("Verification exception should have been thrown");
79     }
80
81     @Test
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);
91             }
92         }
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.");
94     }
95
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");
101     }
102
103     @Test
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");
108     }
109
110     @Test
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:");
115     }
116
117     @Test
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}");
125     }
126
127     @Test
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");
131     }
132
133     static void assertVerificationException(VerificationException e,
134             String string) {
135         assertThat(e.getMessage(), containsString(string));
136     }
137
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());
144         if (ignoreF)
145             verifier.addCliOption("-fn");
146         verifier.setMavenDebug(true);
147         verifier.executeGoal("generate-sources");
148         return verifier;
149     }
150
151     @Test
152     public void testNoOutputDir() throws Exception {
153         Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
154         verifyCorrectLog(v);
155     }
156
157     @Test
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");
163
164         Properties sp = new Properties();
165         try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
166              sp.load(is);
167         }
168         String buildDir = sp.getProperty("target.dir");
169
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");
173
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");
178
179         sp = new Properties();
180         try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
181              sp.load(is);
182         }
183         buildDir = sp.getProperty("target.dir");
184
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");
189     }
190
191 }