aboutsummaryrefslogtreecommitdiffstats
path: root/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineEditorOptionsTest.java
blob: 227cb23f56965b88ab1c95e3b5a66bf97700b8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.auth.clieditor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
import org.onap.policy.apex.model.utilities.TextFileUtils;

/**
 * The Class TestCLIEditorOptions.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class CommandLineEditorOptionsTest {
    // CHECKSTYLE:OFF: MagicNumber

    /**
     * Test script options log model.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsLogModel() throws IOException, ApexModelException {
        final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
        final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");

        final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
                tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};

        final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
        assertEquals(0, cliEditor.getErrorCount());

        // Get the model and log into strings
        final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
        final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());

        // As a sanity check, count the number of non white space characters in log and model files
        final int logCharCount = logString.replaceAll("\\s+", "").length();
        final int modelCharCount = modelString.replaceAll("\\s+", "").length();

        assertEquals(1204, logCharCount);
        assertEquals(2924, modelCharCount);

        tempLogFile.delete();
        tempModelFile.delete();
    }

    /**
     * Test script options no log no model spec.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsNoLogNoModelSpec() throws IOException, ApexModelException {
        final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
        final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");

        final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
                tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl", "-nm"};

        final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
        assertEquals(0, cliEditor.getErrorCount());

        // Get the model and log into strings
        final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
        final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());

        // As a sanity check, count the number of non white space characters in log and model files
        final int logCharCount = logString.replaceAll("\\s+", "").length();
        final int modelCharCount = modelString.replaceAll("\\s+", "").length();

        assertEquals(0, logCharCount);
        assertEquals(0, modelCharCount);

        tempLogFile.delete();
        tempModelFile.delete();
    }

    /**
     * Test script options log no model spec.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsLogNoModelSpec() throws IOException, ApexModelException {
        final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
        final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");

        final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
                tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nm"};

        final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
        assertEquals(0, cliEditor.getErrorCount());

        // Get the model and log into strings
        final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
        final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());

        System.err.println(modelString);
        // As a sanity check, count the number of non white space characters in log and model files
        final int logCharCount = logString.replaceAll("\\s+", "").length();
        final int modelCharCount = modelString.replaceAll("\\s+", "").length();

        assertEquals(1204, logCharCount);
        assertEquals(0, modelCharCount);

        tempLogFile.delete();
        tempModelFile.delete();
    }

    /**
     * Test script options no log model spec.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsNoLogModelSpec() throws IOException, ApexModelException {
        final File tempLogFile = File.createTempFile("ShellPolicyModel", ".log");
        final File tempModelFile = File.createTempFile("ShellPolicyModel", ".json");

        final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-l",
                tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath(), "-nl"};

        final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
        assertEquals(0, cliEditor.getErrorCount());

        // Get the model and log into strings
        final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
        final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());

        // As a sanity check, count the number of non white space characters in log and model files
        final int logCharCount = logString.replaceAll("\\s+", "").length();
        final int modelCharCount = modelString.replaceAll("\\s+", "").length();

        assertEquals(0, logCharCount);
        assertEquals(2924, modelCharCount);

        tempLogFile.delete();
        tempModelFile.delete();
    }

    /**
     * Test script options no log no model no spec.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsNoLogNoModelNoSpec() throws IOException, ApexModelException {
        final String[] cliArgs =
                new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex", "-nl", "-nm"};

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        System.setOut(new PrintStream(baos));
        final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
        assertEquals(0, cliEditor.getErrorCount());

        // Cursor for log
        assertFalse(baos.toString().contains(">"));

        // Curly bracket from JSON model
        assertFalse(baos.toString().contains("{"));
    }

    /**
     * Test script options log model no spec.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsLogModelNoSpec() throws IOException, ApexModelException {
        final String[] cliArgs = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex"};

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        final PrintStream stdout = System.out;
        System.setOut(new PrintStream(baos));
        final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
        assertEquals(0, cliEditor.getErrorCount());

        // Cursor for log
        assertTrue(baos.toString().contains(">"));

        // Curly bracket from JSON model
        assertTrue(baos.toString().contains("{"));

        System.setOut(stdout);
    }

    /**
     * Test script options input output model.
     *
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws ApexModelException if there is an Apex error
     */
    @Test
    public void testScriptOptionsInputOutputModel() throws IOException, ApexModelException {
        final File tempLogFileIn = File.createTempFile("ShellPolicyModelIn", ".log");
        final File tempLogFileOut = File.createTempFile("ShellPolicyModelOut", ".log");
        final File tempModelFileIn = File.createTempFile("ShellPolicyModelIn", ".json");
        final File tempModelFileOut = File.createTempFile("ShellPolicyModelOut", ".json");

        // Generate input model
        final String[] cliArgsIn = new String[] {"-c", "src/main/resources/examples/scripts/ShellPolicyModel.apex",
            "-l", tempLogFileIn.getAbsolutePath(), "-o", tempModelFileIn.getAbsolutePath()};

        final ApexCommandLineEditorMain cliEditorIn = new ApexCommandLineEditorMain(cliArgsIn);
        assertEquals(0, cliEditorIn.getErrorCount());

        // Get the model and log into strings
        final String tempLogFileInString = TextFileUtils.getTextFileAsString(tempLogFileIn.getCanonicalPath());
        final String tempModelFileInString = TextFileUtils.getTextFileAsString(tempModelFileIn.getCanonicalPath());

        // As a sanity check, count the number of non white space characters in log and model files
        final int tempLogFileInCharCount = tempLogFileInString.replaceAll("\\s+", "").length();
        final int tempModelFileInCharCount = tempModelFileInString.replaceAll("\\s+", "").length();

        assertEquals(1204, tempLogFileInCharCount);
        assertEquals(2924, tempModelFileInCharCount);

        final String[] cliArgsOut = new String[] {"-i", tempModelFileIn.getAbsolutePath(), "-c",
            "src/main/resources/examples/scripts/ShellPolicyModelAddSchema.apex", "-l",
            tempLogFileOut.getAbsolutePath(), "-o", tempModelFileOut.getAbsolutePath()};

        final ApexCommandLineEditorMain cliEditorOut = new ApexCommandLineEditorMain(cliArgsOut);
        assertEquals(0, cliEditorOut.getErrorCount());

        // Get the model and log into strings
        final String tempLogFileOutString = TextFileUtils.getTextFileAsString(tempLogFileOut.getCanonicalPath());
        final String tempModelFileOutString = TextFileUtils.getTextFileAsString(tempModelFileOut.getCanonicalPath());

        // As a sanity check, count the number of non white space characters in log and model files
        final int tempLogFileOutCharCount = tempLogFileOutString.replaceAll("\\s+", "").length();
        final int tempModelFileOutCharCount = tempModelFileOutString.replaceAll("\\s+", "").length();

        assertEquals(1154, tempLogFileOutCharCount);
        assertEquals(3356, tempModelFileOutCharCount);

        tempLogFileIn.delete();
        tempModelFileIn.delete();
        tempLogFileOut.delete();
        tempModelFileOut.delete();
    }
}