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