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