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