Convert junit4 to junit5
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / context / EventAlbumContextTest.java
1 /*-
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
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.testsuites.integration.uservice.context;
24
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;
28
29 import java.io.File;
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;
41
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;
50
51     @TempDir
52     Path tempTestDir;
53
54     /**
55      * Clear relative file root environment variable.
56      */
57     @BeforeEach
58     void clearRelativeFileRoot() {
59         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
60     }
61
62     @Test
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();
67
68         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
69
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";
73
74         testEventAlbumContextTest();
75     }
76
77     @Test
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();
82
83         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
84
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";
88
89         testEventAlbumContextTest();
90     }
91
92     private void testEventAlbumContextTest() throws IOException, ApexException {
93         TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
94
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()};
98
99         new ApexCliToscaEditorMain(cliArgs);
100
101         final String[] args = new String[] {"-p", tempPolicyFile.getAbsolutePath()};
102         final ApexMain apexMain = new ApexMain(args);
103
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+", "");
112                 break;
113             }
114
115             ThreadUtilities.sleep(100);
116         }
117
118         // Shut down Apex
119         apexMain.shutdown();
120
121         assertTrue(outputEventFile.exists(), "Test failed, the output event file was not created");
122         assertTrue(outputEventFile.delete());
123
124         assertFalse(receivedApexOutputString.isEmpty(), "Test failed, the output event file was empty");
125
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+", "");
129
130         assertEquals(outputEventCompareString, receivedApexOutputString);
131     }
132
133 }