d304eb9d039659e2ae1a625592debab0115b18a4
[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.apps.uservice.test.adapt.restclient;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.net.URI;
30 import java.util.Map;
31
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.core.Response;
35
36 import org.glassfish.grizzly.http.server.HttpServer;
37 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
38 import org.glassfish.jersey.server.ResourceConfig;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
43 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.service.engine.main.ApexMain;
46
47 import com.google.gson.Gson;
48
49
50 public class TestFile2REST {
51     private static final String BASE_URI = "http://localhost:32801/TestFile2Rest";
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     @BeforeClass
61     public static void setUp() throws Exception {
62         final ResourceConfig rc = new ResourceConfig(TestRESTClientEndpoint.class);
63         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
64
65         while (!server.isStarted()) {
66             ThreadUtilities.sleep(50);
67         }
68     }
69
70     @AfterClass
71     public static void tearDown() throws Exception {
72         server.shutdown();
73     }
74
75     @Test
76     public void testFileEventsPost() throws MessagingException, ApexException, IOException {
77         final Client client = ClientBuilder.newClient();
78
79         final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPost.json"};
80         final ApexMain apexMain = new ApexMain(args);
81
82         // Wait for the required amount of events to be received or for 10 seconds
83         for (int i = 0; i < 100; i++) {
84             ThreadUtilities.sleep(100);
85             final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
86                     .request("application/json").get();
87
88             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
89             final String responseString = response.readEntity(String.class);
90
91             @SuppressWarnings("unchecked")
92             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
93             if ((double) jsonMap.get("POST") == 100) {
94                 break;
95             }
96         }
97
98         apexMain.shutdown();
99     }
100
101     @Test
102     public void testFileEventsPut() throws MessagingException, ApexException, IOException {
103         final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPut.json"};
104         final ApexMain apexMain = new ApexMain(args);
105
106         final Client client = ClientBuilder.newClient();
107
108         // Wait for the required amount of events to be received or for 10 seconds
109         for (int i = 0; i < 100; i++) {
110             ThreadUtilities.sleep(100);
111             final Response response = client.target("http://localhost:32801/TestFile2Rest/apex/event/Stats")
112                     .request("application/json").get();
113
114             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
115             final String responseString = response.readEntity(String.class);
116
117             @SuppressWarnings("unchecked")
118             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
119             if ((double) jsonMap.get("PUT") == 100) {
120                 break;
121             }
122         }
123
124         apexMain.shutdown();
125     }
126
127     @Test
128     public void testFileEventsNoURL() throws MessagingException, ApexException, IOException {
129         System.setOut(new PrintStream(outContent));
130         System.setErr(new PrintStream(errContent));
131
132         final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventNoURL.json"};
133         final ApexMain apexMain = new ApexMain(args);
134
135         ThreadUtilities.sleep(200);
136         apexMain.shutdown();
137
138         final String outString = outContent.toString();
139
140         System.setOut(stdout);
141         System.setErr(stderr);
142
143         assertTrue(outString.contains("  no URL has been set for event sending on REST client"));
144     }
145
146     @Test
147     public void testFileEventsBadURL() throws MessagingException, ApexException, IOException {
148         System.setOut(new PrintStream(outContent));
149         System.setErr(new PrintStream(errContent));
150
151         final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventBadURL.json"};
152         final ApexMain apexMain = new ApexMain(args);
153
154         ThreadUtilities.sleep(200);
155         apexMain.shutdown();
156
157         final String outString = outContent.toString();
158
159         System.setOut(stdout);
160         System.setErr(stderr);
161
162         assertTrue(outString.contains(
163                 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/Bad\" using HTTP \"POST\" failed with status code 404"));
164     }
165
166     @Test
167     public void testFileEventsBadHTTPMethod() 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/File2RESTJsonEventBadHTTPMethod.json"};
172         final ApexMain apexMain = new ApexMain(args);
173
174         ThreadUtilities.sleep(200);
175         apexMain.shutdown();
176
177         final String outString = outContent.toString();
178
179         System.setOut(stdout);
180         System.setErr(stderr);
181
182         assertTrue(outString.contains(
183                 "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" are supproted for event sending on REST client producer"));
184     }
185
186     @Test
187     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
188         System.setOut(new PrintStream(outContent));
189         System.setErr(new PrintStream(errContent));
190
191         final String[] args = {"src/test/resources/prodcons/File2RESTJsonEventPostBadResponse.json"};
192         final ApexMain apexMain = new ApexMain(args);
193
194         ThreadUtilities.sleep(500);
195         apexMain.shutdown();
196
197         final String outString = outContent.toString();
198
199         System.setOut(stdout);
200         System.setErr(stderr);
201
202         assertTrue(outString.contains(
203                 "send of event to URL \"http://localhost:32801/TestFile2Rest/apex/event/PostEventBadResponse\" using HTTP \"POST\" failed with status code 400"));
204     }
205 }