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