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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* 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.
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
package org.onap.appc.requesthandler;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
import java.time.Instant;
import java.util.HashMap;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.onap.appc.adapter.factory.DmaapMessageAdapterFactoryImpl;
import org.onap.appc.adapter.message.MessageAdapterFactory;
import org.onap.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import org.onap.appc.domainmodel.lcm.*;
import org.onap.appc.domainmodel.lcm.Flags.Mode;
import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
import org.onap.appc.domainmodel.lcm.CommonHeader;
import org.onap.appc.domainmodel.lcm.Flags;
import org.onap.appc.domainmodel.lcm.RequestContext;
import org.onap.appc.domainmodel.lcm.ResponseContext;
import org.onap.appc.domainmodel.lcm.RuntimeContext;
import org.onap.appc.domainmodel.lcm.Status;
import org.onap.appc.domainmodel.lcm.VNFContext;
import org.onap.appc.domainmodel.lcm.VNFOperation;
import org.onap.appc.executor.CommandExecutor;
import org.onap.appc.executor.UnstableVNFException;
import org.onap.appc.executor.objects.LCMCommandStatus;
import org.onap.appc.lifecyclemanager.LifecycleManager;
import org.onap.appc.lifecyclemanager.objects.LifecycleException;
import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException;
import org.onap.appc.lockmanager.api.LockException;
import org.onap.appc.lockmanager.api.LockManager;
import org.onap.appc.messageadapter.MessageAdapter;
import org.onap.appc.messageadapter.impl.MessageAdapterImpl;
import org.onap.appc.requesthandler.exceptions.*;
import org.onap.appc.requesthandler.exceptions.DGWorkflowNotFoundException;
import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
import org.onap.appc.requesthandler.exceptions.InvalidInputException;
import org.onap.appc.requesthandler.exceptions.RequestExpiredException;
import org.onap.appc.requesthandler.exceptions.VNFNotFoundException;
import org.onap.appc.requesthandler.exceptions.WorkflowNotFoundException;
import org.onap.appc.requesthandler.impl.RequestHandlerImpl;
import org.onap.appc.requesthandler.impl.RequestValidatorImpl;
import org.onap.appc.requesthandler.objects.RequestHandlerInput;
import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
import org.onap.appc.transactionrecorder.TransactionRecorder;
import org.onap.appc.transactionrecorder.objects.TransactionRecord;
import org.onap.appc.workflow.WorkFlowManager;
import org.onap.appc.workflow.objects.WorkflowExistsOutput;
import org.onap.appc.workflow.objects.WorkflowRequest;
import org.onap.appc.workingstatemanager.WorkingStateManager;
import org.onap.appc.workingstatemanager.objects.VNFWorkingState;
import org.onap.ccsdk.sli.adaptors.aai.AAIService;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
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 com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
@Ignore
@RunWith(PowerMockRunner.class)
@PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class, MessageAdapterImpl.class})
public class TestRequestHandler {
private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
private RequestHandlerImpl requestHandler;
private RequestValidatorImpl requestValidator;
private WorkFlowManager workflowManager;
private WorkingStateManager workingStateManager ;
private LockManager lockManager;
private Configuration configuration;
private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
private final Bundle bundleService=Mockito.mock(Bundle.class);
private final ServiceReference sref=Mockito.mock(ServiceReference.class);
MessageAdapterFactory factory = new DmaapMessageAdapterFactoryImpl();
@Before
public void init() throws Exception {
configuration = ConfigurationFactory.getConfiguration();
configuration.setProperty("appc.LCM.topic.write" , "TEST");
configuration.setProperty("appc.LCM.client.key" , "TEST");
configuration.setProperty("appc.LCM.client.secret" , "TEST");
PowerMockito.mockStatic(FrameworkUtil.class);
PowerMockito.when(FrameworkUtil.getBundle(MessageAdapterImpl.class)).thenReturn(bundleService);
PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
PowerMockito.when(bundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(sref);
PowerMockito.when(bundleContext.getService(sref)).thenReturn(factory);
requestHandler = new RequestHandlerImpl();
LifecycleManager lifecyclemanager= mock(LifecycleManager.class);
workflowManager= mock(WorkFlowManager.class);
CommandExecutor commandExecutor= mock(CommandExecutor.class);
MessageAdapter messageAdapter = mock(MessageAdapter.class);
workingStateManager = mock(WorkingStateManager.class);
lockManager = mock(LockManager.class);
TransactionRecorder transactionRecorder= mock(TransactionRecorder.class);
requestHandler.setWorkingStateManager(workingStateManager);
requestHandler.setMessageAdapter(messageAdapter);
requestValidator = mock(RequestValidatorImpl.class);
requestValidator.setLifecyclemanager(lifecyclemanager);
requestValidator.setWorkingStateManager(workingStateManager);
requestValidator.setWorkflowManager(workflowManager);
requestValidator.setLifecyclemanager(lifecyclemanager);
requestHandler.setCommandExecutor(commandExecutor);
requestHandler.setRequestValidator(requestValidator);
requestHandler.setLockManager(lockManager);
requestHandler.setTransactionRecorder(transactionRecorder);
doNothing().when(transactionRecorder).store((TransactionRecord) anyObject());
// Mockito.when(commandExecutor.executeCommand((CommandExecutorInput)anyObject())).thenReturn(true);
Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
for(Integer i=130; i<=140 ; i++)
{
Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
}
Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);
Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);
Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);
Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);
Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);
Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);
Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);
Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);
}
private void threadSleep(){
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testNegativeFlowWithRequestingUsedVnfId() throws Exception {
logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input1 = this.getRequestHandlerInput("131", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
mockRuntimeContextAndVnfContext(input1);
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
threadSleep ();
Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
logger.debug("testNegativeFlowWithRequestingUsedVnfId");
logger.debug("=====================testNegativeFlowWithRequestingUsedVnfId=============================");
}
@Test
public void testInvalidVNFExceptionRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void testLifecycleException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new LifecycleException(new Exception(),"Configured","test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.INVALID_VNF_STATE.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void testRequestExpiredException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
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));
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new RequestExpiredException("")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.EXPIRED_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void testMissingVNFdata() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID,Instant.now());
PowerMockito.doThrow(new MissingVNFDataInAAIException("vnf-type")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.MISSING_VNF_DATA_IN_AAI.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void testWorkflowNotFoundException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new WorkflowNotFoundException("Unable to find the DG","VNF-2.0.0.0", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());}
@Test
public void testDGWorkflowNotFoundException() throws Exception {
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));
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure, 0, false, originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new DGWorkflowNotFoundException("Unable to find the DG", "VNF-2.0.0.0", "temp", "Test")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.DG_WORKFLOW_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void testInvalidInputException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID1 = UUID.randomUUID().toString();
String requestID1 = UUID.randomUUID().toString();
String subRequestID1 = UUID.randomUUID().toString();
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input1 = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID1, requestID1, subRequestID1, Instant.now());
PowerMockito.doThrow(new InvalidInputException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode(), output1.getResponseContext().getStatus().getCode());
}
@Test
public void testNoTransitionDefinedException() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("3010", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new NoTransitionDefinedException("Invalid VNF State","Unstable","Test event")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.NO_TRANSITION_DEFINE.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void rejectInvalidRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("3009", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new VNFNotFoundException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.VNF_NOT_FOUND.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void testUnstableWorkingState() throws Exception {
logger.debug("=====================testUnstableWorkingState=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
Mockito.when(workingStateManager.isVNFStable("37")).thenReturn(true,false);
RequestHandlerInput input = this.getRequestHandlerInput("37", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
mockRuntimeContextAndVnfContext(input);
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
RequestHandlerInput input1 = this.getRequestHandlerInput("37", VNFOperation.Configure,1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
mockRuntimeContextAndVnfContext(input1);
RequestHandlerOutput output1 = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(), output1.getResponseContext().getStatus().getCode());
logger.debug("=====================testUnstableWorkingState=============================");
}
@Test
public void testOnRequestExecutionEndSuccessForWorkingState() throws Exception {
logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
mockRuntimeContextAndVnfContext(input1);
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
threadSleep();
requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"137", "", "", ""),true);
input1 = this.getRequestHandlerInput("137", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
mockRuntimeContextAndVnfContext(input1);
output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
logger.debug("=====================testOnRequestExecutionEndSuccessForWorkingState=============================");
}
private void mockRuntimeContextAndVnfContext(RequestHandlerInput input1) throws Exception {
RuntimeContext runtimeContext = PowerMockito.mock(RuntimeContext.class);
VNFContext vnfContext = new VNFContext();
vnfContext.setType("SCP");
vnfContext.setId("137");
when(runtimeContext.getVnfContext()).thenReturn(vnfContext);
when(runtimeContext.getRequestContext()).thenReturn(input1.getRequestContext());
when(runtimeContext.getRpcName()).thenReturn(input1.getRpcName());
ResponseContext responseContext = new ResponseContext();
responseContext.setStatus(new Status(0, null));
responseContext.setAdditionalContext(new HashMap<String, String>(4));
responseContext.setCommonHeader(input1.getRequestContext().getCommonHeader());
runtimeContext.setResponseContext(responseContext);
when(runtimeContext.getResponseContext()).thenReturn(responseContext);
responseContext.setStatus(new Status(0, null));
runtimeContext.setResponseContext(responseContext);
PowerMockito.whenNew(RuntimeContext.class).withAnyArguments().thenReturn(runtimeContext);
}
@Test
public void testOnRequestExecutionEndFailureForWorkingState() throws Exception {
logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerInput input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
mockRuntimeContextAndVnfContext(input1);
RequestHandlerOutput output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
threadSleep();
requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.NO_TRANSITION_DEFINE,"38", "", "", ""),true);
input1 = this.getRequestHandlerInput("38", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
PowerMockito.doThrow(new UnstableVNFException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
mockRuntimeContextAndVnfContext(input1);
output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
}
@Test
public void testOnRequestExecutionEndTTLExpiredForWorkingState() throws Exception {
logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
mockRuntimeContextAndVnfContext(input1);
RequestHandlerOutput output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
threadSleep();
input1 = this.getRequestHandlerInput("39", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
PowerMockito.doThrow(new LockException(" ")).when(lockManager).acquireLock(Matchers.anyString(), Matchers.anyString(), Matchers.anyByte());
output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.LOCKING_FAILURE.getResponseCode(),output.getResponseContext().getStatus().getCode());
logger.debug("=====================testOnRequestExecutionEndFailureForWorkingState=============================");
}
@Test
public void testOnRequestTTLEndForWorkingState() throws Exception {
logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerInput input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
mockRuntimeContextAndVnfContext(input1);
RequestHandlerOutput output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
threadSleep();
RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.EXPIRED_REQUEST_FAILURE,"40", "", "", "");
requestHandler.onRequestTTLEnd(response,true);
input1 = this.getRequestHandlerInput("40", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
PowerMockito.doThrow(new UnstableVNFException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.UNSTABLE_VNF.getResponseCode(),output.getResponseContext().getStatus().getCode());
logger.debug("=====================testOnRequestTTLEndForWorkingState=============================");
}
@Test
public void testForceCommandExecution() throws Exception {
logger.debug("=====================testForceCommandExecution=============================");
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
mockRuntimeContextAndVnfContext(input1);
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
RequestHandlerOutput output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
threadSleep();
RuntimeContext response = this.getAsyncResponse(false,LCMCommandStatus.ACCEPTED,"138", "", "", "");
requestHandler.onRequestTTLEnd(response,true);
input1 = this.getRequestHandlerInput("138", VNFOperation.Configure, 1200,
false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
input1.getRequestContext().getCommonHeader().setFlags(new Flags(null, true, 1200));
mockRuntimeContextAndVnfContext(input1);
output = requestHandler.handleRequest(input1);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(),output.getResponseContext().getStatus().getCode());
logger.debug("=====================testForceCommandExecution=============================");
}
@Test
public void testOnRequestExecutionEndSuccess() throws VNFNotFoundException {
logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Starts =============================");
Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
requestHandler.onRequestExecutionEnd(this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"201", "", "", ""),true);
logger.debug("=====================Positive TEST - On Request Execution End SUCCESS- Ends =============================");
}
@Test
public void testOnRequestExecutionEndFailure() throws VNFNotFoundException {
logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Starts =============================");
Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
requestHandler.onRequestExecutionEnd(this.getAsyncResponse(false,LCMCommandStatus.DG_FAILURE,"202", "", "", ""),true);
logger.debug("=====================Positive TEST - On Request Execution End FAILURE- Ends =============================");
}
private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force,String originatorId, String requestId, String subRequestId, Instant 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);
input.setRpcName(convertActionNameToUrl(action.name()));
requestContext.getCommonHeader().setRequestId(requestId);
requestContext.getCommonHeader().setSubRequestId(subRequestId);
requestContext.getCommonHeader().setOriginatorId(originatorId);
requestContext.getCommonHeader().setFlags(new Flags(null, force, ttl));
requestContext.getCommonHeader().setTimestamp(timeStamp);
requestContext.getCommonHeader().setApiVer(API_VERSION);
return input;
}
private RuntimeContext getAsyncResponse(boolean wfStatus, LCMCommandStatus commandStatus, String vnfId, String originatorId, String requestId, String subRequestId)
{
RuntimeContext output = createRuntimeContextWithSubObjects();
output.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
output.getVnfContext().setId(vnfId);
output.getResponseContext().getCommonHeader().setApiVer("2.0.0");
output.getResponseContext().getCommonHeader().setTimestamp( Instant.now());
output.getResponseContext().setStatus(LCMCommandStatus.SUCCESS.toStatus(null));
output.setTimeStart( Instant.now());
output.getResponseContext().getCommonHeader().setOriginatorId(originatorId);
output.getResponseContext().getCommonHeader().setRequestId(requestId);
output.getResponseContext().getCommonHeader().setSubRequestId(subRequestId);
output.getVnfContext().setType("FIREWALL");
output.getRequestContext().setAction(VNFOperation.Configure);
output.setRpcName("configure");
output.getResponseContext().setPayload("");
return output;
}
@Test
public void rejectDuplicateRequest() throws Exception {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
mockRuntimeContextAndVnfContext(input);
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
input = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
PowerMockito.doThrow(new DuplicateRequestException(" ")).when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.DUPLICATE_REQUEST.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void removeRequestFromRegistryOnRequestCompletion() throws Exception {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("302", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
mockRuntimeContextAndVnfContext(input);
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.SUCCESS,"302",originatorID,requestID,subRequestID);
requestHandler.onRequestExecutionEnd(asyncResponse,true);
input = this.getRequestHandlerInput("310", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
mockRuntimeContextAndVnfContext(input);
output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
@Test
public void removeRequestFromRegistryOnTTLExpiration() throws Exception {
String originatorID = UUID.randomUUID().toString();
String requestID = UUID.randomUUID().toString();
String subRequestID = UUID.randomUUID().toString();
PowerMockito.doNothing().when(requestValidator).validateRequest(Matchers.any(RuntimeContext.class));
Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
RequestHandlerInput input = this.getRequestHandlerInput("303", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
mockRuntimeContextAndVnfContext(input);
RequestHandlerOutput output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
RuntimeContext asyncResponse = this.getAsyncResponse(true,LCMCommandStatus.ACCEPTED,"303",originatorID,requestID,subRequestID);
requestHandler.onRequestTTLEnd(asyncResponse,false);
output = requestHandler.handleRequest(input);
Assert.assertEquals(LCMCommandStatus.ACCEPTED.getResponseCode(), output.getResponseContext().getStatus().getCode());
}
/*@Test
public void getMetricserviceTest() throws Exception{
Method method = RequestHandlerImpl.class.getDeclaredMethod("getMetricservice", null);
method.setAccessible(true);
method.invoke(null, null);
}*/
@Test
public void onRequestExecutionStartTest() throws Exception{
Mockito.doReturn(true).when(workingStateManager).setWorkingState(anyString(),(VNFWorkingState) anyObject(), anyString(),anyBoolean());
requestHandler.onRequestExecutionStart("303",false, null, true);
Assert.assertNotNull(requestHandler);
}
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);
commonHeader.setFlags(new Flags(null, false, 0));
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();
responseContext.setCommonHeader(commonHeader);
responseContext.setStatus(new Status(0, null));
commonHeader.setFlags(new Flags(null, false, 0));
return responseContext;
}
private String convertActionNameToUrl(String action) {
String regex = "([a-z])([A-Z]+)";
String replacement = "$1-$2";
return action.replaceAll(regex, replacement)
.toLowerCase();
}
}
|