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;
27 import javax.ws.rs.client.Client;
28 import javax.ws.rs.client.ClientBuilder;
29 import javax.ws.rs.core.Response;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.service.engine.main.ApexMain;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.gson.GsonMessageBodyHandler;
40 import org.onap.policy.common.utils.network.NetworkUtil;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
45 * This class runs integration tests for execution property in restClient.
47 public class TestExecutionPropertyRest {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
51 private static HttpServletServer server;
52 private static final int PORT = 32801;
54 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
55 private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
57 private final PrintStream stdout = System.out;
58 private final PrintStream stderr = System.err;
64 public static void compilePolicy() {
66 final String[] cliArgs = {
68 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
70 "target/ExecutionPropertiesRestTestPolicyModel.log",
72 "target/ExecutionPropertiesRestTestPolicyModel.json"
76 new ApexCommandLineEditorMain(cliArgs);
82 * @throws Exception the exception
85 public static void setUp() throws Exception {
86 if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
87 throw new IllegalStateException("port " + PORT + " is still in use");
90 server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
91 PORT, "/TestExecutionRest", false, false);
93 server.addServletClass(null, TestRestClientEndpoint.class.getName());
94 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
98 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
99 throw new IllegalStateException("port " + PORT + " is still not in use");
107 * @throws Exception the exception
110 public static void tearDown() throws Exception {
111 if (server != null) {
117 * Clear relative file root environment variable.
120 public void clearRelativeFileRoot() {
121 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
125 * Test Bad Url tag in Parameter .
128 public void testBadUrl() throws Exception {
129 System.setOut(new PrintStream(outContent));
130 System.setErr(new PrintStream(errContent));
132 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadUrl.json"};
133 final ApexMain apexMain = new ApexMain(args);
135 ThreadUtilities.sleep(500);
139 final String outString = outContent.toString();
141 System.setOut(stdout);
142 System.setErr(stderr);
144 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
145 assertTrue(outString.contains("no URL has been set for event sending on RESTCLIENT"));
149 * Test Not find value for tags in Url .
152 public void testNoValueSetForTagUrl() throws Exception {
153 System.setOut(new PrintStream(outContent));
154 System.setErr(new PrintStream(errContent));
156 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json"};
157 final ApexMain apexMain = new ApexMain(args);
159 ThreadUtilities.sleep(2000);
163 final String outString = outContent.toString();
165 System.setOut(stdout);
166 System.setErr(stderr);
168 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
169 assertTrue(outString.contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex"
170 + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy"));
174 * Test Bad Http code Filter.
177 public void testBadCodeFilter() throws Exception {
178 System.setOut(new PrintStream(outContent));
179 System.setErr(new PrintStream(errContent));
181 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json"};
182 final ApexMain apexMain = new ApexMain(args);
184 ThreadUtilities.sleep(500);
188 final String outString = outContent.toString();
190 System.setOut(stdout);
191 System.setErr(stderr);
193 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
194 assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
198 * Test Http code filter set and tag Url are transformed correctly.
201 public void testReplaceUrlTag() throws Exception {
202 final Client client = ClientBuilder.newClient();
204 final String[] args = {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json"};
205 final ApexMain apexMain = new ApexMain(args);
206 ThreadUtilities.sleep(1000);
209 final String outString = outContent.toString();
210 System.setOut(stdout);
211 System.setErr(stderr);
213 Response response = null;
214 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
215 .request("application/json").get();
217 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
218 final String responseEntity = response.readEntity(String.class);
219 assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
223 * Test Http code filter set and multi-tag Url are transformed correctly.
226 public void testReplaceUrlMultiTag() throws Exception {
227 final Client client = ClientBuilder.newClient();
229 final String[] args = {
230 "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json"
233 final ApexMain apexMain = new ApexMain(args);
234 ThreadUtilities.sleep(1500);
237 System.setOut(stdout);
238 System.setErr(stderr);
239 Response response = null;
240 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
241 .request("application/json").get();
242 final String responseEntity = response.readEntity(String.class);
243 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
244 assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));