APEX standalone support for ToscaPolicy format
[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  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.service.engine.main;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.OutputStream;
32 import java.io.PrintStream;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.After;
37 import org.junit.Test;
38 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.service.parameters.ApexParameters;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
41
42 /**
43  * Test the ApexMain class.
44  */
45 public class ApexMainTest {
46     private PrintStream stdout = System.out;
47
48     /**
49      * Method for cleanup after each test.
50      *
51      * @throws Exception if an error occurs
52      */
53     @After
54     public void teardown() throws Exception {
55         System.setOut(stdout);
56     }
57
58     @Test
59     public void testNullParameters() throws ApexException {
60         OutputStream outContent = new ByteArrayOutputStream();
61         System.setOut(new PrintStream(outContent));
62
63         ApexMain.main(null);
64         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
65                 .contains("Tosca Policy file was not specified as an argument"));
66         assertThat(outContent.toString())
67             .contains("Tosca Policy file was not specified as an argument");
68     }
69
70     @Test
71     public void testBadArguments() throws ApexException {
72         OutputStream outContent = new ByteArrayOutputStream();
73         System.setOut(new PrintStream(outContent));
74
75         String[] args = { "-whee" };
76
77         final ApexMain apexMain = new ApexMain(args);
78         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
79                 .contains("invalid command line arguments specified : Unrecognized option: -whee"));
80         assertNotNull(apexMain);
81         apexMain.shutdown();
82     }
83
84     @Test
85     public void testHelp() throws ApexException {
86         OutputStream outContent = new ByteArrayOutputStream();
87         System.setOut(new PrintStream(outContent));
88
89         String[] args = { "-h" };
90
91         final ApexMain apexMain = new ApexMain(args);
92         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
93                 .contains("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
94         assertNotNull(apexMain);
95         apexMain.shutdown();
96     }
97
98     @Test
99     public void testBadParameters() throws ApexException {
100         OutputStream outContent = new ByteArrayOutputStream();
101         System.setOut(new PrintStream(outContent));
102
103         String[] args = { "-p", "src/test/resources/parameters/badParams.json" };
104
105         final ApexMain apexMain = new ApexMain(args);
106         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
107                 .contains("parameter group has status INVALID"));
108         assertNotNull(apexMain);
109         apexMain.shutdown();
110     }
111
112     @Test
113     public void testCorrectParameters() throws ApexException {
114         OutputStream outContent = new ByteArrayOutputStream();
115         System.setOut(new PrintStream(outContent));
116
117         String[] args = { "-p", "src/test/resources/parameters/correctParams.json" };
118
119         final ApexMain apexMain = new ApexMain(args);
120         assertEquals("MyApexEngine",
121             apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
122         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
123                 .contains("Added the action listener to the engine"));
124         apexMain.shutdown();
125     }
126
127     @Test
128     public void testJavaProperties() throws ApexException {
129         OutputStream outContent = new ByteArrayOutputStream();
130         System.setOut(new PrintStream(outContent));
131
132         String[] args = { "-p", "src/test/resources/parameters/correctParamsJavaProperties.json" };
133
134         final ApexMain apexMain = new ApexMain(args);
135         assertEquals("MyApexEngine",
136             apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
137
138         assertEquals("trust-store-file", System.getProperty("javax.net.ssl.trustStore"));
139         assertEquals("Pol1cy_0nap", System.getProperty("javax.net.ssl.trustStorePassword"));
140         await().atMost(10000, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
141                 .contains("Added the action listener to the engine"));
142         apexMain.shutdown();
143     }
144
145     @Test
146     public void testCorrectParametersWithMultiplePolicies() throws ApexException {
147         OutputStream outContent = new ByteArrayOutputStream();
148         System.setOut(new PrintStream(outContent));
149         Map<ToscaPolicyIdentifier, String[]> argsMap = new HashMap<ToscaPolicyIdentifier, String[]>();
150         String[] args = {"-p", "src/test/resources/parameters/correctParams.json"};
151         argsMap.put(new ToscaPolicyIdentifier("id1", "v1"), args);
152         final ApexMain apexMain = new ApexMain(argsMap);
153         ApexParameters apexParam = (ApexParameters) apexMain.getApexParametersMap().values().toArray()[0];
154         assertEquals("MyApexEngine", apexParam.getEngineServiceParameters().getName());
155         apexMain.shutdown();
156         final String outString = outContent.toString();
157         assertThat(outString).contains("Added the action listener to the engine");
158     }
159
160     @Test
161     public void testInCorrectParametersWithMultiplePolicies() throws ApexException {
162         OutputStream outContent = new ByteArrayOutputStream();
163         System.setOut(new PrintStream(outContent));
164         Map<ToscaPolicyIdentifier, String[]> argsMap = new HashMap<ToscaPolicyIdentifier, String[]>();
165         String[] args = {"-p", "src/test/resources/parameters/correctParams.json"};
166         argsMap.put(new ToscaPolicyIdentifier("id1", "v1"), args);
167         argsMap.put(new ToscaPolicyIdentifier("id2", "v2"), args);
168         final ApexMain apexMain = new ApexMain(argsMap);
169         ApexParameters apexParam = (ApexParameters) apexMain.getApexParametersMap().values().toArray()[0];
170         assertEquals("MyApexEngine", apexParam.getEngineServiceParameters().getName());
171         apexMain.shutdown();
172         final String outString = outContent.toString();
173         assertThat(outString).contains("I/O Parameters [TheFileConsumer1]/[FirstProducer] for id2:v2 are duplicates. "
174             + "So this policy is not executed");
175         assertEquals(1, apexMain.getApexParametersMap().size()); // only id1:v1 is kept in the map, id2:v2 failed
176     }
177
178     @Test
179     public void testInvalidArgsWithMultiplePolicies() throws ApexException {
180         OutputStream outContent = new ByteArrayOutputStream();
181         System.setOut(new PrintStream(outContent));
182         Map<ToscaPolicyIdentifier, String[]> argsMap = new HashMap<ToscaPolicyIdentifier, String[]>();
183         String[] args = {"-c", "file1", "-m", "file2"};
184         argsMap.put(new ToscaPolicyIdentifier("id1", "v1"), args);
185         final ApexMain apexMain = new ApexMain(argsMap);
186         final String outString = outContent.toString();
187         apexMain.shutdown();
188         assertThat(outString).contains("Arguments validation failed", "start of Apex service failed");
189         assertThat(apexMain.getApexParametersMap()).isEmpty(); // No policy is running in the engine
190     }
191 }