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