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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.main;
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;
30 import java.io.ByteArrayOutputStream;
31 import java.io.OutputStream;
32 import java.io.PrintStream;
33 import java.util.HashMap;
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;
43 * Test the ApexMain class.
45 public class ApexMainTest {
46 private PrintStream stdout = System.out;
49 * Method for cleanup after each test.
51 * @throws Exception if an error occurs
54 public void teardown() throws Exception {
55 System.setOut(stdout);
59 public void testNullParameters() throws ApexException {
60 OutputStream outContent = new ByteArrayOutputStream();
61 System.setOut(new PrintStream(outContent));
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");
71 public void testBadArguments() throws ApexException {
72 OutputStream outContent = new ByteArrayOutputStream();
73 System.setOut(new PrintStream(outContent));
75 String[] args = { "-whee" };
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);
85 public void testHelp() throws ApexException {
86 OutputStream outContent = new ByteArrayOutputStream();
87 System.setOut(new PrintStream(outContent));
89 String[] args = { "-h" };
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);
99 public void testBadParameters() throws ApexException {
100 OutputStream outContent = new ByteArrayOutputStream();
101 System.setOut(new PrintStream(outContent));
103 String[] args = { "-p", "src/test/resources/parameters/badParams.json" };
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);
113 public void testCorrectParameters() throws ApexException {
114 OutputStream outContent = new ByteArrayOutputStream();
115 System.setOut(new PrintStream(outContent));
117 String[] args = { "-p", "src/test/resources/parameters/correctParams.json" };
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"));
128 public void testJavaProperties() throws ApexException {
129 OutputStream outContent = new ByteArrayOutputStream();
130 System.setOut(new PrintStream(outContent));
132 String[] args = { "-p", "src/test/resources/parameters/correctParamsJavaProperties.json" };
134 final ApexMain apexMain = new ApexMain(args);
135 assertEquals("MyApexEngine",
136 apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
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"));
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());
156 final String outString = outContent.toString();
157 assertThat(outString).contains("Added the action listener to the engine");
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());
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
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();
188 assertThat(outString).contains("Arguments validation failed", "start of Apex service failed");
189 assertThat(apexMain.getApexParametersMap()).isEmpty(); // No policy is running in the engine