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