2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
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.testsuites.integration.uservice.adapt.restclient;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30 import java.util.concurrent.TimeUnit;
31 import javax.ws.rs.client.Client;
32 import javax.ws.rs.client.ClientBuilder;
33 import javax.ws.rs.core.Response;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.service.engine.main.ApexMain;
42 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
43 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
44 import org.onap.policy.common.gson.GsonMessageBodyHandler;
45 import org.onap.policy.common.utils.network.NetworkUtil;
46 import org.slf4j.ext.XLogger;
47 import org.slf4j.ext.XLoggerFactory;
50 * This class runs integration tests for execution property in restClient.
52 public class TestExecutionPropertyRest {
54 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
56 private static HttpServletServer server;
57 private static final int PORT = 32801;
59 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
60 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
62 private final PrintStream stdout = System.out;
63 private final PrintStream stderr = System.err;
65 private ApexMain apexMain;
70 * @throws Exception the exception
73 public static void setUp() throws Exception {
74 if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
75 throw new IllegalStateException("port " + PORT + " is still in use");
78 server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
79 PORT, "/TestExecutionRest", false, false);
81 server.addServletClass(null, TestRestClientEndpoint.class.getName());
82 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
86 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
87 throw new IllegalStateException("port " + PORT + " is still not in use");
95 * @throws Exception the exception
98 public static void tearDown() throws Exception {
108 public void beforeTest() {
109 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
110 System.setOut(new PrintStream(outContent));
111 System.setErr(new PrintStream(errContent));
116 * @throws ApexException the exception.
119 public void afterTest() throws ApexException {
120 if (null != apexMain) {
123 System.setOut(stdout);
124 System.setErr(stderr);
128 * Test Bad Url tag in Parameter .
131 public void testBadUrl() throws Exception {
133 final String[] cliArgs = new String[] {
135 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
137 "target/ExecutionPropertiesRestTestPolicyModel.json",
139 "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json",
141 "src/test/resources/tosca/ToscaTemplate.json",
143 "target/classes/APEXPolicy.json"
147 new ApexCliToscaEditorMain(cliArgs);
150 final String[] args = {
152 "target/classes/APEXPolicy.json"
156 apexMain = new ApexMain(args);
158 final String outString = outContent.toString();
160 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
161 assertThat(outString).contains("invalid URL http://localhost:32801/TestExecutionRest/apex/event/tagId}"
162 + " has been set for event sending on RESTCLIENT");
166 * Test Not find value for tags in Url .
169 public void testNoValueSetForTagUrl() throws Exception {
171 final String[] cliArgs = new String[] {
173 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
175 "target/ExecutionPropertiesRestTestPolicyModel.json",
177 "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json",
179 "src/test/resources/tosca/ToscaTemplate.json",
181 "target/classes/APEXPolicy.json"
185 new ApexCliToscaEditorMain(cliArgs);
188 final String[] args = {
190 "target/classes/APEXPolicy.json"
194 apexMain = new ApexMain(args);
196 await().atMost(5, TimeUnit.SECONDS)
197 .until(() -> outContent.toString()
198 .contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex"
199 + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy"));
200 assertTrue(apexMain.isAlive());
201 LOGGER.info("testNoValueSetForTagUrl-OUTSTRING=\n" + outContent.toString() + "\nEnd-TagUrl");
205 * Test Bad Http code Filter.
208 public void testBadCodeFilter() throws Exception {
210 final String[] cliArgs = new String[] {
212 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
214 "target/ExecutionPropertiesRestTestPolicyModel.json",
216 "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json",
218 "src/test/resources/tosca/ToscaTemplate.json",
220 "target/classes/APEXPolicy.json"
224 new ApexCliToscaEditorMain(cliArgs);
227 final String[] args = {
229 "target/classes/APEXPolicy.json"
233 apexMain = new ApexMain(args);
235 await().atMost(5, TimeUnit.SECONDS).until(() -> outContent.toString()
236 .contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
237 assertTrue(apexMain.isAlive());
238 LOGGER.info("testBadCodeFilter-OUTSTRING=\n" + outContent.toString() + "\nEnd-TagUrl");
242 * Test Http code filter set and tag Url are transformed correctly.
245 public void testReplaceUrlTag() throws Exception {
246 final Client client = ClientBuilder.newClient();
248 final String[] cliArgs = new String[] {
250 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
252 "target/ExecutionPropertiesRestTestPolicyModel.json",
254 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json",
256 "src/test/resources/tosca/ToscaTemplate.json",
258 "target/classes/APEXPolicy.json"
262 new ApexCliToscaEditorMain(cliArgs);
265 final String[] args = {
267 "target/classes/APEXPolicy.json"
270 apexMain = new ApexMain(args);
272 await().atMost(5, TimeUnit.SECONDS).until(() -> {
273 Response response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
274 .request("application/json").get();
275 return response.readEntity(String.class).contains("\"PostProperUrl\": 1");
277 assertTrue(apexMain.isAlive());
278 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outContent.toString() + "\nEnd-TagUrl");
282 * Test Http code filter set and multi-tag Url are transformed correctly.
285 public void testReplaceUrlMultiTag() throws Exception {
286 final Client client = ClientBuilder.newClient();
288 final String[] cliArgs = new String[] {
290 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
292 "target/ExecutionPropertiesRestTestPolicyModel.json",
294 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json",
296 "src/test/resources/tosca/ToscaTemplate.json",
298 "target/classes/APEXPolicy.json"
302 new ApexCliToscaEditorMain(cliArgs);
305 final String[] args = {
307 "target/classes/APEXPolicy.json"
310 apexMain = new ApexMain(args);
312 await().atMost(5, TimeUnit.SECONDS).until(() -> {
313 Response response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
314 .request("application/json").get();
315 return response.readEntity(String.class).contains("\"PostProperUrl\": 3");
317 assertTrue(apexMain.isAlive());
318 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + outContent.toString() + "\nEnd-MultiTagUrl");