25ff12243f406e7704d6a4f61fdff2c367b44367
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2020-2021 Bell Canada. 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.assertThatThrownBy;
26 import static org.junit.Assert.fail;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.service.engine.main.ApexMain;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
40 import org.onap.policy.common.gson.GsonMessageBodyHandler;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
43 import org.slf4j.ext.XLogger;
44 import org.slf4j.ext.XLoggerFactory;
45
46 /**
47  * The Class TestRest2File.
48  */
49 public class TestRest2File {
50     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
51
52     private static final int PORT = 32801;
53     private static HttpServletServer 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      * Clear relative file root environment variable.
63      */
64     @Before
65     public void clearRelativeFileRoot() {
66         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
67     }
68
69     /**
70      * Sets the up.
71      *
72      * @throws Exception the exception
73      */
74     @Before
75     public void setUp() throws Exception {
76         server = HttpServletServerFactoryInstance.getServerFactory().build("TestRest2File", false, null, PORT,
77             "/TestRest2File", false, false);
78
79         server.addServletClass(null, TestRestClientEndpoint.class.getName());
80         server.setSerializationProvider(GsonMessageBodyHandler.class.getName());
81
82         server.start();
83
84         if (!NetworkUtil.isTcpPortOpen("localHost", PORT, 60, 500L)) {
85             throw new IllegalStateException("port " + PORT + " is still not in use");
86         }
87     }
88
89     /**
90      * Tear down.
91      *
92      * @throws Exception the exception
93      */
94     @After
95     public void tearDown() throws Exception {
96         if (server != null) {
97             server.stop();
98         }
99     }
100
101     /**
102      * Test rest events in.
103      *
104      * @throws MessagingException the messaging exception
105      * @throws ApexException the apex exception
106      * @throws IOException Signals that an I/O exception has occurred.
107      */
108     @Test
109     public void testRestEventsIn() throws MessagingException, ApexException, IOException {
110         final String[] args = {"-rfr", "target", "-p", "target/examples/config/SampleDomain/REST2FileJsonEvent.json"};
111
112         final ApexMain apexMain = new ApexMain(args);
113
114         ThreadUtilities.sleep(5000);
115         apexMain.shutdown();
116
117         final String outputEventText =
118             TextFileUtils.getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
119
120         checkRequiredString(outputEventText,
121             "04\",\n" + "  \"version\": \"0.0.1\",\n" + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\"");
122     }
123
124     /**
125      * Test file empty events.
126      *
127      * @throws MessagingException the messaging exception
128      * @throws ApexException the apex exception
129      * @throws IOException Signals that an I/O exception has occurred.
130      */
131     @Test
132     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
133         System.setOut(new PrintStream(outContent));
134         System.setErr(new PrintStream(errContent));
135
136         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEmptyEvents.json"};
137         final ApexMain apexMain = new ApexMain(args);
138
139         ThreadUtilities.sleep(5000);
140         apexMain.shutdown();
141
142         final String outString = outContent.toString();
143
144         System.setOut(stdout);
145         System.setErr(stderr);
146
147         checkRequiredString(outString,
148             "received an empty event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"");
149     }
150
151     /**
152      * Test file events no url.
153      *
154      * @throws MessagingException the messaging exception
155      * @throws ApexException the apex exception
156      * @throws IOException Signals that an I/O exception has occurred.
157      */
158     @Test
159     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
160         System.setOut(new PrintStream(outContent));
161         System.setErr(new PrintStream(errContent));
162
163         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventNoURL.json"};
164         final ApexMain apexMain = new ApexMain(args);
165
166         ThreadUtilities.sleep(5000);
167         apexMain.shutdown();
168
169         final String outString = outContent.toString();
170
171         System.setOut(stdout);
172         System.setErr(stderr);
173
174         checkRequiredString(outString, " no URL has been set for event sending on RESTCLIENT");
175     }
176
177     /**
178      * Test file events bad url.
179      *
180      * @throws MessagingException the messaging exception
181      * @throws ApexException the apex exception
182      * @throws IOException Signals that an I/O exception has occurred.
183      */
184     @Test
185     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
186         System.setOut(new PrintStream(outContent));
187         System.setErr(new PrintStream(errContent));
188
189         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadURL.json"};
190         final ApexMain apexMain = new ApexMain(args);
191
192         ThreadUtilities.sleep(5000);
193         apexMain.shutdown();
194
195         final String outString = outContent.toString();
196
197         System.setOut(stdout);
198         System.setErr(stderr);
199
200         checkRequiredString(outString, "reception of event from URL "
201             + "\"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404");
202     }
203
204     /**
205      * Test file events bad http method.
206      */
207     @Test
208     public void testFileEventsBadHttpMethod() {
209         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json"};
210         assertThatThrownBy(() -> new ApexMain(args))
211             .hasRootCauseMessage("specified HTTP method of \"POST\" is invalid, "
212                 + "only HTTP method \"GET\" is supported for event reception on REST client consumer (FirstConsumer)");
213
214     }
215
216     /**
217      * Test file events bad response.
218      *
219      * @throws MessagingException the messaging exception
220      * @throws ApexException the apex exception
221      * @throws IOException Signals that an I/O exception has occurred.
222      */
223     @Test
224     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
225         System.setOut(new PrintStream(outContent));
226         System.setErr(new PrintStream(errContent));
227
228         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadResponse.json"};
229         final ApexMain apexMain = new ApexMain(args);
230
231         ThreadUtilities.sleep(5000);
232         apexMain.shutdown();
233
234         final String outString = outContent.toString();
235
236         System.setOut(stdout);
237         System.setErr(stderr);
238
239         checkRequiredString(outString,
240             "reception of event from URL " + "\"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" "
241                 + "failed with status code 400 and message \"");
242     }
243
244     /**
245      * Check if a required string exists in the output.
246      *
247      * @param outputEventText the text to examine
248      * @param requiredString the string to search for
249      */
250     private void checkRequiredString(String outputEventText, String requiredString) {
251         if (!outputEventText.contains(requiredString)) {
252             LOGGER.error("\n***output text:\n" + outputEventText + "\n***");
253             fail("\n***test output did not contain required string:\n" + requiredString + "\n***");
254         }
255     }
256 }