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