aa3562ae54e7e63c8035ad7752b3b23c2dc79ad7
[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
32 import java.io.IOException;
33 import java.net.URI;
34 import java.util.UUID;
35 import java.util.concurrent.Future;
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.glassfish.grizzly.http.server.HttpServer;
42 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
43 import org.glassfish.jersey.server.ResourceConfig;
44 import org.junit.AfterClass;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.policy.so.SoManager.SoCallback;
48
49 public class SoManagerTest implements SoCallback {
50     private static final String LOCALHOST_99999999 = "http:/localhost:99999999";
51     private static final String CITIZEN = "citizen";
52     private static final String RETURN_ONGING202 = "ReturnOnging202";
53     private static final String RETURN_ONGING200 = "ReturnOnging200";
54     private static final String RETURN_FAILED = "ReturnFailed";
55     private static final String RETURN_COMPLETED = "ReturnCompleted";
56     private static final String RETURN_BAD_JSON = "ReturnBadJson";
57     private static final String RETURN_BAD_AFTER_WAIT = "ReturnBadAfterWait";
58     private static final String ONGOING = "ONGOING";
59     private static final String FAILED = "FAILED";
60     private static final String COMPLETE = "COMPLETE";
61     private static final String DATE1 = "2018-03-23 16:31";
62     private static final String SERVICE_INSTANTIATION_V7 = "/serviceInstantiation/v7";
63     private static final String BASE_URI = "http://localhost:46553/TestSOManager";
64     private static final String BASE_SO_URI = BASE_URI + "/SO";
65     private static HttpServer server;
66
67     /**
68      * Set up test class.
69      */
70     @BeforeClass
71     public static void setUp() throws IOException {
72         final ResourceConfig rc = new ResourceConfig(SoDummyServer.class);
73         //Grizzly by default doesn't allow payload for HTTP methods (ex: DELETE), for which HTTP spec doesn't
74         // explicitly state that.
75         //allow it before starting the server
76         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc, false);
77         server.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true);
78         server.start();
79     }
80
81     @AfterClass
82     public static void tearDown() {
83         server.shutdown();
84     }
85
86     @Test
87     public void testGrizzlyServer() throws IOException {
88         try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
89             HttpGet httpGet = new HttpGet("http://localhost:46553/TestSOManager/SO/Stats");
90             CloseableHttpResponse response = httpclient.execute(httpGet);
91
92             String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
93             assertTrue(returnBody.matches("^\\{\"GET\": [0-9]*,\"STAT\": [0-9]*,\"POST\": [0-9]*,\"PUT\": [0-9]*,"
94                     + "\"DELETE\": [0-9]*\\}$"));
95         }
96     }
97
98     @Test
99     public void testServiceInstantiation() {
100         SoManager manager = new SoManager(null, null, null);
101         assertNotNull(manager);
102         manager.setRestGetTimeout(100);
103
104         SoResponse response = manager.createModuleInstance(LOCALHOST_99999999, BASE_SO_URI, "sean",
105                 CITIZEN, null);
106         assertNull(response);
107
108         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
109                         CITIZEN, null);
110         assertNull(response);
111
112         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
113                         CITIZEN, new SoRequest());
114         assertNull(response);
115
116         SoRequest request = new SoRequest();
117         request.setRequestId(UUID.randomUUID());
118         request.setRequestScope("Test");
119         request.setRequestType(RETURN_BAD_JSON);
120         request.setStartTime(DATE1);
121         request.setRequestStatus(new SoRequestStatus());
122         request.getRequestStatus().setRequestState(ONGOING);
123
124         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
125                         CITIZEN, request);
126         assertNull(response);
127
128         request.setRequestType(RETURN_COMPLETED);
129         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
130                         CITIZEN, request);
131         assertNotNull(response);
132         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
133
134         request.setRequestType(RETURN_FAILED);
135         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
136                         CITIZEN, request);
137         assertNotNull(response);
138         assertEquals(FAILED, response.getRequest().getRequestStatus().getRequestState());
139
140         // Use scope to set the number of iterations we'll wait for
141
142         request.setRequestType(RETURN_ONGING200);
143         request.setRequestScope("10");
144         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
145                         CITIZEN, request);
146         assertNotNull(response);
147         assertNotNull(response.getRequest());
148         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
149
150         request.setRequestType(RETURN_ONGING202);
151         request.setRequestScope("20");
152         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
153                         CITIZEN, request);
154         assertNotNull(response);
155         assertNotNull(response.getRequest());
156         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
157
158         // Test timeout after 20 attempts for a response
159         request.setRequestType(RETURN_ONGING202);
160         request.setRequestScope("21");
161         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
162                         CITIZEN, request);
163         assertNull(response);
164
165         // Test bad response after 3 attempts for a response
166         request.setRequestType(RETURN_BAD_AFTER_WAIT);
167         request.setRequestScope("3");
168         response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
169                         CITIZEN, request);
170         assertNull(response);
171     }
172
173     @Test
174     public void testVfModuleCreation() throws Exception {
175         SoManager manager = new SoManager(LOCALHOST_99999999, "sean", CITIZEN);
176         assertNotNull(manager);
177         manager.setRestGetTimeout(100);
178
179         SoRequest soRequest = new SoRequest();
180         soRequest.setOperationType(SoOperationType.SCALE_OUT);
181         Future<SoResponse> asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this,
182                         UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
183         SoResponse response = asyncRestCallFuture.get();
184         assertEquals(999, response.getHttpResponseCode());
185
186         manager = new SoManager(BASE_SO_URI, "sean", CITIZEN);
187         manager.setRestGetTimeout(100);
188         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
189                         UUID.randomUUID().toString(), soRequest);
190         response = asyncRestCallFuture.get();
191         assertEquals(999, response.getHttpResponseCode());
192
193         SoRequest request = new SoRequest();
194         request.setRequestId(UUID.randomUUID());
195         request.setRequestScope("Test");
196         request.setRequestType(RETURN_BAD_JSON);
197         request.setStartTime(DATE1);
198         request.setRequestStatus(new SoRequestStatus());
199         request.getRequestStatus().setRequestState(ONGOING);
200         request.setOperationType(SoOperationType.SCALE_OUT);
201
202         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
203                         UUID.randomUUID().toString(), request);
204         response = asyncRestCallFuture.get();
205         assertEquals(999, response.getHttpResponseCode());
206
207         request.setRequestType(RETURN_COMPLETED);
208
209         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
210                         UUID.randomUUID().toString(), request);
211         response = asyncRestCallFuture.get();
212         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
213
214         request.setRequestType(RETURN_FAILED);
215         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
216                         UUID.randomUUID().toString(), request);
217         response = asyncRestCallFuture.get();
218         assertEquals(FAILED, response.getRequest().getRequestStatus().getRequestState());
219
220         // Use scope to set the number of iterations we'll wait for
221
222         request.setRequestType(RETURN_ONGING200);
223         request.setRequestScope("10");
224         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
225                         UUID.randomUUID().toString(), request);
226         response = asyncRestCallFuture.get();
227         assertNotNull(response.getRequest());
228         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
229
230         request.setRequestType(RETURN_ONGING202);
231         request.setRequestScope("20");
232         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
233                         UUID.randomUUID().toString(), request);
234         response = asyncRestCallFuture.get();
235         assertNotNull(response.getRequest());
236         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
237
238         // Test timeout after 20 attempts for a response
239         request.setRequestType(RETURN_ONGING202);
240         request.setRequestScope("21");
241         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
242                         UUID.randomUUID().toString(), request);
243         response = asyncRestCallFuture.get();
244         assertEquals(999, response.getHttpResponseCode());
245
246         // Test bad response after 3 attempts for a response
247         request.setRequestType(RETURN_BAD_AFTER_WAIT);
248         request.setRequestScope("3");
249         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
250                         UUID.randomUUID().toString(), request);
251         response = asyncRestCallFuture.get();
252         assertEquals(999, response.getHttpResponseCode());
253     }
254
255     @Test
256     public void testVfModuleDeletion() throws Exception {
257         SoManager manager = new SoManager(LOCALHOST_99999999, "sean", CITIZEN);
258         assertNotNull(manager);
259         manager.setRestGetTimeout(100);
260
261         SoRequest soRequest = new SoRequest();
262         soRequest.setOperationType(SoOperationType.DELETE_VF_MODULE);
263         Future<SoResponse> asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this,
264                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
265         SoResponse response = asyncRestCallFuture.get();
266         assertEquals(999, response.getHttpResponseCode());
267
268         manager = new SoManager(BASE_SO_URI, "sean", CITIZEN);
269         manager.setRestGetTimeout(100);
270
271         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
272                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
273         response = asyncRestCallFuture.get();
274         assertEquals(999, response.getHttpResponseCode());
275
276         SoRequest request = new SoRequest();
277         request.setRequestId(UUID.randomUUID());
278         request.setRequestScope("Test");
279         request.setRequestType(RETURN_BAD_JSON);
280         request.setStartTime(DATE1);
281         request.setRequestStatus(new SoRequestStatus());
282         request.getRequestStatus().setRequestState(ONGOING);
283         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
284
285         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
286                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
287         response = asyncRestCallFuture.get();
288         assertEquals(999, response.getHttpResponseCode());
289
290         request.setRequestType(RETURN_COMPLETED);
291
292         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
293                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
294         response = asyncRestCallFuture.get();
295         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
296
297         request.setRequestType(RETURN_FAILED);
298         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
299                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
300         response = asyncRestCallFuture.get();
301         assertEquals(FAILED, response.getRequest().getRequestStatus().getRequestState());
302
303         // Use scope to set the number of iterations we'll wait for
304
305         request.setRequestType(RETURN_ONGING200);
306         request.setRequestScope("10");
307         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
308                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
309         response = asyncRestCallFuture.get();
310         assertNotNull(response.getRequest());
311         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
312
313         request.setRequestType(RETURN_ONGING202);
314         request.setRequestScope("20");
315         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
316                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
317         response = asyncRestCallFuture.get();
318         assertNotNull(response.getRequest());
319         assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
320
321         // Test timeout after 20 attempts for a response
322         request.setRequestType(RETURN_ONGING202);
323         request.setRequestScope("21");
324         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
325                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
326         response = asyncRestCallFuture.get();
327         assertEquals(999, response.getHttpResponseCode());
328
329         // Test bad response after 3 attempts for a response
330         request.setRequestType(RETURN_BAD_AFTER_WAIT);
331         request.setRequestScope("3");
332         asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
333                 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
334         response = asyncRestCallFuture.get();
335         assertEquals(999, response.getHttpResponseCode());
336     }
337
338     @Override
339     public void onSoResponseWrapper(SoResponseWrapper wrapper) {
340         //
341         // Nothing really needed to do
342         //
343     }
344 }