Merge "Limit the references IdentityCodec retains"
[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
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
20 import java.net.URL;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Properties;
24
25 import org.apache.maven.it.VerificationException;
26 import org.apache.maven.it.Verifier;
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.util.YangValidationException: Not existing module imported:unknownDep:2013-02-27 by:private:2013-02-27");
67             return;
68         }
69
70         fail("Verification exception should have been thrown");
71     }
72
73     @Test
74     public void testNamingConflict() throws Exception {
75         Verifier v = setUp("test-parent/NamingConflict/", false);
76         v.verifyErrorFreeLog();
77         String baseDir = v.getBasedir();
78         String fileName = v.getLogFileName();
79         List<String> lines = v.loadFile(baseDir, fileName, false);
80         for (String s : lines) {
81             if (s.contains("conflict")) {
82                 System.err.println(s);
83             }
84         }
85         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.");
86     }
87
88     static void verifyCorrectLog(final Verifier v) throws VerificationException {
89         v.verifyErrorFreeLog();
90         v.verifyTextInLog("[INFO] yang-to-sources: YANG files parsed from");
91         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
92         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
93     }
94
95     @Test
96     public void testNoGenerators() throws Exception {
97         Verifier v = setUp("test-parent/NoGenerators/", false);
98         v.verifyErrorFreeLog();
99         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
100     }
101
102     @Test
103     public void testInvalidVersion() throws Exception {
104         Verifier v = setUp("test-parent/InvalidVersion/", false);
105         v.verifyErrorFreeLog();
106         v.verifyTextInLog("[WARNING] yang-to-sources: Dependency resolution conflict:");
107     }
108
109     @Test
110     public void testUnknownGenerator() throws Exception {
111         Verifier v = setUp("test-parent/UnknownGenerator/", true);
112         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
113         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
114         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
115         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
116         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
117     }
118
119     @Test
120     public void testNoYangFiles() throws Exception {
121         Verifier v = setUp("test-parent/NoYangFiles/", false);
122         v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
123     }
124
125     static void assertVerificationException(final VerificationException e,
126             final String string) {
127         assertThat(e.getMessage(), containsString(string));
128     }
129
130     static Verifier setUp(final String project, final boolean ignoreF)
131             throws VerificationException, URISyntaxException {
132         final URL path = YangToSourcesPluginTestIT.class.getResource("/"
133                 + project + "pom.xml");
134         File parent = new File(path.toURI());
135         Verifier verifier = new Verifier(parent.getParent());
136         if (ignoreF) {
137             verifier.addCliOption("-fn");
138         }
139         verifier.setMavenDebug(true);
140         verifier.executeGoal("generate-sources");
141         return verifier;
142     }
143
144     @Test
145     public void testNoOutputDir() throws Exception {
146         Verifier v = YangToSourcesPluginTestIT.setUp("test-parent/NoOutputDir/", false);
147         verifyCorrectLog(v);
148     }
149
150     @Test
151     public void testFindResourceOnCp() throws Exception {
152         Verifier v1 = new Verifier(new File(getClass().getResource(
153                 "/test-parent/GenerateTest1/pom.xml").toURI()).getParent());
154         v1.executeGoal("clean");
155         v1.executeGoal("package");
156
157         Properties sp = new Properties();
158         try (InputStream is = new FileInputStream(v1.getBasedir() + "/it-project.properties")) {
159             sp.load(is);
160         }
161         String buildDir = sp.getProperty("target.dir");
162
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");
166
167         Verifier v2 = new Verifier(new File(getClass().getResource(
168                 "/test-parent/GenerateTest2/pom.xml").toURI()).getParent());
169         v2.executeGoal("clean");
170         v2.executeGoal("package");
171
172         sp = new Properties();
173         try (InputStream is = new FileInputStream(v2.getBasedir() + "/it-project.properties")) {
174             sp.load(is);
175         }
176         buildDir = sp.getProperty("target.dir");
177
178         v2.assertFilePresent(buildDir + "/classes/META-INF/yang/private.yang");
179         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile1.yang");
180         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile2.yang");
181         v2.assertFileNotPresent(buildDir + "/classes/META-INF/yang/testfile3.yang");
182     }
183
184 }