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 org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
28 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
29 import org.onap.policy.apex.service.engine.main.ApexMain;
30 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
31 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
32 import org.onap.policy.common.gson.GsonMessageBodyHandler;
33 import org.onap.policy.common.utils.network.NetworkUtil;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
37 import javax.ws.rs.client.Client;
38 import javax.ws.rs.client.ClientBuilder;
39 import javax.ws.rs.core.Response;
40 import java.io.ByteArrayOutputStream;
41 import java.io.PrintStream;
43 import static org.junit.Assert.assertTrue;
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 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(
93 "TestExecutionPropertyRest", false, null, PORT, "/TestExecutionRest", false, false);
95 server.addServletClass(null, TestRestClientEndpoint.class.getName());
96 server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
100 if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 2000, 1L)) {
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 proper URL has been set for event sending on REST client"));
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 =
207 { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json" };
208 final ApexMain apexMain = new ApexMain(args);
209 ThreadUtilities.sleep(1000);
212 final String outString = outContent.toString();
213 System.setOut(stdout);
214 System.setErr(stderr);
216 Response response = null;
217 response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
218 .request("application/json").get();
220 LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
221 final String responseEntity = response.readEntity(String.class);
222 assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
226 * Test Http code filter set and multi-tag Url are transformed correctly.
229 public void testReplaceUrlMultiTag() throws Exception {
230 final Client client = ClientBuilder.newClient();
231 final String[] args =
232 { "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"));