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