2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc;
25 import org.junit.rules.TemporaryFolder;
26 import org.openecomp.appc.yang.YANGGenerator;
27 import org.openecomp.appc.yang.exception.YANGGenerationException;
28 import org.openecomp.appc.yang.impl.YANGGeneratorFactory;
33 * The Class TestYANGGenerator - Junit Test Class for all related test cases.
36 public class TestYANGGenerator {
38 private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
39 private static String tosca;
40 private static String toscaWithSyntaxError;
41 private static String expectedYang;
44 public TemporaryFolder temporaryFolder = new TemporaryFolder();
47 * Run before test method.
49 * @throws IOException Signals that an I/O exception has occurred.
52 public void runBeforeTestMethod() throws IOException {
53 tosca= getFileContent("tosca/toscaFile.yml");
54 toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml");
55 expectedYang = getFileContent("yang/expectedYang.yang");
59 * Test YANG generator for success.
61 * @throws IOException Signals that an I/O exception has occurred.
62 * @throws YANGGenerationException the YANG generation exception
65 public void TestYANGGeneratorForSuccess() throws IOException, YANGGenerationException {
66 File tempFile = temporaryFolder.newFile("generatedYang.yang");
67 OutputStream out = new FileOutputStream(tempFile);
68 Assert.assertNotNull(tosca);
69 Assert.assertFalse("tosca file is emply or blank", tosca.equals(""));
70 yangGenerator.generateYANG("ATD456", tosca, out);
73 String generatedYang = getFileContent(tempFile);
74 Assert.assertEquals(expectedYang,generatedYang);
77 @Test(expected = YANGGenerationException.class)
78 public void testYangGenerationForSyntaxError() throws IOException, YANGGenerationException {
79 File tempFile = temporaryFolder.newFile("generatedYang.yang");
80 OutputStream out = new FileOutputStream(tempFile);
81 yangGenerator.generateYANG("ATD456",toscaWithSyntaxError,out);
86 * Test for Yang Generator which generates YANG that is not matching with expected YANG.
88 * @throws IOException Signals that an I/O exception has occurred.
89 * @throws YANGGenerationException - the YANG generation exception
92 public void unmatchedYangGenerationTest() throws IOException, YANGGenerationException {
93 File tempFile = temporaryFolder.newFile("generatedYang.yang");
94 OutputStream out = new FileOutputStream(tempFile);
95 yangGenerator.generateYANG("112476", tosca, out);
98 String generatedYang = getFileContent(tempFile);
99 Assert.assertNotSame(expectedYang, generatedYang);
104 * Yang generation test for empty tosca input.
106 * @throws YANGGenerationException the YANG generation exception
108 @Test(expected = YANGGenerationException.class)
109 public void YangGenerationTestForEmptyUniqueIDInput() throws IOException, YANGGenerationException {
110 // OutputStream out = new FileOutputStream(classLoader.getResource("yang/generatedYang.yang").getFile());
111 File tempFile = temporaryFolder.newFile("generatedYang.yang");
112 OutputStream out = new FileOutputStream(tempFile);
113 yangGenerator.generateYANG("", tosca, out);
117 * Yang generation test for empty tosca input.
119 * @throws YANGGenerationException the YANG generation exception
121 @Test(expected = YANGGenerationException.class)
122 public void YangGenerationTestForUnSupportedType() throws IOException, YANGGenerationException {
123 tosca= getFileContent("tosca/toscaFileWithUnsupportedTypes.yml");
124 File tempFile = temporaryFolder.newFile("generatedYang.yang");
125 OutputStream out = new FileOutputStream(tempFile);
126 yangGenerator.generateYANG("", tosca, out);
130 * Yang generation test for empty tosca input.
132 * @throws YANGGenerationException the YANG generation exception
134 @Test(expected = YANGGenerationException.class)
135 public void YangGenerationTestForEmptyToscaInput() throws IOException, YANGGenerationException {
136 File tempFile = temporaryFolder.newFile("generatedYang.yang");
137 OutputStream out = new FileOutputStream(tempFile);
138 yangGenerator.generateYANG("1111", "", out);
142 * YANG generation test with invalid method arguments.
144 * @throws YANGGenerationException the YANG generation exception
146 @Test(expected = YANGGenerationException.class)
147 public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException {
148 yangGenerator.generateYANG("112476", "ToscaSAMPLE", null);
151 @Test(expected = YANGGenerationException.class)
152 public void YANGGenerationTestWithIOException() throws IOException, YANGGenerationException {
153 File tempFile = temporaryFolder.newFile("generatedYang.yang");
154 OutputStream out = new FileOutputStream(tempFile);
157 yangGenerator.generateYANG("1111", tosca, out);
161 private String getFileContent(String fileName) throws IOException
163 ClassLoader classLoader = new TestYANGGenerator().getClass().getClassLoader();
164 InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
165 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
166 String line = buf.readLine();
167 StringBuilder sb = new StringBuilder();
169 while (line != null) {
170 sb.append(line).append("\n");
171 line = buf.readLine();
173 String fileString = sb.toString();
178 private String getFileContent(File file) throws IOException
180 InputStream is = new FileInputStream(file);
181 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
182 String line = buf.readLine();
183 StringBuilder sb = new StringBuilder();
185 while (line != null) {
186 sb.append(line).append("\n");
187 line = buf.readLine();
189 String fileString = sb.toString();