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