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