b61c709617a04e40c15a189d038c884f3dd3d35e
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
22
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;
36
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;
42
43 import static org.junit.Assert.assertTrue;
44
45 /**
46  * This class runs integration tests for execution property in restClient.
47  */
48 public class TestExecutionPropertyRest {
49
50     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
51
52     private static final String BASE_URI = "http://localhost:32801/TestExecutionRest";
53     private static HttpServletServer server;
54     private static final int PORT = 32801;
55
56     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
58
59     private final PrintStream stdout = System.out;
60     private final PrintStream stderr = System.err;
61
62     /**
63      * Compile the policy.
64      */
65     @BeforeClass
66     public static void compilePolicy() {
67         // @formatter:off
68         final String[] cliArgs = {
69             "-c",
70             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
71             "-l",
72             "target/ExecutionPropertiesRestTestPolicyModel.log",
73             "-o",
74             "target/ExecutionPropertiesRestTestPolicyModel.json"
75         };
76         // @formatter:on
77
78         new ApexCommandLineEditorMain(cliArgs);
79     }
80
81     /**
82      * Sets the up.
83      *
84      * @throws Exception the exception
85      */
86     @BeforeClass
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");
90         }
91
92         server = HttpServletServerFactoryInstance.getServerFactory().build(
93             "TestExecutionPropertyRest", false, null, PORT, "/TestExecutionRest", false, false);
94
95         server.addServletClass(null, TestRestClientEndpoint.class.getName());
96         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
97
98         server.start();
99
100         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 2000, 1L)) {
101             throw new IllegalStateException("port " + PORT + " is still not in use");
102         }
103
104     }
105
106     /**
107      * Tear down.
108      *
109      * @throws Exception the exception
110      */
111     @AfterClass
112     public static void tearDown() throws Exception {
113         if (server != null) {
114             server.stop();
115         }
116     }
117
118     /**
119      * Clear relative file root environment variable.
120      */
121     @Before
122     public void clearRelativeFileRoot() {
123         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
124     }
125
126     /**
127      * Test Bad Url tag in Parameter .
128      */
129     @Test
130     public void testBadUrl() throws Exception {
131         System.setOut(new PrintStream(outContent));
132         System.setErr(new PrintStream(errContent));
133
134         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json" };
135         final ApexMain apexMain = new ApexMain(args);
136
137         ThreadUtilities.sleep(500);
138
139         apexMain.shutdown();
140
141         final String outString = outContent.toString();
142
143         System.setOut(stdout);
144         System.setErr(stderr);
145
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"));
148     }
149
150     /**
151      * Test Not find value for tags in Url .
152      */
153     @Test
154     public void testNoValueSetForTagUrl() throws Exception {
155         System.setOut(new PrintStream(outContent));
156         System.setErr(new PrintStream(errContent));
157
158         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json" };
159         final ApexMain apexMain = new ApexMain(args);
160
161         ThreadUtilities.sleep(2000);
162
163         apexMain.shutdown();
164
165         final String outString = outContent.toString();
166
167         System.setOut(stdout);
168         System.setErr(stderr);
169
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"));
173     }
174
175     /**
176      * Test Bad Http code Filter.
177      */
178     @Test
179     public void testBadCodeFilter() throws Exception {
180         System.setOut(new PrintStream(outContent));
181         System.setErr(new PrintStream(errContent));
182
183         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json" };
184         final ApexMain apexMain = new ApexMain(args);
185
186         ThreadUtilities.sleep(500);
187
188         apexMain.shutdown();
189
190         final String outString = outContent.toString();
191
192         System.setOut(stdout);
193         System.setErr(stderr);
194
195         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
196         assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
197     }
198
199     /**
200      * Test Http code filter set and tag Url are transformed correctly.
201      */
202     @Test
203     public void testReplaceUrlTag() throws Exception {
204         final Client client = ClientBuilder.newClient();
205
206         final String[] args =
207             { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json" };
208         final ApexMain apexMain = new ApexMain(args);
209         ThreadUtilities.sleep(1000);
210         apexMain.shutdown();
211
212         final String outString = outContent.toString();
213         System.setOut(stdout);
214         System.setErr(stderr);
215
216         Response response = null;
217         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
218                 .request("application/json").get();
219
220         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
221         final String responseEntity = response.readEntity(String.class);
222         assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
223     }
224
225     /**
226      * Test Http code filter set and multi-tag Url are transformed correctly.
227      */
228     @Test
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);
235         apexMain.shutdown();
236
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"));
245     }
246
247 }