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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.auth.clieditor;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
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;
39 * The Class TestCLIEditorEventsContext.
41 public class CommandLineEditorEventsContextTest {
42 // CHECKSTYLE:OFF: MagicNumber
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/");
47 private static final Path SUB_FOLDER = SRC_MAIN_FOLDER.resolve("examples/scripts/");
49 private static final String SPACES = "\\s+";
50 private static final String EMPTY_STRING = "";
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");
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";
60 public TemporaryFolder temporaryFolder = new TemporaryFolder();
63 * Test java context model.
65 * @throws IOException Signals that an I/O exception has occurred.
66 * @throws ApexModelException if an Apex error happens
69 public void testJavaContextModel() throws IOException, ApexModelException {
71 final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
72 final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
74 final String[] cliArgs = new String[] {"-c", APEX_JAVA_POLICY_FILE.toString(), "-l",
75 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
77 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
78 assertEquals(0, cliEditor.getErrorCount());
80 // Get the model and log into strings
81 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
82 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
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();
88 assertThat(logCharCount).isGreaterThan(20000);
89 assertThat(modelCharCount).isGreaterThan(30000);
93 * Test avro context model.
95 * @throws IOException Signals that an I/O exception has occurred.
96 * @throws ApexModelException if an Apex error happens
99 public void testAvroContextModel() throws IOException, ApexModelException {
101 final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
102 final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
104 final String[] cliArgs = new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l",
105 tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath()};
107 final ApexCommandLineEditorMain cliEditor = new ApexCommandLineEditorMain(cliArgs);
108 assertEquals(0, cliEditor.getErrorCount());
110 // Get the model and log into strings
111 final String logString = TextFileUtils.getTextFileAsString(tempLogFile.getCanonicalPath());
112 final String modelString = TextFileUtils.getTextFileAsString(tempModelFile.getCanonicalPath());
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();
118 assertThat(logCharCount).isGreaterThan(20000);
119 assertThat(modelCharCount).isGreaterThan(30000);
124 public void test_emptyMetadataCommandFileWithEmptyJsonTag_errorcountGreaterThanOne() throws IOException {
126 final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
127 final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
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();
133 final String[] cliArgs =
134 new String[] {"-c", APEX_AVRO_POLICY_FILE.toString(), "-l", tempLogFile.getAbsolutePath(), "-o",
135 tempModelFile.getAbsolutePath(), "-m", modelFile, "-a", apexPropertiesLocation};
137 final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
138 assertEquals(1, objUnderTest.getErrorCount());
143 public void test_emptyMetadataCommandFile_errorcountGreaterThanOne() throws IOException {
145 final File tempLogFile = temporaryFolder.newFile(LOG_FILE);
146 final File tempModelFile = temporaryFolder.newFile(JSON_FILE);
148 final File modelFile = temporaryFolder.newFile("empty_commands.json");
150 final String apexPropertiesLocation =
151 SRC_MAIN_FOLDER.resolve("etc/editor").resolve("ApexModelProperties.json").toString();
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};
157 final ApexCommandLineEditorMain objUnderTest = new ApexCommandLineEditorMain(cliArgs);
158 assertEquals(1, objUnderTest.getErrorCount());