6adb3b007aac44f8ac8e002c01fb8af5a051cdd3
[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 java.net.URI;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.core.Response;
31 import org.glassfish.grizzly.http.server.HttpServer;
32 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
39 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
40 import org.onap.policy.apex.service.engine.main.ApexMain;
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 final String BASE_URI = "http://localhost:32801/TestExecutionRest";
53     private static HttpServer server;
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         final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
88         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
89
90         if (NetworkUtil.isTcpPortOpen(BASE_URI, 32801, 1, 1L)) {
91             throw new IllegalStateException("port " + 32801 + " is still in use");
92         }
93
94     }
95
96     /**
97      * Tear down.
98      *
99      * @throws Exception the exception
100      */
101     @AfterClass
102     public static void tearDown() throws Exception {
103         server.shutdown();
104     }
105
106     /**
107      * Clear relative file root environment variable.
108      */
109     @Before
110     public void clearRelativeFileRoot() {
111         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
112     }
113
114     /**
115      * Test Bad Url tag in Parameter .
116      */
117     @Test
118     public void testBadUrl() throws Exception {
119         System.setOut(new PrintStream(outContent));
120         System.setErr(new PrintStream(errContent));
121
122         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json" };
123         final ApexMain apexMain = new ApexMain(args);
124
125         ThreadUtilities.sleep(500);
126
127         apexMain.shutdown();
128
129         final String outString = outContent.toString();
130
131         System.setOut(stdout);
132         System.setErr(stderr);
133
134         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
135         assertTrue(outString.contains("no proper URL has been set for event sending on REST client"));
136     }
137
138     /**
139      * Test Not find value for tags in Url .
140      */
141     @Test
142     public void testNoValueSetForTagUrl() throws Exception {
143         System.setOut(new PrintStream(outContent));
144         System.setErr(new PrintStream(errContent));
145
146         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json" };
147         final ApexMain apexMain = new ApexMain(args);
148
149         ThreadUtilities.sleep(2000);
150
151         apexMain.shutdown();
152
153         final String outString = outContent.toString();
154
155         System.setOut(stdout);
156         System.setErr(stderr);
157
158         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
159         assertTrue(outString.contains("key\"Number\"specified on url \"http://localhost:32801/TestExecutionRest/apex"
160                 + "/event/{tagId}/{Number}\"not found in execution properties passed by the current policy"));
161     }
162
163     /**
164      * Test Bad Http code Filter.
165      */
166     @Test
167     public void testBadCodeFilter() throws Exception {
168         System.setOut(new PrintStream(outContent));
169         System.setErr(new PrintStream(errContent));
170
171         final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json" };
172         final ApexMain apexMain = new ApexMain(args);
173
174         ThreadUtilities.sleep(500);
175
176         apexMain.shutdown();
177
178         final String outString = outContent.toString();
179
180         System.setOut(stdout);
181         System.setErr(stderr);
182
183         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
184         assertTrue(outString.contains("failed with status code 500 and message \"{\"testToRun\": FetchHttpCode}"));
185     }
186
187     /**
188      * Test Http code filter set and tag Url are transformed correctly.
189      */
190     @Test
191     public void testReplaceUrlTag() throws Exception {
192         final Client client = ClientBuilder.newClient();
193
194         final String[] args =
195             { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json" };
196         final ApexMain apexMain = new ApexMain(args);
197         ThreadUtilities.sleep(1000);
198         apexMain.shutdown();
199
200         final String outString = outContent.toString();
201         System.setOut(stdout);
202         System.setErr(stderr);
203
204         Response response = null;
205         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
206                 .request("application/json").get();
207
208         LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl");
209         final String responseEntity = response.readEntity(String.class);
210         assertTrue(responseEntity.contains("\"PostProperUrl\": 1"));
211     }
212
213     /**
214      * Test Http code filter set and multi-tag Url are transformed correctly.
215      */
216     @Test
217     public void testReplaceUrlMultiTag() throws Exception {
218         final Client client = ClientBuilder.newClient();
219         final String[] args =
220             { "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json" };
221         final ApexMain apexMain = new ApexMain(args);
222         ThreadUtilities.sleep(1500);
223         apexMain.shutdown();
224
225         System.setOut(stdout);
226         System.setErr(stderr);
227         Response response = null;
228         response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl")
229                 .request("application/json").get();
230         final String responseEntity = response.readEntity(String.class);
231         LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl");
232         assertTrue(responseEntity.contains("\"PostProperUrl\": 3"));
233     }
234
235 }