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 * ================================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.so;
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;
31 import java.io.IOException;
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;
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;
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);
70 public static void tearDown() throws Exception {
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);
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]*\\}$"));
86 public void testServiceInstantiation() throws IOException {
87 SoManager manager = new SoManager();
88 assertNotNull(manager);
89 manager.setRestGetTimeout(100);
91 SoResponse response = manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean",
95 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
99 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
100 "citizen", new SoRequest());
101 assertNull(response);
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");
111 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
113 assertNull(response);
115 request.setRequestType("ReturnCompleted");
116 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
118 assertNotNull(response);
119 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
121 request.setRequestType("ReturnFailed");
122 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
124 assertNotNull(response);
125 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
127 // Use scope to set the number of iterations we'll wait for
129 request.setRequestType("ReturnOnging200");
130 request.setRequestScope(new Integer(10).toString());
131 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
133 assertNotNull(response);
134 assertNotNull(response.getRequest());
135 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
137 request.setRequestType("ReturnOnging202");
138 request.setRequestScope(new Integer(20).toString());
139 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
141 assertNotNull(response);
142 assertNotNull(response.getRequest());
143 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
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",
150 assertNull(response);
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",
157 assertNull(response);
161 public void testVfModuleCreation() throws IOException {
162 SoManager manager = new SoManager();
163 assertNotNull(manager);
164 manager.setRestGetTimeout(100);
166 PolicyEngine.manager.setEnvironmentProperty("so.username", "sean");
167 PolicyEngine.manager.setEnvironmentProperty("so.password", "citizen");
169 WorkingMemory wm = new DummyWorkingMemory();
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);
177 SoResponse response = asyncRestCallFuture.get();
178 assertEquals(999, response.getHttpResponseCode());
179 } catch (Exception e) {
180 fail("test should not throw an exception");
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);
187 SoResponse response = asyncRestCallFuture.get();
188 assertEquals(999, response.getHttpResponseCode());
189 } catch (Exception e) {
190 fail("test should not throw an exception");
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);
202 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
203 UUID.randomUUID().toString(), request);
205 SoResponse response = asyncRestCallFuture.get();
206 assertEquals(999, response.getHttpResponseCode());
207 } catch (Exception e) {
208 fail("test should not throw an exception");
211 request.setRequestType("ReturnCompleted");
213 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
214 UUID.randomUUID().toString(), request);
216 SoResponse response = asyncRestCallFuture.get();
217 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
218 } catch (Exception e) {
219 fail("test should not throw an exception");
222 request.setRequestType("ReturnFailed");
223 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
224 UUID.randomUUID().toString(), request);
226 SoResponse response = asyncRestCallFuture.get();
227 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
228 } catch (Exception e) {
229 fail("test should not throw an exception");
232 // Use scope to set the number of iterations we'll wait for
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);
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");
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);
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");
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);
264 SoResponse response = asyncRestCallFuture.get();
265 assertEquals(999, response.getHttpResponseCode());
266 } catch (Exception e) {
267 fail("test should not throw an exception");
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);
276 SoResponse response = asyncRestCallFuture.get();
277 assertEquals(999, response.getHttpResponseCode());
278 } catch (Exception e) {
279 fail("test should not throw an exception");
284 public void testVfModuleDeletion() {
285 SoManager manager = new SoManager();
286 assertNotNull(manager);
287 manager.setRestGetTimeout(100);
289 PolicyEngine.manager.setEnvironmentProperty("so.username", "sean");
290 PolicyEngine.manager.setEnvironmentProperty("so.password", "citizen");
292 WorkingMemory wm = new DummyWorkingMemory();
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);
300 SoResponse response = asyncRestCallFuture.get();
301 assertEquals(999, response.getHttpResponseCode());
302 } catch (Exception e) {
303 fail("test should not throw an exception");
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);
310 SoResponse response = asyncRestCallFuture.get();
311 assertEquals(999, response.getHttpResponseCode());
312 } catch (Exception e) {
313 fail("test should not throw an exception");
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);
325 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
326 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
328 SoResponse response = asyncRestCallFuture.get();
329 assertEquals(999, response.getHttpResponseCode());
330 } catch (Exception e) {
331 fail("test should not throw an exception");
334 request.setRequestType("ReturnCompleted");
336 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
337 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
339 SoResponse response = asyncRestCallFuture.get();
340 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
341 } catch (Exception e) {
342 fail("test should not throw an exception");
345 request.setRequestType("ReturnFailed");
346 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(),
347 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
349 SoResponse response = asyncRestCallFuture.get();
350 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
351 } catch (Exception e) {
352 fail("test should not throw an exception");
355 // Use scope to set the number of iterations we'll wait for
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);
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");
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);
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");
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);
387 SoResponse response = asyncRestCallFuture.get();
388 assertEquals(999, response.getHttpResponseCode());
389 } catch (Exception e) {
390 fail("test should not throw an exception");
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);
399 SoResponse response = asyncRestCallFuture.get();
400 assertEquals(999, response.getHttpResponseCode());
401 } catch (Exception e) {
402 fail("test should not throw an exception");