Changes for checkstyle 8.32
[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 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
35 import org.onap.policy.apex.service.engine.main.ApexMain;
36
37 /**
38  * This class runs integration tests for execution properties.
39  */
40 public class TestExecutionProperties {
41     /**
42      * Compile the policy.
43      */
44     @BeforeClass
45     public static void compilePolicy() {
46         // @formatter:off
47         final String[] cliArgs = {
48             "-c",
49             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesTestPolicyModel.apex",
50             "-l",
51             "target/ExecutionPropertiesTestPolicyModel.log",
52             "-o",
53             "target/ExecutionPropertiesTestPolicyModel.json"
54         };
55         // @formatter:on
56
57         new ApexCommandLineEditorMain(cliArgs);
58     }
59
60     /**
61      * Clear relative file root environment variable.
62      */
63     @Before
64     public void clearRelativeFileRoot() {
65         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
66     }
67
68     /**
69      * Test read only execution properties are returned from policy.
70      */
71     @Test
72     public void testReadOnly() throws Exception {
73         testExecutionProperties("readOnly");
74     }
75
76     /**
77      * Test where execution properties set in task.
78      */
79     @Test
80     public void testEmptyToDefined() throws Exception {
81         testExecutionProperties("emptyToDefined");
82     }
83
84     /**
85      * Test where execution properties cleared in task.
86      */
87     @Test
88     public void testDefinedToEmpty() throws Exception {
89         testExecutionProperties("definedToEmpty");
90     }
91
92     /**
93      * Test where an execution properties added in task.
94      */
95     @Test
96     public void testAddProperty() throws Exception {
97         testExecutionProperties("addProperty");
98     }
99
100     /**
101      * Test empty properties are transferred correctly.
102      */
103     @Test
104     public void testEmptyToEmpty() throws Exception {
105         testExecutionProperties("emptyToEmpty");
106     }
107
108     private void testExecutionProperties(final String testName) throws Exception {
109         File compiledPolicyFile = new File("target/ExecutionPropertiesTestPolicyModel.json");
110         assertTrue(compiledPolicyFile.exists());
111
112         File outFile = new File("target/" + testName + "_out.properties");
113         outFile.deleteOnExit();
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         await().atMost(1, TimeUnit.SECONDS).until(() -> apexMain.isAlive());
126         await().atMost(10, TimeUnit.SECONDS).until(() -> outFile.exists());
127         await().atMost(1, TimeUnit.SECONDS).until(() -> outFile.length() > 0);
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(outFile));
137
138         assertEquals(expectedProperties, actualProperties);
139     }
140 }