89bb13f7c2984b4a6ccc965b6b1030e2590ae51f
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.requesthandler;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Matchers;
33 import org.mockito.Mockito;
34 import org.onap.appc.configuration.Configuration;
35 import org.onap.appc.configuration.ConfigurationFactory;
36 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
37 import org.onap.appc.domainmodel.lcm.CommonHeader;
38 import org.onap.appc.domainmodel.lcm.Flags;
39 import org.onap.appc.domainmodel.lcm.RequestContext;
40 import org.onap.appc.domainmodel.lcm.ResponseContext;
41 import org.onap.appc.domainmodel.lcm.RuntimeContext;
42 import org.onap.appc.domainmodel.lcm.Status;
43 import org.onap.appc.domainmodel.lcm.VNFContext;
44 import org.onap.appc.domainmodel.lcm.VNFOperation;
45 import org.onap.appc.exceptions.InvalidInputException;
46 import org.onap.appc.executor.CommandExecutor;
47 import org.onap.appc.executor.objects.LCMCommandStatus;
48 import org.onap.appc.lifecyclemanager.LifecycleManager;
49 import org.onap.appc.lifecyclemanager.objects.LifecycleException;
50 import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException;
51 import org.onap.appc.lockmanager.api.LockException;
52 import org.onap.appc.lockmanager.api.LockManager;
53 import org.onap.appc.messageadapter.MessageAdapter;
54 import org.onap.appc.messageadapter.impl.MessageAdapterImpl;
55 import org.onap.appc.requesthandler.exceptions.DGWorkflowNotFoundException;
56 import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
57 import org.onap.appc.requesthandler.exceptions.LCMOperationsDisabledException;
58 import org.onap.appc.requesthandler.exceptions.MissingVNFDataInAAIException;
59 import org.onap.appc.requesthandler.exceptions.RequestExpiredException;
60 import org.onap.appc.requesthandler.exceptions.VNFNotFoundException;
61 import org.onap.appc.requesthandler.exceptions.WorkflowNotFoundException;
62 import org.onap.appc.requesthandler.impl.RequestHandlerImpl;
63 import org.onap.appc.requesthandler.impl.RequestValidatorImpl;
64 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
65 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
66 import org.onap.appc.transactionrecorder.TransactionRecorder;
67 import org.onap.appc.workflow.WorkFlowManager;
68 import org.onap.appc.workflow.objects.WorkflowExistsOutput;
69 import org.osgi.framework.Bundle;
70 import org.osgi.framework.BundleContext;
71 import org.osgi.framework.FrameworkUtil;
72 import org.osgi.framework.ServiceReference;
73 import org.powermock.api.mockito.PowerMockito;
74 import org.powermock.core.classloader.annotations.PrepareForTest;
75 import org.powermock.modules.junit4.PowerMockRunner;
76
77 import java.util.Date;
78 import java.util.HashMap;
79 import java.util.UUID;
80
81 import static org.mockito.Matchers.anyObject;
82 import static org.mockito.Mockito.doNothing;
83 import static org.mockito.Mockito.mock;
84 import static org.powermock.api.mockito.PowerMockito.when;
85
86 @RunWith(PowerMockRunner.class)
87 @PrepareForTest( {FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class, MessageAdapterImpl.class})
88 public class RequestHandlerTest {
89
90     private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestHandlerTest.class);
91
92     private RequestHandlerImpl requestHandler;
93     private RequestValidatorImpl requestValidator;
94     private WorkFlowManager workflowManager;
95     private LockManager lockManager;
96     private Configuration configuration;
97
98     @Before
99     public void init() throws Exception {
100         configuration = ConfigurationFactory.getConfiguration();
101                 
102         configuration.setProperty("appc.LCM.topic.write" , "TEST");
103         configuration.setProperty("appc.LCM.client.key" , "TEST");
104         configuration.setProperty("appc.LCM.client.secret" , "TEST");
105         
106         PowerMockito.mockStatic(FrameworkUtil.class);
107         
108     
109         requestHandler = new RequestHandlerImpl();
110         LifecycleManager lifecyclemanager= mock(LifecycleManager.class);
111         workflowManager= mock(WorkFlowManager.class);
112
113         CommandExecutor commandExecutor= mock(CommandExecutor.class);
114         MessageAdapter messageAdapter = mock(MessageAdapter.class);
115         lockManager = mock(LockManager.class);
116         TransactionRecorder transactionRecorder= mock(TransactionRecorder.class);
117
118         requestHandler.setMessageAdapter(messageAdapter);
119         requestValidator = mock(RequestValidatorImpl.class);
120         requestValidator.setWorkflowManager(workflowManager);
121         requestHandler.setCommandExecutor(commandExecutor);
122         requestHandler.setRequestValidator(requestValidator);
123         requestHandler.setLockManager(lockManager);
124         requestHandler.setTransactionRecorder(transactionRecorder);
125
126         doNothing().when(transactionRecorder).store(anyObject());
127 //        Mockito.when(commandExecutor.executeCommand((CommandExecutorInput)anyObject())).thenReturn(true);
128     }
129
130     private void threadSleep(){
131         try {
132             Thread.sleep(5);
133         } catch (InterruptedException e) {
134             e.printStackTrace();
135         }
136     }
137     //TODO needs to be fixed
138     /*@Test
139     public void testNegativeFlowWithRequestingUsedVnfId() throws Exception {
140         logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
141         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
142         RequestHandlerInput input1 = this.getRequestHandlerInput("131", VNFOperation.Configure, 1200,
143                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
144         mockRuntimeContextAndVnfContext(input1);
145         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
146         PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
147         RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
148         threadSleep ();
149         Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
150         logger.debug("testNegativeFlowWithRequestingUsedVnfId");
151         logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
152     }
153
154     @Test
155     public void testInvalidVNFExceptionRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException,  DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
156         String originatorID = UUID.randomUUID().toString();
157         String requestID = UUID.randomUUID().toString();
158         String subRequestID = UUID.randomUUID().toString();
159         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
160         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0,false,originatorID, requestID, subRequestID,new Date());
161         ///PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
162         RequestHandlerOutput output = requestHandler.handleRequest(input);
163         Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
164     }
165
166     @Test
167     public void testLifecycleException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException,  DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
168         String originatorID = UUID.randomUUID().toString();
169         String requestID = UUID.randomUUID().toString();
170         String subRequestID = UUID.randomUUID().toString();
171         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
172         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
173         RequestHandlerOutput output = requestHandler.handleRequest(input);
174         Assert.assertEquals(LCMCommandStatus.INVALID_VNF_STATE.getResponseCode(), output.getResponseContext().getStatus().getCode());
175     }
176
177
178     @Test
179     public void testRequestExpiredException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
180         String originatorID = UUID.randomUUID().toString();
181         String requestID = UUID.randomUUID().toString();
182         String subRequestID = UUID.randomUUID().toString();
183         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
184         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
185         RequestHandlerOutput output = requestHandler.handleRequest(input);
186         Assert.assertEquals(LCMCommandStatus.EXPIRED_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
187     }
188
189     @Test
190     public void testMissingVNFdata() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException,  DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
191         String originatorID = UUID.randomUUID().toString();
192         String requestID = UUID.randomUUID().toString();
193         String subRequestID = UUID.randomUUID().toString();
194
195         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
196         *//*PowerMockito.doThrow(new MissingVNFDataInAAIException("vnf-type")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));*//*
197         RequestHandlerOutput output = requestHandler.handleRequest(input);
198         Assert.assertEquals(LCMCommandStatus.MISSING_VNF_DATA_IN_AAI.getResponseCode(), output.getResponseContext().getStatus().getCode());
199     }
200
201     @Test
202     public void testWorkflowNotFoundException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
203         String originatorID = UUID.randomUUID().toString();
204         String requestID = UUID.randomUUID().toString();
205         String subRequestID = UUID.randomUUID().toString();
206         //PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
207         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
208         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
209         //PowerMockito.doThrow(new WorkflowNotFoundException("Unable to find the DG","VNF-2.0.0.0", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
210         RequestHandlerOutput output = requestHandler.handleRequest(input);
211         Assert.assertEquals(LCMCommandStatus.WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());}
212
213     @Test
214     public void testDGWorkflowNotFoundException() throws Exception {
215         String originatorID = UUID.randomUUID().toString();
216         String requestID = UUID.randomUUID().toString();
217         String subRequestID = UUID.randomUUID().toString();
218         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true, true));
219         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0, false, originatorID, requestID, subRequestID, new Date());
220         PowerMockito.doThrow(new DGWorkflowNotFoundException("Unable to find the DG", "VNF-2.0.0.0", "temp", "Test","Test","Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
221         RequestHandlerOutput output = requestHandler.handleRequest(input);
222         Assert.assertEquals(LCMCommandStatus.DG_WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
223     }
224
225     @Test
226     public void testInvalidInputException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
227         String originatorID1 = UUID.randomUUID().toString();
228         String requestID1 = UUID.randomUUID().toString();
229         String subRequestID1 = UUID.randomUUID().toString();
230         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
231         RequestHandlerInput input1 = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID1, requestID1, subRequestID1,new Date());
232         //PowerMockito.doThrow(new InvalidInputException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
233         RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
234         Assert.assertEquals(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode(), output1.getResponseContext().getStatus().getCode());
235     }
236
237     @Test
238     public void testNoTransitionDefinedException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
239         String originatorID = UUID.randomUUID().toString();
240         String requestID = UUID.randomUUID().toString();
241         String subRequestID = UUID.randomUUID().toString();
242         //PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
243         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
244         RequestHandlerInput input = this.getRequestHandlerInput("3010", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
245         ///PowerMockito.doThrow(new NoTransitionDefinedException("Invalid VNF State","Unstable","Test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
246         RequestHandlerOutput output = requestHandler.handleRequest(input);
247         Assert.assertEquals(LCMCommandStatus.NO_TRANSITION_DEFINE.getResponseCode(), output.getResponseContext().getStatus().getCode());
248     }
249
250     @Test
251     public void rejectInvalidRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
252         String originatorID = UUID.randomUUID().toString();
253         String requestID = UUID.randomUUID().toString();
254         String subRequestID = UUID.randomUUID().toString();
255         ///PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
256         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
257         RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
258         ///PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
259         RequestHandlerOutput output = requestHandler.handleRequest(input);
260         Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
261     }
262
263     @Test
264     public void testUnstableWorkingState() throws Exception {
265         logger.debug("=====================testUnstableWorkingState=============================");
266         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
267         RequestHandlerInput input = this.getRequestHandlerInput("37", VNFOperation.Configure, 1200,
268                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
269         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
270         mockRuntimeContextAndVnfContext(input);
271         RequestHandlerOutput output = requestHandler.handleRequest(input);
272
273         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
274
275         RequestHandlerInput input1 = this.getRequestHandlerInput("37", VNFOperation.Configure,1200,
276                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
277         PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
278         mockRuntimeContextAndVnfContext(input1);
279         RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
280
281         Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
282         logger.debug("=====================testUnstableWorkingState=============================");
283     }
284
285     @Test
286     public void testOnRequestExecutionEndSuccessForWorkingState() throws Exception {
287         logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
288         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
289         RequestHandlerInput input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
290                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
291         mockRuntimeContextAndVnfContext(input1);
292
293         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
294
295
296         RequestHandlerOutput output = requestHandler.handleRequest(input1);
297         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
298         threadSleep();
299
300         requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"137", "", "", ""));
301
302         input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
303                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
304         mockRuntimeContextAndVnfContext(input1);
305         output = requestHandler.handleRequest(input1);
306         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
307         logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
308     }
309
310     private void mockRuntimeContextAndVnfContext(RequestHandlerInput input1) throws Exception {
311         RuntimeContext runtimeContext = PowerMockito.mock(RuntimeContext.class);
312         VNFContext vnfContext = new VNFContext();
313         vnfContext.setType("SCP");
314         vnfContext.setId("137");
315         when(runtimeContext.getVnfContext()).thenReturn(vnfContext);
316         when(runtimeContext.getRequestContext()).thenReturn(input1.getRequestContext());
317         when(runtimeContext.getRpcName()).thenReturn(input1.getRpcName());
318         Date startTime = new Date();
319         when(runtimeContext.getTimeStart()).thenReturn(startTime.toInstant());
320
321
322         ResponseContext responseContext = new ResponseContext();
323         responseContext.setStatus(new Status());
324         responseContext.setAdditionalContext(new HashMap<>(4));
325         responseContext.setCommonHeader(input1.getRequestContext().getCommonHeader());
326         runtimeContext.setResponseContext(responseContext);
327         when(runtimeContext.getResponseContext()).thenReturn(responseContext);
328         responseContext.setStatus(new Status());
329         runtimeContext.setResponseContext(responseContext);
330         PowerMockito.whenNew(RuntimeContext.class).withAnyArguments().thenReturn(runtimeContext);
331
332     }
333
334     @Test
335     public void testOnRequestExecutionEndFailureForWorkingState() throws Exception {
336         logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
337         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
338
339         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
340
341         RuntimeContext noneMockRuntimeContext = this.getAsyncResponse(false,LCMCommandStatus.NO_TRANSITION_DEFINE,"38", "", "", "");
342
343         RequestHandlerInput input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
344                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
345         mockRuntimeContextAndVnfContext(input1);
346         RequestHandlerOutput output = requestHandler.handleRequest(input1);
347         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
348         threadSleep();
349         requestHandler.onRequestExecutionEnd(noneMockRuntimeContext);
350
351         input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
352                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
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(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(),new Date());
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(),new Date());
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(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(),new Date());
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);
397         input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
398                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
399         output = requestHandler.handleRequest(input1);
400         Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
401         logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
402     }
403
404     @Test
405     public void testForceCommandExecution() throws Exception {
406         logger.debug("=====================testForceCommandExecution=============================");
407         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
408         RequestHandlerInput input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
409                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
410         mockRuntimeContextAndVnfContext(input1);
411
412         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
413         RequestHandlerOutput output = requestHandler.handleRequest(input1);
414         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
415         threadSleep();
416         RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.ACCEPTED,"138", "", "", "");
417         requestHandler.onRequestTTLEnd(response);
418         input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
419                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
420         input1.getRequestContext().getCommonHeader().getFlags().setForce(true);
421         mockRuntimeContextAndVnfContext(input1);
422         output = requestHandler.handleRequest(input1);
423         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
424         logger.debug("=====================testForceCommandExecution=============================");
425     }
426
427     @Test
428     public void testOnRequestExecutionEndSuccess() throws VNFNotFoundException {
429         logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Starts =============================");
430         requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"201", "", "", ""));
431         logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Ends =============================");
432     }
433
434     @Test
435     public void testOnRequestExecutionEndFailure() throws  VNFNotFoundException {
436         logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Starts =============================");
437         requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.DG_FAILURE,"202", "", "", ""));
438         logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Ends =============================");
439     }
440
441     private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force,String originatorId, String requestId, String subRequestId,Date timeStamp){
442         String API_VERSION= "2.0.0";
443         RequestHandlerInput input = new RequestHandlerInput();
444         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
445         RequestContext requestContext = runtimeContext.getRequestContext();
446         input.setRequestContext(requestContext);
447         requestContext.getActionIdentifiers().setVnfId(vnfID);
448         requestContext.setAction(action);
449         input.setRpcName(convertActionNameToUrl(action.name()));
450         requestContext.getCommonHeader().setRequestId(requestId);
451         requestContext.getCommonHeader().setSubRequestId(subRequestId);
452         requestContext.getCommonHeader().setOriginatorId(originatorId);
453         requestContext.getCommonHeader().getFlags().setTtl(ttl);
454         requestContext.getCommonHeader().getFlags().setForce(force);
455         requestContext.getCommonHeader().setTimestamp(timeStamp);
456         requestContext.getCommonHeader().setApiVer(API_VERSION);
457         return input;
458     }
459
460     private RuntimeContext getAsyncResponse(boolean wfStatus, LCMCommandStatus commandStatus, String vnfId, String originatorId, String requestId, String subRequestId)
461     {
462         RuntimeContext output = createRuntimeContextWithSubObjects();
463
464
465         output.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
466         output.getVnfContext().setId(vnfId);
467         output.getResponseContext().getCommonHeader().setApiVer("2.0.0");
468         output.getResponseContext().getCommonHeader().setTimestamp(new Date());
469         output.getResponseContext().getStatus().setCode(LCMCommandStatus.SUCCESS.getResponseCode());
470         output.setTimeStart(new Date().toInstant());
471         output.getResponseContext().getCommonHeader().setOriginatorId(originatorId);
472         output.getResponseContext().getCommonHeader().setRequestId(requestId);
473         output.getResponseContext().getCommonHeader().setSubRequestId(subRequestId);
474
475         output.getVnfContext().setType("FIREWALL");
476         output.getRequestContext().setAction(VNFOperation.Configure);
477         output.setRpcName("configure");
478         output.getResponseContext().setPayload("");
479         return output;
480     }
481
482     @Test
483     public void rejectDuplicateRequest() throws Exception {
484         String originatorID = UUID.randomUUID().toString();
485         String requestID = UUID.randomUUID().toString();
486         String subRequestID = UUID.randomUUID().toString();
487         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
488
489         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
490         RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
491         mockRuntimeContextAndVnfContext(input);
492
493         RequestHandlerOutput output = requestHandler.handleRequest(input);
494         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
495
496         input = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
497
498         PowerMockito.doThrow(new DuplicateRequestException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
499         output = requestHandler.handleRequest(input);
500         Assert.assertEquals(LCMCommandStatus.DUPLICATE_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
501     }
502
503     @Test
504     public void removeRequestFromRegistryOnRequestCompletion() throws Exception {
505         String originatorID = UUID.randomUUID().toString();
506         String requestID = UUID.randomUUID().toString();
507         String subRequestID = UUID.randomUUID().toString();
508         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
509
510         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
511         RequestHandlerInput input = this.getRequestHandlerInput("302", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
512         mockRuntimeContextAndVnfContext(input);
513
514         RequestHandlerOutput output = requestHandler.handleRequest(input);
515         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
516
517         RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"302",originatorID,requestID,subRequestID);
518         requestHandler.onRequestExecutionEnd(asyncResponse);
519
520         input = this.getRequestHandlerInput("310", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
521         mockRuntimeContextAndVnfContext(input);
522         output = requestHandler.handleRequest(input);
523         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
524     }
525
526     @Test
527     public void removeRequestFromRegistryOnTTLExpiration() throws Exception {
528         String originatorID = UUID.randomUUID().toString();
529         String requestID = UUID.randomUUID().toString();
530         String subRequestID = UUID.randomUUID().toString();
531
532         PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
533
534         Mockito.when(workflowManager.workflowExists(anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
535         RequestHandlerInput input = this.getRequestHandlerInput("303", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
536         mockRuntimeContextAndVnfContext(input);
537         RequestHandlerOutput output = requestHandler.handleRequest(input);
538         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
539
540         RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.ACCEPTED,"303",originatorID,requestID,subRequestID);
541         requestHandler.onRequestTTLEnd(asyncResponse);
542
543         output = requestHandler.handleRequest(input);
544         Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
545     }*/
546
547     /*@Test
548     public void getMetricserviceTest() throws Exception{
549         Method method = RequestHandlerImpl.class.getDeclaredMethod("getMetricservice", null);
550         method.setAccessible(true);
551         method.invoke(null, null);
552
553     }*/
554     @Test
555     public void onRequestExecutionStartTest() throws  Exception{
556                 requestHandler.onRequestExecutionStart("303",false,  true);
557         Assert.assertNotNull(requestHandler);
558     }
559
560
561     private RuntimeContext createRuntimeContextWithSubObjects() {
562         RuntimeContext runtimeContext = new RuntimeContext();
563         RequestContext requestContext = new RequestContext();
564         runtimeContext.setRequestContext(requestContext);
565         ResponseContext responseContext = createResponseContextWithSuObjects();
566         runtimeContext.setResponseContext(responseContext);
567         CommonHeader commonHeader = new CommonHeader();
568         requestContext.setCommonHeader(commonHeader);
569         Flags flags = new Flags();
570         commonHeader.setFlags(flags);
571         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
572         requestContext.setActionIdentifiers(actionIdentifiers);
573         VNFContext vnfContext = new VNFContext();
574         runtimeContext.setVnfContext(vnfContext);
575         return runtimeContext;
576
577     }
578
579     private ResponseContext createResponseContextWithSuObjects(){
580         ResponseContext responseContext = new ResponseContext();
581         CommonHeader commonHeader = new CommonHeader();
582         Flags flags = new Flags();
583         Status status = new Status();
584         responseContext.setCommonHeader(commonHeader);
585         responseContext.setStatus(status);
586         commonHeader.setFlags(flags);
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