398a8587bcb5acd8eb56faa42e6726a32bd2dbcc
[appc.git] / appc-asdc-listener / appc-yang-generator / src / test / java / org / openecomp / appc / TestYANGGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc;
23
24 import org.junit.*;
25 import org.junit.rules.TemporaryFolder;
26 import org.openecomp.appc.yang.YANGGenerator;
27 import org.openecomp.appc.yang.exception.YANGGenerationException;
28 import org.openecomp.appc.yang.impl.YANGGeneratorFactory;
29
30 import java.io.*;
31
32 /**
33  * The Class TestYANGGenerator - Junit Test Class for all related test cases.
34  */
35 @Ignore
36 public class TestYANGGenerator {
37
38         private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
39         private static String tosca;
40         private static String toscaWithSyntaxError;
41         private static String expectedYang;
42
43         @Rule
44         public TemporaryFolder temporaryFolder = new TemporaryFolder();
45
46         /**
47          * Run before test method.
48          *
49          * @throws IOException Signals that an I/O exception has occurred.
50          */
51         @Before
52         public void runBeforeTestMethod() throws IOException {
53                 tosca= getFileContent("tosca/toscaFile.yml");
54                 toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml");
55                 expectedYang = getFileContent("yang/expectedYang.yang");
56         }
57
58         /**
59          * Test YANG generator for success.
60          *
61          * @throws IOException Signals that an I/O exception has occurred.
62          * @throws YANGGenerationException the YANG generation exception
63          */
64         @Test
65         public void TestYANGGeneratorForSuccess() throws IOException, YANGGenerationException {
66                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
67                 OutputStream out = new FileOutputStream(tempFile);
68                 Assert.assertNotNull(tosca);
69                 Assert.assertFalse("tosca file is emply or blank", tosca.equals(""));
70                 yangGenerator.generateYANG("ATD456", tosca, out);
71                 out.flush();
72                 out.close();
73                 String generatedYang = getFileContent(tempFile);
74                 Assert.assertEquals(expectedYang,generatedYang);
75         }
76
77         @Test(expected = YANGGenerationException.class)
78         public void testYangGenerationForSyntaxError() throws IOException, YANGGenerationException {
79                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
80                 OutputStream out = new FileOutputStream(tempFile);
81                 yangGenerator.generateYANG("ATD456",toscaWithSyntaxError,out);
82         }
83
84
85         /**
86          * Test for Yang Generator which generates YANG that is not matching with expected YANG.
87          *
88          * @throws IOException Signals that an I/O exception has occurred.
89          * @throws YANGGenerationException - the YANG generation exception
90          */
91         @Test
92         public void unmatchedYangGenerationTest() throws IOException, YANGGenerationException {
93                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
94                 OutputStream out = new FileOutputStream(tempFile);
95                 yangGenerator.generateYANG("112476", tosca, out);
96                 out.flush();
97                 out.close();
98                 String generatedYang = getFileContent(tempFile);
99                 Assert.assertNotSame(expectedYang, generatedYang);
100
101         }
102
103     /**
104      * Yang generation test for empty tosca input.
105      *
106      * @throws YANGGenerationException the YANG generation exception
107      */
108     @Test(expected = YANGGenerationException.class)
109     public void YangGenerationTestForEmptyUniqueIDInput() throws IOException, YANGGenerationException {
110 //        OutputStream out = new FileOutputStream(classLoader.getResource("yang/generatedYang.yang").getFile());
111                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
112                 OutputStream out = new FileOutputStream(tempFile);
113         yangGenerator.generateYANG("", tosca, out);
114     }
115
116     /**
117      * Yang generation test for empty tosca input.
118      *
119      * @throws YANGGenerationException the YANG generation exception
120      */
121     @Test(expected = YANGGenerationException.class)
122     public void YangGenerationTestForUnSupportedType() throws IOException, YANGGenerationException {
123         tosca= getFileContent("tosca/toscaFileWithUnsupportedTypes.yml");
124                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
125                 OutputStream out = new FileOutputStream(tempFile);
126         yangGenerator.generateYANG("", tosca, out);
127     }
128
129         /**
130          * Yang generation test for empty tosca input.
131          *
132          * @throws YANGGenerationException the YANG generation exception
133          */
134         @Test(expected = YANGGenerationException.class)
135         public void YangGenerationTestForEmptyToscaInput() throws IOException, YANGGenerationException {
136                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
137                 OutputStream out = new FileOutputStream(tempFile);
138                 yangGenerator.generateYANG("1111", "", out);
139         }
140
141         /**
142          * YANG generation test with invalid method arguments.
143          *
144          * @throws YANGGenerationException the YANG generation exception
145          */
146         @Test(expected = YANGGenerationException.class)
147         public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException {     
148                 yangGenerator.generateYANG("112476", "ToscaSAMPLE", null);
149         }
150
151         @Test(expected = YANGGenerationException.class)
152         public void YANGGenerationTestWithIOException() throws IOException, YANGGenerationException {
153                 File tempFile = temporaryFolder.newFile("generatedYang.yang");
154                 OutputStream out = new FileOutputStream(tempFile);
155                 out.flush();
156                 out.close();
157                 yangGenerator.generateYANG("1111", tosca, out);
158         }
159
160
161         private String getFileContent(String fileName) throws IOException
162         {
163                 ClassLoader classLoader = new TestYANGGenerator().getClass().getClassLoader();
164                 InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
165                 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
166                 String line = buf.readLine();
167                 StringBuilder sb = new StringBuilder();
168
169                 while (line != null) {
170                         sb.append(line).append("\n");
171                         line = buf.readLine();
172                 }
173                 String fileString = sb.toString();
174                 is.close();
175                 return fileString;
176         }
177
178         private String getFileContent(File file) throws IOException
179         {
180                 InputStream is = new FileInputStream(file);
181                 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
182                 String line = buf.readLine();
183                 StringBuilder sb = new StringBuilder();
184
185                 while (line != null) {
186                         sb.append(line).append("\n");
187                         line = buf.readLine();
188                 }
189                 String fileString = sb.toString();
190                 is.close();
191                 return fileString;
192         }
193
194 }