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