2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.auth.clieditor;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
34 import org.onap.policy.common.utils.resources.TextFileUtils;
37 * The Class TestCLIEditorOptions.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class CommandLineEditorOptionsTest {
42 // CHECKSTYLE:OFF: MagicNumber
45 * Test script options log model.
47 * @throws IOException Signals that an I/O exception has occurred.
48 * @throws ApexModelException if there is an Apex error
51 public void testScriptOptionsLogModel() throws IOException, ApexModelException {
52 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
53 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
55 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
56 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
58 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
59 assertEquals(0, cliEditor.getErrorCount());
61 // Get the model and log into strings
62 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
63 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
65 // As a sanity check, count the number of non white space characters in log and model files
66 final int logCharCount = logString.replaceAll("\\s+", "").length();
67 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
69 assertEquals(1204, logCharCount);
70 assertEquals(2561, modelCharCount);
73 tempModelFile.delete();
77 * Test script options no log no model spec.
79 * @throws IOException Signals that an I/O exception has occurred.
80 * @throws ApexModelException if there is an Apex error
83 public void testScriptOptionsNoLogNoModelSpec() throws IOException, ApexModelException {
84 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
85 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
87 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
88 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl", "-nm"};
90 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
91 assertEquals(0, cliEditor.getErrorCount());
93 // Get the model and log into strings
94 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
95 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
97 // As a sanity check, count the number of non white space characters in log and model files
98 final int logCharCount = logString.replaceAll("\\s+", "").length();
99 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
101 assertEquals(0, logCharCount);
102 assertEquals(0, modelCharCount);
104 tempLogFile.delete();
105 tempModelFile.delete();
109 * Test script options log no model spec.
111 * @throws IOException Signals that an I/O exception has occurred.
112 * @throws ApexModelException if there is an Apex error
115 public void testScriptOptionsLogNoModelSpec() throws IOException, ApexModelException {
116 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
117 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
119 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
120 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nm"};
122 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
123 assertEquals(0, cliEditor.getErrorCount());
125 // Get the model and log into strings
126 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
127 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
129 System.err.println(modelString);
130 // As a sanity check, count the number of non white space characters in log and model files
131 final int logCharCount = logString.replaceAll("\\s+", "").length();
132 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
134 assertEquals(1204, logCharCount);
135 assertEquals(0, modelCharCount);
137 tempLogFile.delete();
138 tempModelFile.delete();
142 * Test script options no log model spec.
144 * @throws IOException Signals that an I/O exception has occurred.
145 * @throws ApexModelException if there is an Apex error
148 public void testScriptOptionsNoLogModelSpec() throws IOException, ApexModelException {
149 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
150 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
152 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
153 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl"};
155 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
156 assertEquals(0, cliEditor.getErrorCount());
158 // Get the model and log into strings
159 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
160 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
162 // As a sanity check, count the number of non white space characters in log and model files
163 final int logCharCount = logString.replaceAll("\\s+", "").length();
164 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
166 assertEquals(0, logCharCount);
167 assertEquals(2561, modelCharCount);
169 tempLogFile.delete();
170 tempModelFile.delete();
174 * Test script options no log no model no spec.
176 * @throws IOException Signals that an I/O exception has occurred.
177 * @throws ApexModelException if there is an Apex error
180 public void testScriptOptionsNoLogNoModelNoSpec() throws IOException, ApexModelException {
181 final String[] cliArgs =
182 new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-nl", "-nm"};
184 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
186 System.setOut(new PrintStream(baos));
187 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
188 assertEquals(0, cliEditor.getErrorCount());
191 assertFalse(baos.toString().contains(">"));
193 // Curly bracket from JSON model
194 assertFalse(baos.toString().contains("{"));
198 * Test script options log model no spec.
200 * @throws IOException Signals that an I/O exception has occurred.
201 * @throws ApexModelException if there is an Apex error
204 public void testScriptOptionsLogModelNoSpec() throws IOException, ApexModelException {
205 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex"};
207 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
209 final PrintStream stdout = System.out;
210 System.setOut(new PrintStream(baos));
211 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
212 assertEquals(0, cliEditor.getErrorCount());
215 assertTrue(baos.toString().contains(">"));
217 // Curly bracket from JSON model
218 assertTrue(baos.toString().contains("{"));
220 System.setOut(stdout);
224 * Test script options input output model.
226 * @throws IOException Signals that an I/O exception has occurred.
227 * @throws ApexModelException if there is an Apex error
230 public void testScriptOptionsInputOutputModel() throws IOException, ApexModelException {
231 final File tempLogFileIn = File.createTempFile("ShellPolicyModelIn", ".log");
232 final File tempLogFileOut = File.createTempFile("ShellPolicyModelOut", ".log");
233 final File tempModelFileIn = File.createTempFile("ShellPolicyModelIn", ".json");
234 final File tempModelFileOut = File.createTempFile("ShellPolicyModelOut", ".json");
236 // Generate input model
237 final String[] cliArgsIn = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex",
238 "-l", tempLogFileIn.getAbsolutePath(), "-o", tempModelFileIn.getAbsolutePath()};
240 final ApexCommandLineEditorMain cliEditorIn = new ApexCommandLineEditorMain(cliArgsIn);
241 assertEquals(0, cliEditorIn.getErrorCount());
243 // Get the model and log into strings
244 final String tempLogFileInString = TextFileUtils.getTextFileAsString(tempLogFileIn.getCanonicalPath());
245 final String tempModelFileInString = TextFileUtils.getTextFileAsString(tempModelFileIn.getCanonicalPath());
247 // As a sanity check, count the number of non white space characters in log and model files
248 final int tempLogFileInCharCount = tempLogFileInString.replaceAll("\\s+", "").length();
249 final int tempModelFileInCharCount = tempModelFileInString.replaceAll("\\s+", "").length();
251 assertEquals(1204, tempLogFileInCharCount);
252 assertEquals(2561, tempModelFileInCharCount);
254 final String[] cliArgsOut = new String[] {"-i", tempModelFileIn.getAbsolutePath(), "-c",
255 "src/main/resources/examples/scripts/ShellPolicyModelAddSchema.apex", "-l",
256 tempLogFileOut.getAbsolutePath(), "-o", tempModelFileOut.getAbsolutePath()};
258 final ApexCommandLineEditorMain cliEditorOut = new ApexCommandLineEditorMain(cliArgsOut);
259 assertEquals(0, cliEditorOut.getErrorCount());
261 // Get the model and log into strings
262 final String tempLogFileOutString = TextFileUtils.getTextFileAsString(tempLogFileOut.getCanonicalPath());
263 final String tempModelFileOutString = TextFileUtils.getTextFileAsString(tempModelFileOut.getCanonicalPath());
265 // As a sanity check, count the number of non white space characters in log and model files
266 final int tempLogFileOutCharCount = tempLogFileOutString.replaceAll("\\s+", "").length();
267 final int tempModelFileOutCharCount = tempModelFileOutString.replaceAll("\\s+", "").length();
269 assertEquals(1154, tempLogFileOutCharCount);
270 assertEquals(2993, tempModelFileOutCharCount);
272 tempLogFileIn.delete();
273 tempModelFileIn.delete();
274 tempLogFileOut.delete();
275 tempModelFileOut.delete();