2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 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.junit.Assert.assertTrue;
26 import java.io.ByteArrayOutputStream;
27 import java.io.PrintStream;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.core.Response;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
36 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
37 import org.onap.policy.apex.service.engine.main.ApexMain;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
40 import org.onap.policy.common.gson.GsonMessageBodyHandler;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
46 * This class runs integration tests for execution property in restClient.
48 public class TestExecutionPropertyRest {
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
52 private static HttpServletServer server;
53 private static final int PORT = 32801;
55 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
58 private final PrintStream stdout = System.out;
59 private final PrintStream stderr = System.err;
64 * @throws Exception the exception
67 public static void setUp() throws Exception {
68 if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
69 throw new IllegalStateException("port " + PORT + " is still in use");
72 server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
73 PORT, "/TestExecutionRest", false, false);
75 server.addServletClass(null, TestRestClientEndpoint.class.getName());
76 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
80 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
81 throw new IllegalStateException("port " + PORT + " is still not in use");
89 * @throws Exception the exception
92 public static void tearDown() throws Exception {
99 * Clear relative file root environment variable.
102 public void clearRelativeFileRoot() {
103 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
107 * Test Bad Url tag in Parameter .
110 public void testBadUrl() throws Exception {
112 final String[] cliArgs = new String[] {
114 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
116 "target/ExecutionPropertiesRestTestPolicyModel.json",
118 "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json",
120 "src/test/resources/tosca/ToscaTemplate.json",
122 "target/classes/APEXPolicy.json"
126 new ApexCliToscaEditorMain(cliArgs);
129 final String[] args = {
131 "target/classes/APEXPolicy.json"
135 System.setOut(new PrintStream(outContent));
136 System.setErr(new PrintStream(errContent));
138 final ApexMain apexMain = new ApexMain(args);
140 ThreadUtilities.sleep(500);
144 final String outString = outContent.toString();
146 System.setOut(stdout);
147 System.setErr(stderr);
149 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
150 assertTrue(outString.contains("invalid URL http://localhost:32801/TestExecutionRest/apex/event/tagId}"
151 + " has been set for event sending on RESTCLIENT"));
155 * Test Not find value for tags in Url .
158 public void testNoValueSetForTagUrl() throws Exception {
160 final String[] cliArgs = new String[] {
162 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
164 "target/ExecutionPropertiesRestTestPolicyModel.json",
166 "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json",
168 "src/test/resources/tosca/ToscaTemplate.json",
170 "target/classes/APEXPolicy.json"
174 new ApexCliToscaEditorMain(cliArgs);
177 final String[] args = {
179 "target/classes/APEXPolicy.json"
183 System.setOut(new PrintStream(outContent));
184 System.setErr(new PrintStream(errContent));
186 final ApexMain apexMain = new ApexMain(args);
188 ThreadUtilities.sleep(2000);
192 final String outString = outContent.toString();
194 System.setOut(stdout);
195 System.setErr(stderr);
197 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
198 assertTrue(outString.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"));
203 * Test Bad Http code Filter.
206 public void testBadCodeFilter() throws Exception {
208 final String[] cliArgs = new String[] {
210 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
212 "target/ExecutionPropertiesRestTestPolicyModel.json",
214 "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json",
216 "src/test/resources/tosca/ToscaTemplate.json",
218 "target/classes/APEXPolicy.json"
222 new ApexCliToscaEditorMain(cliArgs);
225 final String[] args = {
227 "target/classes/APEXPolicy.json"
231 System.setOut(new PrintStream(outContent));
232 System.setErr(new PrintStream(errContent));
234 final ApexMain apexMain = new ApexMain(args);
236 ThreadUtilities.sleep(500);
240 final String outString = outContent.toString();
242 System.setOut(stdout);
243 System.setErr(stderr);
245 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
246 assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
250 * Test Http code filter set and tag Url are transformed correctly.
253 public void testReplaceUrlTag() throws Exception {
254 final Client client = ClientBuilder.newClient();
256 final String[] cliArgs = new String[] {
258 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
260 "target/ExecutionPropertiesRestTestPolicyModel.json",
262 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json",
264 "src/test/resources/tosca/ToscaTemplate.json",
266 "target/classes/APEXPolicy.json"
270 new ApexCliToscaEditorMain(cliArgs);
273 final String[] args = {
275 "target/classes/APEXPolicy.json"
278 final ApexMain apexMain = new ApexMain(args);
279 ThreadUtilities.sleep(1000);
282 final String outString = outContent.toString();
283 System.setOut(stdout);
284 System.setErr(stderr);
286 Response response = null;
287 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
288 .request("application/json").get();
290 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
291 final String responseEntity = response.readEntity(String.class);
292 assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
296 * Test Http code filter set and multi-tag Url are transformed correctly.
299 public void testReplaceUrlMultiTag() throws Exception {
300 final Client client = ClientBuilder.newClient();
302 final String[] cliArgs = new String[] {
304 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
306 "target/ExecutionPropertiesRestTestPolicyModel.json",
308 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json",
310 "src/test/resources/tosca/ToscaTemplate.json",
312 "target/classes/APEXPolicy.json"
316 new ApexCliToscaEditorMain(cliArgs);
319 final String[] args = {
321 "target/classes/APEXPolicy.json"
324 final ApexMain apexMain = new ApexMain(args);
325 ThreadUtilities.sleep(1500);
328 System.setOut(stdout);
329 System.setErr(stderr);
330 Response response = null;
331 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
332 .request("application/json").get();
333 final String responseEntity = response.readEntity(String.class);
334 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
335 assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));