2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.requesthandler;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.openecomp.appc.domainmodel.lcm.ActionIdentifiers;
34 import org.openecomp.appc.domainmodel.lcm.CommonHeader;
35 import org.openecomp.appc.domainmodel.lcm.Flags;
36 import org.openecomp.appc.domainmodel.lcm.RequestContext;
37 import org.openecomp.appc.domainmodel.lcm.ResponseContext;
38 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
39 import org.openecomp.appc.domainmodel.lcm.Status;
40 import org.openecomp.appc.domainmodel.lcm.VNFContext;
41 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
42 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
43 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
44 import org.openecomp.appc.requesthandler.exceptions.InvalidInputException;
45 import org.openecomp.appc.requesthandler.exceptions.LCMOperationsDisabledException;
46 import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
47 import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
48 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
49 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
50 import org.openecomp.appc.workflow.WorkFlowManager;
51 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
52 import org.openecomp.appc.workingstatemanager.WorkingStateManager;
53 import org.openecomp.sdnc.sli.SvcLogicContext;
54 import org.openecomp.sdnc.sli.SvcLogicResource;
55 import org.openecomp.sdnc.sli.aai.AAIService;
56 import org.osgi.framework.Bundle;
57 import org.osgi.framework.BundleContext;
58 import org.osgi.framework.FrameworkUtil;
59 import org.osgi.framework.ServiceReference;
60 import org.powermock.api.mockito.PowerMockito;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
64 import java.time.Instant;
65 import java.util.UUID;
67 import static junit.framework.TestCase.assertNotNull;
68 import static org.junit.Assert.assertNull;
69 import static org.mockito.Matchers.*;
71 @SuppressWarnings("unchecked")
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,
74 RequestValidatorImpl.class, TransactionRecorder.class})
75 public class RequestValidatorTest {
76 private final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
78 private RequestValidatorImpl requestValidator;
80 private AAIService aaiAdapter ;
81 private LifecycleManager lifecyclemanager;
82 private WorkFlowManager workflowManager;
83 private WorkingStateManager workingStateManager ;
84 private LCMStateManager lcmStateManager;
86 private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
87 private final Bundle bundleService=Mockito.mock(Bundle.class);
88 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
91 public void init() throws Exception {
93 AAIService aaiService = Mockito.mock(AAIService.class);
94 PowerMockito.mockStatic(FrameworkUtil.class);
95 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
96 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
97 PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
98 PowerMockito.when(bundleContext.<AAIService>getService(sref)).thenReturn(aaiService);
99 PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),
100 anyObject())).thenAnswer(invocation -> {
101 Object[] args = invocation.getArguments();
102 SvcLogicContext ctx =(SvcLogicContext)args[6];
103 String prefix = (String)args[4];
104 String key = (String)args[3];
105 if(key.contains("'28'")){
106 return SvcLogicResource.QueryStatus.FAILURE ;
107 }else if ( key.contains("'8'")) {
108 return SvcLogicResource.QueryStatus.NOT_FOUND ;
110 ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
111 ctx.setAttribute(prefix + ".orchestration-status", "Instantiated");
113 return SvcLogicResource.QueryStatus.SUCCESS ;
115 PowerMockito.when(aaiService.update(anyString(),anyString(), anyObject(),anyString(), anyObject()))
116 .thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
119 aaiAdapter = Mockito.mock(AAIService.class);
120 lifecyclemanager= Mockito.mock(LifecycleManager.class);
121 workflowManager= Mockito.mock(WorkFlowManager.class);
122 workingStateManager = Mockito.mock(WorkingStateManager.class);
123 lcmStateManager = Mockito.mock(LCMStateManager.class);
125 requestValidator = new RequestValidatorImpl();
126 requestValidator.setWorkflowManager(workflowManager);
127 requestValidator.setLifecyclemanager(lifecyclemanager);
128 requestValidator.setWorkingStateManager(workingStateManager);
129 requestValidator.setLcmStateManager(lcmStateManager);
131 Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(true);
134 public AAIService getAaiadapter() {
135 return this.aaiAdapter;
138 private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl,
139 boolean force, String originatorId, String requestId,
140 String subRequestId, Instant timeStamp){
141 String API_VERSION= "2.0.0";
142 RequestHandlerInput input = new RequestHandlerInput();
143 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
144 RequestContext requestContext = runtimeContext.getRequestContext();
145 input.setRequestContext(requestContext);
146 requestContext.getActionIdentifiers().setVnfId(vnfID);
147 requestContext.setAction(action);
148 if (action != null) {
149 input.setRpcName(convertActionNameToUrl(action.name()));
151 input.setRpcName(null);
153 requestContext.getCommonHeader().setRequestId(requestId);
154 requestContext.getCommonHeader().setSubRequestId(subRequestId);
155 requestContext.getCommonHeader().setOriginatorId(originatorId);
156 requestContext.getCommonHeader().setFlags(new Flags(null, force, ttl));
157 requestContext.getCommonHeader().getTimeStamp();
158 requestContext.getCommonHeader().setApiVer(API_VERSION);
159 requestContext.getCommonHeader().setTimestamp(timeStamp);
164 public void testNullVnfID() throws Exception {
165 logger.debug("=====================testNullVnfID=============================");
166 Mockito.when(workflowManager.workflowExists(anyObject()))
167 .thenReturn(new WorkflowExistsOutput(true,true));
168 RequestHandlerInput input = this.getRequestHandlerInput(null, VNFOperation.Configure, 30,
169 false, UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
172 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
174 requestValidator.validateRequest(runtimeContext);
175 }catch(InvalidInputException e ) {
179 logger.debug("=====================testNullVnfID=============================");
183 public void testPositiveFlowWithConfigure() throws Exception {
184 logger.debug("=====================testPositiveFlowWithConfigure=============================");
185 Mockito.when(workflowManager.workflowExists(anyObject()))
186 .thenReturn(new WorkflowExistsOutput(true,true));
187 Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
188 RequestHandlerInput input = this.getRequestHandlerInput("1", VNFOperation.Configure, 30,
189 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
192 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
194 requestValidator.validateRequest(runtimeContext);
195 }catch(Exception e ) {
199 logger.debug("testPositiveFlowWithConfigure");
200 logger.debug("=====================testPositiveFlowWithConfigure=============================");
204 public void testVnfNotFound() throws Exception {
205 logger.debug("=====================testVnfNotFound=============================");
206 Mockito.when(workflowManager.workflowExists(anyObject()))
207 .thenReturn(new WorkflowExistsOutput(true,true));
208 RequestHandlerInput input = this.getRequestHandlerInput("8", VNFOperation.Configure, 30,
209 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
212 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
214 requestValidator.validateRequest(runtimeContext);
215 }catch(Exception e ) {
219 logger.debug("=====================testVnfNotFound=============================");
223 public void testNullCommand() throws Exception {
224 logger.debug("=====================testNullCommand=============================");
225 Mockito.when(workflowManager.workflowExists(anyObject()))
226 .thenReturn(new WorkflowExistsOutput(true,true));
227 RequestHandlerInput input = this.getRequestHandlerInput("7", null,30,
228 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
231 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
233 requestValidator.validateRequest(runtimeContext);
234 }catch(InvalidInputException e ) {
238 logger.debug("=====================testNullCommand=============================");
242 public void testNullVnfIDAndCommand() throws Exception {
243 logger.debug("=====================testNullVnfIDAndCommand=============================");
244 Mockito.when(workflowManager.workflowExists(anyObject()))
245 .thenReturn(new WorkflowExistsOutput(true,true));
246 RequestHandlerInput input = this.getRequestHandlerInput(null, null,30,
247 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
250 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
252 requestValidator.validateRequest(runtimeContext);
253 }catch(InvalidInputException e ) {
257 logger.debug("=====================testNullVnfIDAndCommand=============================");
261 public void testWorkflowNotFound() throws Exception {
262 logger.debug("=====================testWorkflowNotFound=============================");
263 Mockito.when(workflowManager.workflowExists(anyObject()))
264 .thenReturn(new WorkflowExistsOutput(false,false));
265 RequestHandlerInput input = this.getRequestHandlerInput("10", VNFOperation.Configure, 30,
266 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
269 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
271 requestValidator.validateRequest(runtimeContext);
272 }catch(Exception e ) {
276 logger.debug("=====================testWorkflowNotFound=============================");
280 public void testUnstableVnfWithConfigure() throws Exception {
281 logger.debug("=====================testUnstableVnfWithConfigure=============================");
282 Mockito.when(workflowManager.workflowExists(anyObject()))
283 .thenReturn(new WorkflowExistsOutput(true,true));
284 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString()))
285 .thenThrow( new NoTransitionDefinedException("","",""));
287 RequestHandlerInput input = this.getRequestHandlerInput("11", VNFOperation.Configure, 30,
288 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
291 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
293 requestValidator.validateRequest(runtimeContext);
294 }catch(Exception e ) {
298 logger.debug("=====================testUnstableVnfWithConfigure=============================");
302 public void testUnstableVnfWithTest() throws Exception {
303 logger.debug("=====================testUnstableVnfWithTest=============================");
304 Mockito.when(workflowManager.workflowExists(anyObject()))
305 .thenReturn(new WorkflowExistsOutput(true,true));
306 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString()))
307 .thenThrow( new NoTransitionDefinedException("","",""));
308 RequestHandlerInput input = this.getRequestHandlerInput("12", VNFOperation.Test,30,
309 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
312 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
314 requestValidator.validateRequest(runtimeContext);
315 }catch(Exception e ) {
319 logger.debug("=====================testUnstableVnfWithTest=============================");
323 public void testUnstableVnfWithStart() throws Exception {
324 logger.debug("=====================testUnstableVnfWithStart=============================");
325 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString()))
326 .thenThrow( new NoTransitionDefinedException("","",""));
328 RequestHandlerInput input = this.getRequestHandlerInput("13", VNFOperation.Start,30,
329 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
332 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
334 requestValidator.validateRequest(runtimeContext);
335 }catch(Exception e ) {
339 logger.debug("=====================testUnstableVnfWithStart=============================");
343 public void testUnstableVnfWithTerminate() throws Exception {
344 logger.debug("=====================testUnstableVnfWithTerminate=============================");
345 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString()))
346 .thenThrow( new NoTransitionDefinedException("","",""));
347 RequestHandlerInput input = this.getRequestHandlerInput("14", VNFOperation.Terminate,30,
348 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
351 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
353 requestValidator.validateRequest(runtimeContext);
354 }catch(Exception e ) {
358 logger.debug("=====================testUnstableVnfWithTerminate=============================");
362 public void testUnstableVnfWithRestart() throws Exception {
363 logger.debug("=====================testUnstableVnfWithRestart=============================");
364 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString()))
365 .thenThrow( new NoTransitionDefinedException("","",""));
367 RequestHandlerInput input = this.getRequestHandlerInput("26", VNFOperation.Restart,30,
368 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
371 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
373 requestValidator.validateRequest(runtimeContext);
374 }catch(Exception e ) {
378 logger.debug("=====================testUnstableVnfWithRestart=============================");
382 public void testUnstableVnfWithRebuild() throws Exception {
383 logger.debug("=====================testUnstableVnfWithRebuild=============================");
384 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString()))
385 .thenThrow( new NoTransitionDefinedException("","",""));
387 RequestHandlerInput input = this.getRequestHandlerInput("27", VNFOperation.Rebuild,30,
388 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
391 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
393 requestValidator.validateRequest(runtimeContext);
394 }catch(Exception e ) {
398 logger.debug("=====================testUnstableVnfWithRebuild=============================");
402 public void testAAIDown() throws Exception {
403 logger.debug("=====================testAAIDown=============================");
404 RequestHandlerInput input = this.getRequestHandlerInput("28", VNFOperation.Configure, 30,
405 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),
408 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
410 requestValidator.validateRequest(runtimeContext);
411 }catch(Exception e ) {
415 logger.debug("=====================testAAIDown=============================");
419 public void testNegativeFlowWithTimeStamp() throws Exception {
420 logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
421 Instant now = Instant.now();
422 Instant past = now.minusMillis(1000000);
423 RequestHandlerInput input = this.getRequestHandlerInput("35", VNFOperation.Configure, 30,
424 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),past);
426 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
429 requestValidator.validateRequest(runtimeContext);
430 }catch(Exception e ) {
434 logger.debug("testNegativeFlowWithTimeStamp");
435 logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
439 public void rejectDuplicateRequest() throws Exception {
440 String originatorID = UUID.randomUUID().toString();
441 String requestID = UUID.randomUUID().toString();
442 String subRequestID = UUID.randomUUID().toString();
444 Mockito.when(workflowManager.workflowExists(anyObject()))
445 .thenReturn(new WorkflowExistsOutput(true,true));
446 Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true);
447 Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true);
448 RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,
449 originatorID, requestID, subRequestID, Instant.now());
451 RequestHandlerInput input1 = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,
452 originatorID, requestID, subRequestID, Instant.now());
454 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
455 RuntimeContext runtimeContext1 = putInputToRuntimeContext(input1);
458 requestValidator.validateRequest(runtimeContext);
459 }catch(Exception e ) {
465 requestValidator.validateRequest(runtimeContext1);
466 }catch(Exception e ) {
473 public void testLockOperation() throws Exception {
474 Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
475 testOperation("no-matter", VNFOperation.Lock);
479 public void testUnlockOperation() throws Exception {
480 Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
481 testOperation("no-matter", VNFOperation.Unlock);
485 public void testCheckLockOperation() throws Exception {
486 Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
487 testOperation("no-matter", VNFOperation.CheckLock);
490 @Test(expected = NoTransitionDefinedException.class)
491 public void testLockOperationNegative() throws Exception {
492 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Lock.toString())))
493 .thenThrow(new NoTransitionDefinedException("", "", ""));
494 Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
495 testOperation("no-matter", VNFOperation.Lock);
498 @Test(expected = NoTransitionDefinedException.class)
499 public void testUnlockOperationNegative() throws Exception {
500 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Unlock.toString())))
501 .thenThrow(new NoTransitionDefinedException("", "", ""));
502 Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
503 testOperation("no-matter", VNFOperation.Unlock);
506 @Test(expected = NoTransitionDefinedException.class)
507 public void testCheckLockOperationNegative() throws Exception {
508 Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.CheckLock.toString())))
509 .thenThrow(new NoTransitionDefinedException("", "", ""));
510 Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
511 testOperation("no-matter", VNFOperation.CheckLock);
514 @Test(expected = LCMOperationsDisabledException.class)
515 public void testLCMOperationsDisabled() throws Exception {
516 Mockito.when(lcmStateManager.isLCMOperationEnabled()).thenReturn(false);
517 testOperation("no-matter", VNFOperation.Configure);
519 private void testOperation(String resource, VNFOperation operation) throws Exception {
520 String originatorID = UUID.randomUUID().toString();
521 String requestID = UUID.randomUUID().toString();
522 String subRequestID = UUID.randomUUID().toString();
524 RequestHandlerInput input = this.getRequestHandlerInput(resource, operation, 0, false, originatorID,
525 requestID, subRequestID, Instant.now());
526 RuntimeContext runtimeContext = putInputToRuntimeContext(input);
527 requestValidator.validateRequest(runtimeContext);
530 private RuntimeContext createRuntimeContextWithSubObjects() {
531 RuntimeContext runtimeContext = new RuntimeContext();
532 RequestContext requestContext = new RequestContext();
533 runtimeContext.setRequestContext(requestContext);
534 ResponseContext responseContext = createResponseContextWithSuObjects();
535 runtimeContext.setResponseContext(responseContext);
536 CommonHeader commonHeader = new CommonHeader();
537 requestContext.setCommonHeader(commonHeader);
538 commonHeader.setFlags(new Flags(null, false, 0));
539 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
540 requestContext.setActionIdentifiers(actionIdentifiers);
541 VNFContext vnfContext = new VNFContext();
542 runtimeContext.setVnfContext(vnfContext);
543 return runtimeContext;
547 private ResponseContext createResponseContextWithSuObjects(){
548 ResponseContext responseContext = new ResponseContext();
549 CommonHeader commonHeader = new CommonHeader();
550 responseContext.setCommonHeader(commonHeader);
551 responseContext.setStatus(new Status(0, null));
552 commonHeader.setFlags(new Flags(null, false, 0));
553 return responseContext;
556 private String convertActionNameToUrl(String action) {
557 String regex = "([a-z])([A-Z]+)";
558 String replacement = "$1-$2";
559 return action.replaceAll(regex, replacement)
563 private RuntimeContext putInputToRuntimeContext(RequestHandlerInput input) {
564 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
565 runtimeContext.setRequestContext(input.getRequestContext());
566 runtimeContext.setRpcName(input.getRpcName());
567 runtimeContext.getVnfContext().setId(input.getRequestContext().getActionIdentifiers().getVnfId());
568 return runtimeContext;