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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.main;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayOutputStream;
29 import java.io.OutputStream;
30 import java.io.PrintStream;
31 import java.util.HashMap;
33 import java.util.concurrent.TimeUnit;
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;
42 * Test the ApexMain class.
44 public class ApexMainTest {
45 private PrintStream stdout = System.out;
48 * Method for cleanup after each test.
50 * @throws Exception if an error occurs
53 public void teardown() throws Exception {
54 System.setOut(stdout);
58 public void testNullParameters() throws ApexException {
59 OutputStream outContent = new ByteArrayOutputStream();
60 System.setOut(new PrintStream(outContent));
63 await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
64 .contains("Apex configuration file was not specified as an argument"));
68 public void testBadArguments() throws ApexException {
69 OutputStream outContent = new ByteArrayOutputStream();
70 System.setOut(new PrintStream(outContent));
72 String[] args = { "-whee" };
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"));
81 public void testHelp() throws ApexException {
82 OutputStream outContent = new ByteArrayOutputStream();
83 System.setOut(new PrintStream(outContent));
85 String[] args = { "-h" };
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...]"));
94 public void testBadParameters() throws ApexException {
95 OutputStream outContent = new ByteArrayOutputStream();
96 System.setOut(new PrintStream(outContent));
98 String[] args = { "-c", "src/test/resources/parameters/badParams.json" };
100 final ApexMain apexMain = new ApexMain(args);
101 await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
102 .contains("parameter group has status INVALID"));
107 public void testCorrectParameters() throws ApexException {
108 OutputStream outContent = new ByteArrayOutputStream();
109 System.setOut(new PrintStream(outContent));
111 String[] args = { "-c", "src/test/resources/parameters/correctParams.json" };
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"));
122 public void testJavaProperties() throws ApexException {
123 OutputStream outContent = new ByteArrayOutputStream();
124 System.setOut(new PrintStream(outContent));
126 String[] args = { "-c", "src/test/resources/parameters/correctParamsJavaProperties.json" };
128 final ApexMain apexMain = new ApexMain(args);
129 assertEquals("MyApexEngine",
130 apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
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"));
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());
151 final String outString = outContent.toString();
152 assertTrue(outString.contains("Added the action listener to the engine"));
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());
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
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();
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