2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.openecomp.appc.requesthandler;
 
  25 import org.junit.Before;
 
  26 import org.junit.Test;
 
  27 import org.junit.Ignore;
 
  28 import org.junit.runner.RunWith;
 
  29 import org.mockito.Mockito;
 
  30 import org.mockito.invocation.InvocationOnMock;
 
  31 import org.mockito.stubbing.Answer;
 
  32 import org.openecomp.appc.domainmodel.lcm.*;
 
  33 import org.openecomp.appc.executor.UnstableVNFException;
 
  34 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
 
  35 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
 
  36 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
 
  37 import org.openecomp.appc.requesthandler.exceptions.*;
 
  38 import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
 
  39 import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
 
  40 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
 
  41 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
 
  42 import org.openecomp.appc.workflow.WorkFlowManager;
 
  43 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
 
  44 import org.openecomp.appc.workflow.objects.WorkflowRequest;
 
  45 import org.openecomp.appc.workingstatemanager.WorkingStateManager;
 
  46 import com.att.eelf.configuration.EELFLogger;
 
  47 import com.att.eelf.configuration.EELFManager;
 
  48 import org.openecomp.sdnc.sli.SvcLogicContext;
 
  49 import org.openecomp.sdnc.sli.SvcLogicResource;
 
  50 import org.openecomp.sdnc.sli.aai.AAIService;
 
  51 import org.osgi.framework.Bundle;
 
  52 import org.osgi.framework.BundleContext;
 
  53 import org.osgi.framework.FrameworkUtil;
 
  54 import org.osgi.framework.ServiceReference;
 
  55 import org.powermock.api.mockito.PowerMockito;
 
  56 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  57 import org.powermock.modules.junit4.PowerMockRunner;
 
  59 import java.util.Date;
 
  61 import java.util.UUID;
 
  63 import static junit.framework.TestCase.assertNotNull;
 
  64 import static org.junit.Assert.assertNull;
 
  65 import static org.mockito.Matchers.*;
 
  68 @RunWith(PowerMockRunner.class)
 
  69 @PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class})
 
  71 public class TestRequestValidator {
 
  73     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
 
  75     private static final String TTL_FLAG= "TTL";
 
  77     private RequestValidatorImpl requestValidator;
 
  79     AAIService aaiAdapter ;
 
  80     LifecycleManager lifecyclemanager;
 
  81     WorkFlowManager workflowManager;
 
  82     WorkingStateManager workingStateManager ;
 
  85     private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
 
  86     private final Bundle bundleService=Mockito.mock(Bundle.class);
 
  87     private final ServiceReference sref=Mockito.mock(ServiceReference.class);
 
  92     public void init() throws Exception {
 
  94 //        dao = Mockito.mock(AppcDAOImpl.class);
 
  95 //        PowerMockito.whenNew(AppcDAOImpl.class).withNoArguments().thenReturn(dao);
 
  96 //        Mockito.doNothing().when(dao).storeTransactionRecord((TransactionRecord)anyObject());
 
  97         //   PowerMockito.when(dao.queryWorkflow(anyString(),anyString())).thenReturn(true);
 
 100         AAIService aaiService = Mockito.mock(AAIService.class);;
 
 101         PowerMockito.mockStatic(FrameworkUtil.class);
 
 102         PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
 
 103         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
 
 104         PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
 
 105         PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
 
 106         PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
 
 108             public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
 
 109                 Object[] args = invocation.getArguments();
 
 110                 SvcLogicContext ctx =(SvcLogicContext)args[6];
 
 111                 String prefix = (String)args[4];
 
 112                 String key = (String)args[3];
 
 113                 if(key.contains("'28'")){
 
 114                     return  SvcLogicResource.QueryStatus.FAILURE ;
 
 115                 }else if ( key.contains("'8'")) {
 
 116                     return  SvcLogicResource.QueryStatus.NOT_FOUND ;
 
 118                     ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
 
 119                     ctx.setAttribute(prefix + ".orchestration-status", "Instantiated");
 
 121                 return  SvcLogicResource.QueryStatus.SUCCESS ;
 
 124         PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
 
 125         //PowerMockito.when(requestHandler.getVnfdata(anyString(), anyString(), (SvcLogicContext)anyObject())).thenReturn()
 
 129         aaiAdapter = Mockito.mock(AAIService.class);
 
 130         lifecyclemanager= Mockito.mock(LifecycleManager.class);
 
 131         workflowManager= Mockito.mock(WorkFlowManager.class);
 
 132         workingStateManager = Mockito.mock(WorkingStateManager.class);
 
 134         //  transactionRecorder= spy(TransactionRecorder.class);
 
 135         requestValidator = new RequestValidatorImpl();
 
 136 //        requestValidator = Mockito.mock(RequestValidator.class);
 
 137         requestValidator.setWorkflowManager(workflowManager);
 
 138         requestValidator.setLifecyclemanager(lifecyclemanager);
 
 139         requestValidator.setWorkingStateManager(workingStateManager);
 
 142        /* Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
 
 143         Mockito.when(aaiAdapter.requestGenericVnfData("1")).thenReturn(getGenericVnf("FIREWALL","INSTNATIATED"));*/
 
 144         // Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(true);
 
 146         /*PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 147         Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
 
 148         PowerMockito.when(getAaiadapter().requestGenericVnfData("8")).thenThrow(new AAIAdapterException("404"));
 
 149         Mockito.when(workingStateManager.isVNFStable("8")).thenReturn(true);
 
 150         PowerMockito.when(getAaiadapter().requestGenericVnfData("9")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 151         Mockito.when(workingStateManager.isVNFStable("9")).thenReturn(true);
 
 152         PowerMockito.when(getAaiadapter().requestGenericVnfData("10")).thenReturn(getGenericVnf("WrongRouter","INSTANTIATED"));
 
 153         Mockito.when(workingStateManager.isVNFStable("10")).thenReturn(true);
 
 154         PowerMockito.when(getAaiadapter().requestGenericVnfData("11")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 155         Mockito.when(workingStateManager.isVNFStable("11")).thenReturn(true);
 
 156         PowerMockito.when(getAaiadapter().requestGenericVnfData("12")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
 
 157         Mockito.when(workingStateManager.isVNFStable("12")).thenReturn(true);
 
 158         PowerMockito.when(getAaiadapter().requestGenericVnfData("13")).thenReturn(getGenericVnf("FIREWALL","TESTING"));
 
 159         Mockito.when(workingStateManager.isVNFStable("13")).thenReturn(true);
 
 160         PowerMockito.when(getAaiadapter().requestGenericVnfData("14")).thenReturn(getGenericVnf("FIREWALL","REBUILDING"));
 
 161         Mockito.when(workingStateManager.isVNFStable("14")).thenReturn(true);
 
 162         PowerMockito.when(getAaiadapter().requestGenericVnfData("26")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
 
 163         Mockito.when(workingStateManager.isVNFStable("26")).thenReturn(true);
 
 164         PowerMockito.when(getAaiadapter().requestGenericVnfData("27")).thenReturn(getGenericVnf("FIREWALL","RESTARTING"));
 
 165         Mockito.when(workingStateManager.isVNFStable("27")).thenReturn(true);
 
 166         PowerMockito.when(getAaiadapter().requestGenericVnfData("28")).thenThrow(new RuntimeException("AAI Down Excpetion"));
 
 167         Mockito.when(workingStateManager.isVNFStable("28")).thenReturn(true);
 
 168         PowerMockito.when(getAaiadapter().requestGenericVnfData("35")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 169         Mockito.when(workingStateManager.isVNFStable("35")).thenReturn(true);*/
 
 171         /*for(Integer i=130; i<=140 ; i++)
 
 173             PowerMockito.when(getAaiadapter().requestGenericVnfData(i.toString())).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 174             Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
 
 176         PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 177         Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
 
 178         PowerMockito.when(getAaiadapter().requestGenericVnfData("40")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 179         Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);
 
 182         PowerMockito.when(getAaiadapter().requestGenericVnfData("38")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 183         Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);
 
 186         PowerMockito.when(getAaiadapter().requestGenericVnfData("201")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","CONFIGURED"));
 
 187         Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
 
 188         PowerMockito.when(getAaiadapter().requestGenericVnfData("202")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","ERROR"));
 
 189         Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);
 
 191         PowerMockito.when(getAaiadapter().requestGenericVnfData("301")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 192         Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);
 
 194         PowerMockito.when(getAaiadapter().requestGenericVnfData("302")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 195         Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);
 
 197         PowerMockito.when(getAaiadapter().requestGenericVnfData("303")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 198         Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);
 
 200         PowerMockito.when(getAaiadapter().requestGenericVnfData("309")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 201         Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);
 
 203         PowerMockito.when(getAaiadapter().requestGenericVnfData("310")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
 
 204         Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);*/
 
 206     public AAIService getAaiadapter() {
 
 207         return this.aaiAdapter;
 
 209 /*    public GenericVnf getGenericVnf(String vnfType, String operationalState) {
 
 210         GenericVnf genericVnf = new GenericVnf();
 
 211         genericVnf.setVnfType(vnfType);
 
 212         // genericVnf.setOperationalState(operationalState);
 
 213         genericVnf.setOrchestrationStatus(operationalState);
 
 216     private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp){
 
 217         String API_VERSION= "2.0.0";
 
 218         RequestHandlerInput input = new RequestHandlerInput();
 
 219         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
 
 220         RequestContext requestContext = runtimeContext.getRequestContext();
 
 221         input.setRequestContext(requestContext);
 
 222         requestContext.getActionIdentifiers().setVnfId(vnfID);
 
 223         requestContext.setAction(action);
 
 224         if (action != null) {
 
 225             input.setRpcName(convertActionNameToUrl(action.name()));
 
 228             input.setRpcName(null);
 
 230         requestContext.getCommonHeader().setRequestId(requestId);
 
 231         requestContext.getCommonHeader().setSubRequestId(subRequestId);
 
 232         requestContext.getCommonHeader().setOriginatorId(originatorId);
 
 233         requestContext.getCommonHeader().getFlags().setTtl(ttl);
 
 234         requestContext.getCommonHeader().getFlags().setForce(force);
 
 235         requestContext.getCommonHeader().getTimeStamp();
 
 236         requestContext.getCommonHeader().setApiVer(API_VERSION);
 
 237         requestContext.getCommonHeader().setTimestamp(timeStamp);
 
 241     public void testNullVnfID() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 242         logger.debug("=====================testNullVnfID=============================");
 
 243         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 244         RequestHandlerInput input = this.getRequestHandlerInput(null, VNFOperation.Configure, 30,
 
 245                 false, UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 247         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 249             requestValidator.validateRequest(runtimeContext);
 
 250         }catch(InvalidInputException e ) {
 
 253 //        assertEquals(new InvalidInputException("vnfID or command is null") ,ex);
 
 255         logger.debug("=====================testNullVnfID=============================");
 
 259     public void testPositiveFlowWithConfigure() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 260         logger.debug("=====================testPositiveFlowWithConfigure=============================");
 
 261         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 262         Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
 
 263         RequestHandlerInput input = this.getRequestHandlerInput("1", VNFOperation.Configure, 30,
 
 264                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 266         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 268             requestValidator.validateRequest(runtimeContext);
 
 269         }catch(Exception e ) {
 
 273         logger.debug("testPositiveFlowWithConfigure");
 
 274         logger.debug("=====================testPositiveFlowWithConfigure=============================");
 
 278     public void testVnfNotFound() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 279         logger.debug("=====================testVnfNotFound=============================");
 
 280         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 281         RequestHandlerInput input = this.getRequestHandlerInput("8", VNFOperation.Configure, 30,
 
 282                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 284         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 286             requestValidator.validateRequest(runtimeContext);
 
 287         }catch(Exception e ) {
 
 291         logger.debug("=====================testVnfNotFound=============================");
 
 297     public void testNullCommand() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 298         logger.debug("=====================testNullCommand=============================");
 
 299         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 300         RequestHandlerInput input = this.getRequestHandlerInput("7", null,30,
 
 301                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 303        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 305            requestValidator.validateRequest(runtimeContext);
 
 306        }catch(InvalidInputException e ) {
 
 310         logger.debug("=====================testNullCommand=============================");
 
 314     public void testNullVnfIDAndCommand() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 315         logger.debug("=====================testNullVnfIDAndCommand=============================");
 
 316         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 317         RequestHandlerInput input = this.getRequestHandlerInput(null, null,30,
 
 318                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 320         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 322             requestValidator.validateRequest(runtimeContext);
 
 323         }catch(InvalidInputException e ) {
 
 327         logger.debug("=====================testNullVnfIDAndCommand=============================");
 
 331     public void testWorkflowNotFound() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 332         logger.debug("=====================testWorkflowNotFound=============================");
 
 333         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(false,false));
 
 334         RequestHandlerInput input = this.getRequestHandlerInput("10", VNFOperation.Configure, 30,
 
 335                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 337         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 339             requestValidator.validateRequest(runtimeContext);
 
 340         }catch(Exception e ) {
 
 344         logger.debug("=====================testWorkflowNotFound=============================");
 
 348     public void testUnstableVnfWithConfigure() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 349         logger.debug("=====================testUnstableVnfWithConfigure=============================");
 
 350         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 351         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
 
 353         RequestHandlerInput input = this.getRequestHandlerInput("11", VNFOperation.Configure, 30,
 
 354                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 356         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 358             requestValidator.validateRequest(runtimeContext);
 
 359         }catch(Exception e ) {
 
 363         logger.debug("=====================testUnstableVnfWithConfigure=============================");
 
 367     public void testUnstableVnfWithTest() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 368         logger.debug("=====================testUnstableVnfWithTest=============================");
 
 369         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 370         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
 
 371         RequestHandlerInput input = this.getRequestHandlerInput("12", VNFOperation.Test,30,
 
 372                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 374         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 376             requestValidator.validateRequest(runtimeContext);
 
 377         }catch(Exception e ) {
 
 381         logger.debug("=====================testUnstableVnfWithTest=============================");
 
 385     public void testUnstableVnfWithStart() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 386         logger.debug("=====================testUnstableVnfWithStart=============================");
 
 387         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
 
 389         RequestHandlerInput input = this.getRequestHandlerInput("13", VNFOperation.Start,30,
 
 390                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 392         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 394             requestValidator.validateRequest(runtimeContext);
 
 395         }catch(Exception e ) {
 
 399         logger.debug("=====================testUnstableVnfWithStart=============================");
 
 403     public void testUnstableVnfWithTerminate() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 404         logger.debug("=====================testUnstableVnfWithTerminate=============================");
 
 405         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
 
 406         RequestHandlerInput input = this.getRequestHandlerInput("14", VNFOperation.Terminate,30,
 
 407                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 409         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 411             requestValidator.validateRequest(runtimeContext);
 
 412         }catch(Exception e ) {
 
 416         logger.debug("=====================testUnstableVnfWithTerminate=============================");
 
 420     public void testUnstableVnfWithRestart() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 421         logger.debug("=====================testUnstableVnfWithRestart=============================");
 
 422         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
 
 424         RequestHandlerInput input = this.getRequestHandlerInput("26", VNFOperation.Restart,30,
 
 425                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 427         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 429             requestValidator.validateRequest(runtimeContext);
 
 430         }catch(Exception e ) {
 
 434         logger.debug("=====================testUnstableVnfWithRestart=============================");
 
 438     public void testUnstableVnfWithRebuild() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 439         logger.debug("=====================testUnstableVnfWithRebuild=============================");
 
 440         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
 
 442         // Mockito.doReturn(this.getGenericVnf("Firewall", "NOT_INSTANTIATED")).when(getAaiadapter()).requestGenericVnfData("8");
 
 443         RequestHandlerInput input = this.getRequestHandlerInput("27", VNFOperation.Rebuild,30,
 
 444                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 446         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 448             requestValidator.validateRequest(runtimeContext);
 
 449         }catch(Exception e ) {
 
 453         logger.debug("=====================testUnstableVnfWithRebuild=============================");
 
 460     public void testAAIDown() throws Exception {
 
 461         logger.debug("=====================testAAIDown=============================");
 
 462         //            AAIAdapter aaiAdapter = Mockito.mock(AAIAdapterImpl.class);
 
 463         //            RequestHandler requestHandler=RequestHandlerSingleton.getRequestHandler(new WorkFlowManagerImpl(),aaiAdapter,new LifecycleManagerImpl());
 
 464         //            RequestHandler requestHandler = new RequestHandlerImpl(new WorkFlowManagerImpl(),aaiAdapter,new LifecycleManagerImpl());
 
 465         RequestHandlerInput input = this.getRequestHandlerInput("28", VNFOperation.Configure, 30,
 
 466                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
 
 468         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 470             requestValidator.validateRequest(runtimeContext);
 
 472         }catch(Exception e ) {
 
 476         logger.debug("=====================testAAIDown=============================");
 
 480     public void testNegativeFlowWithTimeStamp() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 481         logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
 
 482         Date now = new Date();
 
 483         Date past = new Date();
 
 484         past.setTime(now.getTime() -1000000 );
 
 485         RequestHandlerInput input = this.getRequestHandlerInput("35", VNFOperation.Configure, 30,
 
 486                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),past);
 
 488         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 491             requestValidator.validateRequest(runtimeContext);
 
 492         }catch(Exception e ) {
 
 496         logger.debug("testNegativeFlowWithTimeStamp");
 
 497         logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
 
 502     public void rejectDuplicateRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
 
 503         String originatorID = UUID.randomUUID().toString();
 
 504         String requestID = UUID.randomUUID().toString();
 
 505         String subRequestID = UUID.randomUUID().toString();
 
 507         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
 
 508         Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true);
 
 509         Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true);
 
 510         RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
 
 512         RequestHandlerInput input1 = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
 
 514         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 515         RuntimeContext runtimeContext1 = putInputToRuntimeContext(input1);
 
 518             requestValidator.validateRequest(runtimeContext);
 
 519         }catch(Exception e ) {
 
 525             requestValidator.validateRequest(runtimeContext1);
 
 526         }catch(Exception e ) {
 
 533     public void testLockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
 
 534         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
 
 535         testOperation("no-matter", VNFOperation.Lock);
 
 539     public void testUnlockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
 
 540         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
 
 541         testOperation("no-matter", VNFOperation.Unlock);
 
 545     public void testCheckLockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
 
 546         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
 
 547         testOperation("no-matter", VNFOperation.CheckLock);
 
 550     @Test(expected = NoTransitionDefinedException.class)
 
 551     public void testLockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
 
 552         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Lock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
 
 553         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
 
 554         testOperation("no-matter", VNFOperation.Lock);
 
 557     @Test(expected = NoTransitionDefinedException.class)
 
 558     public void testUnlockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
 
 559         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Unlock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
 
 560         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
 
 561         testOperation("no-matter", VNFOperation.Unlock);
 
 564     @Test(expected = NoTransitionDefinedException.class)
 
 565     public void testCheckLockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
 
 566         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.CheckLock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
 
 567         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
 
 568         testOperation("no-matter", VNFOperation.CheckLock);
 
 571     private void testOperation(String resource, VNFOperation operation) throws WorkflowNotFoundException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, InvalidInputException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, RequestExpiredException {
 
 572         String originatorID = UUID.randomUUID().toString();
 
 573         String requestID = UUID.randomUUID().toString();
 
 574         String subRequestID = UUID.randomUUID().toString();
 
 576         RequestHandlerInput input = this.getRequestHandlerInput(resource, operation, 0, false, originatorID, requestID, subRequestID, new Date());
 
 577         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
 
 578         requestValidator.validateRequest(runtimeContext);
 
 582     private RuntimeContext createRuntimeContextWithSubObjects() {
 
 583         RuntimeContext runtimeContext = new RuntimeContext();
 
 584         RequestContext requestContext = new RequestContext();
 
 585         runtimeContext.setRequestContext(requestContext);
 
 586         ResponseContext responseContext = createResponseContextWithSuObjects();
 
 587         runtimeContext.setResponseContext(responseContext);
 
 588         CommonHeader commonHeader = new CommonHeader();
 
 589         requestContext.setCommonHeader(commonHeader);
 
 590         Flags flags = new Flags();
 
 591         commonHeader.setFlags(flags);
 
 592         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
 
 593         requestContext.setActionIdentifiers(actionIdentifiers);
 
 594         VNFContext vnfContext = new VNFContext();
 
 595         runtimeContext.setVnfContext(vnfContext);
 
 596         return runtimeContext;
 
 600     private ResponseContext createResponseContextWithSuObjects(){
 
 601         ResponseContext responseContext = new ResponseContext();
 
 602         CommonHeader commonHeader = new CommonHeader();
 
 603         Flags flags = new Flags();
 
 604         Status status = new Status();
 
 605         responseContext.setCommonHeader(commonHeader);
 
 606         responseContext.setStatus(status);
 
 607         commonHeader.setFlags(flags);
 
 608         return responseContext;
 
 611     private String convertActionNameToUrl(String action) {
 
 612         String regex = "([a-z])([A-Z]+)";
 
 613         String replacement = "$1-$2";
 
 614         return action.replaceAll(regex, replacement)
 
 618     private RuntimeContext putInputToRuntimeContext(RequestHandlerInput input) {
 
 619         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
 
 620         runtimeContext.setRequestContext(input.getRequestContext());
 
 621         runtimeContext.setRpcName(input.getRpcName());
 
 622         runtimeContext.getVnfContext().setId(input.getRequestContext().getActionIdentifiers().getVnfId());
 
 623         //runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(input.getRequestContext().getActionIdentifiers().getVnfId());
 
 624         return runtimeContext;
 
 626         //String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp