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