2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc;
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;
35 * The Class TestYANGGenerator - Junit Test Class for all related test cases.
38 public class TestYANGGenerator {
40 private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
41 private static String tosca;
42 private static String toscaWithSyntaxError;
43 private static String expectedYang;
46 public TemporaryFolder temporaryFolder = new TemporaryFolder();
49 * Run before test method.
51 * @throws IOException Signals that an I/O exception has occurred.
54 public void runBeforeTestMethod() throws IOException {
55 tosca= getFileContent("tosca/toscaFile.yml");
56 toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml");
57 expectedYang = getFileContent("yang/expectedYang.yang");
61 * Test YANG generator for success.
63 * @throws IOException Signals that an I/O exception has occurred.
64 * @throws YANGGenerationException the YANG generation exception
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);
75 String generatedYang = getFileContent(tempFile);
76 Assert.assertEquals(expectedYang,generatedYang);
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);
88 * Test for Yang Generator which generates YANG that is not matching with expected YANG.
90 * @throws IOException Signals that an I/O exception has occurred.
91 * @throws YANGGenerationException - the YANG generation exception
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);
100 String generatedYang = getFileContent(tempFile);
101 Assert.assertNotSame(expectedYang, generatedYang);
106 * Yang generation test for empty tosca input.
108 * @throws YANGGenerationException the YANG generation exception
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);
119 * Yang generation test for empty tosca input.
121 * @throws YANGGenerationException the YANG generation exception
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);
132 * Yang generation test for empty tosca input.
134 * @throws YANGGenerationException the YANG generation exception
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);
144 * YANG generation test with invalid method arguments.
146 * @throws YANGGenerationException the YANG generation exception
148 @Test(expected = YANGGenerationException.class)
149 public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException {
150 yangGenerator.generateYANG("112476", "ToscaSAMPLE", null);
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);
159 yangGenerator.generateYANG("1111", tosca, out);
163 private String getFileContent(String fileName) throws IOException
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();
171 while (line != null) {
172 sb.append(line).append("\n");
173 line = buf.readLine();
175 String fileString = sb.toString();
180 private String getFileContent(File file) throws IOException
182 InputStream is = new FileInputStream(file);
183 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
184 String line = buf.readLine();
185 StringBuilder sb = new StringBuilder();
187 while (line != null) {
188 sb.append(line).append("\n");
189 line = buf.readLine();
191 String fileString = sb.toString();