aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-dispatcher/appc-request-handler/appc-request-handler-core/src/test/java/org/openecomp/appc/requesthandler/TestRequestValidator.java
blob: 6c287a580d25245ab4671910df077c7754db8503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.requesthandler;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.openecomp.appc.domainmodel.lcm.*;
import org.openecomp.appc.executor.UnstableVNFException;
import org.openecomp.appc.lifecyclemanager.LifecycleManager;
import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
import org.openecomp.appc.requesthandler.exceptions.*;
import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
import org.openecomp.appc.transactionrecorder.TransactionRecorder;
import org.openecomp.appc.workflow.WorkFlowManager;
import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
import org.openecomp.appc.workflow.objects.WorkflowRequest;
import org.openecomp.appc.workingstatemanager.WorkingStateManager;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.openecomp.sdnc.sli.SvcLogicContext;
import org.openecomp.sdnc.sli.SvcLogicResource;
import org.openecomp.sdnc.sli.aai.AAIService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.*;


@RunWith(PowerMockRunner.class)
@PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class})
public class TestRequestValidator {

    private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);

    private static final String TTL_FLAG= "TTL";

    private RequestValidatorImpl requestValidator;

    AAIService aaiAdapter ;
    LifecycleManager lifecyclemanager;
    WorkFlowManager workflowManager;
    WorkingStateManager workingStateManager ;
//    AppcDAOImpl dao ;

    private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
    private final Bundle bundleService=Mockito.mock(Bundle.class);
    private final ServiceReference sref=Mockito.mock(ServiceReference.class);



    @Before
    public void init() throws Exception {

//        dao = Mockito.mock(AppcDAOImpl.class);
//        PowerMockito.whenNew(AppcDAOImpl.class).withNoArguments().thenReturn(dao);
//        Mockito.doNothing().when(dao).storeTransactionRecord((TransactionRecord)anyObject());
        //   PowerMockito.when(dao.queryWorkflow(anyString(),anyString())).thenReturn(true);

        // ***
        AAIService aaiService = Mockito.mock(AAIService.class);;
        PowerMockito.mockStatic(FrameworkUtil.class);
        PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
        PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
        PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
        PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
        PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
            @Override
            public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                SvcLogicContext ctx =(SvcLogicContext)args[6];
                String prefix = (String)args[4];
                String key = (String)args[3];
                if(key.contains("'28'")){
                    return  SvcLogicResource.QueryStatus.FAILURE ;
                }else if ( key.contains("'8'")) {
                    return  SvcLogicResource.QueryStatus.NOT_FOUND ;
                }else {
                    ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
                    ctx.setAttribute(prefix + ".orchestration-status", "Instantiated");
                }
                return  SvcLogicResource.QueryStatus.SUCCESS ;
            }
        });
        PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
        //PowerMockito.when(requestHandler.getVnfdata(anyString(), anyString(), (SvcLogicContext)anyObject())).thenReturn()
        //  ***


        aaiAdapter = Mockito.mock(AAIService.class);
        lifecyclemanager= Mockito.mock(LifecycleManager.class);
        workflowManager= Mockito.mock(WorkFlowManager.class);
        workingStateManager = Mockito.mock(WorkingStateManager.class);

        //  transactionRecorder= spy(TransactionRecorder.class);
        requestValidator = new RequestValidatorImpl();
//        requestValidator = Mockito.mock(RequestValidator.class);
        requestValidator.setWorkflowManager(workflowManager);
        requestValidator.setLifecyclemanager(lifecyclemanager);
        requestValidator.setWorkingStateManager(workingStateManager);


       /* Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
        Mockito.when(aaiAdapter.requestGenericVnfData("1")).thenReturn(getGenericVnf("FIREWALL","INSTNATIATED"));*/
        // Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(true);

        /*PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("8")).thenThrow(new AAIAdapterException("404"));
        Mockito.when(workingStateManager.isVNFStable("8")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("9")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("9")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("10")).thenReturn(getGenericVnf("WrongRouter","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("10")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("11")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("11")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("12")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("12")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("13")).thenReturn(getGenericVnf("FIREWALL","TESTING"));
        Mockito.when(workingStateManager.isVNFStable("13")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("14")).thenReturn(getGenericVnf("FIREWALL","REBUILDING"));
        Mockito.when(workingStateManager.isVNFStable("14")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("26")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("26")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("27")).thenReturn(getGenericVnf("FIREWALL","RESTARTING"));
        Mockito.when(workingStateManager.isVNFStable("27")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("28")).thenThrow(new RuntimeException("AAI Down Excpetion"));
        Mockito.when(workingStateManager.isVNFStable("28")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("35")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("35")).thenReturn(true);*/

        /*for(Integer i=130; i<=140 ; i++)
        {
            PowerMockito.when(getAaiadapter().requestGenericVnfData(i.toString())).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
            Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
        }
        PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("40")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);


        PowerMockito.when(getAaiadapter().requestGenericVnfData("38")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);


        PowerMockito.when(getAaiadapter().requestGenericVnfData("201")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","CONFIGURED"));
        Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
        PowerMockito.when(getAaiadapter().requestGenericVnfData("202")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","ERROR"));
        Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);

        PowerMockito.when(getAaiadapter().requestGenericVnfData("301")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);

        PowerMockito.when(getAaiadapter().requestGenericVnfData("302")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);

        PowerMockito.when(getAaiadapter().requestGenericVnfData("303")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);

        PowerMockito.when(getAaiadapter().requestGenericVnfData("309")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);

        PowerMockito.when(getAaiadapter().requestGenericVnfData("310")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
        Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);*/
    }
    public AAIService getAaiadapter() {
        return this.aaiAdapter;
    }
/*    public GenericVnf getGenericVnf(String vnfType, String operationalState) {
        GenericVnf genericVnf = new GenericVnf();
        genericVnf.setVnfType(vnfType);
        // genericVnf.setOperationalState(operationalState);
        genericVnf.setOrchestrationStatus(operationalState);
        return genericVnf;
    }*/
    private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp){
        String API_VERSION= "2.0.0";
        RequestHandlerInput input = new RequestHandlerInput();
        RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
        RequestContext requestContext = runtimeContext.getRequestContext();
        input.setRequestContext(requestContext);
        requestContext.getActionIdentifiers().setVnfId(vnfID);
        requestContext.setAction(action);
        if (action != null) {
            input.setRpcName(convertActionNameToUrl(action.name()));
        }
        else{
            input.setRpcName(null);
        }
        requestContext.getCommonHeader().setRequestId(requestId);
        requestContext.getCommonHeader().setSubRequestId(subRequestId);
        requestContext.getCommonHeader().setOriginatorId(originatorId);
        requestContext.getCommonHeader().getFlags().setTtl(ttl);
        requestContext.getCommonHeader().getFlags().setForce(force);
        requestContext.getCommonHeader().getTimeStamp();
        requestContext.getCommonHeader().setApiVer(API_VERSION);
        requestContext.getCommonHeader().setTimestamp(timeStamp);
        return input;
    }
    @Test
    public void testNullVnfID() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testNullVnfID=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        RequestHandlerInput input = this.getRequestHandlerInput(null, VNFOperation.Configure, 30,
                false, UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(InvalidInputException e ) {
            ex = e;
        }
//        assertEquals(new InvalidInputException("vnfID or command is null") ,ex);
        assertNotNull(ex);
        logger.debug("=====================testNullVnfID=============================");
    }

    @Test
    public void testPositiveFlowWithConfigure() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testPositiveFlowWithConfigure=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
        RequestHandlerInput input = this.getRequestHandlerInput("1", VNFOperation.Configure, 30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNull(ex);
        logger.debug("testPositiveFlowWithConfigure");
        logger.debug("=====================testPositiveFlowWithConfigure=============================");
    }

    @Test
    public void testVnfNotFound() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testVnfNotFound=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        RequestHandlerInput input = this.getRequestHandlerInput("8", VNFOperation.Configure, 30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testVnfNotFound=============================");
    }



   @Test
    public void testNullCommand() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testNullCommand=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        RequestHandlerInput input = this.getRequestHandlerInput("7", null,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
       Exception ex =null;
       RuntimeContext runtimeContext = putInputToRuntimeContext(input);
       try {
           requestValidator.validateRequest(runtimeContext);
       }catch(InvalidInputException e ) {
           ex = e;
       }
       assertNotNull(ex);
        logger.debug("=====================testNullCommand=============================");
    }

    @Test
    public void testNullVnfIDAndCommand() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testNullVnfIDAndCommand=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        RequestHandlerInput input = this.getRequestHandlerInput(null, null,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(InvalidInputException e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testNullVnfIDAndCommand=============================");
    }

    @Test
    public void testWorkflowNotFound() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testWorkflowNotFound=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(false,false));
        RequestHandlerInput input = this.getRequestHandlerInput("10", VNFOperation.Configure, 30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testWorkflowNotFound=============================");
    }

    @Test
    public void testUnstableVnfWithConfigure() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testUnstableVnfWithConfigure=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));

        RequestHandlerInput input = this.getRequestHandlerInput("11", VNFOperation.Configure, 30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testUnstableVnfWithConfigure=============================");
    }

    @Test
    public void testUnstableVnfWithTest() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testUnstableVnfWithTest=============================");
        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
        RequestHandlerInput input = this.getRequestHandlerInput("12", VNFOperation.Test,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testUnstableVnfWithTest=============================");
    }

    @Test
    public void testUnstableVnfWithStart() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testUnstableVnfWithStart=============================");
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));

        RequestHandlerInput input = this.getRequestHandlerInput("13", VNFOperation.Start,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testUnstableVnfWithStart=============================");
    }

    @Test
    public void testUnstableVnfWithTerminate() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testUnstableVnfWithTerminate=============================");
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
        RequestHandlerInput input = this.getRequestHandlerInput("14", VNFOperation.Terminate,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testUnstableVnfWithTerminate=============================");
    }

    @Test
    public void testUnstableVnfWithRestart() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testUnstableVnfWithRestart=============================");
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));

        RequestHandlerInput input = this.getRequestHandlerInput("26", VNFOperation.Restart,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testUnstableVnfWithRestart=============================");
    }

    @Test
    public void testUnstableVnfWithRebuild() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testUnstableVnfWithRebuild=============================");
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));

        // Mockito.doReturn(this.getGenericVnf("Firewall", "NOT_INSTANTIATED")).when(getAaiadapter()).requestGenericVnfData("8");
        RequestHandlerInput input = this.getRequestHandlerInput("27", VNFOperation.Rebuild,30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testUnstableVnfWithRebuild=============================");
    }




    @Test
    public void testAAIDown() throws Exception {
        logger.debug("=====================testAAIDown=============================");
        //            AAIAdapter aaiAdapter = Mockito.mock(AAIAdapterImpl.class);
        //            RequestHandler requestHandler=RequestHandlerSingleton.getRequestHandler(new WorkFlowManagerImpl(),aaiAdapter,new LifecycleManagerImpl());
        //            RequestHandler requestHandler = new RequestHandlerImpl(new WorkFlowManagerImpl(),aaiAdapter,new LifecycleManagerImpl());
        RequestHandlerInput input = this.getRequestHandlerInput("28", VNFOperation.Configure, 30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),new Date());
		Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        try {
            requestValidator.validateRequest(runtimeContext);

        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("=====================testAAIDown=============================");
    }

    @Test
    public void testNegativeFlowWithTimeStamp() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
        Date now = new Date();
        Date past = new Date();
        past.setTime(now.getTime() -1000000 );
        RequestHandlerInput input = this.getRequestHandlerInput("35", VNFOperation.Configure, 30,
                false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),past);
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
       
        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
        logger.debug("testNegativeFlowWithTimeStamp");
        logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
    }


    @Test
    public void rejectDuplicateRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
        String originatorID = UUID.randomUUID().toString();
        String requestID = UUID.randomUUID().toString();
        String subRequestID = UUID.randomUUID().toString();

        Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
        Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true);
        Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true);
        RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());

        RequestHandlerInput input1 = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,new Date());
        Exception ex =null;
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        RuntimeContext runtimeContext1 = putInputToRuntimeContext(input1);

        try {
            requestValidator.validateRequest(runtimeContext);
        }catch(Exception e ) {
            ex = e;
        }
        assertNull(ex);

        try {
            requestValidator.validateRequest(runtimeContext1);
        }catch(Exception e ) {
            ex = e;
        }
        assertNotNull(ex);
    }

    @Test
    public void testLockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
        Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
        testOperation("no-matter", VNFOperation.Lock);
    }

    @Test
    public void testUnlockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
        Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
        testOperation("no-matter", VNFOperation.Unlock);
    }

    @Test
    public void testCheckLockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
        Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
        testOperation("no-matter", VNFOperation.CheckLock);
    }

    @Test(expected = NoTransitionDefinedException.class)
    public void testLockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Lock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
        Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
        testOperation("no-matter", VNFOperation.Lock);
    }

    @Test(expected = NoTransitionDefinedException.class)
    public void testUnlockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Unlock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
        Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
        testOperation("no-matter", VNFOperation.Unlock);
    }

    @Test(expected = NoTransitionDefinedException.class)
    public void testCheckLockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
        Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.CheckLock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
        Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
        testOperation("no-matter", VNFOperation.CheckLock);
    }

    private void testOperation(String resource, VNFOperation operation) throws WorkflowNotFoundException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, InvalidInputException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, RequestExpiredException {
        String originatorID = UUID.randomUUID().toString();
        String requestID = UUID.randomUUID().toString();
        String subRequestID = UUID.randomUUID().toString();

        RequestHandlerInput input = this.getRequestHandlerInput(resource, operation, 0, false, originatorID, requestID, subRequestID, new Date());
        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
        requestValidator.validateRequest(runtimeContext);
    }


    private RuntimeContext createRuntimeContextWithSubObjects() {
        RuntimeContext runtimeContext = new RuntimeContext();
        RequestContext requestContext = new RequestContext();
        runtimeContext.setRequestContext(requestContext);
        ResponseContext responseContext = createResponseContextWithSuObjects();
        runtimeContext.setResponseContext(responseContext);
        CommonHeader commonHeader = new CommonHeader();
        requestContext.setCommonHeader(commonHeader);
        Flags flags = new Flags();
        commonHeader.setFlags(flags);
        ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
        requestContext.setActionIdentifiers(actionIdentifiers);
        VNFContext vnfContext = new VNFContext();
        runtimeContext.setVnfContext(vnfContext);
        return runtimeContext;

    }

    private ResponseContext createResponseContextWithSuObjects(){
        ResponseContext responseContext = new ResponseContext();
        CommonHeader commonHeader = new CommonHeader();
        Flags flags = new Flags();
        Status status = new Status();
        responseContext.setCommonHeader(commonHeader);
        responseContext.setStatus(status);
        commonHeader.setFlags(flags);
        return responseContext;
    }

    private String convertActionNameToUrl(String action) {
        String regex = "([a-z])([A-Z]+)";
        String replacement = "$1-$2";
        return action.replaceAll(regex, replacement)
                .toLowerCase();
    }

    private RuntimeContext putInputToRuntimeContext(RequestHandlerInput input) {
        RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
        runtimeContext.setRequestContext(input.getRequestContext());
        runtimeContext.setRpcName(input.getRpcName());
        runtimeContext.getVnfContext().setId(input.getRequestContext().getActionIdentifiers().getVnfId());
        //runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(input.getRequestContext().getActionIdentifiers().getVnfId());
        return runtimeContext;

        //String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp
    }
}