2 * ============LICENSE_START=======================================================
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 * ================================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
25 package org.onap.policy.so;
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;
33 import java.io.IOException;
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;
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;
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);
72 public static void tearDown() throws Exception {
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);
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]*\\}$"));
88 public void testServiceInstantiation() throws IOException {
89 SoManager manager = new SoManager();
90 assertNotNull(manager);
91 manager.setRestGetTimeout(100);
93 SoResponse response = manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean",
97 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
101 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
102 "citizen", new SoRequest());
103 assertNull(response);
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");
113 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
115 assertNull(response);
117 request.setRequestType("ReturnCompleted");
118 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
120 assertNotNull(response);
121 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
123 request.setRequestType("ReturnFailed");
124 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
126 assertNotNull(response);
127 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
129 // Use scope to set the number of iterations we'll wait for
131 request.setRequestType("ReturnOnging200");
132 request.setRequestScope(new Integer(10).toString());
133 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
135 assertNotNull(response);
136 assertNotNull(response.getRequest());
137 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
139 request.setRequestType("ReturnOnging202");
140 request.setRequestScope(new Integer(20).toString());
141 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
143 assertNotNull(response);
144 assertNotNull(response.getRequest());
145 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
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",
152 assertNull(response);
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",
159 assertNull(response);
163 public void testVfModuleCreation() throws IOException {
164 SoManager manager = new SoManager();
165 assertNotNull(manager);
166 manager.setRestGetTimeout(100);
168 PolicyEngine.manager.setEnvironmentProperty("so.username", "sean");
169 PolicyEngine.manager.setEnvironmentProperty("so.password", "citizen");
171 WorkingMemory wm = new DummyWorkingMemory();
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);
179 SoResponse response = asyncRestCallFuture.get();
180 assertEquals(999, response.getHttpResponseCode());
181 } catch (Exception e) {
182 fail("test should not throw an exception");
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);
189 SoResponse response = asyncRestCallFuture.get();
190 assertEquals(999, response.getHttpResponseCode());
191 } catch (Exception e) {
192 fail("test should not throw an exception");
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);
204 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
205 UUID.randomUUID().toString(), request);
207 SoResponse response = asyncRestCallFuture.get();
208 assertEquals(999, response.getHttpResponseCode());
209 } catch (Exception e) {
210 fail("test should not throw an exception");
213 request.setRequestType("ReturnCompleted");
215 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
216 UUID.randomUUID().toString(), request);
218 SoResponse response = asyncRestCallFuture.get();
219 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
220 } catch (Exception e) {
221 fail("test should not throw an exception");
224 request.setRequestType("ReturnFailed");
225 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
226 UUID.randomUUID().toString(), request);
228 SoResponse response = asyncRestCallFuture.get();
229 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
230 } catch (Exception e) {
231 fail("test should not throw an exception");
234 // Use scope to set the number of iterations we'll wait for
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);
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");
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);
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");
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);
266 SoResponse response = asyncRestCallFuture.get();
267 assertEquals(999, response.getHttpResponseCode());
268 } catch (Exception e) {
269 fail("test should not throw an exception");
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);
278 SoResponse response = asyncRestCallFuture.get();
279 assertEquals(999, response.getHttpResponseCode());
280 } catch (Exception e) {
281 fail("test should not throw an exception");
286 public void testVfModuleDeletion() {
287 SoManager manager = new SoManager();
288 assertNotNull(manager);
289 manager.setRestGetTimeout(100);
291 PolicyEngine.manager.setEnvironmentProperty("so.username", "sean");
292 PolicyEngine.manager.setEnvironmentProperty("so.password", "citizen");
294 WorkingMemory wm = new DummyWorkingMemory();
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);
302 SoResponse response = asyncRestCallFuture.get();
303 assertEquals(999, response.getHttpResponseCode());
304 } catch (Exception e) {
305 fail("test should not throw an exception");
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);
312 SoResponse response = asyncRestCallFuture.get();
313 assertEquals(999, response.getHttpResponseCode());
314 } catch (Exception e) {
315 fail("test should not throw an exception");
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);
327 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
328 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
330 SoResponse response = asyncRestCallFuture.get();
331 assertEquals(999, response.getHttpResponseCode());
332 } catch (Exception e) {
333 fail("test should not throw an exception");
336 request.setRequestType("ReturnCompleted");
338 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
339 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
341 SoResponse response = asyncRestCallFuture.get();
342 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
343 } catch (Exception e) {
344 fail("test should not throw an exception");
347 request.setRequestType("ReturnFailed");
348 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
349 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
351 SoResponse response = asyncRestCallFuture.get();
352 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
353 } catch (Exception e) {
354 fail("test should not throw an exception");
357 // Use scope to set the number of iterations we'll wait for
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);
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");
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);
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");
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);
389 SoResponse response = asyncRestCallFuture.get();
390 assertEquals(999, response.getHttpResponseCode());
391 } catch (Exception e) {
392 fail("test should not throw an exception");
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);
401 SoResponse response = asyncRestCallFuture.get();
402 assertEquals(999, response.getHttpResponseCode());
403 } catch (Exception e) {
404 fail("test should not throw an exception");