Merge "Added initial draft of Normalized Yang Data Tree model."
[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.IOException;
17 import java.io.InputStream;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.apache.maven.it.VerificationException;
24 import org.apache.maven.it.Verifier;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27
28 public class YangToSourcesPluginTestIT {
29     private static final String SRC_PROPERTIES = "target/it-project.properties";
30     private static final String VERSION_PROP = "it-project.version";
31     private static Properties props;
32
33     // TODO Test yang files in transitive dependencies
34
35     @Test
36     public void testYangRootNotExist() throws URISyntaxException {
37         try {
38             setUp("YangRootNotExist/", false);
39         } catch (VerificationException e) {
40             assertVerificationException(e,
41                     "[ERROR] yang-to-sources: Unable to parse yang files from ");
42             assertVerificationException(
43                     e,
44                     "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from ");
45             return;
46         }
47
48         fail("Verification exception should have been thrown");
49     }
50
51     @Test
52     public void testCorrect() throws Exception {
53         Verifier v = setUp("Correct/", false);
54         verifyCorrectLog(v);
55     }
56
57     @Test
58     public void testAdditionalConfiguration() throws Exception {
59         Verifier v = setUp("AdditionalConfig/", false);
60         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}");
61         v.verifyTextInLog("[DEBUG] yang-to-sources: Additional configuration picked up for : org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: {c1=config}");
62         v.verifyTextInLog(File.separator
63                 + "files marked as resources: META-INF/yang");
64         v.verifyTextInLog("target"
65                 + File.separator
66                 + "generated-resources 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("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("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(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("NoGenerators/", false);
108         v.verifyErrorFreeLog();
109         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
110     }
111
112     @Test
113     public void testUnknownGenerator() throws Exception {
114         Verifier v = setUp("UnknownGenerator/", true);
115         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
116         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
117         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl");
118         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.yangtools.yang2sources.spi.CodeGeneratorTestImpl: null");
119         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
120     }
121
122     @Test
123     public void testNoYangFiles() throws Exception {
124         Verifier v = setUp("NoYangFiles/", false);
125         v.verifyTextInLog("[INFO] yang-to-sources: No input files found");
126     }
127
128     static void assertVerificationException(VerificationException e,
129             String string) {
130         assertThat(e.getMessage(), containsString(string));
131     }
132
133     @BeforeClass
134     public static void generateProps() throws IOException {
135         final Properties sp = new Properties();
136         try (InputStream is = new FileInputStream(new File(SRC_PROPERTIES))) {
137              sp.load(is);
138         }
139
140         props = new Properties(System.getProperties());
141         props.put(VERSION_PROP, sp.getProperty(VERSION_PROP));
142     }
143
144     static Verifier setUp(String project, boolean ignoreF)
145             throws VerificationException, URISyntaxException {
146         final URL path = YangToSourcesPluginTestIT.class.getResource("/"
147                 + project + "pom.xml");
148         File parent = new File(path.toURI());
149         Verifier verifier = new Verifier(parent.getParent());
150         if (ignoreF)
151             verifier.addCliOption("-fn");
152         verifier.setMavenDebug(true);
153         verifier.setSystemProperties(props);
154         verifier.executeGoal("generate-sources");
155         return verifier;
156     }
157
158     @Test
159     public void testNoOutputDir() throws Exception {
160         Verifier v = YangToSourcesPluginTestIT.setUp("NoOutputDir/", false);
161         verifyCorrectLog(v);
162     }
163
164     @Test
165     public void testFindResourceOnCp() throws Exception {
166         Verifier v1 = new Verifier(new File(getClass().getResource(
167                 "/GenerateTest1/pom.xml").toURI()).getParent());
168         v1.setSystemProperties(props);
169         v1.executeGoal("clean");
170         v1.executeGoal("package");
171         v1.assertFilePresent("target/classes/META-INF/yang/testfile1.yang");
172         v1.assertFilePresent("target/classes/META-INF/yang/testfile2.yang");
173         v1.assertFilePresent("target/classes/META-INF/yang/testfile3.yang");
174
175         Verifier v2 = YangToSourcesPluginTestIT.setUp("GenerateTest2/", false);
176         v2.executeGoal("clean");
177         v2.executeGoal("package");
178         v2.assertFilePresent("target/classes/META-INF/yang/private.yang");
179         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile1.yang");
180         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile2.yang");
181         v2.assertFileNotPresent("target/classes/META-INF/yang/testfile3.yang");
182     }
183 }