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 / executionproperties / TestExecutionProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.executionproperties;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.util.Properties;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
34 import org.onap.policy.apex.service.engine.main.ApexMain;
35
36 /**
37  * This class runs integration tests for execution properties.
38  */
39 public class TestExecutionProperties {
40
41     /**
42      * Clear relative file root environment variable.
43      */
44     @Before
45     public void clearRelativeFileRoot() {
46         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
47     }
48
49     /**
50      * Test read only execution properties are returned from policy.
51      */
52     @Test
53     public void testReadOnly() throws Exception {
54         testExecutionProperties("readOnly");
55     }
56
57     /**
58      * Test where execution properties set in task.
59      */
60     @Test
61     public void testEmptyToDefined() throws Exception {
62         testExecutionProperties("emptyToDefined");
63     }
64
65     /**
66      * Test where execution properties cleared in task.
67      */
68     @Test
69     public void testDefinedToEmpty() throws Exception {
70         testExecutionProperties("definedToEmpty");
71     }
72
73     /**
74      * Test where an execution properties added in task.
75      */
76     @Test
77     public void testAddProperty() throws Exception {
78         testExecutionProperties("addProperty");
79     }
80
81     /**
82      * Test empty properties are transferred correctly.
83      */
84     @Test
85     public void testEmptyToEmpty() throws Exception {
86         testExecutionProperties("emptyToEmpty");
87     }
88
89     private void testExecutionProperties(final String testName) throws Exception {
90         // @formatter:off
91         final String[] cliArgs = new String[] {
92             "-c",
93             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesTestPolicyModel.apex",
94             "-l",
95             "target/ExecutionPropertiesTestPolicyModel.log",
96             "-ac",
97             "src/test/resources/testdata/executionproperties/" + testName + "_conf.json",
98             "-t",
99             "src/test/resources/tosca/ToscaTemplate.json",
100             "-ot",
101             "target/classes/APEXPolicy.json"
102         };
103         // @formatter:on
104
105         new ApexCliToscaEditorMain(cliArgs);
106
107         File outFile = new File("target/" + testName + "_out.properties");
108         outFile.deleteOnExit();
109
110         // @formatter:off
111         final String[] args = {
112             "-rfr",
113             "target/classes",
114             "-p",
115             "target/classes/APEXPolicy.json"
116         };
117         // @formatter:on
118         final ApexMain apexMain = new ApexMain(args);
119
120         await().atMost(1, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
121         await().atMost(10, TimeUnit.SECONDS).until(() -> outFile.exists());
122         await().atMost(1, TimeUnit.SECONDS).until(() -> outFile.length() > 0);
123
124         apexMain.shutdown();
125
126         Properties expectedProperties = new Properties();
127         expectedProperties.load(new FileInputStream(
128                 new File("src/test/resources/testdata/executionproperties/" + testName + "_out_expected.properties")));
129
130         Properties actualProperties = new Properties();
131         actualProperties.load(new FileInputStream(outFile));
132
133         assertEquals(expectedProperties, actualProperties);
134     }
135 }