2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020, 2024 Nordix Foundation.
5 * Modifications Copyright (C) 2020 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.testsuites.integration.uservice.context;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import java.io.IOException;
31 import java.nio.file.Path;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.io.TempDir;
35 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
36 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.common.utils.resources.TextFileUtils;
42 class EventAlbumContextTest {
43 private File tempCommandFile;
44 private File tempLogFile;
45 private File tempPolicyFile;
46 private String eventContextString;
47 private String configFile;
48 private String outputFile;
49 private String compareFile;
55 * Clear relative file root environment variable.
58 void clearRelativeFileRoot() {
59 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
63 void testJavaEventAlbumContextTest() throws IOException, ApexException {
64 tempCommandFile = tempTestDir.resolve("TestPolicyJavaEventContext.apex").toFile();
65 tempLogFile = tempTestDir.resolve("TestPolicyJavaEventContext.log").toFile();
66 tempPolicyFile = tempTestDir.resolve("TestPolicyJavaEventContext.json").toFile();
68 eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
70 configFile = "src/test/resources/prodcons/Context_JavaEventAlbum_file2file.json";
71 outputFile = "target/Context_JavaEventAlbum_EventOut.json";
72 compareFile = "src/test/resources/events/Context_JavaEventAlbum_EventOutCompare.json";
74 testEventAlbumContextTest();
78 void testAvroEventAlbumContextTest() throws IOException, ApexException {
79 tempCommandFile = tempTestDir.resolve("TestPolicyAvroEventContext.apex").toFile();
80 tempLogFile = tempTestDir.resolve("TestPolicyAvroEventContext.log").toFile();
81 tempPolicyFile = tempTestDir.resolve("TestPolicyAvroEventContext.json").toFile();
83 eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
85 configFile = "src/test/resources/prodcons/Context_AvroEventAlbum_file2file.json";
86 outputFile = "target/Context_AvroEventAlbum_EventOut.json";
87 compareFile = "src/test/resources/events/Context_AvroEventAlbum_EventOutCompare.json";
89 testEventAlbumContextTest();
92 private void testEventAlbumContextTest() throws IOException, ApexException {
93 TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
95 final String[] cliArgs = new String[] {"-c", tempCommandFile.getCanonicalPath(), "-l",
96 tempLogFile.getAbsolutePath(), "-ac", configFile, "-t", "src/test/resources/tosca/ToscaTemplate.json",
97 "-ot", tempPolicyFile.getAbsolutePath()};
99 new ApexCliToscaEditorMain(cliArgs);
101 final String[] args = new String[] {"-p", tempPolicyFile.getAbsolutePath()};
102 final ApexMain apexMain = new ApexMain(args);
104 // The output event will be in this file
105 final File outputEventFile = new File(outputFile);
106 String receivedApexOutputString = "";
107 for (int tenthsOfSecondsToWait = 100; tenthsOfSecondsToWait > 0; tenthsOfSecondsToWait--) {
108 if (outputEventFile.exists() && outputEventFile.length() > 0) {
109 // The output event is in this file
110 receivedApexOutputString =
111 TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath()).replaceAll("\\s+", "");
115 ThreadUtilities.sleep(100);
121 assertTrue(outputEventFile.exists(), "Test failed, the output event file was not created");
122 assertTrue(outputEventFile.delete());
124 assertFalse(receivedApexOutputString.isEmpty(), "Test failed, the output event file was empty");
126 // We compare the output to what we expect to get
127 final String expectedFileContent = TextFileUtils.getTextFileAsString(compareFile);
128 final String outputEventCompareString = expectedFileContent.replaceAll("\\s+", "");
130 assertEquals(outputEventCompareString, receivedApexOutputString);