2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
23 import static org.junit.Assert.assertTrue;
25 import java.io.ByteArrayOutputStream;
26 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;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
37 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
41 import org.onap.policy.common.gson.GsonMessageBodyHandler;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.slf4j.ext.XLogger;
44 import org.slf4j.ext.XLoggerFactory;
47 * This class runs integration tests for execution property in restClient.
49 public class TestExecutionPropertyRest {
51 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
53 private static HttpServletServer server;
54 private static final int PORT = 32801;
56 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
59 private final PrintStream stdout = System.out;
60 private final PrintStream stderr = System.err;
66 public static void compilePolicy() {
68 final String[] cliArgs = {
70 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
72 "target/ExecutionPropertiesRestTestPolicyModel.log",
74 "target/ExecutionPropertiesRestTestPolicyModel.json"
78 new ApexCommandLineEditorMain(cliArgs);
84 * @throws Exception the exception
87 public static void setUp() throws Exception {
88 if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
89 throw new IllegalStateException("port " + PORT + " is still in use");
92 server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
93 PORT, "/TestExecutionRest", false, false);
95 server.addServletClass(null, TestRestClientEndpoint.class.getName());
96 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
100 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
101 throw new IllegalStateException("port " + PORT + " is still not in use");
109 * @throws Exception the exception
112 public static void tearDown() throws Exception {
113 if (server != null) {
119 * Clear relative file root environment variable.
122 public void clearRelativeFileRoot() {
123 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
127 * Test Bad Url tag in Parameter .
130 public void testBadUrl() throws Exception {
131 System.setOut(new PrintStream(outContent));
132 System.setErr(new PrintStream(errContent));
134 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadUrl.json"};
135 final ApexMain apexMain = new ApexMain(args);
137 ThreadUtilities.sleep(500);
141 final String outString = outContent.toString();
143 System.setOut(stdout);
144 System.setErr(stderr);
146 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
147 assertTrue(outString.contains("no URL has been set for event sending on RESTCLIENT"));
151 * Test Not find value for tags in Url .
154 public void testNoValueSetForTagUrl() throws Exception {
155 System.setOut(new PrintStream(outContent));
156 System.setErr(new PrintStream(errContent));
158 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json"};
159 final ApexMain apexMain = new ApexMain(args);
161 ThreadUtilities.sleep(2000);
165 final String outString = outContent.toString();
167 System.setOut(stdout);
168 System.setErr(stderr);
170 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
171 assertTrue(outString.contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex"
172 + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy"));
176 * Test Bad Http code Filter.
179 public void testBadCodeFilter() throws Exception {
180 System.setOut(new PrintStream(outContent));
181 System.setErr(new PrintStream(errContent));
183 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json"};
184 final ApexMain apexMain = new ApexMain(args);
186 ThreadUtilities.sleep(500);
190 final String outString = outContent.toString();
192 System.setOut(stdout);
193 System.setErr(stderr);
195 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
196 assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
200 * Test Http code filter set and tag Url are transformed correctly.
203 public void testReplaceUrlTag() throws Exception {
204 final Client client = ClientBuilder.newClient();
206 final String[] args = {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json"};
207 final ApexMain apexMain = new ApexMain(args);
208 ThreadUtilities.sleep(1000);
211 final String outString = outContent.toString();
212 System.setOut(stdout);
213 System.setErr(stderr);
215 Response response = null;
216 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
217 .request("application/json").get();
219 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
220 final String responseEntity = response.readEntity(String.class);
221 assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
225 * Test Http code filter set and multi-tag Url are transformed correctly.
228 public void testReplaceUrlMultiTag() throws Exception {
229 final Client client = ClientBuilder.newClient();
231 final String[] args = {
232 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json"
235 final ApexMain apexMain = new ApexMain(args);
236 ThreadUtilities.sleep(1500);
239 System.setOut(stdout);
240 System.setErr(stderr);
241 Response response = null;
242 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
243 .request("application/json").get();
244 final String responseEntity = response.readEntity(String.class);
245 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
246 assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));