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