8cbc1698fabb6363a38202ad023cf6a7b0fa0f28
[policy/apex-pdp.git] / auth / cli-editor / src / test / java / org / onap / policy / apex / auth / clieditor / CommandLineEditorEventsContextTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 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
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.TemporaryFolder;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
35 import org.onap.policy.common.utils.resources.TextFileUtils;
36
37 /**
38  * The Class TestCLIEditorEventsContext.
39  */
40 public class CommandLineEditorEventsContextTest {
41     // CHECKSTYLE:OFF: MagicNumber
42
43     private static final Path SRC_MAIN_FOLDER = Paths.get("src/main/resources/");
44     private static final Path SRC_TEST_FOLDER = Paths.get("src/test/resources/");
45
46     private static final Path SUB_FOLDER = SRC_MAIN_FOLDER.resolve("examples/scripts/");
47
48     private static final String SPACES = "\\s+";
49     private static final String EMPTY_STRING = "";
50
51     private static final Path APEX_AVRO_POLICY_FILE = SUB_FOLDER.resolve("TestPolicyAvroEventContext.apex");
52     private static final Path APEX_JAVA_POLICY_FILE = SUB_FOLDER.resolve("TestPolicyJavaEventContext.apex");
53
54     private static final String FILE_NAME = "TestPolicyJavaEventsAndContext";
55     private static final String JSON_FILE = FILE_NAME + ".json";
56     private static final String LOG_FILE = FILE_NAME + ".log";
57
58     @Rule
59     public TemporaryFolder temporaryFolder = new TemporaryFolder();
60
61     /**
62      * Test java context model.
63      *
64      * @throws IOException Signals that an I/O exception has occurred.
65      * @throws ApexModelException if an Apex error happens
66      */
67     @Test
68     public void testJavaContextModel() throws IOException, ApexModelException {
69
70         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
71         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
72
73         final String[] cliArgs = new String[] {"-c", APEX_JAVA_POLICY_FILE.toString(), "-l",
74             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
75
76         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
77         assertEquals(0, cliEditor.getErrorCount());
78
79         // Get the model and log into strings
80         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
81         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
82
83         // As a sanity check, count the number of non white space characters in log and model files
84         final int logCharCount = logString.replaceAll(SPACES, EMPTY_STRING).length();
85         final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length();
86
87         assertEquals(25892, logCharCount);
88         assertEquals(46048, modelCharCount);
89     }
90
91     /**
92      * Test avro context model.
93      *
94      * @throws IOException Signals that an I/O exception has occurred.
95      * @throws ApexModelException if an Apex error happens
96      */
97     @Test
98     public void testAvroContextModel() throws IOException, ApexModelException {
99
100         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
101         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
102
103         final String[] cliArgs = new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l",
104             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
105
106         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
107         assertEquals(0, cliEditor.getErrorCount());
108
109         // Get the model and log into strings
110         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
111         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
112
113         // As a sanity check, count the number of non white space characters in log and model files
114         final int logCharCount = logString.replaceAll(SPACES, EMPTY_STRING).length();
115         final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length();
116
117         assertEquals(30068, logCharCount);
118         assertEquals(52596, modelCharCount);
119
120     }
121
122     @Test
123     public void test_emptyMetadataCommandFileWithEmptyJsonTag_errorcountGreaterThanOne() throws IOException {
124
125         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
126         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
127
128         final String modelFile = SRC_TEST_FOLDER.resolve("model").resolve("empty_commands.json").toString();
129         final String apexPropertiesLocation =
130                 SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString();
131
132         final String[] cliArgs =
133                 new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
134                     tempModelFile.getAbsolutePath(), "-m", modelFile, "-a", apexPropertiesLocation};
135
136         final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
137         assertEquals(1, objUnderTest.getErrorCount());
138
139     }
140
141     @Test
142     public void test_emptyMetadataCommandFile_errorcountGreaterThanOne() throws IOException {
143
144         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
145         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
146
147         final File modelFile = temporaryFolder.newFile("empty_commands.json");
148
149         final String apexPropertiesLocation =
150                 SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString();
151
152         final String[] cliArgs =
153                 new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
154                     tempModelFile.getAbsolutePath(), "-m", modelFile.getAbsolutePath(), "-a", apexPropertiesLocation};
155
156         final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
157         assertEquals(1, objUnderTest.getErrorCount());
158
159     }
160
161 }