Merge changes I9886ddb7,I851fa16e,I3b7b0bf5,Ib7f2ce87,I7d0cf295,I35ae6fcb,I14c86257...
[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 java.io.File;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.List;
20 import java.util.Properties;
21 import org.apache.maven.it.VerificationException;
22 import org.apache.maven.it.Verifier;
23 import org.junit.Test;
24
25 public class YangToSourcesPluginTestIT {
26
27     // TODO Test yang files in transitive dependencies
28
29     @Test
30     public void testYangRootNotExist() throws URISyntaxException {
31         try {
32             setUp("test-parent/YangRootNotExist/", false);
33         } catch (VerificationException e) {
34             assertVerificationException(e,
35                     "[ERROR] yang-to-sources: Unable to parse yang files from ");
36             assertVerificationException(
37                     e,
38                     "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
39             return;
40         }
41
42         fail("Verification exception should have been thrown");
43     }
44
45     @Test
46     public void testCorrect() throws Exception {
47         Verifier v = setUp("test-parent/Correct/", false);
48         verifyCorrectLog(v);
49     }
50
51     @Test
52     public void testAdditionalConfiguration() throws Exception {
53         Verifier v = setUp("test-parent/AdditionalConfig/", false);
54         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}");
55         v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
56         v.verifyTextInLog(File.separator
57                 + "files marked as resources: META-INF/yang");
58         v.verifyTextInLog("target"
59                 + File.separator
60                 + "generated-resources marked as resources for generator: org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
61     }
62
63     @Test
64     public void testMissingYangInDep() throws Exception {
65         try {
66             setUp("test-parent/MissingYangInDep/", false);
67         } catch (VerificationException e) {
68             assertVerificationException(
69                     e,
70                     "org.opendaylight.yangtools.yang.parser.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
71             return;
72         }
73
74         fail("Verification exception should have been thrown");
75     }
76
77     @Test
78     public void testNamingConflict() throws Exception {
79         Verifier v = setUp("test-parent/NamingConflict/", false);
80         v.verifyErrorFreeLog();
81         String baseDir = v.getBasedir();
82         String fileName = v.getLogFileName();
83         List<String> lines = v.loadFile(baseDir, fileName, false);
84         for (String s : lines) {
85             if (s.contains("conflict")) {
86                 System.err.println(s);
87             }
88         }
89         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.");
90     }
91
92     static void verifyCorrectLog(Verifier v) throws VerificationException {
93         v.verifyErrorFreeLog();
94         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
95         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
96         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
97     }
98
99     @Test
100     public void testNoGenerators() throws Exception {
101         Verifier v = setUp("test-parent/NoGenerators/", false);
102         v.verifyErrorFreeLog();
103         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
104     }
105
106     @Test
107     public void testInvalidVersion() throws Exception {
108         Verifier v = setUp("test-parent/InvalidVersion/", false);
109         v.verifyErrorFreeLog();
110         v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
111     }
112
113     @Test
114     public void testUnknownGenerator() throws Exception {
115         Verifier v = setUp("test-parent/UnknownGenerator/", true);
116         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
117         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
118         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
119         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
120         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
121     }
122
123     @Test
124     public void testNoYangFiles() throws Exception {
125         Verifier v = setUp("test-parent/NoYangFiles/", false);
126         v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
127     }
128
129     static void assertVerificationException(VerificationException e,
130             String string) {
131         assertThat(e.getMessage(), containsString(string));
132     }
133
134     static Verifier setUp(String project, boolean ignoreF)
135             throws VerificationException, URISyntaxException {
136         final URL path = YangToSourcesPluginTestIT.class.getResource("/"
137                 + project + "pom.xml");
138         File parent = new File(path.toURI());
139         Verifier verifier = new Verifier(parent.getParent());
140         if (ignoreF)
141             verifier.addCliOption("-fn");
142         verifier.setMavenDebug(true);
143         verifier.executeGoal("generate-sources");
144         return verifier;
145     }
146
147     @Test
148     public void testNoOutputDir() throws Exception {
149         Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
150         verifyCorrectLog(v);
151     }
152
153     @Test
154     public void testFindResourceOnCp() throws Exception {
155         Verifier v1 = new Verifier(new File(getClass().getResource(
156                 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
157         v1.executeGoal("clean");
158         v1.executeGoal("package");
159
160         Properties sp = new Properties();
161         try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
162              sp.load(is);
163         }
164         String buildDir = sp.getProperty("target.dir");
165
166         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
167         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
168         v1.assertFilePresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
169
170         Verifier v2 = new Verifier(new File(getClass().getResource(
171                 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
172         v2.executeGoal("clean");
173         v2.executeGoal("package");
174
175         sp = new Properties();
176         try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
177              sp.load(is);
178         }
179         buildDir = sp.getProperty("target.dir");
180
181         v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
182         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
183         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
184         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
185     }
186
187 }