2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.requesthandler;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Matchers;
29 import org.mockito.Mockito;
30 import org.openecomp.appc.domainmodel.lcm.*;
31 import org.openecomp.appc.executor.CommandExecutor;
32 import org.openecomp.appc.executor.UnstableVNFException;
33 import org.openecomp.appc.executor.objects.CommandExecutorInput;
34 import org.openecomp.appc.executor.objects.LCMCommandStatus;
35 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
36 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
37 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
38 import org.openecomp.appc.lockmanager.api.LockException;
39 import org.openecomp.appc.lockmanager.api.LockManager;
40 import org.openecomp.appc.messageadapter.MessageAdapter;
41 import org.openecomp.appc.requesthandler.exceptions.*;
42 import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
43 import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
44 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
45 import org.openecomp.appc.requesthandler.objects.RequestHandlerOutput;
46 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
47 import org.openecomp.appc.transactionrecorder.objects.TransactionRecord;
48 import org.openecomp.appc.workflow.WorkFlowManager;
49 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
50 import org.openecomp.appc.workflow.objects.WorkflowRequest;
51 import org.openecomp.appc.workingstatemanager.WorkingStateManager;
52 import org.openecomp.appc.workingstatemanager.objects.VNFWorkingState;
53 import com.att.eelf.configuration.EELFLogger;
54 import com.att.eelf.configuration.EELFManager;
55 import org.osgi.framework.FrameworkUtil;
56 import org.powermock.api.mockito.PowerMockito;
57 import org.powermock.core.classloader.annotations.PrepareForTest;
58 import org.powermock.modules.junit4.PowerMockRunner;
61 import static org.mockito.Matchers.*;
62 import static org.mockito.Mockito.doNothing;
63 import static org.mockito.Mockito.mock;
64 import static org.powermock.api.mockito.PowerMockito.when;
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class})
68 public class TestRequestHandler {
70 private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
72 private RequestHandlerImpl requestHandler;
73 private RequestValidatorImpl requestValidator;
74 private WorkFlowManager workflowManager;
75 private WorkingStateManager workingStateManager ;
76 private LockManager lockManager;
79 public void init() throws Exception {
81 requestHandler = new RequestHandlerImpl();
82 LifecycleManager lifecyclemanager= mock(LifecycleManager.class);
83 workflowManager= mock(WorkFlowManager.class);
84 CommandExecutor commandExecutor= mock(CommandExecutor.class);
85 MessageAdapter messageAdapter = mock(MessageAdapter.class);
86 workingStateManager = mock(WorkingStateManager.class);
87 lockManager = mock(LockManager.class);
88 TransactionRecorder transactionRecorder= mock(TransactionRecorder.class);
90 requestHandler.setWorkingStateManager(workingStateManager);
91 requestHandler.setMessageAdapter(messageAdapter);
92 requestValidator = mock(RequestValidatorImpl.class);
93 requestValidator.setLifecyclemanager(lifecyclemanager);
94 requestValidator.setWorkingStateManager(workingStateManager);
95 requestValidator.setWorkflowManager(workflowManager);
96 requestValidator.setLifecyclemanager(lifecyclemanager);
97 requestHandler.setCommandExecutor(commandExecutor);
98 requestHandler.setRequestValidator(requestValidator);
99 requestHandler.setLockManager(lockManager);
100 requestHandler.setTransactionRecorder(transactionRecorder);
102 doNothing().when(transactionRecorder).store((TransactionRecord) anyObject());
103 // Mockito.when(commandExecutor.executeCommand((CommandExecutorInput)anyObject())).thenReturn(true);
104 Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
105 for(Integer i=130; i<=140 ; i++)
107 Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
109 Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
110 Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);
111 Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);
112 Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
113 Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);
114 Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);
115 Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);
116 Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);
117 Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);
118 Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);
121 private void threadSleep(){
124 } catch (InterruptedException e) {
130 public void testNegativeFlowWithRequestingUsedVnfId() throws Exception {
131 logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
132 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
133 RequestHandlerInput input1 = this.getRequestHandlerInput("131", VNFOperation.Configure, 1200,
134 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
135 mockRuntimeContextAndVnfContext(input1);
136 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
137 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
138 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
140 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
141 logger.debug("testNegativeFlowWithRequestingUsedVnfId");
142 logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
146 public void testInvalidVNFExceptionRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
147 String originatorID = UUID.randomUUID().toString();
148 String requestID = UUID.randomUUID().toString();
149 String subRequestID = UUID.randomUUID().toString();
150 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
151 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
152 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0,false,originatorID, requestID, subRequestID,new Date());
153 PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
154 RequestHandlerOutput output = requestHandler.handleRequest(input);
155 Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
159 public void testLifecycleException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
160 String originatorID = UUID.randomUUID().toString();
161 String requestID = UUID.randomUUID().toString();
162 String subRequestID = UUID.randomUUID().toString();
163 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
164 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
165 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
166 PowerMockito.doThrow(new LifecycleException(new Exception(),"Configured","test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
167 RequestHandlerOutput output = requestHandler.handleRequest(input);
168 Assert.assertEquals(LCMCommandStatus.ACTION_NOT_SUPPORTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
173 public void testRequestExpiredException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
174 String originatorID = UUID.randomUUID().toString();
175 String requestID = UUID.randomUUID().toString();
176 String subRequestID = UUID.randomUUID().toString();
177 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
178 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
179 PowerMockito.doThrow(new RequestExpiredException("")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
180 RequestHandlerOutput output = requestHandler.handleRequest(input);
181 Assert.assertEquals(LCMCommandStatus.EXPIRED_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
185 public void testWorkflowNotFoundException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
186 String originatorID = UUID.randomUUID().toString();
187 String requestID = UUID.randomUUID().toString();
188 String subRequestID = UUID.randomUUID().toString();
189 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
190 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
191 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
192 PowerMockito.doThrow(new WorkflowNotFoundException("Unable to find the DG","VNF-2.0.0.0", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
193 RequestHandlerOutput output = requestHandler.handleRequest(input);
194 Assert.assertEquals(LCMCommandStatus.WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());}
197 public void testDGWorkflowNotFoundException() throws Exception {
198 String originatorID = UUID.randomUUID().toString();
199 String requestID = UUID.randomUUID().toString();
200 String subRequestID = UUID.randomUUID().toString();
201 Mockito.when(workflowManager.workflowExists((WorkflowRequest) anyObject())).thenReturn(new WorkflowExistsOutput(true, true));
202 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0, false, originatorID, requestID, subRequestID, new Date());
203 PowerMockito.doThrow(new DGWorkflowNotFoundException("Unable to find the DG", "VNF-2.0.0.0", "temp", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
204 RequestHandlerOutput output = requestHandler.handleRequest(input);
205 Assert.assertEquals(LCMCommandStatus.DG_WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
209 public void testInvalidInputException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
210 String originatorID1 = UUID.randomUUID().toString();
211 String requestID1 = UUID.randomUUID().toString();
212 String subRequestID1 = UUID.randomUUID().toString();
213 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
214 RequestHandlerInput input1 = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID1, requestID1, subRequestID1,new Date());
215 PowerMockito.doThrow(new InvalidInputException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
216 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
217 Assert.assertEquals(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode(), output1.getResponseContext().getStatus().getCode());
221 public void testNoTransitionDefinedException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
222 String originatorID = UUID.randomUUID().toString();
223 String requestID = UUID.randomUUID().toString();
224 String subRequestID = UUID.randomUUID().toString();
225 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
226 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
227 RequestHandlerInput input = this.getRequestHandlerInput("3010", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
228 PowerMockito.doThrow(new NoTransitionDefinedException("Invalid VNF State","Unstable","Test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
229 RequestHandlerOutput output = requestHandler.handleRequest(input);
230 Assert.assertEquals(LCMCommandStatus.NO_TRANSITION_DEFINE.getResponseCode(), output.getResponseContext().getStatus().getCode());
234 public void rejectInvalidRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
235 String originatorID = UUID.randomUUID().toString();
236 String requestID = UUID.randomUUID().toString();
237 String subRequestID = UUID.randomUUID().toString();
238 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
239 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
240 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
241 PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
242 RequestHandlerOutput output = requestHandler.handleRequest(input);
243 Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
247 public void testUnstableWorkingState() throws Exception {
248 logger.debug("=====================testUnstableWorkingState=============================");
249 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
250 Mockito.when(workingStateManager.isVNFStable("37")).thenReturn(true,false);
251 RequestHandlerInput input = this.getRequestHandlerInput("37", VNFOperation.Configure, 1200,
252 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
253 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
254 mockRuntimeContextAndVnfContext(input);
255 RequestHandlerOutput output = requestHandler.handleRequest(input);
257 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
259 RequestHandlerInput input1 = this.getRequestHandlerInput("37", VNFOperation.Configure,1200,
260 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
261 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
262 mockRuntimeContextAndVnfContext(input1);
263 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
265 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
266 logger.debug("=====================testUnstableWorkingState=============================");
270 public void testOnRequestExecutionEndSuccessForWorkingState() throws Exception {
271 logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
272 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
273 RequestHandlerInput input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
274 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
275 mockRuntimeContextAndVnfContext(input1);
277 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
280 RequestHandlerOutput output = requestHandler.handleRequest(input1);
281 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
284 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"137", "", "", ""),true);
286 input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
287 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
288 mockRuntimeContextAndVnfContext(input1);
289 output = requestHandler.handleRequest(input1);
290 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
291 logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
294 private void mockRuntimeContextAndVnfContext(RequestHandlerInput input1) throws Exception {
295 RuntimeContext runtimeContext = PowerMockito.mock(RuntimeContext.class);
296 VNFContext vnfContext = new VNFContext();
297 vnfContext.setType("SCP");
298 vnfContext.setId("137");
299 when(runtimeContext.getVnfContext()).thenReturn(vnfContext);
300 when(runtimeContext.getRequestContext()).thenReturn(input1.getRequestContext());
301 when(runtimeContext.getRpcName()).thenReturn(input1.getRpcName());
304 ResponseContext responseContext = new ResponseContext();
305 responseContext.setStatus(new Status());
306 responseContext.setAdditionalContext(new HashMap<String, String>(4));
307 responseContext.setCommonHeader(input1.getRequestContext().getCommonHeader());
308 runtimeContext.setResponseContext(responseContext);
309 when(runtimeContext.getResponseContext()).thenReturn(responseContext);
310 responseContext.setStatus(new Status());
311 runtimeContext.setResponseContext(responseContext);
312 PowerMockito.whenNew(RuntimeContext.class).withAnyArguments().thenReturn(runtimeContext);
318 public void testOnRequestExecutionEndFailureForWorkingState() throws Exception {
319 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
320 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
322 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
324 RequestHandlerInput input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
325 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
326 mockRuntimeContextAndVnfContext(input1);
327 RequestHandlerOutput output = requestHandler.handleRequest(input1);
328 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
330 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.NO_TRANSITION_DEFINE,"38", "", "", ""),true);
332 input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
333 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
334 PowerMockito.doThrow(new UnstableVNFException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
335 mockRuntimeContextAndVnfContext(input1);
336 output = requestHandler.handleRequest(input1);
337 Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
339 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
343 public void testOnRequestExecutionEndTTLExpiredForWorkingState() throws Exception {
344 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
345 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
347 RequestHandlerInput input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1,
348 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
350 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
351 mockRuntimeContextAndVnfContext(input1);
353 RequestHandlerOutput output = requestHandler.handleRequest(input1);
354 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
356 input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1200,
357 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
358 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
359 output = requestHandler.handleRequest(input1);
360 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(),output.getResponseContext().getStatus().getCode());
361 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
365 public void testOnRequestTTLEndForWorkingState() throws Exception {
366 logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
367 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
369 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
371 RequestHandlerInput input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
372 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
373 mockRuntimeContextAndVnfContext(input1);
374 RequestHandlerOutput output = requestHandler.handleRequest(input1);
375 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
377 RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.EXPIRED_REQUEST_FAILURE,"40", "", "", "");
378 requestHandler.onRequestTTLEnd(response,true);
379 input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
380 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
381 PowerMockito.doThrow(new UnstableVNFException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
382 output = requestHandler.handleRequest(input1);
383 Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
384 logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
388 public void testForceCommandExecution() throws Exception {
389 logger.debug("=====================testForceCommandExecution=============================");
390 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
391 RequestHandlerInput input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
392 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
393 mockRuntimeContextAndVnfContext(input1);
395 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
396 RequestHandlerOutput output = requestHandler.handleRequest(input1);
397 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
399 RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.ACCEPTED,"138", "", "", "");
400 requestHandler.onRequestTTLEnd(response,true);
401 input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
402 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
403 input1.getRequestContext().getCommonHeader().getFlags().setForce(true);
404 mockRuntimeContextAndVnfContext(input1);
405 output = requestHandler.handleRequest(input1);
406 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
407 logger.debug("=====================testForceCommandExecution=============================");
411 public void testOnRequestExecutionEndSuccess() throws VNFNotFoundException {
412 logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Starts =============================");
413 Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
414 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"201", "", "", ""),true);
415 logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Ends =============================");
419 public void testOnRequestExecutionEndFailure() throws VNFNotFoundException {
420 logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Starts =============================");
421 Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
422 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.DG_FAILURE,"202", "", "", ""),true);
423 logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Ends =============================");
426 private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force,String originatorId, String requestId, String subRequestId,Date timeStamp){
427 String API_VERSION= "2.0.0";
428 RequestHandlerInput input = new RequestHandlerInput();
429 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
430 RequestContext requestContext = runtimeContext.getRequestContext();
431 input.setRequestContext(requestContext);
432 requestContext.getActionIdentifiers().setVnfId(vnfID);
433 requestContext.setAction(action);
434 input.setRpcName(convertActionNameToUrl(action.name()));
435 requestContext.getCommonHeader().setRequestId(requestId);
436 requestContext.getCommonHeader().setSubRequestId(subRequestId);
437 requestContext.getCommonHeader().setOriginatorId(originatorId);
438 requestContext.getCommonHeader().getFlags().setTtl(ttl);
439 requestContext.getCommonHeader().getFlags().setForce(force);
440 requestContext.getCommonHeader().setTimestamp(timeStamp);
441 requestContext.getCommonHeader().setApiVer(API_VERSION);
445 private RuntimeContext getAsyncResponse(boolean wfStatus, LCMCommandStatus commandStatus, String vnfId, String originatorId, String requestId, String subRequestId)
447 RuntimeContext output = createRuntimeContextWithSubObjects();
450 output.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
451 output.getVnfContext().setId(vnfId);
452 output.getResponseContext().getCommonHeader().setApiVer("2.0.0");
453 output.getResponseContext().getCommonHeader().setTimestamp(new Date());
454 output.getResponseContext().getStatus().setCode(LCMCommandStatus.SUCCESS.getResponseCode());
455 output.setTimeStart(new Date());
456 output.getResponseContext().getCommonHeader().setOriginatorId(originatorId);
457 output.getResponseContext().getCommonHeader().setRequestId(requestId);
458 output.getResponseContext().getCommonHeader().setSubRequestId(subRequestId);
460 output.getVnfContext().setType("FIREWALL");
461 output.getRequestContext().setAction(VNFOperation.Configure);
462 output.setRpcName("configure");
463 output.getResponseContext().setPayload("");
468 public void rejectDuplicateRequest() throws Exception {
469 String originatorID = UUID.randomUUID().toString();
470 String requestID = UUID.randomUUID().toString();
471 String subRequestID = UUID.randomUUID().toString();
472 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
474 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
475 RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
476 mockRuntimeContextAndVnfContext(input);
478 RequestHandlerOutput output = requestHandler.handleRequest(input);
479 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
481 input = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
483 PowerMockito.doThrow(new DuplicateRequestException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
484 output = requestHandler.handleRequest(input);
485 Assert.assertEquals(LCMCommandStatus.DUPLICATE_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
489 public void removeRequestFromRegistryOnRequestCompletion() throws Exception {
490 String originatorID = UUID.randomUUID().toString();
491 String requestID = UUID.randomUUID().toString();
492 String subRequestID = UUID.randomUUID().toString();
493 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
495 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
496 RequestHandlerInput input = this.getRequestHandlerInput("302", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
497 mockRuntimeContextAndVnfContext(input);
499 RequestHandlerOutput output = requestHandler.handleRequest(input);
500 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
502 RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"302",originatorID,requestID,subRequestID);
503 requestHandler.onRequestExecutionEnd(asyncResponse,true);
505 input = this.getRequestHandlerInput("310", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
506 mockRuntimeContextAndVnfContext(input);
507 output = requestHandler.handleRequest(input);
508 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
512 public void removeRequestFromRegistryOnTTLExpiration() throws Exception {
513 String originatorID = UUID.randomUUID().toString();
514 String requestID = UUID.randomUUID().toString();
515 String subRequestID = UUID.randomUUID().toString();
517 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
519 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
520 RequestHandlerInput input = this.getRequestHandlerInput("303", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
521 mockRuntimeContextAndVnfContext(input);
522 RequestHandlerOutput output = requestHandler.handleRequest(input);
523 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
525 RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.ACCEPTED,"303",originatorID,requestID,subRequestID);
526 requestHandler.onRequestTTLEnd(asyncResponse,false);
528 output = requestHandler.handleRequest(input);
529 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
533 public void getMetricserviceTest() throws Exception{
534 Method method = RequestHandlerImpl.class.getDeclaredMethod("getMetricservice", null);
535 method.setAccessible(true);
536 method.invoke(null, null);
540 public void onRequestExecutionStartTest() throws Exception{
541 Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
542 requestHandler.onRequestExecutionStart("303",false, null, true);
543 Assert.assertNotNull(requestHandler);
547 private RuntimeContext createRuntimeContextWithSubObjects() {
548 RuntimeContext runtimeContext = new RuntimeContext();
549 RequestContext requestContext = new RequestContext();
550 runtimeContext.setRequestContext(requestContext);
551 ResponseContext responseContext = createResponseContextWithSuObjects();
552 runtimeContext.setResponseContext(responseContext);
553 CommonHeader commonHeader = new CommonHeader();
554 requestContext.setCommonHeader(commonHeader);
555 Flags flags = new Flags();
556 commonHeader.setFlags(flags);
557 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
558 requestContext.setActionIdentifiers(actionIdentifiers);
559 VNFContext vnfContext = new VNFContext();
560 runtimeContext.setVnfContext(vnfContext);
561 return runtimeContext;
565 private ResponseContext createResponseContextWithSuObjects(){
566 ResponseContext responseContext = new ResponseContext();
567 CommonHeader commonHeader = new CommonHeader();
568 Flags flags = new Flags();
569 Status status = new Status();
570 responseContext.setCommonHeader(commonHeader);
571 responseContext.setStatus(status);
572 commonHeader.setFlags(flags);
573 return responseContext;
576 private String convertActionNameToUrl(String action) {
577 String regex = "([a-z])([A-Z]+)";
578 String replacement = "$1-$2";
579 return action.replaceAll(regex, replacement)