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