2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.requesthandler;
27 import static org.mockito.Matchers.anyBoolean;
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.mock;
32 import static org.powermock.api.mockito.PowerMockito.when;
34 import java.time.Instant;
35 import java.util.HashMap;
36 import java.util.UUID;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Matchers;
44 import org.mockito.Mockito;
45 import org.openecomp.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
46 import org.openecomp.appc.adapter.message.MessageAdapterFactory;
47 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
48 import org.openecomp.appc.configuration.Configuration;
49 import org.openecomp.appc.configuration.ConfigurationFactory;
50 import org.openecomp.appc.domainmodel.lcm.*;
51 import org.openecomp.appc.domainmodel.lcm.Flags.Mode;
52 import org.openecomp.appc.domainmodel.lcm.ActionIdentifiers;
53 import org.openecomp.appc.domainmodel.lcm.CommonHeader;
54 import org.openecomp.appc.domainmodel.lcm.Flags;
55 import org.openecomp.appc.domainmodel.lcm.RequestContext;
56 import org.openecomp.appc.domainmodel.lcm.ResponseContext;
57 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
58 import org.openecomp.appc.domainmodel.lcm.Status;
59 import org.openecomp.appc.domainmodel.lcm.VNFContext;
60 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
61 import org.openecomp.appc.executor.CommandExecutor;
62 import org.openecomp.appc.executor.UnstableVNFException;
63 import org.openecomp.appc.executor.objects.LCMCommandStatus;
64 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
65 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
66 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
67 import org.openecomp.appc.lockmanager.api.LockException;
68 import org.openecomp.appc.lockmanager.api.LockManager;
69 import org.openecomp.appc.messageadapter.MessageAdapter;
70 import org.openecomp.appc.messageadapter.impl.MessageAdapterImpl;
71 import org.openecomp.appc.requesthandler.exceptions.*;
72 import org.openecomp.appc.requesthandler.exceptions.DGWorkflowNotFoundException;
73 import org.openecomp.appc.requesthandler.exceptions.DuplicateRequestException;
74 import org.openecomp.appc.requesthandler.exceptions.InvalidInputException;
75 import org.openecomp.appc.requesthandler.exceptions.RequestExpiredException;
76 import org.openecomp.appc.requesthandler.exceptions.VNFNotFoundException;
77 import org.openecomp.appc.requesthandler.exceptions.WorkflowNotFoundException;
78 import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
79 import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
80 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
81 import org.openecomp.appc.requesthandler.objects.RequestHandlerOutput;
82 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
83 import org.openecomp.appc.transactionrecorder.objects.TransactionRecord;
84 import org.openecomp.appc.workflow.WorkFlowManager;
85 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
86 import org.openecomp.appc.workflow.objects.WorkflowRequest;
87 import org.openecomp.appc.workingstatemanager.WorkingStateManager;
88 import org.openecomp.appc.workingstatemanager.objects.VNFWorkingState;
89 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
91 import com.att.eelf.configuration.EELFLogger;
92 import com.att.eelf.configuration.EELFManager;
94 import org.osgi.framework.Bundle;
95 import org.osgi.framework.BundleContext;
96 import org.osgi.framework.FrameworkUtil;
97 import org.osgi.framework.ServiceReference;
98 import org.powermock.api.mockito.PowerMockito;
99 import org.powermock.core.classloader.annotations.PrepareForTest;
100 import org.powermock.modules.junit4.PowerMockRunner;
102 import com.att.eelf.configuration.EELFLogger;
103 import com.att.eelf.configuration.EELFManager;
106 @RunWith(PowerMockRunner.class)
107 @PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class, MessageAdapterImpl.class})
108 public class TestRequestHandler {
110 private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
112 private RequestHandlerImpl requestHandler;
113 private RequestValidatorImpl requestValidator;
114 private WorkFlowManager workflowManager;
115 private WorkingStateManager workingStateManager ;
116 private LockManager lockManager;
117 private Configuration configuration;
119 private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
120 private final Bundle bundleService=Mockito.mock(Bundle.class);
121 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
122 MessageAdapterFactory factory = new DmaapMessageAdapterFactoryImpl();
126 public void init() throws Exception {
127 configuration = ConfigurationFactory.getConfiguration();
129 configuration.setProperty("appc.LCM.topic.write" , "TEST");
130 configuration.setProperty("appc.LCM.client.key" , "TEST");
131 configuration.setProperty("appc.LCM.client.secret" , "TEST");
133 PowerMockito.mockStatic(FrameworkUtil.class);
134 PowerMockito.when(FrameworkUtil.getBundle(MessageAdapterImpl.class)).thenReturn(bundleService);
135 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
136 PowerMockito.when(bundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(sref);
137 PowerMockito.when(bundleContext.getService(sref)).thenReturn(factory);
140 requestHandler = new RequestHandlerImpl();
141 LifecycleManager lifecyclemanager= mock(LifecycleManager.class);
142 workflowManager= mock(WorkFlowManager.class);
143 CommandExecutor commandExecutor= mock(CommandExecutor.class);
144 MessageAdapter messageAdapter = mock(MessageAdapter.class);
145 workingStateManager = mock(WorkingStateManager.class);
146 lockManager = mock(LockManager.class);
147 TransactionRecorder transactionRecorder= mock(TransactionRecorder.class);
149 requestHandler.setWorkingStateManager(workingStateManager);
150 requestHandler.setMessageAdapter(messageAdapter);
151 requestValidator = mock(RequestValidatorImpl.class);
152 requestValidator.setLifecyclemanager(lifecyclemanager);
153 requestValidator.setWorkingStateManager(workingStateManager);
154 requestValidator.setWorkflowManager(workflowManager);
155 requestValidator.setLifecyclemanager(lifecyclemanager);
156 requestHandler.setCommandExecutor(commandExecutor);
157 requestHandler.setRequestValidator(requestValidator);
158 requestHandler.setLockManager(lockManager);
159 requestHandler.setTransactionRecorder(transactionRecorder);
161 doNothing().when(transactionRecorder).store((TransactionRecord) anyObject());
162 // Mockito.when(commandExecutor.executeCommand((CommandExecutorInput)anyObject())).thenReturn(true);
163 Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
164 for(Integer i=130; i<=140 ; i++)
166 Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
168 Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
169 Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);
170 Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);
171 Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
172 Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);
173 Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);
174 Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);
175 Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);
176 Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);
177 Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);
180 private void threadSleep(){
183 } catch (InterruptedException e) {
189 public void testNegativeFlowWithRequestingUsedVnfId() throws Exception {
190 logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
191 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
192 RequestHandlerInput input1 = this.getRequestHandlerInput("131", VNFOperation.Configure, 1200,
193 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
194 mockRuntimeContextAndVnfContext(input1);
195 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
196 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
197 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
199 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
200 logger.debug("testNegativeFlowWithRequestingUsedVnfId");
201 logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
205 public void testInvalidVNFExceptionRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
206 String originatorID = UUID.randomUUID().toString();
207 String requestID = UUID.randomUUID().toString();
208 String subRequestID = UUID.randomUUID().toString();
209 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
210 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
211 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0,false,originatorID, requestID, subRequestID, Instant.now());
212 PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
213 RequestHandlerOutput output = requestHandler.handleRequest(input);
214 Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
218 public void testLifecycleException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
219 String originatorID = UUID.randomUUID().toString();
220 String requestID = UUID.randomUUID().toString();
221 String subRequestID = UUID.randomUUID().toString();
222 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
223 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
224 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
225 PowerMockito.doThrow(new LifecycleException(new Exception(),"Configured","test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
226 RequestHandlerOutput output = requestHandler.handleRequest(input);
227 Assert.assertEquals(LCMCommandStatus.INVALID_VNF_STATE.getResponseCode(), output.getResponseContext().getStatus().getCode());
232 public void testRequestExpiredException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
233 String originatorID = UUID.randomUUID().toString();
234 String requestID = UUID.randomUUID().toString();
235 String subRequestID = UUID.randomUUID().toString();
236 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
237 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
238 PowerMockito.doThrow(new RequestExpiredException("")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
239 RequestHandlerOutput output = requestHandler.handleRequest(input);
240 Assert.assertEquals(LCMCommandStatus.EXPIRED_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
244 public void testMissingVNFdata() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
245 String originatorID = UUID.randomUUID().toString();
246 String requestID = UUID.randomUUID().toString();
247 String subRequestID = UUID.randomUUID().toString();
249 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,Instant.now());
250 PowerMockito.doThrow(new MissingVNFDataInAAIException("vnf-type")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
251 RequestHandlerOutput output = requestHandler.handleRequest(input);
252 Assert.assertEquals(LCMCommandStatus.MISSING_VNF_DATA_IN_AAI.getResponseCode(), output.getResponseContext().getStatus().getCode());
256 public void testWorkflowNotFoundException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
257 String originatorID = UUID.randomUUID().toString();
258 String requestID = UUID.randomUUID().toString();
259 String subRequestID = UUID.randomUUID().toString();
260 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
261 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
262 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
263 PowerMockito.doThrow(new WorkflowNotFoundException("Unable to find the DG","VNF-2.0.0.0", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
264 RequestHandlerOutput output = requestHandler.handleRequest(input);
265 Assert.assertEquals(LCMCommandStatus.WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());}
268 public void testDGWorkflowNotFoundException() throws Exception {
269 String originatorID = UUID.randomUUID().toString();
270 String requestID = UUID.randomUUID().toString();
271 String subRequestID = UUID.randomUUID().toString();
272 Mockito.when(workflowManager.workflowExists((WorkflowRequest) anyObject())).thenReturn(new WorkflowExistsOutput(true, true));
273 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0, false, originatorID, requestID, subRequestID, Instant.now());
274 PowerMockito.doThrow(new DGWorkflowNotFoundException("Unable to find the DG", "VNF-2.0.0.0", "temp", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
275 RequestHandlerOutput output = requestHandler.handleRequest(input);
276 Assert.assertEquals(LCMCommandStatus.DG_WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
280 public void testInvalidInputException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
281 String originatorID1 = UUID.randomUUID().toString();
282 String requestID1 = UUID.randomUUID().toString();
283 String subRequestID1 = UUID.randomUUID().toString();
284 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
285 RequestHandlerInput input1 = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID1, requestID1, subRequestID1, Instant.now());
286 PowerMockito.doThrow(new InvalidInputException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
287 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
288 Assert.assertEquals(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode(), output1.getResponseContext().getStatus().getCode());
292 public void testNoTransitionDefinedException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
293 String originatorID = UUID.randomUUID().toString();
294 String requestID = UUID.randomUUID().toString();
295 String subRequestID = UUID.randomUUID().toString();
296 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
297 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
298 RequestHandlerInput input = this.getRequestHandlerInput("3010", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
299 PowerMockito.doThrow(new NoTransitionDefinedException("Invalid VNF State","Unstable","Test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
300 RequestHandlerOutput output = requestHandler.handleRequest(input);
301 Assert.assertEquals(LCMCommandStatus.NO_TRANSITION_DEFINE.getResponseCode(), output.getResponseContext().getStatus().getCode());
305 public void rejectInvalidRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
306 String originatorID = UUID.randomUUID().toString();
307 String requestID = UUID.randomUUID().toString();
308 String subRequestID = UUID.randomUUID().toString();
309 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
310 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
311 RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
312 PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
313 RequestHandlerOutput output = requestHandler.handleRequest(input);
314 Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
318 public void testUnstableWorkingState() throws Exception {
319 logger.debug("=====================testUnstableWorkingState=============================");
320 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
321 Mockito.when(workingStateManager.isVNFStable("37")).thenReturn(true,false);
322 RequestHandlerInput input = this.getRequestHandlerInput("37", VNFOperation.Configure, 1200,
323 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
324 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
325 mockRuntimeContextAndVnfContext(input);
326 RequestHandlerOutput output = requestHandler.handleRequest(input);
328 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
330 RequestHandlerInput input1 = this.getRequestHandlerInput("37", VNFOperation.Configure,1200,
331 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
332 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
333 mockRuntimeContextAndVnfContext(input1);
334 RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
336 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
337 logger.debug("=====================testUnstableWorkingState=============================");
341 public void testOnRequestExecutionEndSuccessForWorkingState() throws Exception {
342 logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
343 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
344 RequestHandlerInput input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
345 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
346 mockRuntimeContextAndVnfContext(input1);
348 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
351 RequestHandlerOutput output = requestHandler.handleRequest(input1);
352 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
355 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"137", "", "", ""),true);
357 input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
358 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
359 mockRuntimeContextAndVnfContext(input1);
360 output = requestHandler.handleRequest(input1);
361 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
362 logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
365 private void mockRuntimeContextAndVnfContext(RequestHandlerInput input1) throws Exception {
366 RuntimeContext runtimeContext = PowerMockito.mock(RuntimeContext.class);
367 VNFContext vnfContext = new VNFContext();
368 vnfContext.setType("SCP");
369 vnfContext.setId("137");
370 when(runtimeContext.getVnfContext()).thenReturn(vnfContext);
371 when(runtimeContext.getRequestContext()).thenReturn(input1.getRequestContext());
372 when(runtimeContext.getRpcName()).thenReturn(input1.getRpcName());
375 ResponseContext responseContext = new ResponseContext();
376 responseContext.setStatus(new Status(0, null));
377 responseContext.setAdditionalContext(new HashMap<String, String>(4));
378 responseContext.setCommonHeader(input1.getRequestContext().getCommonHeader());
379 runtimeContext.setResponseContext(responseContext);
380 when(runtimeContext.getResponseContext()).thenReturn(responseContext);
381 responseContext.setStatus(new Status(0, null));
382 runtimeContext.setResponseContext(responseContext);
383 PowerMockito.whenNew(RuntimeContext.class).withAnyArguments().thenReturn(runtimeContext);
388 public void testOnRequestExecutionEndFailureForWorkingState() throws Exception {
389 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
390 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
392 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
394 RequestHandlerInput input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
395 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
396 mockRuntimeContextAndVnfContext(input1);
397 RequestHandlerOutput output = requestHandler.handleRequest(input1);
398 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
400 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.NO_TRANSITION_DEFINE,"38", "", "", ""),true);
402 input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
403 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
404 PowerMockito.doThrow(new UnstableVNFException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
405 mockRuntimeContextAndVnfContext(input1);
406 output = requestHandler.handleRequest(input1);
407 Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
409 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
413 public void testOnRequestExecutionEndTTLExpiredForWorkingState() throws Exception {
414 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
415 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
417 RequestHandlerInput input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1,
418 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
420 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
421 mockRuntimeContextAndVnfContext(input1);
423 RequestHandlerOutput output = requestHandler.handleRequest(input1);
424 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
426 input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1200,
427 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
428 PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
429 output = requestHandler.handleRequest(input1);
430 Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(),output.getResponseContext().getStatus().getCode());
431 logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
435 public void testOnRequestTTLEndForWorkingState() throws Exception {
436 logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
437 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
439 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
441 RequestHandlerInput input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
442 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
443 mockRuntimeContextAndVnfContext(input1);
444 RequestHandlerOutput output = requestHandler.handleRequest(input1);
445 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
447 RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.EXPIRED_REQUEST_FAILURE,"40", "", "", "");
448 requestHandler.onRequestTTLEnd(response,true);
449 input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
450 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
451 PowerMockito.doThrow(new UnstableVNFException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
452 output = requestHandler.handleRequest(input1);
453 Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
454 logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
458 public void testForceCommandExecution() throws Exception {
459 logger.debug("=====================testForceCommandExecution=============================");
460 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
461 RequestHandlerInput input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
462 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
463 mockRuntimeContextAndVnfContext(input1);
465 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
466 RequestHandlerOutput output = requestHandler.handleRequest(input1);
467 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
469 RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.ACCEPTED,"138", "", "", "");
470 requestHandler.onRequestTTLEnd(response,true);
471 input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
472 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
473 input1.getRequestContext().getCommonHeader().setFlags(new Flags(null, true, 1200));
474 mockRuntimeContextAndVnfContext(input1);
475 output = requestHandler.handleRequest(input1);
476 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
477 logger.debug("=====================testForceCommandExecution=============================");
481 public void testOnRequestExecutionEndSuccess() throws VNFNotFoundException {
482 logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Starts =============================");
483 Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
484 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"201", "", "", ""),true);
485 logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Ends =============================");
489 public void testOnRequestExecutionEndFailure() throws VNFNotFoundException {
490 logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Starts =============================");
491 Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
492 requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.DG_FAILURE,"202", "", "", ""),true);
493 logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Ends =============================");
496 private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force,String originatorId, String requestId, String subRequestId, Instant timeStamp){
497 String API_VERSION= "2.0.0";
498 RequestHandlerInput input = new RequestHandlerInput();
499 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
500 RequestContext requestContext = runtimeContext.getRequestContext();
501 input.setRequestContext(requestContext);
502 requestContext.getActionIdentifiers().setVnfId(vnfID);
503 requestContext.setAction(action);
504 input.setRpcName(convertActionNameToUrl(action.name()));
505 requestContext.getCommonHeader().setRequestId(requestId);
506 requestContext.getCommonHeader().setSubRequestId(subRequestId);
507 requestContext.getCommonHeader().setOriginatorId(originatorId);
508 requestContext.getCommonHeader().setFlags(new Flags(null, force, ttl));
509 requestContext.getCommonHeader().setTimestamp(timeStamp);
510 requestContext.getCommonHeader().setApiVer(API_VERSION);
514 private RuntimeContext getAsyncResponse(boolean wfStatus, LCMCommandStatus commandStatus, String vnfId, String originatorId, String requestId, String subRequestId)
516 RuntimeContext output = createRuntimeContextWithSubObjects();
519 output.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
520 output.getVnfContext().setId(vnfId);
521 output.getResponseContext().getCommonHeader().setApiVer("2.0.0");
522 output.getResponseContext().getCommonHeader().setTimestamp( Instant.now());
523 output.getResponseContext().setStatus(LCMCommandStatus.SUCCESS.toStatus(null));
524 output.setTimeStart( Instant.now());
525 output.getResponseContext().getCommonHeader().setOriginatorId(originatorId);
526 output.getResponseContext().getCommonHeader().setRequestId(requestId);
527 output.getResponseContext().getCommonHeader().setSubRequestId(subRequestId);
529 output.getVnfContext().setType("FIREWALL");
530 output.getRequestContext().setAction(VNFOperation.Configure);
531 output.setRpcName("configure");
532 output.getResponseContext().setPayload("");
537 public void rejectDuplicateRequest() throws Exception {
538 String originatorID = UUID.randomUUID().toString();
539 String requestID = UUID.randomUUID().toString();
540 String subRequestID = UUID.randomUUID().toString();
541 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
543 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
544 RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
545 mockRuntimeContextAndVnfContext(input);
547 RequestHandlerOutput output = requestHandler.handleRequest(input);
548 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
550 input = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
552 PowerMockito.doThrow(new DuplicateRequestException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
553 output = requestHandler.handleRequest(input);
554 Assert.assertEquals(LCMCommandStatus.DUPLICATE_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
558 public void removeRequestFromRegistryOnRequestCompletion() throws Exception {
559 String originatorID = UUID.randomUUID().toString();
560 String requestID = UUID.randomUUID().toString();
561 String subRequestID = UUID.randomUUID().toString();
562 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
564 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
565 RequestHandlerInput input = this.getRequestHandlerInput("302", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
566 mockRuntimeContextAndVnfContext(input);
568 RequestHandlerOutput output = requestHandler.handleRequest(input);
569 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
571 RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"302",originatorID,requestID,subRequestID);
572 requestHandler.onRequestExecutionEnd(asyncResponse,true);
574 input = this.getRequestHandlerInput("310", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
575 mockRuntimeContextAndVnfContext(input);
576 output = requestHandler.handleRequest(input);
577 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
581 public void removeRequestFromRegistryOnTTLExpiration() throws Exception {
582 String originatorID = UUID.randomUUID().toString();
583 String requestID = UUID.randomUUID().toString();
584 String subRequestID = UUID.randomUUID().toString();
586 PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
588 Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
589 RequestHandlerInput input = this.getRequestHandlerInput("303", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
590 mockRuntimeContextAndVnfContext(input);
591 RequestHandlerOutput output = requestHandler.handleRequest(input);
592 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
594 RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.ACCEPTED,"303",originatorID,requestID,subRequestID);
595 requestHandler.onRequestTTLEnd(asyncResponse,false);
597 output = requestHandler.handleRequest(input);
598 Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
602 public void getMetricserviceTest() throws Exception{
603 Method method = RequestHandlerImpl.class.getDeclaredMethod("getMetricservice", null);
604 method.setAccessible(true);
605 method.invoke(null, null);
609 public void onRequestExecutionStartTest() throws Exception{
610 Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
611 requestHandler.onRequestExecutionStart("303",false, null, true);
612 Assert.assertNotNull(requestHandler);
616 private RuntimeContext createRuntimeContextWithSubObjects() {
617 RuntimeContext runtimeContext = new RuntimeContext();
618 RequestContext requestContext = new RequestContext();
619 runtimeContext.setRequestContext(requestContext);
620 ResponseContext responseContext = createResponseContextWithSuObjects();
621 runtimeContext.setResponseContext(responseContext);
622 CommonHeader commonHeader = new CommonHeader();
623 requestContext.setCommonHeader(commonHeader);
624 commonHeader.setFlags(new Flags(null, false, 0));
625 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
626 requestContext.setActionIdentifiers(actionIdentifiers);
627 VNFContext vnfContext = new VNFContext();
628 runtimeContext.setVnfContext(vnfContext);
629 return runtimeContext;
633 private ResponseContext createResponseContextWithSuObjects(){
634 ResponseContext responseContext = new ResponseContext();
635 CommonHeader commonHeader = new CommonHeader();
636 responseContext.setCommonHeader(commonHeader);
637 responseContext.setStatus(new Status(0, null));
638 commonHeader.setFlags(new Flags(null, false, 0));
639 return responseContext;
642 private String convertActionNameToUrl(String action) {
643 String regex = "([a-z])([A-Z]+)";
644 String replacement = "$1-$2";
645 return action.replaceAll(regex, replacement)