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