a5362a8f13028eacd5c91582578bddb85253246c
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / CommandLineEditorOptionsTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.auth.clieditor;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.PrintStream;
32
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
35 import org.onap.policy.common.utils.resources.TextFileUtils;
36
37 /**
38  * The Class TestCLIEditorOptions.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class CommandLineEditorOptionsTest {
43     // CHECKSTYLE:OFF: MagicNumber
44
45     /**
46      * Test script options log model.
47      *
48      * @throws IOException Signals that an I/O exception has occurred.
49      * @throws ApexModelException if there is an Apex error
50      */
51     @Test
52     public void testScriptOptionsLogModel() throws IOException, ApexModelException {
53         final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
54         final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
55
56         final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
57             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
58
59         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
60         assertEquals(0, cliEditor.getErrorCount());
61
62         // Get the model and log into strings
63         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
64         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
65
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();
69
70         assertEquals(1204, logCharCount);
71         assertEquals(2561, modelCharCount);
72
73         tempLogFile.delete();
74         tempModelFile.delete();
75     }
76
77     /**
78      * Test script options no log no model spec.
79      *
80      * @throws IOException Signals that an I/O exception has occurred.
81      * @throws ApexModelException if there is an Apex error
82      */
83     @Test
84     public void testScriptOptionsNoLogNoModelSpec() throws IOException, ApexModelException {
85         final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
86         final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
87
88         final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
89             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl", "-nm"};
90
91         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
92         assertEquals(0, cliEditor.getErrorCount());
93
94         // Get the model and log into strings
95         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
96         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
97
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();
101
102         assertEquals(0, logCharCount);
103         assertEquals(0, modelCharCount);
104
105         tempLogFile.delete();
106         tempModelFile.delete();
107     }
108
109     /**
110      * Test script options log no model spec.
111      *
112      * @throws IOException Signals that an I/O exception has occurred.
113      * @throws ApexModelException if there is an Apex error
114      */
115     @Test
116     public void testScriptOptionsLogNoModelSpec() throws IOException, ApexModelException {
117         final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
118         final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
119
120         final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
121             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nm"};
122
123         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
124         assertEquals(0, cliEditor.getErrorCount());
125
126         // Get the model and log into strings
127         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
128         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
129
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();
134
135         assertEquals(1204, logCharCount);
136         assertEquals(0, modelCharCount);
137
138         tempLogFile.delete();
139         tempModelFile.delete();
140     }
141
142     /**
143      * Test script options no log model spec.
144      *
145      * @throws IOException Signals that an I/O exception has occurred.
146      * @throws ApexModelException if there is an Apex error
147      */
148     @Test
149     public void testScriptOptionsNoLogModelSpec() throws IOException, ApexModelException {
150         final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
151         final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");
152
153         final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
154             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl"};
155
156         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
157         assertEquals(0, cliEditor.getErrorCount());
158
159         // Get the model and log into strings
160         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
161         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
162
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();
166
167         assertEquals(0, logCharCount);
168         assertEquals(2561, modelCharCount);
169
170         tempLogFile.delete();
171         tempModelFile.delete();
172     }
173
174     /**
175      * Test script options no log no model no spec.
176      *
177      * @throws IOException Signals that an I/O exception has occurred.
178      * @throws ApexModelException if there is an Apex error
179      */
180     @Test
181     public void testScriptOptionsNoLogNoModelNoSpec() throws IOException, ApexModelException {
182         final String[] cliArgs =
183                 new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-nl", "-nm"};
184
185         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
186
187         System.setOut(new PrintStream(baos));
188         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
189         assertEquals(0, cliEditor.getErrorCount());
190
191         // Cursor for log
192         assertFalse(baos.toString().contains(">"));
193
194         // Curly bracket from JSON model
195         assertFalse(baos.toString().contains("{"));
196     }
197
198     /**
199      * Test script options log model no spec.
200      *
201      * @throws IOException Signals that an I/O exception has occurred.
202      * @throws ApexModelException if there is an Apex error
203      */
204     @Test
205     public void testScriptOptionsLogModelNoSpec() throws IOException, ApexModelException {
206         final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex"};
207
208         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
209
210         final PrintStream stdout = System.out;
211         System.setOut(new PrintStream(baos));
212         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
213         assertEquals(0, cliEditor.getErrorCount());
214
215         // Cursor for log
216         assertTrue(baos.toString().contains(">"));
217
218         // Curly bracket from JSON model
219         assertTrue(baos.toString().contains("{"));
220
221         System.setOut(stdout);
222     }
223
224     /**
225      * Test script options input output model.
226      *
227      * @throws IOException Signals that an I/O exception has occurred.
228      * @throws ApexModelException if there is an Apex error
229      */
230     @Test
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");
236
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()};
240
241         final ApexCommandLineEditorMain cliEditorIn = new ApexCommandLineEditorMain(cliArgsIn);
242         assertEquals(0, cliEditorIn.getErrorCount());
243
244         // Get the model and log into strings
245         final String tempLogFileInString = TextFileUtils.getTextFileAsString(tempLogFileIn.getCanonicalPath());
246         final String tempModelFileInString = TextFileUtils.getTextFileAsString(tempModelFileIn.getCanonicalPath());
247
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();
251
252         assertEquals(1204, tempLogFileInCharCount);
253         assertEquals(2561, tempModelFileInCharCount);
254
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()};
258
259         final ApexCommandLineEditorMain cliEditorOut = new ApexCommandLineEditorMain(cliArgsOut);
260         assertEquals(0, cliEditorOut.getErrorCount());
261
262         // Get the model and log into strings
263         final String tempLogFileOutString = TextFileUtils.getTextFileAsString(tempLogFileOut.getCanonicalPath());
264         final String tempModelFileOutString = TextFileUtils.getTextFileAsString(tempModelFileOut.getCanonicalPath());
265
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();
269
270         assertEquals(1154, tempLogFileOutCharCount);
271         assertEquals(2993, tempModelFileOutCharCount);
272
273         tempLogFileIn.delete();
274         tempModelFileIn.delete();
275         tempLogFileOut.delete();
276         tempModelFileOut.delete();
277     }
278 }