efd12f4e4c14474f1d3113a7290feda4cc81e013
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.auth.clieditor;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
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.apex.model.utilities.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[]
73             { "-c", APEX_JAVA_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
74                             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(25962, logCharCount);
88         assertEquals(46189, 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[]
104             { "-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
105                             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         assertEquals(30407, logCharCount);
119         assertEquals(53022, modelCharCount);
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 = SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json")
131                         .toString();
132
133         final String[] cliArgs = new String[]
134             { "-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 = SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json")
151                         .toString();
152
153         final String[] cliArgs = new String[] {
154             "-c",
155             APEX_AVRO_POLICY_FILE.toString(),
156             "-l",
157             tempLogFile.getAbsolutePath(),
158             "-o",
159             tempModelFile.getAbsolutePath(),
160             "-m",
161             modelFile.getAbsolutePath(),
162             "-a",
163             apexPropertiesLocation
164         };
165
166         final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
167         assertEquals(1, objUnderTest.getErrorCount());
168
169     }
170
171 }