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;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
35 import org.onap.policy.common.utils.resources.TextFileUtils;
38 * The Class TestCLIEditorOptions.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class CommandLineEditorOptionsTest {
43 // CHECKSTYLE:OFF: MagicNumber
46 * Test script options log model.
48 * @throws IOException Signals that an I/O exception has occurred.
49 * @throws ApexModelException if there is an Apex error
52 public void testScriptOptionsLogModel() throws IOException, ApexModelException {
53 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
54 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
56 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
57 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
59 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
60 assertEquals(0, cliEditor.getErrorCount());
62 // Get the model and log into strings
63 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
64 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
66 // As a sanity check, count the number of non white space characters in log and model files
67 final int logCharCount = logString.replaceAll("\\s+", "").length();
68 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
70 assertEquals(1204, logCharCount);
71 assertEquals(2561, modelCharCount);
74 tempModelFile.delete();
78 * Test script options no log no model spec.
80 * @throws IOException Signals that an I/O exception has occurred.
81 * @throws ApexModelException if there is an Apex error
84 public void testScriptOptionsNoLogNoModelSpec() throws IOException, ApexModelException {
85 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
86 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
88 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
89 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl", "-nm"};
91 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
92 assertEquals(0, cliEditor.getErrorCount());
94 // Get the model and log into strings
95 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
96 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
98 // As a sanity check, count the number of non white space characters in log and model files
99 final int logCharCount = logString.replaceAll("\\s+", "").length();
100 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
102 assertEquals(0, logCharCount);
103 assertEquals(0, modelCharCount);
105 tempLogFile.delete();
106 tempModelFile.delete();
110 * Test script options log no model spec.
112 * @throws IOException Signals that an I/O exception has occurred.
113 * @throws ApexModelException if there is an Apex error
116 public void testScriptOptionsLogNoModelSpec() throws IOException, ApexModelException {
117 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
118 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
120 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
121 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nm"};
123 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
124 assertEquals(0, cliEditor.getErrorCount());
126 // Get the model and log into strings
127 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
128 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
130 System.err.println(modelString);
131 // As a sanity check, count the number of non white space characters in log and model files
132 final int logCharCount = logString.replaceAll("\\s+", "").length();
133 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
135 assertEquals(1204, logCharCount);
136 assertEquals(0, modelCharCount);
138 tempLogFile.delete();
139 tempModelFile.delete();
143 * Test script options no log model spec.
145 * @throws IOException Signals that an I/O exception has occurred.
146 * @throws ApexModelException if there is an Apex error
149 public void testScriptOptionsNoLogModelSpec() throws IOException, ApexModelException {
150 final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
151 final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
153 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
154 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl"};
156 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
157 assertEquals(0, cliEditor.getErrorCount());
159 // Get the model and log into strings
160 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
161 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
163 // As a sanity check, count the number of non white space characters in log and model files
164 final int logCharCount = logString.replaceAll("\\s+", "").length();
165 final int modelCharCount = modelString.replaceAll("\\s+", "").length();
167 assertEquals(0, logCharCount);
168 assertEquals(2561, modelCharCount);
170 tempLogFile.delete();
171 tempModelFile.delete();
175 * Test script options no log no model no spec.
177 * @throws IOException Signals that an I/O exception has occurred.
178 * @throws ApexModelException if there is an Apex error
181 public void testScriptOptionsNoLogNoModelNoSpec() throws IOException, ApexModelException {
182 final String[] cliArgs =
183 new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-nl", "-nm"};
185 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
187 System.setOut(new PrintStream(baos));
188 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
189 assertEquals(0, cliEditor.getErrorCount());
192 assertFalse(baos.toString().contains(">"));
194 // Curly bracket from JSON model
195 assertFalse(baos.toString().contains("{"));
199 * Test script options log model no spec.
201 * @throws IOException Signals that an I/O exception has occurred.
202 * @throws ApexModelException if there is an Apex error
205 public void testScriptOptionsLogModelNoSpec() throws IOException, ApexModelException {
206 final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex"};
208 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
210 final PrintStream stdout = System.out;
211 System.setOut(new PrintStream(baos));
212 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
213 assertEquals(0, cliEditor.getErrorCount());
216 assertTrue(baos.toString().contains(">"));
218 // Curly bracket from JSON model
219 assertTrue(baos.toString().contains("{"));
221 System.setOut(stdout);
225 * Test script options input output model.
227 * @throws IOException Signals that an I/O exception has occurred.
228 * @throws ApexModelException if there is an Apex error
231 public void testScriptOptionsInputOutputModel() throws IOException, ApexModelException {
232 final File tempLogFileIn = File.createTempFile("ShellPolicyModelIn", ".log");
233 final File tempLogFileOut = File.createTempFile("ShellPolicyModelOut", ".log");
234 final File tempModelFileIn = File.createTempFile("ShellPolicyModelIn", ".json");
235 final File tempModelFileOut = File.createTempFile("ShellPolicyModelOut", ".json");
237 // Generate input model
238 final String[] cliArgsIn = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex",
239 "-l", tempLogFileIn.getAbsolutePath(), "-o", tempModelFileIn.getAbsolutePath()};
241 final ApexCommandLineEditorMain cliEditorIn = new ApexCommandLineEditorMain(cliArgsIn);
242 assertEquals(0, cliEditorIn.getErrorCount());
244 // Get the model and log into strings
245 final String tempLogFileInString = TextFileUtils.getTextFileAsString(tempLogFileIn.getCanonicalPath());
246 final String tempModelFileInString = TextFileUtils.getTextFileAsString(tempModelFileIn.getCanonicalPath());
248 // As a sanity check, count the number of non white space characters in log and model files
249 final int tempLogFileInCharCount = tempLogFileInString.replaceAll("\\s+", "").length();
250 final int tempModelFileInCharCount = tempModelFileInString.replaceAll("\\s+", "").length();
252 assertEquals(1204, tempLogFileInCharCount);
253 assertEquals(2561, tempModelFileInCharCount);
255 final String[] cliArgsOut = new String[] {"-i", tempModelFileIn.getAbsolutePath(), "-c",
256 "src/main/resources/examples/scripts/ShellPolicyModelAddSchema.apex", "-l",
257 tempLogFileOut.getAbsolutePath(), "-o", tempModelFileOut.getAbsolutePath()};
259 final ApexCommandLineEditorMain cliEditorOut = new ApexCommandLineEditorMain(cliArgsOut);
260 assertEquals(0, cliEditorOut.getErrorCount());
262 // Get the model and log into strings
263 final String tempLogFileOutString = TextFileUtils.getTextFileAsString(tempLogFileOut.getCanonicalPath());
264 final String tempModelFileOutString = TextFileUtils.getTextFileAsString(tempModelFileOut.getCanonicalPath());
266 // As a sanity check, count the number of non white space characters in log and model files
267 final int tempLogFileOutCharCount = tempLogFileOutString.replaceAll("\\s+", "").length();
268 final int tempModelFileOutCharCount = tempModelFileOutString.replaceAll("\\s+", "").length();
270 assertEquals(1154, tempLogFileOutCharCount);
271 assertEquals(2993, tempModelFileOutCharCount);
273 tempLogFileIn.delete();
274 tempModelFileIn.delete();
275 tempLogFileOut.delete();
276 tempModelFileOut.delete();