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