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.glassfish.grizzly.http.server.HttpServer;
44 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
45 import org.glassfish.jersey.server.ResourceConfig;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.onap.policy.so.SoManager.SoCallback;
51 public class SoManagerTest implements SoCallback {
52 private static final String BASE_URI = "http://localhost:46553/TestSOManager";
53 private static final String BASE_SO_URI = BASE_URI + "/SO";
54 private static HttpServer server;
60 public static void setUp() throws IOException {
61 final ResourceConfig rc = new ResourceConfig(SoDummyServer.class);
62 //Grizzly by default doesn't allow payload for HTTP methods (ex: DELETE), for which HTTP spec doesn't
63 // explicitly state that.
64 //allow it before starting the server
65 server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc, false);
66 server.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true);
71 public static void tearDown() throws Exception {
76 public void testGrizzlyServer() throws ClientProtocolException, IOException {
77 CloseableHttpClient httpclient = HttpClients.createDefault();
78 HttpGet httpGet = new HttpGet("http://localhost:46553/TestSOManager/SO/Stats");
79 CloseableHttpResponse response = httpclient.execute(httpGet);
81 String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
82 assertTrue(returnBody.matches("^\\{\"GET\": [0-9]*,\"STAT\": [0-9]*,\"POST\": [0-9]*,\"PUT\": [0-9]*,"
83 + "\"DELETE\": [0-9]*\\}$"));
87 public void testServiceInstantiation() throws IOException {
88 SoManager manager = new SoManager(null, null, null);
89 assertNotNull(manager);
90 manager.setRestGetTimeout(100);
92 SoResponse response = manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean",
96 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
100 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
101 "citizen", new SoRequest());
102 assertNull(response);
104 SoRequest request = new SoRequest();
105 request.setRequestId(UUID.randomUUID());
106 request.setRequestScope("Test");
107 request.setRequestType("ReturnBadJson");
108 request.setStartTime("2018-03-23 16:31");
109 request.setRequestStatus(new SoRequestStatus());
110 request.getRequestStatus().setRequestState("ONGOING");
112 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
114 assertNull(response);
116 request.setRequestType("ReturnCompleted");
117 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
119 assertNotNull(response);
120 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
122 request.setRequestType("ReturnFailed");
123 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
125 assertNotNull(response);
126 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
128 // Use scope to set the number of iterations we'll wait for
130 request.setRequestType("ReturnOnging200");
131 request.setRequestScope(new Integer(10).toString());
132 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
134 assertNotNull(response);
135 assertNotNull(response.getRequest());
136 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
138 request.setRequestType("ReturnOnging202");
139 request.setRequestScope(new Integer(20).toString());
140 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
142 assertNotNull(response);
143 assertNotNull(response.getRequest());
144 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
146 // Test timeout after 20 attempts for a response
147 request.setRequestType("ReturnOnging202");
148 request.setRequestScope(new Integer(21).toString());
149 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
151 assertNull(response);
153 // Test bad response after 3 attempts for a response
154 request.setRequestType("ReturnBadAfterWait");
155 request.setRequestScope(new Integer(3).toString());
156 response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean",
158 assertNull(response);
162 public void testVfModuleCreation() throws IOException {
163 SoManager manager = new SoManager("http:/localhost:99999999", "sean", "citizen");
164 assertNotNull(manager);
165 manager.setRestGetTimeout(100);
167 SoRequest soRequest = new SoRequest();
168 soRequest.setOperationType(SoOperationType.SCALE_OUT);
169 Future<SoResponse> asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this,
170 UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
172 SoResponse response = asyncRestCallFuture.get();
173 assertEquals(999, response.getHttpResponseCode());
174 } catch (Exception e) {
175 fail("test should not throw an exception");
178 manager = new SoManager(BASE_SO_URI, "sean", "citizen");
179 manager.setRestGetTimeout(100);
180 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
181 UUID.randomUUID().toString(), soRequest);
183 SoResponse response = asyncRestCallFuture.get();
184 assertEquals(999, response.getHttpResponseCode());
185 } catch (Exception e) {
186 fail("test should not throw an exception");
189 SoRequest request = new SoRequest();
190 request.setRequestId(UUID.randomUUID());
191 request.setRequestScope("Test");
192 request.setRequestType("ReturnBadJson");
193 request.setStartTime("2018-03-23 16:31");
194 request.setRequestStatus(new SoRequestStatus());
195 request.getRequestStatus().setRequestState("ONGOING");
196 request.setOperationType(SoOperationType.SCALE_OUT);
198 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
199 UUID.randomUUID().toString(), request);
201 SoResponse response = asyncRestCallFuture.get();
202 assertEquals(999, response.getHttpResponseCode());
203 } catch (Exception e) {
204 fail("test should not throw an exception");
207 request.setRequestType("ReturnCompleted");
209 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
210 UUID.randomUUID().toString(), request);
212 SoResponse response = asyncRestCallFuture.get();
213 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
214 } catch (Exception e) {
215 fail("test should not throw an exception");
218 request.setRequestType("ReturnFailed");
219 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
220 UUID.randomUUID().toString(), request);
222 SoResponse response = asyncRestCallFuture.get();
223 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
224 } catch (Exception e) {
225 fail("test should not throw an exception");
228 // Use scope to set the number of iterations we'll wait for
230 request.setRequestType("ReturnOnging200");
231 request.setRequestScope(new Integer(10).toString());
232 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
233 UUID.randomUUID().toString(), request);
235 SoResponse response = asyncRestCallFuture.get();
236 assertNotNull(response.getRequest());
237 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
238 } catch (Exception e) {
239 fail("test should not throw an exception");
242 request.setRequestType("ReturnOnging202");
243 request.setRequestScope(new Integer(20).toString());
244 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
245 UUID.randomUUID().toString(), request);
247 SoResponse response = asyncRestCallFuture.get();
248 assertNotNull(response.getRequest());
249 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
250 } catch (Exception e) {
251 fail("test should not throw an exception");
254 // Test timeout after 20 attempts for a response
255 request.setRequestType("ReturnOnging202");
256 request.setRequestScope(new Integer(21).toString());
257 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
258 UUID.randomUUID().toString(), request);
260 SoResponse response = asyncRestCallFuture.get();
261 assertEquals(999, response.getHttpResponseCode());
262 } catch (Exception e) {
263 fail("test should not throw an exception");
266 // Test bad response after 3 attempts for a response
267 request.setRequestType("ReturnBadAfterWait");
268 request.setRequestScope(new Integer(3).toString());
269 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
270 UUID.randomUUID().toString(), request);
272 SoResponse response = asyncRestCallFuture.get();
273 assertEquals(999, response.getHttpResponseCode());
274 } catch (Exception e) {
275 fail("test should not throw an exception");
280 public void testVfModuleDeletion() {
281 SoManager manager = new SoManager("http:/localhost:99999999", "sean", "citizen");
282 assertNotNull(manager);
283 manager.setRestGetTimeout(100);
285 SoRequest soRequest = new SoRequest();
286 soRequest.setOperationType(SoOperationType.DELETE_VF_MODULE);
287 Future<SoResponse> asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this,
288 UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest);
290 SoResponse response = asyncRestCallFuture.get();
291 assertEquals(999, response.getHttpResponseCode());
292 } catch (Exception e) {
293 fail("test should not throw an exception");
296 manager = new SoManager(BASE_SO_URI, "sean", "citizen");
297 manager.setRestGetTimeout(100);
299 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
300 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 SoRequest request = new SoRequest();
309 request.setRequestId(UUID.randomUUID());
310 request.setRequestScope("Test");
311 request.setRequestType("ReturnBadJson");
312 request.setStartTime("2018-03-23 16:31");
313 request.setRequestStatus(new SoRequestStatus());
314 request.getRequestStatus().setRequestState("ONGOING");
315 request.setOperationType(SoOperationType.DELETE_VF_MODULE);
317 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
318 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
320 SoResponse response = asyncRestCallFuture.get();
321 assertEquals(999, response.getHttpResponseCode());
322 } catch (Exception e) {
323 fail("test should not throw an exception");
326 request.setRequestType("ReturnCompleted");
328 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
329 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
331 SoResponse response = asyncRestCallFuture.get();
332 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
333 } catch (Exception e) {
334 fail("test should not throw an exception");
337 request.setRequestType("ReturnFailed");
338 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
339 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
341 SoResponse response = asyncRestCallFuture.get();
342 assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
343 } catch (Exception e) {
344 fail("test should not throw an exception");
347 // Use scope to set the number of iterations we'll wait for
349 request.setRequestType("ReturnOnging200");
350 request.setRequestScope(new Integer(10).toString());
351 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
352 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
354 SoResponse response = asyncRestCallFuture.get();
355 assertNotNull(response.getRequest());
356 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
357 } catch (Exception e) {
358 fail("test should not throw an exception");
361 request.setRequestType("ReturnOnging202");
362 request.setRequestScope(new Integer(20).toString());
363 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
364 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
366 SoResponse response = asyncRestCallFuture.get();
367 assertNotNull(response.getRequest());
368 assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
369 } catch (Exception e) {
370 fail("test should not throw an exception");
373 // Test timeout after 20 attempts for a response
374 request.setRequestType("ReturnOnging202");
375 request.setRequestScope(new Integer(21).toString());
376 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
377 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
379 SoResponse response = asyncRestCallFuture.get();
380 assertEquals(999, response.getHttpResponseCode());
381 } catch (Exception e) {
382 fail("test should not throw an exception");
385 // Test bad response after 3 attempts for a response
386 request.setRequestType("ReturnBadAfterWait");
387 request.setRequestScope(new Integer(3).toString());
388 asyncRestCallFuture = manager.asyncSoRestCall(UUID.randomUUID().toString(), this, UUID.randomUUID().toString(),
389 UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
391 SoResponse response = asyncRestCallFuture.get();
392 assertEquals(999, response.getHttpResponseCode());
393 } catch (Exception e) {
394 fail("test should not throw an exception");
399 public void onSoResponseWrapper(SoResponseWrapper wrapper) {
401 // Nothing really needed to do