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