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;
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;
41 * Test the ApexMain class.
43 public class ApexMainTest {
44 private PrintStream stdout = System.out;
47 * Method for cleanup after each test.
49 * @throws Exception if an error occurs
52 public void teardown() throws Exception {
53 System.setOut(stdout);
57 public void testNullParameters() throws ApexException {
58 OutputStream outContent = new ByteArrayOutputStream();
59 System.setOut(new PrintStream(outContent));
62 await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
63 .contains("Apex configuration file was not specified as an argument"));
67 public void testBadArguments() throws ApexException {
68 OutputStream outContent = new ByteArrayOutputStream();
69 System.setOut(new PrintStream(outContent));
71 String[] args = { "-whee" };
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"));
80 public void testHelp() throws ApexException {
81 OutputStream outContent = new ByteArrayOutputStream();
82 System.setOut(new PrintStream(outContent));
84 String[] args = { "-h" };
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...]"));
93 public void testBadParameters() throws ApexException {
94 OutputStream outContent = new ByteArrayOutputStream();
95 System.setOut(new PrintStream(outContent));
97 String[] args = { "-c", "src/test/resources/parameters/badParams.json" };
99 final ApexMain apexMain = new ApexMain(args);
100 await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
101 .contains("parameter group has status INVALID"));
106 public void testCorrectParameters() throws ApexException {
107 OutputStream outContent = new ByteArrayOutputStream();
108 System.setOut(new PrintStream(outContent));
110 String[] args = { "-c", "src/test/resources/parameters/correctParams.json" };
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"));
121 public void testJavaProperties() throws ApexException {
122 OutputStream outContent = new ByteArrayOutputStream();
123 System.setOut(new PrintStream(outContent));
125 String[] args = { "-c", "src/test/resources/parameters/correctParamsJavaProperties.json" };
127 final ApexMain apexMain = new ApexMain(args);
128 assertEquals("MyApexEngine",
129 apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
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"));
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());
150 final String outString = outContent.toString();
151 assertTrue(outString.contains("Added the action listener to the engine"));
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());
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
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();
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