4b05c525c173ccbd6fc0f00a8e7eb4a4beeae2d7
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / main / ApexMainTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
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.service.engine.main;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.OutputStream;
30 import java.io.PrintStream;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.concurrent.TimeUnit;
34
35 import org.junit.After;
36 import org.junit.Test;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.service.parameters.ApexParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
40
41 /**
42  * Test the ApexMain class.
43  */
44 public class ApexMainTest {
45     private PrintStream stdout = System.out;
46
47     /**
48      * Method for cleanup after each test.
49      *
50      * @throws Exception if an error occurs
51      */
52     @After
53     public void teardown() throws Exception {
54         System.setOut(stdout);
55     }
56
57     @Test
58     public void testNullParameters() throws ApexException {
59         OutputStream outContent = new ByteArrayOutputStream();
60         System.setOut(new PrintStream(outContent));
61
62         ApexMain.main(null);
63         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
64                 .contains("Apex configuration file was not specified as an argument"));
65     }
66
67     @Test
68     public void testBadArguments() throws ApexException {
69         OutputStream outContent = new ByteArrayOutputStream();
70         System.setOut(new PrintStream(outContent));
71
72         String[] args = { "-whee" };
73
74         final ApexMain apexMain = new ApexMain(args);
75         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
76                 .contains("invalid command line arguments specified : Unrecognized option: -whee"));
77         apexMain.shutdown();
78     }
79
80     @Test
81     public void testHelp() throws ApexException {
82         OutputStream outContent = new ByteArrayOutputStream();
83         System.setOut(new PrintStream(outContent));
84
85         String[] args = { "-h" };
86
87         final ApexMain apexMain = new ApexMain(args);
88         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
89                 .contains("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
90         apexMain.shutdown();
91     }
92
93     @Test
94     public void testBadParameters() throws ApexException {
95         OutputStream outContent = new ByteArrayOutputStream();
96         System.setOut(new PrintStream(outContent));
97
98         String[] args = { "-c", "src/test/resources/parameters/badParams.json" };
99
100         final ApexMain apexMain = new ApexMain(args);
101         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
102                 .contains("parameter group has status INVALID"));
103         apexMain.shutdown();
104     }
105
106     @Test
107     public void testCorrectParameters() throws ApexException {
108         OutputStream outContent = new ByteArrayOutputStream();
109         System.setOut(new PrintStream(outContent));
110
111         String[] args = { "-c", "src/test/resources/parameters/correctParams.json" };
112
113         final ApexMain apexMain = new ApexMain(args);
114         assertEquals("MyApexEngine",
115             apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
116         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
117                 .contains("Added the action listener to the engine"));
118         apexMain.shutdown();
119     }
120
121     @Test
122     public void testJavaProperties() throws ApexException {
123         OutputStream outContent = new ByteArrayOutputStream();
124         System.setOut(new PrintStream(outContent));
125
126         String[] args = { "-c", "src/test/resources/parameters/correctParamsJavaProperties.json" };
127
128         final ApexMain apexMain = new ApexMain(args);
129         assertEquals("MyApexEngine",
130             apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
131
132         assertEquals("trust-store-file", System.getProperty("javax.net.ssl.trustStore"));
133         assertEquals("Pol1cy_0nap", System.getProperty("javax.net.ssl.trustStorePassword"));
134         await().atMost(10000, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
135                 .contains("Added the action listener to the engine"));
136         apexMain.shutdown();
137     }
138
139     @Test
140     public void testCorrectParametersWithMultiplePolicies() throws ApexException {
141         OutputStream outContent = new ByteArrayOutputStream();
142         System.setOut(new PrintStream(outContent));
143         Map<ToscaPolicyIdentifier, String[]> argsMap = new HashMap<ToscaPolicyIdentifier, String[]>();
144         String[] args = {"-c", "src/test/resources/parameters/correctParams.json", "-m",
145             "src/test/resources/policymodels/SmallModel.json"};
146         argsMap.put(new ToscaPolicyIdentifier("id1", "v1"), args);
147         final ApexMain apexMain = new ApexMain(argsMap);
148         ApexParameters apexParam = (ApexParameters) apexMain.getApexParametersMap().values().toArray()[0];
149         assertEquals("MyApexEngine", apexParam.getEngineServiceParameters().getName());
150         apexMain.shutdown();
151         final String outString = outContent.toString();
152         assertTrue(outString.contains("Added the action listener to the engine"));
153     }
154
155     @Test
156     public void testInCorrectParametersWithMultiplePolicies() throws ApexException {
157         OutputStream outContent = new ByteArrayOutputStream();
158         System.setOut(new PrintStream(outContent));
159         Map<ToscaPolicyIdentifier, String[]> argsMap = new HashMap<ToscaPolicyIdentifier, String[]>();
160         String[] args = {"-c", "src/test/resources/parameters/correctParams.json", "-m",
161             "src/test/resources/policymodels/SmallModel.json"};
162         argsMap.put(new ToscaPolicyIdentifier("id1", "v1"), args);
163         argsMap.put(new ToscaPolicyIdentifier("id2", "v2"), args);
164         final ApexMain apexMain = new ApexMain(argsMap);
165         ApexParameters apexParam = (ApexParameters) apexMain.getApexParametersMap().values().toArray()[0];
166         assertEquals("MyApexEngine", apexParam.getEngineServiceParameters().getName());
167         apexMain.shutdown();
168         final String outString = outContent.toString();
169         assertTrue(outString.contains("I/O Parameters for id2:v2 has duplicates. So this policy is not executed"));
170         assertTrue(apexMain.getApexParametersMap().size() == 1); // only id1:v1 is kept in the map, id2:v2 failed
171     }
172
173     @Test
174     public void testInvalidArgsWithMultiplePolicies() throws ApexException {
175         OutputStream outContent = new ByteArrayOutputStream();
176         System.setOut(new PrintStream(outContent));
177         Map<ToscaPolicyIdentifier, String[]> argsMap = new HashMap<ToscaPolicyIdentifier, String[]>();
178         String[] args = {"-c", "file1", "-m", "file2"};
179         argsMap.put(new ToscaPolicyIdentifier("id1", "v1"), args);
180         final ApexMain apexMain = new ApexMain(argsMap);
181         final String outString = outContent.toString();
182         apexMain.shutdown();
183         assertTrue(
184             outString.contains("Arguments validation failed") && outString.contains("start of Apex service failed"));
185         assertTrue(apexMain.getApexParametersMap().isEmpty()); // No policy is running in the engine
186     }
187 }