Changes for checkstyle 8.32
[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 import javax.ws.rs.client.Client;
28 import javax.ws.rs.client.ClientBuilder;
29 import javax.ws.rs.core.Response;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.service.engine.main.ApexMain;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.gson.GsonMessageBodyHandler;
40 import org.onap.policy.common.utils.network.NetworkUtil;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
43
44 /**
45  * This class runs integration tests for execution property in restClient.
46  */
47 public class TestExecutionPropertyRest {
48
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestExecutionPropertyRest.class);
50
51     private static HttpServletServer server;
52     private static final int PORT = 32801;
53
54     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
55     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
56
57     private final PrintStream stdout = System.out;
58     private final PrintStream stderr = System.err;
59
60     /**
61      * Compile the policy.
62      */
63     @BeforeClass
64     public static void compilePolicy() {
65         // @formatter:off
66         final String[] cliArgs = {
67             "-c",
68             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
69             "-l",
70             "target/ExecutionPropertiesRestTestPolicyModel.log",
71             "-o",
72             "target/ExecutionPropertiesRestTestPolicyModel.json"
73         };
74         // @formatter:on
75
76         new ApexCommandLineEditorMain(cliArgs);
77     }
78
79     /**
80      * Sets the up.
81      *
82      * @throws Exception the exception
83      */
84     @BeforeClass
85     public static void setUp() throws Exception {
86         if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
87             throw new IllegalStateException("port " + PORT + " is still in use");
88         }
89
90         server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null,
91                 PORT, "/TestExecutionRest", false, false);
92
93         server.addServletClass(null, TestRestClientEndpoint.class.getName());
94         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
95
96         server.start();
97
98         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
99             throw new IllegalStateException("port " + PORT + " is still not in use");
100         }
101
102     }
103
104     /**
105      * Tear down.
106      *
107      * @throws Exception the exception
108      */
109     @AfterClass
110     public static void tearDown() throws Exception {
111         if (server != null) {
112             server.stop();
113         }
114     }
115
116     /**
117      * Clear relative file root environment variable.
118      */
119     @Before
120     public void clearRelativeFileRoot() {
121         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
122     }
123
124     /**
125      * Test Bad Url tag in Parameter .
126      */
127     @Test
128     public void testBadUrl() throws Exception {
129         System.setOut(new PrintStream(outContent));
130         System.setErr(new PrintStream(errContent));
131
132         final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadUrl.json"};
133         final ApexMain apexMain = new ApexMain(args);
134
135         ThreadUtilities.sleep(500);
136
137         apexMain.shutdown();
138
139         final String outString = outContent.toString();
140
141         System.setOut(stdout);
142         System.setErr(stderr);
143
144         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
145         assertTrue(outString.contains("no URL has been set for event sending on RESTCLIENT"));
146     }
147
148     /**
149      * Test Not find value for tags in Url .
150      */
151     @Test
152     public void testNoValueSetForTagUrl() throws Exception {
153         System.setOut(new PrintStream(outContent));
154         System.setErr(new PrintStream(errContent));
155
156         final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json"};
157         final ApexMain apexMain = new ApexMain(args);
158
159         ThreadUtilities.sleep(2000);
160
161         apexMain.shutdown();
162
163         final String outString = outContent.toString();
164
165         System.setOut(stdout);
166         System.setErr(stderr);
167
168         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
169         assertTrue(outString.contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex"
170                 + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy"));
171     }
172
173     /**
174      * Test Bad Http code Filter.
175      */
176     @Test
177     public void testBadCodeFilter() throws Exception {
178         System.setOut(new PrintStream(outContent));
179         System.setErr(new PrintStream(errContent));
180
181         final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json"};
182         final ApexMain apexMain = new ApexMain(args);
183
184         ThreadUtilities.sleep(500);
185
186         apexMain.shutdown();
187
188         final String outString = outContent.toString();
189
190         System.setOut(stdout);
191         System.setErr(stderr);
192
193         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
194         assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
195     }
196
197     /**
198      * Test Http code filter set and tag Url are transformed correctly.
199      */
200     @Test
201     public void testReplaceUrlTag() throws Exception {
202         final Client client = ClientBuilder.newClient();
203
204         final String[] args = {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json"};
205         final ApexMain apexMain = new ApexMain(args);
206         ThreadUtilities.sleep(1000);
207         apexMain.shutdown();
208
209         final String outString = outContent.toString();
210         System.setOut(stdout);
211         System.setErr(stderr);
212
213         Response response = null;
214         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
215                 .request("application/json").get();
216
217         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
218         final String responseEntity = response.readEntity(String.class);
219         assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
220     }
221
222     /**
223      * Test Http code filter set and multi-tag Url are transformed correctly.
224      */
225     @Test
226     public void testReplaceUrlMultiTag() throws Exception {
227         final Client client = ClientBuilder.newClient();
228         // @formatter:off
229         final String[] args = {
230             "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json"
231         };
232         // @formatter:on
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 }