298a56be6e1cf1dc8bdfefa9b542e3a4c2847cb7
[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  * ================================================================================
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 static org.junit.Assert.assertTrue;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.PrintStream;
27
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.core.Response;
31
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
37 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
38 import org.onap.policy.apex.service.engine.main.ApexMain;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
41 import org.onap.policy.common.gson.GsonMessageBodyHandler;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.slf4j.ext.XLogger;
44 import org.slf4j.ext.XLoggerFactory;
45
46 /**
47  * This class runs integration tests for execution property in restClient.
48  */
49 public class TestExecutionPropertyRest {
50
51     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
52
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("TestExecutionPropertyRest", false, null,
93                 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, 60, 500L)) {
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 URL has been set for event sending on RESTCLIENT"));
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 = {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json"};
207         final ApexMain apexMain = new ApexMain(args);
208         ThreadUtilities.sleep(1000);
209         apexMain.shutdown();
210
211         final String outString = outContent.toString();
212         System.setOut(stdout);
213         System.setErr(stderr);
214
215         Response response = null;
216         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
217                 .request("application/json").get();
218
219         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
220         final String responseEntity = response.readEntity(String.class);
221         assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
222     }
223
224     /**
225      * Test Http code filter set and multi-tag Url are transformed correctly.
226      */
227     @Test
228     public void testReplaceUrlMultiTag() throws Exception {
229         final Client client = ClientBuilder.newClient();
230         // @formatter:off
231         final String[] args = {
232             "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json"
233         };
234         // @formatter:on
235         final ApexMain apexMain = new ApexMain(args);
236         ThreadUtilities.sleep(1500);
237         apexMain.shutdown();
238
239         System.setOut(stdout);
240         System.setErr(stderr);
241         Response response = null;
242         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
243                 .request("application/json").get();
244         final String responseEntity = response.readEntity(String.class);
245         LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
246         assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));
247     }
248
249 }