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