Remove drools PDP dependency
[policy/models.git] / models-interactions / model-impl / so / src / test / java / org / onap / policy / so / SoManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * TestSOManager
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
8  * Modifications Copyright (C) 2019 Nordix Foundation.
9  * ================================================================================
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.so;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32
33 import java.io.IOException;
34 import java.net.URI;
35 import java.util.UUID;
36 import java.util.concurrent.Future;
37 import org.apache.http.client.ClientProtocolException;
38 import org.apache.http.client.methods.CloseableHttpResponse;
39 import org.apache.http.client.methods.HttpGet;
40 import org.apache.http.impl.client.CloseableHttpClient;
41 import org.apache.http.impl.client.HttpClients;
42 import org.apache.http.util.EntityUtils;
43 import org.glassfish.grizzly.http.server.HttpServer;
44 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
45 import org.glassfish.jersey.server.ResourceConfig;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.onap.policy.so.SoManager.SoCallback;
50
51 public class SoManagerTest implements SoCallback {
52     private static final String BASE_URI = "http://localhost:46553/TestSOManager";
53     private static final String BASE_SO_URI = BASE_URI + "/SO";
54     private static HttpServer server;
55
56     /**
57      * Set up test class.
58      */
59     @BeforeClass
60     public static void setUp() throws IOException {
61         final ResourceConfig rc = new ResourceConfig(SoDummyServer.class);
62         //Grizzly by default doesn't allow payload for HTTP methods (ex: DELETE), for which HTTP spec doesn't
63         // explicitly state that.
64         //allow it before starting the server
65         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc, false);
66         server.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true);
67         server.start();
68     }
69
70     @AfterClass
71     public static void tearDown() throws Exception {
72         server.shutdown();
73     }
74
75     @Test
76     public void testGrizzlyServer() throws ClientProtocolException, IOException {
77         CloseableHttpClient httpclient = HttpClients.createDefault();
78         HttpGet httpGet = new HttpGet("http://localhost:46553/TestSOManager/SO/Stats");
79         CloseableHttpResponse response = httpclient.execute(httpGet);
80
81         String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
82         assertTrue(returnBody.matches("^\\{\"GET\": [0-9]*,\"STAT\": [0-9]*,\"POST\": [0-9]*,\"PUT\": [0-9]*,"
83                 + "\"DELETE\": [0-9]*\\}$"));
84     }
85
86     @Test
87     public void testServiceInstantiation() throws IOException {
88         SoManager manager = new SoManager(null, null, null);
89         assertNotNull(manager);
90         manager.setRestGetTimeout(100);
91
92         SoResponse response = manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean",
93                 "citizen", null);
94         assertNull(response);
95
96         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
97                         "citizen", null);
98         assertNull(response);
99
100         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
101                         "citizen", new SoRequest());
102         assertNull(response);
103
104         SoRequest request = new SoRequest();
105         request.setRequestId(UUID.randomUUID());
106         request.setRequestScope("Test");
107         request.setRequestType("ReturnBadJson");
108         request.setStartTime("2018-03-23 16:31");
109         request.setRequestStatus(new SoRequestStatus());
110         request.getRequestStatus().setRequestState("ONGOING");
111
112         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
113                         "citizen", request);
114         assertNull(response);
115
116         request.setRequestType("ReturnCompleted");
117         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
118                         "citizen", request);
119         assertNotNull(response);
120         assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
121
122         request.setRequestType("ReturnFailed");
123         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
124                         "citizen", request);
125         assertNotNull(response);
126         assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
127
128         // Use scope to set the number of iterations we'll wait for
129
130         request.setRequestType("ReturnOnging200");
131         request.setRequestScope(new Integer(10).toString());
132         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
133                         "citizen", request);
134         assertNotNull(response);
135         assertNotNull(response.getRequest());
136         assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
137
138         request.setRequestType("ReturnOnging202");
139         request.setRequestScope(new Integer(20).toString());
140         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
141                         "citizen", request);
142         assertNotNull(response);
143         assertNotNull(response.getRequest());
144         assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
145
146         // Test timeout after 20 attempts for a response
147         request.setRequestType("ReturnOnging202");
148         request.setRequestScope(new Integer(21).toString());
149         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
150                         "citizen", request);
151         assertNull(response);
152
153         // Test bad response after 3 attempts for a response
154         request.setRequestType("ReturnBadAfterWait");
155         request.setRequestScope(new Integer(3).toString());
156         response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
157                         "citizen", request);
158         assertNull(response);
159     }
160
161     @Test
162     public void testVfModuleCreation() throws IOException {
163         SoManager manager = new SoManager("http:/localhost:99999999", "sean", "citizen");
164         assertNotNull(manager);
165         manager.setRestGetTimeout(100);
166
167         SoRequest soRequest = new SoRequest();
168         soRequest.setOperationType(SoOperationType.SCALE_OUT);
169         Future<SoResponse> asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this,
170                         UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
171         try {
172             SoResponse response = asyncRestCallFuture.get();
173             assertEquals(999, response.getHttpResponseCode());
174         } catch (Exception e) {
175             fail("test should not throw an exception");
176         }
177
178         manager = new SoManager(BASE_SO_URI, "sean", "citizen");
179         manager.setRestGetTimeout(100);
180         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
181                         UUID.randomUUID().toString(), soRequest);
182         try {
183             SoResponse response = asyncRestCallFuture.get();
184             assertEquals(999, response.getHttpResponseCode());
185         } catch (Exception e) {
186             fail("test should not throw an exception");
187         }
188
189         SoRequest request = new SoRequest();
190         request.setRequestId(UUID.randomUUID());
191         request.setRequestScope("Test");
192         request.setRequestType("ReturnBadJson");
193         request.setStartTime("2018-03-23 16:31");
194         request.setRequestStatus(new SoRequestStatus());
195         request.getRequestStatus().setRequestState("ONGOING");
196         request.setOperationType(SoOperationType.SCALE_OUT);
197
198         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
199                         UUID.randomUUID().toString(), request);
200         try {
201             SoResponse response = asyncRestCallFuture.get();
202             assertEquals(999, response.getHttpResponseCode());
203         } catch (Exception e) {
204             fail("test should not throw an exception");
205         }
206
207         request.setRequestType("ReturnCompleted");
208
209         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
210                         UUID.randomUUID().toString(), request);
211         try {
212             SoResponse response = asyncRestCallFuture.get();
213             assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
214         } catch (Exception e) {
215             fail("test should not throw an exception");
216         }
217
218         request.setRequestType("ReturnFailed");
219         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
220                         UUID.randomUUID().toString(), request);
221         try {
222             SoResponse response = asyncRestCallFuture.get();
223             assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
224         } catch (Exception e) {
225             fail("test should not throw an exception");
226         }
227
228         // Use scope to set the number of iterations we'll wait for
229
230         request.setRequestType("ReturnOnging200");
231         request.setRequestScope(new Integer(10).toString());
232         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
233                         UUID.randomUUID().toString(), request);
234         try {
235             SoResponse response = asyncRestCallFuture.get();
236             assertNotNull(response.getRequest());
237             assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
238         } catch (Exception e) {
239             fail("test should not throw an exception");
240         }
241
242         request.setRequestType("ReturnOnging202");
243         request.setRequestScope(new Integer(20).toString());
244         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
245                         UUID.randomUUID().toString(), request);
246         try {
247             SoResponse response = asyncRestCallFuture.get();
248             assertNotNull(response.getRequest());
249             assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
250         } catch (Exception e) {
251             fail("test should not throw an exception");
252         }
253
254         // Test timeout after 20 attempts for a response
255         request.setRequestType("ReturnOnging202");
256         request.setRequestScope(new Integer(21).toString());
257         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
258                         UUID.randomUUID().toString(), request);
259         try {
260             SoResponse response = asyncRestCallFuture.get();
261             assertEquals(999, response.getHttpResponseCode());
262         } catch (Exception e) {
263             fail("test should not throw an exception");
264         }
265
266         // Test bad response after 3 attempts for a response
267         request.setRequestType("ReturnBadAfterWait");
268         request.setRequestScope(new Integer(3).toString());
269         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
270                         UUID.randomUUID().toString(), request);
271         try {
272             SoResponse response = asyncRestCallFuture.get();
273             assertEquals(999, response.getHttpResponseCode());
274         } catch (Exception e) {
275             fail("test should not throw an exception");
276         }
277     }
278
279     @Test
280     public void testVfModuleDeletion() {
281         SoManager manager = new SoManager("http:/localhost:99999999", "sean", "citizen");
282         assertNotNull(manager);
283         manager.setRestGetTimeout(100);
284
285         SoRequest soRequest = new SoRequest();
286         soRequest.setOperationType(SoOperationType.DELETE_VF_MODULE);
287         Future<SoResponse> asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this,
288                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
289         try {
290             SoResponse response = asyncRestCallFuture.get();
291             assertEquals(999, response.getHttpResponseCode());
292         } catch (Exception e) {
293             fail("test should not throw an exception");
294         }
295
296         manager = new SoManager(BASE_SO_URI, "sean", "citizen");
297         manager.setRestGetTimeout(100);
298
299         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
300                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
301         try {
302             SoResponse response = asyncRestCallFuture.get();
303             assertEquals(999, response.getHttpResponseCode());
304         } catch (Exception e) {
305             fail("test should not throw an exception");
306         }
307
308         SoRequest request = new SoRequest();
309         request.setRequestId(UUID.randomUUID());
310         request.setRequestScope("Test");
311         request.setRequestType("ReturnBadJson");
312         request.setStartTime("2018-03-23 16:31");
313         request.setRequestStatus(new SoRequestStatus());
314         request.getRequestStatus().setRequestState("ONGOING");
315         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
316
317         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
318                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
319         try {
320             SoResponse response = asyncRestCallFuture.get();
321             assertEquals(999, response.getHttpResponseCode());
322         } catch (Exception e) {
323             fail("test should not throw an exception");
324         }
325
326         request.setRequestType("ReturnCompleted");
327
328         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
329                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
330         try {
331             SoResponse response = asyncRestCallFuture.get();
332             assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
333         } catch (Exception e) {
334             fail("test should not throw an exception");
335         }
336
337         request.setRequestType("ReturnFailed");
338         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
339                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
340         try {
341             SoResponse response = asyncRestCallFuture.get();
342             assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
343         } catch (Exception e) {
344             fail("test should not throw an exception");
345         }
346
347         // Use scope to set the number of iterations we'll wait for
348
349         request.setRequestType("ReturnOnging200");
350         request.setRequestScope(new Integer(10).toString());
351         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
352                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
353         try {
354             SoResponse response = asyncRestCallFuture.get();
355             assertNotNull(response.getRequest());
356             assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
357         } catch (Exception e) {
358             fail("test should not throw an exception");
359         }
360
361         request.setRequestType("ReturnOnging202");
362         request.setRequestScope(new Integer(20).toString());
363         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
364                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
365         try {
366             SoResponse response = asyncRestCallFuture.get();
367             assertNotNull(response.getRequest());
368             assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
369         } catch (Exception e) {
370             fail("test should not throw an exception");
371         }
372
373         // Test timeout after 20 attempts for a response
374         request.setRequestType("ReturnOnging202");
375         request.setRequestScope(new Integer(21).toString());
376         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
377                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
378         try {
379             SoResponse response = asyncRestCallFuture.get();
380             assertEquals(999, response.getHttpResponseCode());
381         } catch (Exception e) {
382             fail("test should not throw an exception");
383         }
384
385         // Test bad response after 3 attempts for a response
386         request.setRequestType("ReturnBadAfterWait");
387         request.setRequestScope(new Integer(3).toString());
388         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
389                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
390         try {
391             SoResponse response = asyncRestCallFuture.get();
392             assertEquals(999, response.getHttpResponseCode());
393         } catch (Exception e) {
394             fail("test should not throw an exception");
395         }
396     }
397
398     @Override
399     public void onSoResponseWrapper(SoResponseWrapper wrapper) {
400         //
401         // Nothing really needed to do
402         //
403     }
404 }