9327748b88e86f715a57e48783d86a25a0efa258
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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         new File("target/" + testName + "_out.properties").delete();
114
115         // @formatter:off
116         final String[] args = {
117             "-rfr",
118             "target",
119             "-c",
120             "src/test/resources/testdata/executionproperties/" + testName + "_conf.json"
121         };
122         // @formatter:on
123         final ApexMain apexMain = new ApexMain(args);
124
125         // TODO: Set back to 10 seconds
126         await().atMost(10000, TimeUnit.SECONDS)
127                 .until(() -> new File("target/" + testName + "_out.properties").exists());
128
129         apexMain.shutdown();
130
131         Properties expectedProperties = new Properties();
132         expectedProperties.load(new FileInputStream(
133                 new File("src/test/resources/testdata/executionproperties/" + testName + "_out_expected.properties")));
134
135         Properties actualProperties = new Properties();
136         actualProperties.load(new FileInputStream(new File("target/" + testName + "_out.properties")));
137
138         assertEquals(expectedProperties, actualProperties);
139     }
140 }