2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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;
24 import java.io.ByteArrayOutputStream;
25 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.glassfish.grizzly.http.server.HttpServer;
31 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
32 import org.glassfish.jersey.server.ResourceConfig;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
38 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
39 import org.onap.policy.apex.service.engine.main.ApexMain;
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 final String BASE_URI = "http://localhost:32801/TestExecutionRest";
52 private static HttpServer server;
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 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
87 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
89 if (NetworkUtil.isTcpPortOpen(BASE_URI, 32801, 1, 1L)) {
90 throw new IllegalStateException("port " + 32801 + " is still in use");
98 * @throws Exception the exception
101 public static void tearDown() throws Exception {
106 * Clear relative file root environment variable.
109 public void clearRelativeFileRoot() {
110 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
114 * Test Bad Url tag in Parameter .
117 public void testBadUrl() throws Exception {
118 System.setOut(new PrintStream(outContent));
119 System.setErr(new PrintStream(errContent));
121 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadUrl.json"};
122 final ApexMain apexMain = new ApexMain(args);
124 ThreadUtilities.sleep(500);
128 final String outString = outContent.toString();
130 System.setOut(stdout);
131 System.setErr(stderr);
133 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
134 assertTrue(outString.contains("no proper URL has been set for event sending on REST client"));
138 * Test Not find value for tags in Url .
141 public void testNoValueSetForTagUrl() throws Exception {
142 System.setOut(new PrintStream(outContent));
143 System.setErr(new PrintStream(errContent));
145 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json"};
146 final ApexMain apexMain = new ApexMain(args);
148 ThreadUtilities.sleep(1000);
152 final String outString = outContent.toString();
154 System.setOut(stdout);
155 System.setErr(stderr);
157 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
158 assertTrue(outString.contains("key\"Number\"specified on url \"http://localhost:32801/TestExecutionRest/apex"
159 + "/event/{tagId}/{Number}\"not found in execution properties passed by the current policy"));
163 * Test Bad Http code Filter.
166 public void testBadCodeFilter() throws Exception {
167 System.setOut(new PrintStream(outContent));
168 System.setErr(new PrintStream(errContent));
170 final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json"};
171 final ApexMain apexMain = new ApexMain(args);
173 ThreadUtilities.sleep(500);
177 final String outString = outContent.toString();
179 System.setOut(stdout);
180 System.setErr(stderr);
182 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
183 assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
187 * Test Http code filter set and tag Url are transformed correctly.
190 public void testReplaceUrlTag() throws Exception {
191 final Client client = ClientBuilder.newClient();
193 final String[] args = {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json"};
194 final ApexMain apexMain = new ApexMain(args);
195 ThreadUtilities.sleep(1000);
198 final String outString = outContent.toString();
199 System.setOut(stdout);
200 System.setErr(stderr);
202 Response response = null;
203 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
204 .request("application/json").get();
206 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
207 final String responseEntity = response.readEntity(String.class);
208 assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
212 * Test Http code filter set and multi-tag Url are transformed correctly.
215 public void testReplaceUrlMultiTag() throws Exception {
216 final Client client = ClientBuilder.newClient();
217 final String[] args =
218 {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json"};
219 final ApexMain apexMain = new ApexMain(args);
220 ThreadUtilities.sleep(1500);
223 System.setOut(stdout);
224 System.setErr(stderr);
225 Response response = null;
226 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
227 .request("application/json").get();
228 final String responseEntity = response.readEntity(String.class);
229 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
230 assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));