2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020,2023 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.testsuites.integration.uservice.adapt.restclient;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.awaitility.Awaitility.await;
27 import static org.junit.Assert.assertTrue;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.util.concurrent.TimeUnit;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.core.Response;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
41 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
42 import org.onap.policy.apex.service.engine.main.ApexMain;
43 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
44 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
45 import org.onap.policy.common.gson.GsonMessageBodyHandler;
46 import org.onap.policy.common.utils.network.NetworkUtil;
47 import org.slf4j.ext.XLogger;
48 import org.slf4j.ext.XLoggerFactory;
51 * This class runs integration tests for execution property in restClient.
53 public class TestExecutionPropertyRest {
55 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
57 private static HttpServletServer server;
58 private static final int PORT = 32801;
60 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
61 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
63 private final PrintStream stdout = System.out;
64 private final PrintStream stderr = System.err;
66 private ApexMain apexMain;
71 * @throws Exception the exception
74 public static void setUp() throws Exception {
75 if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
76 throw new IllegalStateException("port " + PORT + " is still in use");
79 server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
80 PORT, false, "/TestExecutionRest", false, false);
82 server.addServletClass(null, TestRestClientEndpoint.class.getName());
83 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
87 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
88 throw new IllegalStateException("port " + PORT + " is still not in use");
96 * @throws Exception the exception
99 public static void tearDown() throws Exception {
100 if (server != null) {
109 public void beforeTest() {
110 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
111 System.setOut(new PrintStream(outContent));
112 System.setErr(new PrintStream(errContent));
117 * @throws ApexException the exception.
120 public void afterTest() throws ApexException {
121 if (null != apexMain) {
124 System.setOut(stdout);
125 System.setErr(stderr);
129 * Test Bad Url tag in Parameter .
132 public void testBadUrl() throws Exception {
134 final String[] cliArgs = new String[] {
136 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
138 "target/ExecutionPropertiesRestTestPolicyModel.json",
140 "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json",
142 "src/test/resources/tosca/ToscaTemplate.json",
144 "target/classes/APEXPolicy.json"
148 new ApexCliToscaEditorMain(cliArgs);
151 final String[] args = {
153 "target/classes/APEXPolicy.json"
157 apexMain = new ApexMain(args);
159 final String outString = outContent.toString();
161 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
162 assertThat(outString).contains("item \"url\" "
163 + "value \"http://localhost:32801/TestExecutionRest/apex/event/tagId}\" INVALID, "
164 + "invalid URL has been set for event sending on RESTCLIENT");
168 * Test Not find value for tags in Url .
171 public void testNoValueSetForTagUrl() throws Exception {
173 final String[] cliArgs = new String[] {
175 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
177 "target/ExecutionPropertiesRestTestPolicyModel.json",
179 "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json",
181 "src/test/resources/tosca/ToscaTemplate.json",
183 "target/classes/APEXPolicy.json"
187 new ApexCliToscaEditorMain(cliArgs);
190 final String[] args = {
192 "target/classes/APEXPolicy.json"
196 apexMain = new ApexMain(args);
198 await().atMost(5, TimeUnit.SECONDS)
199 .until(() -> outContent.toString()
200 .contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex"
201 + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy"));
202 assertTrue(apexMain.isAlive());
203 LOGGER.info("testNoValueSetForTagUrl-OUTSTRING=\n" + outContent.toString() + "\nEnd-TagUrl");
207 * Test Bad Http code Filter.
210 public void testBadCodeFilter() throws Exception {
212 final String[] cliArgs = new String[] {
214 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
216 "target/ExecutionPropertiesRestTestPolicyModel.json",
218 "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json",
220 "src/test/resources/tosca/ToscaTemplate.json",
222 "target/classes/APEXPolicy.json"
226 new ApexCliToscaEditorMain(cliArgs);
229 final String[] args = {
231 "target/classes/APEXPolicy.json"
235 apexMain = new ApexMain(args);
237 await().atMost(5, TimeUnit.SECONDS).until(() -> outContent.toString()
238 .contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
239 assertTrue(apexMain.isAlive());
240 LOGGER.info("testBadCodeFilter-OUTSTRING=\n" + outContent.toString() + "\nEnd-TagUrl");
244 * Test Http code filter set and multi-tag Url are transformed correctly.
247 public void testReplaceUrlMultiTag() throws Exception {
248 final Client client = ClientBuilder.newClient();
250 final String[] cliArgs = new String[] {
252 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
254 "target/ExecutionPropertiesRestTestPolicyModel.json",
256 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json",
258 "src/test/resources/tosca/ToscaTemplate.json",
260 "target/classes/APEXPolicy.json"
264 new ApexCliToscaEditorMain(cliArgs);
267 final String[] args = {
269 "target/classes/APEXPolicy.json"
272 apexMain = new ApexMain(args);
274 await().atMost(5, TimeUnit.SECONDS).until(() -> {
275 Response response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
276 .request("application/json").get();
277 return response.readEntity(String.class).contains("\"PostProperUrl\": 3");
279 assertTrue(apexMain.isAlive());
280 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + outContent.toString() + "\nEnd-MultiTagUrl");