267e35ab605fc469a3785655956c0edcdf35407a
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin-it / src / test / java / org / opendaylight / controller / yang2sources / plugin / it / YangToSourcesPluginTest.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.controller.yang2sources.plugin.it;
9
10 import static org.junit.Assert.*;
11 import static org.junit.matchers.JUnitMatchers.containsString;
12
13 import java.io.File;
14
15 import org.apache.maven.it.VerificationException;
16 import org.apache.maven.it.Verifier;
17 import org.junit.Test;
18
19 public class YangToSourcesPluginTest {
20
21     @Test
22     public void testYangRootNotExist() {
23         try {
24             setUp("YangRootNotExist/", false);
25         } catch (VerificationException e) {
26             assertVerificationException(e,
27                     "[ERROR] yang-to-sources: Unable to parse yang files from unknown");
28             assertVerificationException(
29                     e,
30                     "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from unknown");
31             return;
32         }
33
34         fail("Verification exception should have been thrown");
35     }
36
37     @Test
38     public void testCorrect() throws VerificationException {
39         Verifier v = setUp("Correct/", false);
40         verifyCorrectLog(v);
41
42     }
43
44     static void verifyCorrectLog(Verifier v) throws VerificationException {
45         v.verifyErrorFreeLog();
46         v.verifyTextInLog("[INFO] yang-to-sources: yang files parsed from");
47         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
48         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
49         v.verifyTextInLog("[INFO] yang-to-sources: Resource provider instantiated from org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl");
50         v.verifyTextInLog("[INFO] yang-to-sources: Resource provider org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl call successful");
51     }
52
53     @Test
54     public void testNoGenerators() throws VerificationException {
55         Verifier v = setUp("NoGenerators/", false);
56         v.verifyErrorFreeLog();
57         v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
58     }
59
60     @Test
61     public void testUnknownGenerator() throws VerificationException {
62         Verifier v = setUp("UnknownGenerator/", true);
63         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
64         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
65         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
66         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
67         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
68     }
69
70     @Test
71     public void testNoYangFiles() throws VerificationException {
72         Verifier v = setUp("NoYangFiles/", false);
73         v.verifyTextInLog("[WARNING] yang-to-sources: No yang file found in ");
74         v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl");
75         v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null");
76     }
77
78     static void assertVerificationException(VerificationException e,
79             String string) {
80         assertThat(e.getMessage(), containsString(string));
81     }
82
83     static Verifier setUp(String project, boolean ignoreF)
84             throws VerificationException {
85         Verifier verifier = new Verifier(new File("src/test/resources/"
86                 + project).getAbsolutePath());
87         if (ignoreF)
88             verifier.addCliOption("-fn");
89         verifier.executeGoal("generate-sources");
90         return verifier;
91     }
92
93     @Test
94     public void testNoGeneratorsResources() throws VerificationException {
95         Verifier v = YangToSourcesPluginTest.setUp("NoGenerators_resources/",
96                 false);
97         v.verifyErrorFreeLog();
98         v.verifyTextInLog("[WARNING] yang-to-sources: No resource provider classes provided");
99     }
100
101     @Test
102     public void testUnknownGeneratorResources() throws VerificationException {
103         Verifier v = YangToSourcesPluginTest.setUp(
104                 "UnknownGenerator_resources/", true);
105         v.verifyTextInLog("[ERROR] yang-to-sources: Unable to provide resources with unknown resource provider");
106         v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
107         v.verifyTextInLog("[INFO] yang-to-sources: Resource provider instantiated from org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl");
108         v.verifyTextInLog("[INFO] yang-to-sources: Resource provider org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl call successful");
109         v.verifyTextInLog("[ERROR] yang-to-sources: One or more code resource provider failed, including failed list(resourceProviderClass=exception) {unknown=java.lang.ClassNotFoundException}");
110     }
111
112     @Test
113     public void testNoOutputDir() throws VerificationException {
114         Verifier v = YangToSourcesPluginTest.setUp("NoOutputDir/",
115                 false);
116         verifyCorrectLog(v);
117     }
118
119     @Test
120     public void testFindResourceOnCp() throws VerificationException {
121         Verifier v1 = new Verifier(new File("src/test/resources/GenerateTest1/").getAbsolutePath());
122         v1.executeGoal("package");
123         v1.assertFilePresent("target/external-resources/testfile1.yang");
124         v1.assertFilePresent("target/external-resources/testfile2.yang");
125         v1.assertFilePresent("target/external-resources/testfile3.yang");
126
127         Verifier v2 = YangToSourcesPluginTest.setUp("GenerateTest2/",
128                 false);
129         v2.executeGoal("package");
130     }
131
132 }