APEX standalone support for ToscaPolicy format
[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 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.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
29 import java.io.IOException;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.TemporaryFolder;
34 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.service.engine.main.ApexMain;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.common.utils.resources.TextFileUtils;
40
41 public class EventAlbumContextTest {
42     private File tempCommandFile;
43     private File tempLogFile;
44     private File tempPolicyFile;
45     private String eventContextString;
46     private String configFile;
47     private String outputFile;
48     private String compareFile;
49
50     @Rule
51     public TemporaryFolder tempTestDir = new TemporaryFolder();
52
53     /**
54      * Clear relative file root environment variable.
55      */
56     @Before
57     public void clearRelativeFileRoot() {
58         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
59     }
60
61     @Test
62     public void testJavaEventAlbumContextTest() throws IOException, ApexException {
63         tempCommandFile = tempTestDir.newFile("TestPolicyJavaEventContext.apex");
64         tempLogFile = tempTestDir.newFile("TestPolicyJavaEventContext.log");
65         tempPolicyFile = tempTestDir.newFile("TestPolicyJavaEventContext.json");
66
67         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyJavaEventContext.apex");
68
69         configFile = "src/test/resources/prodcons/Context_JavaEventAlbum_file2file.json";
70         outputFile = "target/Context_JavaEventAlbum_EventOut.json";
71         compareFile = "src/test/resources/events/Context_JavaEventAlbum_EventOutCompare.json";
72
73         testEventAlbumContextTest();
74     }
75
76     @Test
77     public void testAvroEventAlbumContextTest() throws IOException, ApexException {
78         tempCommandFile = tempTestDir.newFile("TestPolicyAvroEventContext.apex");
79         tempLogFile = tempTestDir.newFile("TestPolicyAvroEventContext.log");
80         tempPolicyFile = tempTestDir.newFile("TestPolicyAvroEventContext.json");
81
82         eventContextString = ResourceUtils.getResourceAsString("examples/scripts/TestPolicyAvroEventContext.apex");
83
84         configFile = "src/test/resources/prodcons/Context_AvroEventAlbum_file2file.json";
85         outputFile = "target/Context_AvroEventAlbum_EventOut.json";
86         compareFile = "src/test/resources/events/Context_AvroEventAlbum_EventOutCompare.json";
87
88         testEventAlbumContextTest();
89     }
90
91     private void testEventAlbumContextTest() throws IOException, ApexException {
92         TextFileUtils.putStringAsFile(eventContextString, tempCommandFile);
93
94         final String[] cliArgs = new String[] {"-c", tempCommandFile.getCanonicalPath(), "-l",
95             tempLogFile.getAbsolutePath(), "-ac", configFile, "-t", "src/test/resources/tosca/ToscaTemplate.json",
96             "-ot", tempPolicyFile.getAbsolutePath()};
97
98         new ApexCliToscaEditorMain(cliArgs);
99
100         final String[] args = new String[] {"-p", tempPolicyFile.getAbsolutePath()};
101         final ApexMain apexMain = new ApexMain(args);
102
103         // The output event will be in this file
104         final File outputEventFile = new File(outputFile);
105         String receivedApexOutputString = "";
106         for (int tenthsOfSecondsToWait = 100; tenthsOfSecondsToWait > 0; tenthsOfSecondsToWait--) {
107             if (outputEventFile.exists() && outputEventFile.length() > 0) {
108                 // The output event is in this file
109                 receivedApexOutputString =
110                         TextFileUtils.getTextFileAsString(outputEventFile.getCanonicalPath()).replaceAll("\\s+", "");
111                 break;
112             }
113
114             ThreadUtilities.sleep(100);
115         }
116
117         // Shut down Apex
118         apexMain.shutdown();
119
120         assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
121         outputEventFile.delete();
122
123         assertTrue("Test failed, the output event file was empty", receivedApexOutputString.length() > 0);
124
125         // We compare the output to what we expect to get
126         final String expectedFileContent = TextFileUtils.getTextFileAsString(compareFile);
127         final String outputEventCompareString = expectedFileContent.replaceAll("\\s+", "");
128
129         assertEquals(outputEventCompareString, receivedApexOutputString);
130     }
131
132 }