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;
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;
31 import org.glassfish.grizzly.http.server.HttpServer;
32 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
39 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
40 import org.onap.policy.apex.service.engine.main.ApexMain;
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 final String BASE_URI = "http://localhost:32801/TestExecutionRest";
53 private static HttpServer server;
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;
65 public static void compilePolicy() {
67 final String[] cliArgs = {
69 "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
71 "target/ExecutionPropertiesRestTestPolicyModel.log",
73 "target/ExecutionPropertiesRestTestPolicyModel.json"
77 new ApexCommandLineEditorMain(cliArgs);
83 * @throws Exception the exception
86 public static void setUp() throws Exception {
87 final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
88 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
90 if (NetworkUtil.isTcpPortOpen(BASE_URI, 32801, 1, 1L)) {
91 throw new IllegalStateException("port " + 32801 + " is still in use");
99 * @throws Exception the exception
102 public static void tearDown() throws Exception {
107 * Clear relative file root environment variable.
110 public void clearRelativeFileRoot() {
111 System.clearProperty("APEX_RELATIVE_FILE_ROOT");
115 * Test Bad Url tag in Parameter .
118 public void testBadUrl() throws Exception {
119 System.setOut(new PrintStream(outContent));
120 System.setErr(new PrintStream(errContent));
122 final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json" };
123 final ApexMain apexMain = new ApexMain(args);
125 ThreadUtilities.sleep(500);
129 final String outString = outContent.toString();
131 System.setOut(stdout);
132 System.setErr(stderr);
134 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
135 assertTrue(outString.contains("no proper URL has been set for event sending on REST client"));
139 * Test Not find value for tags in Url .
142 public void testNoValueSetForTagUrl() throws Exception {
143 System.setOut(new PrintStream(outContent));
144 System.setErr(new PrintStream(errContent));
146 final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json" };
147 final ApexMain apexMain = new ApexMain(args);
149 ThreadUtilities.sleep(2000);
153 final String outString = outContent.toString();
155 System.setOut(stdout);
156 System.setErr(stderr);
158 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
159 assertTrue(outString.contains("key\"Number\"specified on url \"http://localhost:32801/TestExecutionRest/apex"
160 + "/event/{tagId}/{Number}\"not found in execution properties passed by the current policy"));
164 * Test Bad Http code Filter.
167 public void testBadCodeFilter() throws Exception {
168 System.setOut(new PrintStream(outContent));
169 System.setErr(new PrintStream(errContent));
171 final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json" };
172 final ApexMain apexMain = new ApexMain(args);
174 ThreadUtilities.sleep(500);
178 final String outString = outContent.toString();
180 System.setOut(stdout);
181 System.setErr(stderr);
183 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
184 assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
188 * Test Http code filter set and tag Url are transformed correctly.
191 public void testReplaceUrlTag() throws Exception {
192 final Client client = ClientBuilder.newClient();
194 final String[] args =
195 { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json" };
196 final ApexMain apexMain = new ApexMain(args);
197 ThreadUtilities.sleep(1000);
200 final String outString = outContent.toString();
201 System.setOut(stdout);
202 System.setErr(stderr);
204 Response response = null;
205 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
206 .request("application/json").get();
208 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
209 final String responseEntity = response.readEntity(String.class);
210 assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
214 * Test Http code filter set and multi-tag Url are transformed correctly.
217 public void testReplaceUrlMultiTag() throws Exception {
218 final Client client = ClientBuilder.newClient();
219 final String[] args =
220 { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json" };
221 final ApexMain apexMain = new ApexMain(args);
222 ThreadUtilities.sleep(1500);
225 System.setOut(stdout);
226 System.setErr(stderr);
227 Response response = null;
228 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
229 .request("application/json").get();
230 final String responseEntity = response.readEntity(String.class);
231 LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
232 assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));