d4eb434c728b712f7714d4d5d944bdacceded934
[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.restrequestor;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import com.google.gson.Gson;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.net.URI;
33 import java.util.Map;
34
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.core.Response;
38
39 import org.glassfish.grizzly.http.server.HttpServer;
40 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
41 import org.glassfish.jersey.server.ResourceConfig;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
47 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
48 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.service.engine.main.ApexMain;
50
51 public class TestRESTRequestor {
52     private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor";
53     private static HttpServer server;
54
55     private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56     private ByteArrayOutputStream errContent = new ByteArrayOutputStream();
57
58     private final PrintStream stdout = System.out;
59     private final PrintStream stderr = System.err;
60
61     @BeforeClass
62     public static void setUp() throws Exception {
63         final ResourceConfig rc = new ResourceConfig(TestRESTREequestorEndpoint.class);
64         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
65
66         while (!server.isStarted()) {
67             ThreadUtilities.sleep(50);
68         }
69     }
70
71     @AfterClass
72     public static void tearDown() throws Exception {
73         server.shutdownNow();
74
75         new File("src/test/resources/events/EventsOut.json").delete();
76         new File("src/test/resources/events/EventsOutMulti0.json").delete();
77         new File("src/test/resources/events/EventsOutMulti1.json").delete();
78     }
79
80     @Before
81     public void resetCounters() {
82         TestRESTREequestorEndpoint.resetCounters();
83     }
84
85     @Test
86     public void testRESTRequestorGet() throws MessagingException, ApexException, IOException {
87         final Client client = ClientBuilder.newClient();
88
89         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" };
90         final ApexMain apexMain = new ApexMain(args);
91
92         // Wait for the required amount of events to be received or for 10 seconds
93         Double getsSoFar = 0.0;
94         for (int i = 0; i < 40; i++) {
95             ThreadUtilities.sleep(100);
96
97             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
98                     .request("application/json").get();
99
100             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
101             final String responseString = response.readEntity(String.class);
102
103             @SuppressWarnings("unchecked")
104             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
105             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
106
107             if (getsSoFar >= 50.0) {
108                 break;
109             }
110         }
111
112         apexMain.shutdown();
113         client.close();
114
115         assertEquals(Double.valueOf(50.0), getsSoFar);
116     }
117
118     @Test
119     public void testRESTRequestorPut() throws MessagingException, ApexException, IOException {
120         final Client client = ClientBuilder.newClient();
121
122         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" };
123         final ApexMain apexMain = new ApexMain(args);
124
125         // Wait for the required amount of events to be received or for 10 seconds
126         Double putsSoFar = 0.0;
127         for (int i = 0; i < 40; i++) {
128             ThreadUtilities.sleep(100);
129
130             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
131                     .request("application/json").get();
132
133             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
134             final String responseString = response.readEntity(String.class);
135
136             @SuppressWarnings("unchecked")
137             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
138             putsSoFar = Double.valueOf(jsonMap.get("PUT").toString());
139
140             if (putsSoFar >= 50.0) {
141                 break;
142             }
143         }
144
145         apexMain.shutdown();
146         client.close();
147
148         assertEquals(Double.valueOf(50.0), putsSoFar);
149     }
150
151     @Test
152     public void testRESTRequestorPost() throws MessagingException, ApexException, IOException {
153         final Client client = ClientBuilder.newClient();
154
155         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" };
156         final ApexMain apexMain = new ApexMain(args);
157
158         // Wait for the required amount of events to be received or for 10 seconds
159         Double postsSoFar = 0.0;
160         for (int i = 0; i < 40; i++) {
161             ThreadUtilities.sleep(100);
162
163             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
164                     .request("application/json").get();
165
166             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
167             final String responseString = response.readEntity(String.class);
168
169             @SuppressWarnings("unchecked")
170             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
171             postsSoFar = Double.valueOf(jsonMap.get("POST").toString());
172
173             if (postsSoFar >= 50.0) {
174                 break;
175             }
176         }
177
178         apexMain.shutdown();
179         client.close();
180
181         assertEquals(Double.valueOf(50.0), postsSoFar);
182     }
183
184     @Test
185     public void testRESTRequestorDelete() throws MessagingException, ApexException, IOException {
186         final Client client = ClientBuilder.newClient();
187
188         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" };
189         final ApexMain apexMain = new ApexMain(args);
190
191         // Wait for the required amount of events to be received or for 10 seconds
192         Double deletesSoFar = 0.0;
193         for (int i = 0; i < 40; i++) {
194             ThreadUtilities.sleep(100);
195
196             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
197                     .request("application/json").get();
198
199             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
200             final String responseString = response.readEntity(String.class);
201
202             @SuppressWarnings("unchecked")
203             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
204             deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString());
205
206             if (deletesSoFar >= 50.0) {
207                 break;
208             }
209         }
210
211         apexMain.shutdown();
212         client.close();
213
214         assertEquals(Double.valueOf(50.0), deletesSoFar);
215     }
216
217     @Test
218     public void testRESTRequestorMultiInputs() throws MessagingException, ApexException, IOException {
219         final Client client = ClientBuilder.newClient();
220
221         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" };
222         final ApexMain apexMain = new ApexMain(args);
223
224         // Wait for the required amount of events to be received or for 10 seconds
225         Double getsSoFar = 0.0;
226         for (int i = 0; i < 40; i++) {
227             ThreadUtilities.sleep(100);
228
229             final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats")
230                     .request("application/json").get();
231
232             assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
233             final String responseString = response.readEntity(String.class);
234
235             @SuppressWarnings("unchecked")
236             final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class);
237             getsSoFar = Double.valueOf(jsonMap.get("GET").toString());
238
239             if (getsSoFar >= 8.0) {
240                 break;
241             }
242         }
243
244         apexMain.shutdown();
245         client.close();
246
247         assertEquals(Double.valueOf(8.0), getsSoFar);
248
249         ThreadUtilities.sleep(1000);
250     }
251
252     @Test
253     public void testRESTRequestorProducerAlone() throws MessagingException, ApexException, IOException {
254         System.setOut(new PrintStream(outContent));
255         System.setErr(new PrintStream(errContent));
256
257         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" };
258
259         final ApexMain apexMain = new ApexMain(args);
260         ThreadUtilities.sleep(200);
261         apexMain.shutdown();
262
263         final String outString = outContent.toString();
264
265         System.setOut(stdout);
266         System.setErr(stderr);
267
268         assertTrue(outString.contains(
269                 "REST Requestor producer (RestRequestorProducer) must run in peered requestor mode with a REST Requestor consumer"));
270     }
271
272     @Test
273     public void testRESTRequestorConsumerAlone() throws MessagingException, ApexException, IOException {
274         System.setOut(new PrintStream(outContent));
275         System.setErr(new PrintStream(errContent));
276
277         final String[] args = { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" };
278
279         final ApexMain apexMain = new ApexMain(args);
280         ThreadUtilities.sleep(200);
281         apexMain.shutdown();
282
283         final String outString = outContent.toString();
284
285         System.setOut(stdout);
286         System.setErr(stderr);
287
288         assertTrue(outString.contains(
289                 "event input for peered mode \"REQUESTOR\": peer \"RestRequestorProducer\" for event handler \"RestRequestorConsumer\" does not exist or is not defined as being synchronous"));
290     }
291 }