2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc;
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;
36 * The Class TestYANGGenerator - Junit Test Class for all related test cases.
39 public class TestYANGGenerator {
41 private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
42 private static String tosca;
43 private static String toscaWithSyntaxError;
44 private static String expectedYang;
47 public TemporaryFolder temporaryFolder = new TemporaryFolder();
50 * Run before test method.
52 * @throws IOException Signals that an I/O exception has occurred.
55 public void runBeforeTestMethod() throws IOException {
56 tosca= getFileContent("tosca/toscaFile.yml");
57 toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml");
58 expectedYang = getFileContent("yang/expectedYang.yang");
62 * Test YANG generator for success.
64 * @throws IOException Signals that an I/O exception has occurred.
65 * @throws YANGGenerationException the YANG generation exception
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);
76 String generatedYang = getFileContent(tempFile);
77 Assert.assertEquals(expectedYang,generatedYang);
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);
89 * Test for Yang Generator which generates YANG that is not matching with expected YANG.
91 * @throws IOException Signals that an I/O exception has occurred.
92 * @throws YANGGenerationException - the YANG generation exception
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);
101 String generatedYang = getFileContent(tempFile);
102 Assert.assertNotSame(expectedYang, generatedYang);
107 * Yang generation test for empty tosca input.
109 * @throws YANGGenerationException the YANG generation exception
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);
120 * Yang generation test for empty tosca input.
122 * @throws YANGGenerationException the YANG generation exception
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);
133 * Yang generation test for empty tosca input.
135 * @throws YANGGenerationException the YANG generation exception
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);
145 * YANG generation test with invalid method arguments.
147 * @throws YANGGenerationException the YANG generation exception
149 @Test(expected = YANGGenerationException.class)
150 public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException {
151 yangGenerator.generateYANG("112476", "ToscaSAMPLE", null);
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);
160 yangGenerator.generateYANG("1111", tosca, out);
164 private String getFileContent(String fileName) throws IOException
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();
172 while (line != null) {
173 sb.append(line).append("\n");
174 line = buf.readLine();
176 String fileString = sb.toString();
181 private String getFileContent(File file) throws IOException
183 InputStream is = new FileInputStream(file);
184 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
185 String line = buf.readLine();
186 StringBuilder sb = new StringBuilder();
188 while (line != null) {
189 sb.append(line).append("\n");
190 line = buf.readLine();
192 String fileString = sb.toString();