f3ce83c5af893cd7e5a6ea5d15cc381eff8177e4
[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 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 final String BASE_URI = "http://localhost:32801/TestExecutionRest";
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      * Compile the policy.
63      */
64     @BeforeClass
65     public static void compilePolicy() {
66         // @formatter:off
67         final String[] cliArgs = {
68             "-c",
69             "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex",
70             "-l",
71             "target/ExecutionPropertiesRestTestPolicyModel.log",
72             "-o",
73             "target/ExecutionPropertiesRestTestPolicyModel.json"
74         };
75         // @formatter:on
76
77         new ApexCommandLineEditorMain(cliArgs);
78     }
79
80     /**
81      * Sets the up.
82      *
83      * @throws Exception the exception
84      */
85     @BeforeClass
86     public static void setUp() throws Exception {
87         if (NetworkUtil.isTcpPortOpen("localHost", PORT, 3, 50L)) {
88             throw new IllegalStateException("port " + PORT + " is still in use");
89         }
90
91         server = HttpServletServerFactoryInstance.getServerFactory().build(
92             "TestExecutionPropertyRest", false, null, PORT, "/TestExecutionRest", false, false);
93
94         server.addServletClass(null, TestRestClientEndpoint.class.getName());
95         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
96
97         server.start();
98
99         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
100             throw new IllegalStateException("port " + PORT + " is still not in use");
101         }
102
103     }
104
105     /**
106      * Tear down.
107      *
108      * @throws Exception the exception
109      */
110     @AfterClass
111     public static void tearDown() throws Exception {
112         if (server != null) {
113             server.stop();
114         }
115     }
116
117     /**
118      * Clear relative file root environment variable.
119      */
120     @Before
121     public void clearRelativeFileRoot() {
122         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
123     }
124
125     /**
126      * Test Bad Url tag in Parameter .
127      */
128     @Test
129     public void testBadUrl() throws Exception {
130         System.setOut(new PrintStream(outContent));
131         System.setErr(new PrintStream(errContent));
132
133         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json" };
134         final ApexMain apexMain = new ApexMain(args);
135
136         ThreadUtilities.sleep(500);
137
138         apexMain.shutdown();
139
140         final String outString = outContent.toString();
141
142         System.setOut(stdout);
143         System.setErr(stderr);
144
145         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
146         assertTrue(outString.contains("no proper URL has been set for event sending on REST client"));
147     }
148
149     /**
150      * Test Not find value for tags in Url .
151      */
152     @Test
153     public void testNoValueSetForTagUrl() throws Exception {
154         System.setOut(new PrintStream(outContent));
155         System.setErr(new PrintStream(errContent));
156
157         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json" };
158         final ApexMain apexMain = new ApexMain(args);
159
160         ThreadUtilities.sleep(2000);
161
162         apexMain.shutdown();
163
164         final String outString = outContent.toString();
165
166         System.setOut(stdout);
167         System.setErr(stderr);
168
169         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
170         assertTrue(outString.contains("key\"Number\"specified on url \"http://localhost:32801/TestExecutionRest/apex"
171                 + "/event/{tagId}/{Number}\"not found in execution properties passed by the current policy"));
172     }
173
174     /**
175      * Test Bad Http code Filter.
176      */
177     @Test
178     public void testBadCodeFilter() throws Exception {
179         System.setOut(new PrintStream(outContent));
180         System.setErr(new PrintStream(errContent));
181
182         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json" };
183         final ApexMain apexMain = new ApexMain(args);
184
185         ThreadUtilities.sleep(500);
186
187         apexMain.shutdown();
188
189         final String outString = outContent.toString();
190
191         System.setOut(stdout);
192         System.setErr(stderr);
193
194         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
195         assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
196     }
197
198     /**
199      * Test Http code filter set and tag Url are transformed correctly.
200      */
201     @Test
202     public void testReplaceUrlTag() throws Exception {
203         final Client client = ClientBuilder.newClient();
204
205         final String[] args =
206             { "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         final String[] args =
231             { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json" };
232         final ApexMain apexMain = new ApexMain(args);
233         ThreadUtilities.sleep(1500);
234         apexMain.shutdown();
235
236         System.setOut(stdout);
237         System.setErr(stderr);
238         Response response = null;
239         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
240                 .request("application/json").get();
241         final String responseEntity = response.readEntity(String.class);
242         LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
243         assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));
244     }
245
246 }