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;
32 import java.io.IOException;
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;
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;
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);
82 public static void tearDown() {
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);
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]*\\}$"));
99 public void testServiceInstantiation() {
100 SoManager manager = new SoManager(null, null, null);
101 assertNotNull(manager);
102 manager.setRestGetTimeout(100);
104 SoResponse response = manager.createModuleInstance(LOCALHOST_99999999, BASE_SO_URI, "sean",
106 assertNull(response);
108 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
110 assertNull(response);
112 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
113 CITIZEN, new SoRequest());
114 assertNull(response);
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);
124 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
126 assertNull(response);
128 request.setRequestType(RETURN_COMPLETED);
129 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
131 assertNotNull(response);
132 assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
134 request.setRequestType(RETURN_FAILED);
135 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
137 assertNotNull(response);
138 assertEquals(FAILED, response.getRequest().getRequestStatus().getRequestState());
140 // Use scope to set the number of iterations we'll wait for
142 request.setRequestType(RETURN_ONGING200);
143 request.setRequestScope("10");
144 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
146 assertNotNull(response);
147 assertNotNull(response.getRequest());
148 assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
150 request.setRequestType(RETURN_ONGING202);
151 request.setRequestScope("20");
152 response = manager.createModuleInstance(BASE_SO_URI + SERVICE_INSTANTIATION_V7, BASE_SO_URI, "sean",
154 assertNotNull(response);
155 assertNotNull(response.getRequest());
156 assertEquals(COMPLETE, response.getRequest().getRequestStatus().getRequestState());
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",
163 assertNull(response);
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",
170 assertNull(response);
174 public void testVfModuleCreation() throws Exception {
175 SoManager manager = new SoManager(LOCALHOST_99999999, "sean", CITIZEN);
176 assertNotNull(manager);
177 manager.setRestGetTimeout(100);
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());
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());
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);
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());
207 request.setRequestType(RETURN_COMPLETED);
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());
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());
220 // Use scope to set the number of iterations we'll wait for
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());
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());
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());
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());
256 public void testVfModuleDeletion() throws Exception {
257 SoManager manager = new SoManager(LOCALHOST_99999999, "sean", CITIZEN);
258 assertNotNull(manager);
259 manager.setRestGetTimeout(100);
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());
268 manager = new SoManager(BASE_SO_URI, "sean", CITIZEN);
269 manager.setRestGetTimeout(100);
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());
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);
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());
290 request.setRequestType(RETURN_COMPLETED);
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());
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());
303 // Use scope to set the number of iterations we'll wait for
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());
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());
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());
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());
339 public void onSoResponseWrapper(SoResponseWrapper wrapper) {
341 // Nothing really needed to do