94bd6b2b392a87e703483d3ad526da775a6c1e43
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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 static org.junit.Assert.fail;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.net.URI;
31
32 import org.glassfish.grizzly.http.server.HttpServer;
33 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
34 import org.glassfish.jersey.server.ResourceConfig;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
40 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
41 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
42 import org.onap.policy.apex.model.utilities.TextFileUtils;
43 import org.onap.policy.apex.service.engine.main.ApexMain;
44 import org.slf4j.ext.XLogger;
45 import org.slf4j.ext.XLoggerFactory;
46
47 /**
48  * The Class TestRest2File.
49  */
50 public class TestRest2File {
51     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestRest2File.class);
52
53     private static final String BASE_URI = "http://localhost:32801/TestRest2File";
54     private HttpServer server;
55
56     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
58
59     private final PrintStream stdout = System.out;
60     private final PrintStream stderr = System.err;
61
62     /**
63      * Clear relative file root environment variable.
64      */
65     @Before
66     public void clearRelativeFileRoot() {
67         System.clearProperty("APEX_RELATIVE_FILE_ROOT");
68     }
69
70     /**
71      * Sets the up.
72      *
73      * @throws Exception the exception
74      */
75     @Before
76     public void setUp() throws Exception {
77         final ResourceConfig rc = new ResourceConfig(TestRestClientEndpoint.class);
78         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
79
80         while (!server.isStarted()) {
81             ThreadUtilities.sleep(50);
82         }
83     }
84
85     /**
86      * Tear down.
87      *
88      * @throws Exception the exception
89      */
90     @After
91     public void tearDown() throws Exception {
92         server.shutdown();
93     }
94
95     /**
96      * Delete temp files.
97      */
98     @AfterClass
99     public static void deleteTempFiles() {
100         new File("src/test/resources/events/EventsOut.json").delete();
101     }
102
103     /**
104      * Test rest events in.
105      *
106      * @throws MessagingException the messaging exception
107      * @throws ApexException the apex exception
108      * @throws IOException Signals that an I/O exception has occurred.
109      */
110     @Test
111     public void testRestEventsIn() throws MessagingException, ApexException, IOException {
112         final String[] args = { "-rfr", "target", "-c", "target/examples/config/SampleDomain/REST2FileJsonEvent.json" };
113
114         final ApexMain apexMain = new ApexMain(args);
115
116         ThreadUtilities.sleep(1000);
117         apexMain.shutdown();
118
119         final String outputEventText = TextFileUtils
120             .getTextFileAsString("target/examples/events/SampleDomain/EventsOut.json");
121
122         if (!outputEventText.contains(
123             "04\",\n" + "  \"version\": \"0.0.1\",\n" + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\"")) {
124             LOGGER.error(outputEventText);
125             fail("test output did not contain required string");
126         }
127     }
128
129     /**
130      * Test file empty events.
131      *
132      * @throws MessagingException the messaging exception
133      * @throws ApexException the apex exception
134      * @throws IOException Signals that an I/O exception has occurred.
135      */
136     @Test
137     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
138         System.setOut(new PrintStream(outContent));
139         System.setErr(new PrintStream(errContent));
140
141         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEmptyEvents.json" };
142         final ApexMain apexMain = new ApexMain(args);
143
144         ThreadUtilities.sleep(1000);
145         apexMain.shutdown();
146
147         final String outString = outContent.toString();
148
149         System.setOut(stdout);
150         System.setErr(stderr);
151
152         if (!outString.contains(
153             "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\"")) {
154             LOGGER.error(outString);
155             fail("test output did not contain required string");
156         }
157     }
158
159     /**
160      * Test file events no url.
161      *
162      * @throws MessagingException the messaging exception
163      * @throws ApexException the apex exception
164      * @throws IOException Signals that an I/O exception has occurred.
165      */
166     @Test
167     public void testFileEventsNoUrl() throws MessagingException, ApexException, IOException {
168         System.setOut(new PrintStream(outContent));
169         System.setErr(new PrintStream(errContent));
170
171         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventNoURL.json" };
172         final ApexMain apexMain = new ApexMain(args);
173
174         ThreadUtilities.sleep(1000);
175         apexMain.shutdown();
176
177         final String outString = outContent.toString();
178
179         System.setOut(stdout);
180         System.setErr(stderr);
181
182         if (!outString.contains(" no URL has been set for event sending on REST client")) {
183             LOGGER.error(outString);
184             fail("test output did not contain required string");
185         }
186     }
187
188     /**
189      * Test file events bad url.
190      *
191      * @throws MessagingException the messaging exception
192      * @throws ApexException the apex exception
193      * @throws IOException Signals that an I/O exception has occurred.
194      */
195     @Test
196     public void testFileEventsBadUrl() throws MessagingException, ApexException, IOException {
197         System.setOut(new PrintStream(outContent));
198         System.setErr(new PrintStream(errContent));
199
200         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadURL.json" };
201         final ApexMain apexMain = new ApexMain(args);
202
203         ThreadUtilities.sleep(1000);
204         apexMain.shutdown();
205
206         final String outString = outContent.toString();
207
208         System.setOut(stdout);
209         System.setErr(stderr);
210
211         if (!outString.contains(
212             "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404")) {
213             LOGGER.error(outString);
214             fail("test output did not contain required string");
215         }
216     }
217
218     /**
219      * Test file events bad http method.
220      *
221      * @throws MessagingException the messaging exception
222      * @throws ApexException the apex exception
223      * @throws IOException Signals that an I/O exception has occurred.
224      */
225     @Test
226     public void testFileEventsBadHttpMethod() throws MessagingException, ApexException, IOException {
227         System.setOut(new PrintStream(outContent));
228         System.setErr(new PrintStream(errContent));
229
230         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json" };
231         final ApexMain apexMain = new ApexMain(args);
232
233         ThreadUtilities.sleep(1000);
234         apexMain.shutdown();
235
236         final String outString = outContent.toString();
237
238         System.setOut(stdout);
239         System.setErr(stderr);
240
241         if (!outString.contains("specified HTTP method of \"POST\" is invalid, "
242             + "only HTTP method \"GET\" is supported for event reception on REST client consumer")) {
243             LOGGER.error(outString);
244             fail("test output did not contain required string");
245         }
246     }
247
248     /**
249      * Test file events bad response.
250      *
251      * @throws MessagingException the messaging exception
252      * @throws ApexException the apex exception
253      * @throws IOException Signals that an I/O exception has occurred.
254      */
255     @Test
256     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
257         System.setOut(new PrintStream(outContent));
258         System.setErr(new PrintStream(errContent));
259
260         final String[] args = { "src/test/resources/prodcons/REST2FileJsonEventBadResponse.json" };
261         final ApexMain apexMain = new ApexMain(args);
262
263         ThreadUtilities.sleep(1000);
264         apexMain.shutdown();
265
266         final String outString = outContent.toString();
267
268         System.setOut(stdout);
269         System.setErr(stderr);
270
271         if (!outString.contains(
272             "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\"")) {
273             LOGGER.error(outString);
274             fail("test output did not contain required string");
275         }
276     }
277 }