2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.requesthandler;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Matchers;
33 import org.mockito.Mockito;
34 import org.onap.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
35 import org.onap.appc.adapter.message.MessageAdapterFactory;
36 import org.onap.appc.configuration.Configuration;
37 import org.onap.appc.configuration.ConfigurationFactory;
38 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
39 import org.onap.appc.domainmodel.lcm.CommonHeader;
40 import org.onap.appc.domainmodel.lcm.Flags;
41 import org.onap.appc.domainmodel.lcm.RequestContext;
42 import org.onap.appc.domainmodel.lcm.ResponseContext;
43 import org.onap.appc.domainmodel.lcm.RuntimeContext;
44 import org.onap.appc.domainmodel.lcm.Status;
45 import org.onap.appc.domainmodel.lcm.VNFContext;
46 import org.onap.appc.domainmodel.lcm.VNFOperation;
47 import org.onap.appc.exceptions.InvalidInputException;
48 import org.onap.appc.executor.CommandExecutor;
49 import org.onap.appc.executor.objects.LCMCommandStatus;
50 import org.onap.appc.lifecyclemanager.LifecycleManager;
51 import org.onap.appc.lifecyclemanager.objects.LifecycleException;
52 import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException;
53 import org.onap.appc.lockmanager.api.LockException;
54 import org.onap.appc.lockmanager.api.LockManager;
55 import org.onap.appc.messageadapter.MessageAdapter;
56 import org.onap.appc.messageadapter.impl.MessageAdapterImpl;
57 import org.onap.appc.requesthandler.exceptions.DGWorkflowNotFoundException;
58 import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
59 import org.onap.appc.requesthandler.exceptions.LCMOperationsDisabledException;
60 import org.onap.appc.requesthandler.exceptions.MissingVNFDataInAAIException;
61 import org.onap.appc.requesthandler.exceptions.RequestExpiredException;
62 import org.onap.appc.requesthandler.exceptions.VNFNotFoundException;
63 import org.onap.appc.requesthandler.exceptions.WorkflowNotFoundException;
64 import org.onap.appc.requesthandler.impl.RequestHandlerImpl;
65 import org.onap.appc.requesthandler.impl.RequestValidatorImpl;
66 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
67 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
68 import org.onap.appc.transactionrecorder.TransactionRecorder;
69 import org.onap.appc.workflow.WorkFlowManager;
70 import org.onap.appc.workflow.objects.WorkflowExistsOutput;
71 import org.osgi.framework.Bundle;
72 import org.osgi.framework.BundleContext;
73 import org.osgi.framework.FrameworkUtil;
74 import org.osgi.framework.ServiceReference;
75 import org.powermock.api.mockito.PowerMockito;
76 import org.powermock.core.classloader.annotations.PrepareForTest;
77 import org.powermock.modules.junit4.PowerMockRunner;
79 import java.util.Date;
80 import java.util.HashMap;
81 import java.util.UUID;
83 import static org.mockito.Matchers.anyObject;
84 import static org.mockito.Mockito.doNothing;
85 import static org.mockito.Mockito.mock;
86 import static org.powermock.api.mockito.PowerMockito.when;
88 @RunWith(PowerMockRunner.class)
89 @PrepareForTest( {FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class, MessageAdapterImpl.class})
90 public class RequestHandlerTest {
92 private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestHandlerTest.class);
94 private RequestHandlerImpl requestHandler;
95 private RequestValidatorImpl requestValidator;
96 private WorkFlowManager workflowManager;
97 private LockManager lockManager;
98 private Configuration configuration;
100 private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
101 private final Bundle bundleService=Mockito.mock(Bundle.class);
102 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
103 private MessageAdapterFactory factory = new DmaapMessageAdapterFactoryImpl();
107 public void init() throws Exception {
108 configuration = ConfigurationFactory.getConfiguration();
110 configuration.setProperty("appc.LCM.topic.write" , "TEST");
111 configuration.setProperty("appc.LCM.client.key" , "TEST");
112 configuration.setProperty("appc.LCM.client.secret" , "TEST");
114 PowerMockito.mockStatic(FrameworkUtil.class);
115 PowerMockito.when(FrameworkUtil.getBundle(MessageAdapterImpl.class)).thenReturn(bundleService);
116 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
117 PowerMockito.when(bundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(sref);
118 PowerMockito.when(bundleContext.getService(sref)).thenReturn(factory);
121 requestHandler = new RequestHandlerImpl();
122 LifecycleManager lifecyclemanager= mock(LifecycleManager.class);
123 workflowManager= mock(WorkFlowManager.class);
125 CommandExecutor commandExecutor= mock(CommandExecutor.class);
126 MessageAdapter messageAdapter = mock(MessageAdapter.class);
127 lockManager = mock(LockManager.class);
128 TransactionRecorder transactionRecorder= mock(TransactionRecorder.class);
130 requestHandler.setMessageAdapter(messageAdapter);
131 requestValidator = mock(RequestValidatorImpl.class);
132 requestValidator.setWorkflowManager(workflowManager);
133 requestHandler.setCommandExecutor(commandExecutor);
134 requestHandler.setRequestValidator(requestValidator);
135 requestHandler.setLockManager(lockManager);
136 requestHandler.setTransactionRecorder(transactionRecorder);
138 doNothing().when(transactionRecorder).store(anyObject());
139 // Mockito.when(commandExecutor.executeCommand((CommandExecutorInput)anyObject())).thenReturn(true);
142 private void threadSleep(){
145 } catch (InterruptedException e) {
149 //TODO needs to be fixed
151 public void testNegativeFlowWithRequestingUsedVnfId() throws Exception {
152 logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
153 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
154 RequestHandlerInput input1 = this.getRequestHandlerInput("131", VNFOperation.Configure, 1200,
155 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
156 mockRuntimeContextAndVnfContext(input1);
157 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
158 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
159 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
161 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
162 logger.debug("testNegativeFlowWithRequestingUsedVnfId");
163 logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
167 public void testInvalidVNFExceptionRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
168 String originatorID = UUID.randomUUID().toString();
169 String requestID = UUID.randomUUID().toString();
170 String subRequestID = UUID.randomUUID().toString();
171 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
172 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0,false,originatorID, requestID, subRequestID,new Date());
173 ///PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
174 RequestHandlerOutput output = requestHandler.handleRequest(input);
175 Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
179 public void testLifecycleException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
180 String originatorID = UUID.randomUUID().toString();
181 String requestID = UUID.randomUUID().toString();
182 String subRequestID = UUID.randomUUID().toString();
183 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
184 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
185 RequestHandlerOutput output = requestHandler.handleRequest(input);
186 Assert.assertEquals(LCMCommandStatus.INVALID_VNF_STATE.getResponseCode(), output.getResponseContext().getStatus().getCode());
191 public void testRequestExpiredException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
192 String originatorID = UUID.randomUUID().toString();
193 String requestID = UUID.randomUUID().toString();
194 String subRequestID = UUID.randomUUID().toString();
195 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
196 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
197 RequestHandlerOutput output = requestHandler.handleRequest(input);
198 Assert.assertEquals(LCMCommandStatus.EXPIRED_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
202 public void testMissingVNFdata() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
203 String originatorID = UUID.randomUUID().toString();
204 String requestID = UUID.randomUUID().toString();
205 String subRequestID = UUID.randomUUID().toString();
207 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
208 *//*PowerMockito.doThrow(new MissingVNFDataInAAIException("vnf-type")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));*//*
209 RequestHandlerOutput output = requestHandler.handleRequest(input);
210 Assert.assertEquals(LCMCommandStatus.MISSING_VNF_DATA_IN_AAI.getResponseCode(), output.getResponseContext().getStatus().getCode());
214 public void testWorkflowNotFoundException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
215 String originatorID = UUID.randomUUID().toString();
216 String requestID = UUID.randomUUID().toString();
217 String subRequestID = UUID.randomUUID().toString();
218 //PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
219 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
220 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
221 //PowerMockito.doThrow(new WorkflowNotFoundException("Unable to find the DG","VNF-2.0.0.0", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
222 RequestHandlerOutput output = requestHandler.handleRequest(input);
223 Assert.assertEquals(LCMCommandStatus.WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());}
226 public void testDGWorkflowNotFoundException() throws Exception {
227 String originatorID = UUID.randomUUID().toString();
228 String requestID = UUID.randomUUID().toString();
229 String subRequestID = UUID.randomUUID().toString();
230 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true, true));
231 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0, false, originatorID, requestID, subRequestID, new Date());
232 PowerMockito.doThrow(new DGWorkflowNotFoundException("Unable to find the DG", "VNF-2.0.0.0", "temp", "Test","Test","Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
233 RequestHandlerOutput output = requestHandler.handleRequest(input);
234 Assert.assertEquals(LCMCommandStatus.DG_WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
238 public void testInvalidInputException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
239 String originatorID1 = UUID.randomUUID().toString();
240 String requestID1 = UUID.randomUUID().toString();
241 String subRequestID1 = UUID.randomUUID().toString();
242 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
243 RequestHandlerInput input1 = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID1, requestID1, subRequestID1,new Date());
244 //PowerMockito.doThrow(new InvalidInputException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
245 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
246 Assert.assertEquals(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode(), output1.getResponseContext().getStatus().getCode());
250 public void testNoTransitionDefinedException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
251 String originatorID = UUID.randomUUID().toString();
252 String requestID = UUID.randomUUID().toString();
253 String subRequestID = UUID.randomUUID().toString();
254 //PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
255 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
256 RequestHandlerInput input = this.getRequestHandlerInput("3010", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
257 ///PowerMockito.doThrow(new NoTransitionDefinedException("Invalid VNF State","Unstable","Test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
258 RequestHandlerOutput output = requestHandler.handleRequest(input);
259 Assert.assertEquals(LCMCommandStatus.NO_TRANSITION_DEFINE.getResponseCode(), output.getResponseContext().getStatus().getCode());
263 public void rejectInvalidRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
264 String originatorID = UUID.randomUUID().toString();
265 String requestID = UUID.randomUUID().toString();
266 String subRequestID = UUID.randomUUID().toString();
267 ///PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
268 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
269 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
270 ///PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
271 RequestHandlerOutput output = requestHandler.handleRequest(input);
272 Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
276 public void testUnstableWorkingState() throws Exception {
277 logger.debug("=====================testUnstableWorkingState=============================");
278 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
279 RequestHandlerInput input = this.getRequestHandlerInput("37", VNFOperation.Configure, 1200,
280 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
281 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
282 mockRuntimeContextAndVnfContext(input);
283 RequestHandlerOutput output = requestHandler.handleRequest(input);
285 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
287 RequestHandlerInput input1 = this.getRequestHandlerInput("37", VNFOperation.Configure,1200,
288 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
289 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
290 mockRuntimeContextAndVnfContext(input1);
291 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
293 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
294 logger.debug("=====================testUnstableWorkingState=============================");
298 public void testOnRequestExecutionEndSuccessForWorkingState() throws Exception {
299 logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
300 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
301 RequestHandlerInput input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
302 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
303 mockRuntimeContextAndVnfContext(input1);
305 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
308 RequestHandlerOutput output = requestHandler.handleRequest(input1);
309 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
312 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"137", "", "", ""));
314 input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
315 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
316 mockRuntimeContextAndVnfContext(input1);
317 output = requestHandler.handleRequest(input1);
318 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
319 logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
322 private void mockRuntimeContextAndVnfContext(RequestHandlerInput input1) throws Exception {
323 RuntimeContext runtimeContext = PowerMockito.mock(RuntimeContext.class);
324 VNFContext vnfContext = new VNFContext();
325 vnfContext.setType("SCP");
326 vnfContext.setId("137");
327 when(runtimeContext.getVnfContext()).thenReturn(vnfContext);
328 when(runtimeContext.getRequestContext()).thenReturn(input1.getRequestContext());
329 when(runtimeContext.getRpcName()).thenReturn(input1.getRpcName());
330 Date startTime = new Date();
331 when(runtimeContext.getTimeStart()).thenReturn(startTime.toInstant());
334 ResponseContext responseContext = new ResponseContext();
335 responseContext.setStatus(new Status());
336 responseContext.setAdditionalContext(new HashMap<>(4));
337 responseContext.setCommonHeader(input1.getRequestContext().getCommonHeader());
338 runtimeContext.setResponseContext(responseContext);
339 when(runtimeContext.getResponseContext()).thenReturn(responseContext);
340 responseContext.setStatus(new Status());
341 runtimeContext.setResponseContext(responseContext);
342 PowerMockito.whenNew(RuntimeContext.class).withAnyArguments().thenReturn(runtimeContext);
347 public void testOnRequestExecutionEndFailureForWorkingState() throws Exception {
348 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
349 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
351 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
353 RuntimeContext noneMockRuntimeContext = this.getAsyncResponse(false,LCMCommandStatus.NO_TRANSITION_DEFINE,"38", "", "", "");
355 RequestHandlerInput input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
356 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
357 mockRuntimeContextAndVnfContext(input1);
358 RequestHandlerOutput output = requestHandler.handleRequest(input1);
359 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
361 requestHandler.onRequestExecutionEnd(noneMockRuntimeContext);
363 input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
364 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
365 mockRuntimeContextAndVnfContext(input1);
366 output = requestHandler.handleRequest(input1);
367 Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
369 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
373 public void testOnRequestExecutionEndTTLExpiredForWorkingState() throws Exception {
374 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
375 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
377 RequestHandlerInput input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1,
378 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
380 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
381 mockRuntimeContextAndVnfContext(input1);
383 RequestHandlerOutput output = requestHandler.handleRequest(input1);
384 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
386 input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1200,
387 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
388 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
389 output = requestHandler.handleRequest(input1);
390 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(),output.getResponseContext().getStatus().getCode());
391 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
395 public void testOnRequestTTLEndForWorkingState() throws Exception {
396 logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
397 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
399 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
401 RequestHandlerInput input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
402 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
403 mockRuntimeContextAndVnfContext(input1);
404 RequestHandlerOutput output = requestHandler.handleRequest(input1);
405 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
407 RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.EXPIRED_REQUEST_FAILURE,"40", "", "", "");
408 requestHandler.onRequestTTLEnd(response);
409 input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
410 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
411 output = requestHandler.handleRequest(input1);
412 Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
413 logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
417 public void testForceCommandExecution() throws Exception {
418 logger.debug("=====================testForceCommandExecution=============================");
419 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
420 RequestHandlerInput input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
421 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
422 mockRuntimeContextAndVnfContext(input1);
424 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
425 RequestHandlerOutput output = requestHandler.handleRequest(input1);
426 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
428 RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.ACCEPTED,"138", "", "", "");
429 requestHandler.onRequestTTLEnd(response);
430 input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
431 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
432 input1.getRequestContext().getCommonHeader().getFlags().setForce(true);
433 mockRuntimeContextAndVnfContext(input1);
434 output = requestHandler.handleRequest(input1);
435 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
436 logger.debug("=====================testForceCommandExecution=============================");
440 public void testOnRequestExecutionEndSuccess() throws VNFNotFoundException {
441 logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Starts =============================");
442 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"201", "", "", ""));
443 logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Ends =============================");
447 public void testOnRequestExecutionEndFailure() throws VNFNotFoundException {
448 logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Starts =============================");
449 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.DG_FAILURE,"202", "", "", ""));
450 logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Ends =============================");
453 private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force,String originatorId, String requestId, String subRequestId,Date timeStamp){
454 String API_VERSION= "2.0.0";
455 RequestHandlerInput input = new RequestHandlerInput();
456 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
457 RequestContext requestContext = runtimeContext.getRequestContext();
458 input.setRequestContext(requestContext);
459 requestContext.getActionIdentifiers().setVnfId(vnfID);
460 requestContext.setAction(action);
461 input.setRpcName(convertActionNameToUrl(action.name()));
462 requestContext.getCommonHeader().setRequestId(requestId);
463 requestContext.getCommonHeader().setSubRequestId(subRequestId);
464 requestContext.getCommonHeader().setOriginatorId(originatorId);
465 requestContext.getCommonHeader().getFlags().setTtl(ttl);
466 requestContext.getCommonHeader().getFlags().setForce(force);
467 requestContext.getCommonHeader().setTimestamp(timeStamp);
468 requestContext.getCommonHeader().setApiVer(API_VERSION);
472 private RuntimeContext getAsyncResponse(boolean wfStatus, LCMCommandStatus commandStatus, String vnfId, String originatorId, String requestId, String subRequestId)
474 RuntimeContext output = createRuntimeContextWithSubObjects();
477 output.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
478 output.getVnfContext().setId(vnfId);
479 output.getResponseContext().getCommonHeader().setApiVer("2.0.0");
480 output.getResponseContext().getCommonHeader().setTimestamp(new Date());
481 output.getResponseContext().getStatus().setCode(LCMCommandStatus.SUCCESS.getResponseCode());
482 output.setTimeStart(new Date().toInstant());
483 output.getResponseContext().getCommonHeader().setOriginatorId(originatorId);
484 output.getResponseContext().getCommonHeader().setRequestId(requestId);
485 output.getResponseContext().getCommonHeader().setSubRequestId(subRequestId);
487 output.getVnfContext().setType("FIREWALL");
488 output.getRequestContext().setAction(VNFOperation.Configure);
489 output.setRpcName("configure");
490 output.getResponseContext().setPayload("");
495 public void rejectDuplicateRequest() throws Exception {
496 String originatorID = UUID.randomUUID().toString();
497 String requestID = UUID.randomUUID().toString();
498 String subRequestID = UUID.randomUUID().toString();
499 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
501 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
502 RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
503 mockRuntimeContextAndVnfContext(input);
505 RequestHandlerOutput output = requestHandler.handleRequest(input);
506 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
508 input = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
510 PowerMockito.doThrow(new DuplicateRequestException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
511 output = requestHandler.handleRequest(input);
512 Assert.assertEquals(LCMCommandStatus.DUPLICATE_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
516 public void removeRequestFromRegistryOnRequestCompletion() throws Exception {
517 String originatorID = UUID.randomUUID().toString();
518 String requestID = UUID.randomUUID().toString();
519 String subRequestID = UUID.randomUUID().toString();
520 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
522 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
523 RequestHandlerInput input = this.getRequestHandlerInput("302", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
524 mockRuntimeContextAndVnfContext(input);
526 RequestHandlerOutput output = requestHandler.handleRequest(input);
527 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
529 RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"302",originatorID,requestID,subRequestID);
530 requestHandler.onRequestExecutionEnd(asyncResponse);
532 input = this.getRequestHandlerInput("310", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
533 mockRuntimeContextAndVnfContext(input);
534 output = requestHandler.handleRequest(input);
535 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
539 public void removeRequestFromRegistryOnTTLExpiration() throws Exception {
540 String originatorID = UUID.randomUUID().toString();
541 String requestID = UUID.randomUUID().toString();
542 String subRequestID = UUID.randomUUID().toString();
544 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
546 Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
547 RequestHandlerInput input = this.getRequestHandlerInput("303", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
548 mockRuntimeContextAndVnfContext(input);
549 RequestHandlerOutput output = requestHandler.handleRequest(input);
550 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
552 RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.ACCEPTED,"303",originatorID,requestID,subRequestID);
553 requestHandler.onRequestTTLEnd(asyncResponse);
555 output = requestHandler.handleRequest(input);
556 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
560 public void getMetricserviceTest() throws Exception{
561 Method method = RequestHandlerImpl.class.getDeclaredMethod("getMetricservice", null);
562 method.setAccessible(true);
563 method.invoke(null, null);
567 public void onRequestExecutionStartTest() throws Exception{
568 requestHandler.onRequestExecutionStart("303",false, true);
569 Assert.assertNotNull(requestHandler);
573 private RuntimeContext createRuntimeContextWithSubObjects() {
574 RuntimeContext runtimeContext = new RuntimeContext();
575 RequestContext requestContext = new RequestContext();
576 runtimeContext.setRequestContext(requestContext);
577 ResponseContext responseContext = createResponseContextWithSuObjects();
578 runtimeContext.setResponseContext(responseContext);
579 CommonHeader commonHeader = new CommonHeader();
580 requestContext.setCommonHeader(commonHeader);
581 Flags flags = new Flags();
582 commonHeader.setFlags(flags);
583 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
584 requestContext.setActionIdentifiers(actionIdentifiers);
585 VNFContext vnfContext = new VNFContext();
586 runtimeContext.setVnfContext(vnfContext);
587 return runtimeContext;
591 private ResponseContext createResponseContextWithSuObjects(){
592 ResponseContext responseContext = new ResponseContext();
593 CommonHeader commonHeader = new CommonHeader();
594 Flags flags = new Flags();
595 Status status = new Status();
596 responseContext.setCommonHeader(commonHeader);
597 responseContext.setStatus(status);
598 commonHeader.setFlags(flags);
599 return responseContext;
602 private String convertActionNameToUrl(String action) {
603 String regex = "([a-z])([A-Z]+)";
604 String replacement = "$1-$2";
605 return action.replaceAll(regex, replacement)