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