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