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