2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc;
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;
34 * The Class TestYANGGenerator - Junit Test Class for all related test cases.
37 public class TestYANGGenerator {
39 private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
40 private static String tosca;
41 private static String toscaWithSyntaxError;
42 private static String expectedYang;
45 public TemporaryFolder temporaryFolder = new TemporaryFolder();
48 * Run before test method.
50 * @throws IOException Signals that an I/O exception has occurred.
53 public void runBeforeTestMethod() throws IOException {
54 tosca= getFileContent("tosca/toscaFile.yml");
55 toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml");
56 expectedYang = getFileContent("yang/expectedYang.yang");
60 * Test YANG generator for success.
62 * @throws IOException Signals that an I/O exception has occurred.
63 * @throws YANGGenerationException the YANG generation exception
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);
74 String generatedYang = getFileContent(tempFile);
75 Assert.assertEquals(expectedYang,generatedYang);
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);
87 * Test for Yang Generator which generates YANG that is not matching with expected YANG.
89 * @throws IOException Signals that an I/O exception has occurred.
90 * @throws YANGGenerationException - the YANG generation exception
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);
99 String generatedYang = getFileContent(tempFile);
100 Assert.assertNotSame(expectedYang, generatedYang);
105 * Yang generation test for empty tosca input.
107 * @throws YANGGenerationException the YANG generation exception
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);
118 * Yang generation test for empty tosca input.
120 * @throws YANGGenerationException the YANG generation exception
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);
131 * Yang generation test for empty tosca input.
133 * @throws YANGGenerationException the YANG generation exception
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);
143 * YANG generation test with invalid method arguments.
145 * @throws YANGGenerationException the YANG generation exception
147 @Test(expected = YANGGenerationException.class)
148 public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException {
149 yangGenerator.generateYANG("112476", "ToscaSAMPLE", null);
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);
158 yangGenerator.generateYANG("1111", tosca, out);
162 private String getFileContent(String fileName) throws IOException
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();
170 while (line != null) {
171 sb.append(line).append("\n");
172 line = buf.readLine();
174 String fileString = sb.toString();
179 private String getFileContent(File file) throws IOException
181 InputStream is = new FileInputStream(file);
182 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
183 String line = buf.readLine();
184 StringBuilder sb = new StringBuilder();
186 while (line != null) {
187 sb.append(line).append("\n");
188 line = buf.readLine();
190 String fileString = sb.toString();