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