d5d288d047052325d25cfe5952189953842cf752
[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  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.auth.clieditor;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.TemporaryFolder;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
36 import org.onap.policy.common.utils.resources.TextFileUtils;
37
38 /**
39  * The Class TestCLIEditorEventsContext.
40  */
41 public class CommandLineEditorEventsContextTest {
42     // CHECKSTYLE:OFF: MagicNumber
43
44     private static final Path SRC_MAIN_FOLDER = Paths.get("src/main/resources/");
45     private static final Path SRC_TEST_FOLDER = Paths.get("src/test/resources/");
46
47     private static final Path SUB_FOLDER = SRC_MAIN_FOLDER.resolve("examples/scripts/");
48
49     private static final String SPACES = "\\s+";
50     private static final String EMPTY_STRING = "";
51
52     private static final Path APEX_AVRO_POLICY_FILE = SUB_FOLDER.resolve("TestPolicyAvroEventContext.apex");
53     private static final Path APEX_JAVA_POLICY_FILE = SUB_FOLDER.resolve("TestPolicyJavaEventContext.apex");
54
55     private static final String FILE_NAME = "TestPolicyJavaEventsAndContext";
56     private static final String JSON_FILE = FILE_NAME + ".json";
57     private static final String LOG_FILE = FILE_NAME + ".log";
58
59     @Rule
60     public TemporaryFolder temporaryFolder = new TemporaryFolder();
61
62     /**
63      * Test java context model.
64      *
65      * @throws IOException Signals that an I/O exception has occurred.
66      * @throws ApexModelException if an Apex error happens
67      */
68     @Test
69     public void testJavaContextModel() throws IOException, ApexModelException {
70
71         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
72         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
73
74         final String[] cliArgs = new String[] {"-c", APEX_JAVA_POLICY_FILE.toString(), "-l",
75             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
76
77         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
78         assertEquals(0, cliEditor.getErrorCount());
79
80         // Get the model and log into strings
81         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
82         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
83
84         // As a sanity check, count the number of non white space characters in log and model files
85         final int logCharCount = logString.replaceAll(SPACES, EMPTY_STRING).length();
86         final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length();
87
88         assertThat(logCharCount).isGreaterThan(20000);
89         assertThat(modelCharCount).isGreaterThan(30000);
90     }
91
92     /**
93      * Test avro context model.
94      *
95      * @throws IOException Signals that an I/O exception has occurred.
96      * @throws ApexModelException if an Apex error happens
97      */
98     @Test
99     public void testAvroContextModel() throws IOException, ApexModelException {
100
101         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
102         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
103
104         final String[] cliArgs = new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l",
105             tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
106
107         final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
108         assertEquals(0, cliEditor.getErrorCount());
109
110         // Get the model and log into strings
111         final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
112         final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
113
114         // As a sanity check, count the number of non white space characters in log and model files
115         final int logCharCount = logString.replaceAll(SPACES, EMPTY_STRING).length();
116         final int modelCharCount = modelString.replaceAll(SPACES, EMPTY_STRING).length();
117
118         assertThat(logCharCount).isGreaterThan(20000);
119         assertThat(modelCharCount).isGreaterThan(30000);
120
121     }
122
123     @Test
124     public void test_emptyMetadataCommandFileWithEmptyJsonTag_errorcountGreaterThanOne() throws IOException {
125
126         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
127         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
128
129         final String modelFile = SRC_TEST_FOLDER.resolve("model").resolve("empty_commands.json").toString();
130         final String apexPropertiesLocation =
131                 SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString();
132
133         final String[] cliArgs =
134                 new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
135                     tempModelFile.getAbsolutePath(), "-m", modelFile, "-a", apexPropertiesLocation};
136
137         final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
138         assertEquals(1, objUnderTest.getErrorCount());
139
140     }
141
142     @Test
143     public void test_emptyMetadataCommandFile_errorcountGreaterThanOne() throws IOException {
144
145         final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
146         final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
147
148         final File modelFile = temporaryFolder.newFile("empty_commands.json");
149
150         final String apexPropertiesLocation =
151                 SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString();
152
153         final String[] cliArgs =
154                 new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
155                     tempModelFile.getAbsolutePath(), "-m", modelFile.getAbsolutePath(), "-a", apexPropertiesLocation};
156
157         final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
158         assertEquals(1, objUnderTest.getErrorCount());
159
160     }
161
162 }