APEX standalone support for ToscaPolicy format
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / adapt / restclient / TestExecutionPropertyRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
23
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.ByteArrayOutputStream;
27 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.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain;
36 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
37 import org.onap.policy.apex.service.engine.main.ApexMain;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
40 import org.onap.policy.common.gson.GsonMessageBodyHandler;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
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 HttpServletServer server;
53     private static final int PORT = 32801;
54
55     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
57
58     private final PrintStream stdout = System.out;
59     private final PrintStream stderr = System.err;
60
61     /**
62      * Sets the up.
63      *
64      * @throws Exception the exception
65      */
66     @BeforeClass
67     public static void setUp() throws Exception {
68         if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
69             throw new IllegalStateException("port " + PORT + " is still in use");
70         }
71
72         server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
73             PORT, "/TestExecutionRest", false, false);
74
75         server.addServletClass(null, TestRestClientEndpoint.class.getName());
76         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
77
78         server.start();
79
80         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
81             throw new IllegalStateException("port " + PORT + " is still not in use");
82         }
83
84     }
85
86     /**
87      * Tear down.
88      *
89      * @throws Exception the exception
90      */
91     @AfterClass
92     public static void tearDown() throws Exception {
93         if (server != null) {
94             server.stop();
95         }
96     }
97
98     /**
99      * Clear relative file root environment variable.
100      */
101     @Before
102     public void clearRelativeFileRoot() {
103         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
104     }
105
106     /**
107      * Test Bad Url tag in Parameter .
108      */
109     @Test
110     public void testBadUrl() throws Exception {
111         // @formatter:off
112         final String[] cliArgs = new String[] {
113             "-c",
114             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
115             "-o",
116             "target/ExecutionPropertiesRestTestPolicyModel.json",
117             "-ac",
118             "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json",
119             "-t",
120             "src/test/resources/tosca/ToscaTemplate.json",
121             "-ot",
122             "target/classes/APEXPolicy.json"
123         };
124         // @formatter:on
125
126         new ApexCliToscaEditorMain(cliArgs);
127
128         // @formatter:off
129         final String[] args = {
130             "-p",
131             "target/classes/APEXPolicy.json"
132         };
133         // @formatter:on
134
135         System.setOut(new PrintStream(outContent));
136         System.setErr(new PrintStream(errContent));
137
138         final ApexMain apexMain = new ApexMain(args);
139
140         ThreadUtilities.sleep(500);
141
142         apexMain.shutdown();
143
144         final String outString = outContent.toString();
145
146         System.setOut(stdout);
147         System.setErr(stderr);
148
149         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
150         assertTrue(outString.contains("invalid URL http://localhost:32801/TestExecutionRest/apex/event/tagId}"
151             + " has been set for event sending on RESTCLIENT"));
152     }
153
154     /**
155      * Test Not find value for tags in Url .
156      */
157     @Test
158     public void testNoValueSetForTagUrl() throws Exception {
159         // @formatter:off
160         final String[] cliArgs = new String[] {
161             "-c",
162             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
163             "-o",
164             "target/ExecutionPropertiesRestTestPolicyModel.json",
165             "-ac",
166             "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json",
167             "-t",
168             "src/test/resources/tosca/ToscaTemplate.json",
169             "-ot",
170             "target/classes/APEXPolicy.json"
171         };
172         // @formatter:on
173
174         new ApexCliToscaEditorMain(cliArgs);
175
176         // @formatter:off
177         final String[] args = {
178             "-p",
179             "target/classes/APEXPolicy.json"
180         };
181         // @formatter:on
182
183         System.setOut(new PrintStream(outContent));
184         System.setErr(new PrintStream(errContent));
185
186         final ApexMain apexMain = new ApexMain(args);
187
188         ThreadUtilities.sleep(2000);
189
190         apexMain.shutdown();
191
192         final String outString = outContent.toString();
193
194         System.setOut(stdout);
195         System.setErr(stderr);
196
197         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
198         assertTrue(outString.contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex"
199             + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy"));
200     }
201
202     /**
203      * Test Bad Http code Filter.
204      */
205     @Test
206     public void testBadCodeFilter() throws Exception {
207         // @formatter:off
208         final String[] cliArgs = new String[] {
209             "-c",
210             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
211             "-o",
212             "target/ExecutionPropertiesRestTestPolicyModel.json",
213             "-ac",
214             "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json",
215             "-t",
216             "src/test/resources/tosca/ToscaTemplate.json",
217             "-ot",
218             "target/classes/APEXPolicy.json"
219         };
220         // @formatter:on
221
222         new ApexCliToscaEditorMain(cliArgs);
223
224         // @formatter:off
225         final String[] args = {
226             "-p",
227             "target/classes/APEXPolicy.json"
228         };
229         // @formatter:on
230
231         System.setOut(new PrintStream(outContent));
232         System.setErr(new PrintStream(errContent));
233
234         final ApexMain apexMain = new ApexMain(args);
235
236         ThreadUtilities.sleep(500);
237
238         apexMain.shutdown();
239
240         final String outString = outContent.toString();
241
242         System.setOut(stdout);
243         System.setErr(stderr);
244
245         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
246         assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
247     }
248
249     /**
250      * Test Http code filter set and tag Url are transformed correctly.
251      */
252     @Test
253     public void testReplaceUrlTag() throws Exception {
254         final Client client = ClientBuilder.newClient();
255         // @formatter:off
256         final String[] cliArgs = new String[] {
257             "-c",
258             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
259             "-o",
260             "target/ExecutionPropertiesRestTestPolicyModel.json",
261             "-ac",
262             "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json",
263             "-t",
264             "src/test/resources/tosca/ToscaTemplate.json",
265             "-ot",
266             "target/classes/APEXPolicy.json"
267         };
268         // @formatter:on
269
270         new ApexCliToscaEditorMain(cliArgs);
271
272         // @formatter:off
273         final String[] args = {
274             "-p",
275             "target/classes/APEXPolicy.json"
276         };
277         // @formatter:on
278         final ApexMain apexMain = new ApexMain(args);
279         ThreadUtilities.sleep(1000);
280         apexMain.shutdown();
281
282         final String outString = outContent.toString();
283         System.setOut(stdout);
284         System.setErr(stderr);
285
286         Response response = null;
287         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
288             .request("application/json").get();
289
290         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
291         final String responseEntity = response.readEntity(String.class);
292         assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
293     }
294
295     /**
296      * Test Http code filter set and multi-tag Url are transformed correctly.
297      */
298     @Test
299     public void testReplaceUrlMultiTag() throws Exception {
300         final Client client = ClientBuilder.newClient();
301         // @formatter:off
302         final String[] cliArgs = new String[] {
303             "-c",
304             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
305             "-o",
306             "target/ExecutionPropertiesRestTestPolicyModel.json",
307             "-ac",
308             "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json",
309             "-t",
310             "src/test/resources/tosca/ToscaTemplate.json",
311             "-ot",
312             "target/classes/APEXPolicy.json"
313         };
314         // @formatter:on
315
316         new ApexCliToscaEditorMain(cliArgs);
317
318         // @formatter:off
319         final String[] args = {
320             "-p",
321             "target/classes/APEXPolicy.json"
322         };
323         // @formatter:on
324         final ApexMain apexMain = new ApexMain(args);
325         ThreadUtilities.sleep(1500);
326         apexMain.shutdown();
327
328         System.setOut(stdout);
329         System.setErr(stderr);
330         Response response = null;
331         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
332             .request("application/json").get();
333         final String responseEntity = response.readEntity(String.class);
334         LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
335         assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));
336     }
337
338 }