From 01e7a30219bcb92f983d3f4eb9a061e108119257 Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Thu, 17 Jan 2019 08:08:00 +0000 Subject: Fix unit tests for appc-command-executor-core Increase line coverage of package from 43% to 93% Issue-ID: APPC-1329 Change-Id: I0a4e2a0a85f6761becde4a74609358267530d92d Signed-off-by: Joss Armstrong --- .../appc-command-executor-core/pom.xml | 6 + .../appc/executor/impl/CommandExecutorImpl.java | 20 +- .../org/onap/appc/executor/impl/CommandTask.java | 70 ++--- .../onap/appc/executor/TestCommandExecutor.java | 151 ---------- .../org/onap/appc/executor/TestCommandTask.java | 230 --------------- .../appc/executor/impl/TestCommandExecutor.java | 138 +++++++++ .../onap/appc/executor/impl/TestCommandTask.java | 325 +++++++++++++++++++++ 7 files changed, 514 insertions(+), 426 deletions(-) delete mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandExecutor.java delete mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandTask.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java (limited to 'appc-dispatcher') diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/pom.xml b/appc-dispatcher/appc-command-executor/appc-command-executor-core/pom.xml index 92f12ab27..5ae88642d 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/pom.xml +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/pom.xml @@ -86,6 +86,12 @@ domain-model-lib ${project.version} + + org.hamcrest + hamcrest-all + 1.3 + test + diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java index 5b3df9e14..9c12f4741 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,26 +78,19 @@ public class CommandExecutorImpl implements CommandExecutor { */ @Override public void executeCommand (CommandExecutorInput commandExecutorInput) throws APPCException{ - if (logger.isTraceEnabled()) { - logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput)); - } + logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput)); CommandTask commandTask; try { - commandTask= new CommandTask(requestHandler,workflowManager); + commandTask= getCommandTask(requestHandler, workflowManager); commandTask.setCommandRequest(new CommandRequest(commandExecutorInput)); long remainingTTL = getRemainingTTL(commandTask.getCommandRequest()); - if (logger.isTraceEnabled()) { - logger.trace("Queuing request with CommandRequest = "+ ObjectUtils.toString(commandTask.getCommandRequest())); - } + logger.trace("Queuing request with CommandRequest = "+ ObjectUtils.toString(commandTask.getCommandRequest())); executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS); } catch (Exception e) { logger.error("Exception: "+e.getMessage()); throw new APPCException(e); } - - if (logger.isTraceEnabled()) { - logger.trace("Exiting from executeCommand"); - } + logger.trace("Exiting from executeCommand"); } private long getRemainingTTL(CommandRequest request) { @@ -104,4 +99,7 @@ public class CommandExecutorImpl implements CommandExecutor { return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis(); } + protected CommandTask getCommandTask(RequestHandler requestHandler, WorkFlowManager workflowManager) { + return new CommandTask(requestHandler, workflowManager); + } } diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java index 458814dab..cfd4ff8d0 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java @@ -5,6 +5,8 @@ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +25,11 @@ package org.onap.appc.executor.impl; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.net.InetAddress; +import java.util.UUID; import org.onap.appc.domainmodel.lcm.Status; import org.onap.appc.domainmodel.lcm.VNFOperation; import org.onap.appc.executor.impl.objects.CommandRequest; @@ -31,8 +38,6 @@ import org.onap.appc.requesthandler.RequestHandler; import org.onap.appc.domainmodel.lcm.RuntimeContext; import org.onap.appc.workflow.WorkFlowManager; import org.onap.appc.workflow.objects.WorkflowRequest; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicResource; @@ -44,10 +49,7 @@ import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.slf4j.MDC; -import java.net.InetAddress; -import java.util.UUID; -import static com.att.eelf.configuration.Configuration.*; /** * This abstract class is base class for all Command tasks. All command task must inherit this class. @@ -96,23 +98,21 @@ public class CommandTask implements Runnable { } else { logger.info("AAIService error from bundlecontext"); logger.warn("Cannot find service reference for org.onap.ccsdk.sli.adaptors.aai.AAIService"); - } } @Override public void run() { - logger.debug("Starting execution of command :"+ commandRequest); + logger.debug("Starting execution of command :" + commandRequest); setInitialLogProperties(commandRequest); final RuntimeContext runtimeContext = commandRequest.getCommandExecutorInput().getRuntimeContext(); - WorkflowRequest workflowRequest = new WorkflowRequest(); workflowRequest.setRequestContext(runtimeContext.getRequestContext()); workflowRequest.setResponseContext(runtimeContext.getResponseContext()); workflowRequest.setVnfContext(runtimeContext.getVnfContext()); - logger.debug("Executing workflow :"+ workflowRequest); + logger.debug("Executing workflow :" + workflowRequest); workflowManager.executeWorkflow(workflowRequest); logger.debug("Completed execution workflow with response:"+ commandRequest.getCommandExecutorInput().getRuntimeContext().getResponseContext()); try { @@ -135,32 +135,30 @@ public class CommandTask implements Runnable { private void updateAAIForTerminate(CommandRequest commandRequest) throws AAIServiceException { final int statusCode = commandRequest.getCommandExecutorInput().getRuntimeContext().getResponseContext().getStatus().getCode(); - if (logger.isDebugEnabled()) { - logger.debug("Workflow Execution Status = "+ statusCode); - } + logger.debug("Workflow Execution Status = "+ statusCode); if (statusCode == 100 || statusCode == 400) { SvcLogicContext ctx = new SvcLogicContext(); ctx = getVnfdata(commandRequest.getCommandExecutorInput().getRuntimeContext().getVnfContext().getId(), "vnf", ctx); - deleteGenericVnfData(commandRequest.getCommandExecutorInput().getRuntimeContext().getVnfContext().getId(), ctx.getAttribute("vnf.resource-version")); - + deleteGenericVnfData(commandRequest.getCommandExecutorInput().getRuntimeContext().getVnfContext().getId(), + ctx.getAttribute("vnf.resource-version")); } } private SvcLogicContext getVnfdata(String vnf_id, String prefix,SvcLogicContext ctx) { - String key="generic-vnf.vnf-id = '"+ vnf_id+"'"+" AND http-header.Real-Time = 'true'"; - logger.debug("inside getVnfdata=== "+key); + String key="generic-vnf.vnf-id = '" + vnf_id + "'" + " AND http-header.Real-Time = 'true'"; + logger.debug("inside getVnfdata=== " + key); try { SvcLogicResource.QueryStatus response = aaiService.query("generic-vnf", false, null, key,prefix, null, ctx); if(SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)){ logger.warn("VNF " + vnf_id + " not found while updating A&AI"); - throw new RuntimeException("VNF not found for vnf_id = "+ vnf_id); + throw new RuntimeException("VNF not found for vnf_id = " + vnf_id); } else if(SvcLogicResource.QueryStatus.FAILURE.equals(response)){ - throw new RuntimeException("Error Querying AAI with vnfID = " +vnf_id); + throw new RuntimeException("Error Querying AAI with vnfID = " + vnf_id); } logger.info("AAIResponse: " + response.toString()); } catch (SvcLogicException e) { - logger.error("Error in getVnfdata "+ e); + logger.error("Error in getVnfdata " + e); throw new RuntimeException(e); } return ctx; @@ -171,39 +169,43 @@ public class CommandTask implements Runnable { String reqId = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getRequestId(); try { - MDC.put(MDC_KEY_REQUEST_ID, UUID.fromString(reqId).toString()); + MDC.put(Configuration.MDC_KEY_REQUEST_ID, UUID.fromString(reqId).toString()); //reaching here without exception means existing RequestId is //valid UUID as per ECOMP logging standards } catch (Exception e) { String reqIdUUID = UUID.randomUUID().toString(); - MDC.put(MDC_KEY_REQUEST_ID, reqIdUUID); + MDC.put(Configuration.MDC_KEY_REQUEST_ID, reqIdUUID); logger.info("Replaced invalid requestID of " + reqId + ". New value is " + reqIdUUID + "."); } - if (request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getActionIdentifiers().getServiceInstanceId() != null) - MDC.put(MDC_SERVICE_INSTANCE_ID, request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getActionIdentifiers().getServiceInstanceId()); - MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getOriginatorId()); - MDC.put(MDC_SERVICE_NAME, request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getAction().name()); + if (request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getActionIdentifiers().getServiceInstanceId() != null) { + MDC.put(Configuration.MDC_SERVICE_INSTANCE_ID, + request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getActionIdentifiers().getServiceInstanceId()); + MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, + request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getOriginatorId()); + MDC.put(Configuration.MDC_SERVICE_NAME, + request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getAction().name()); + } try { - MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); - MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); + MDC.put(Configuration.MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); + MDC.put(Configuration.MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); } catch (Exception e) { - logger.error(e.getMessage(),e); + logger.error(e.getMessage(), e); } - MDC.put(MDC_INSTANCE_UUID, ""); // make instanse_UUID generation once during APPC-instanse deploying + MDC.put(Configuration.MDC_INSTANCE_UUID, ""); // make instanse_UUID generation once during APPC-instance deploying } private void clearRequestLogProperties() { try { - MDC.remove(MDC_KEY_REQUEST_ID); - MDC.remove(MDC_SERVICE_INSTANCE_ID); - MDC.remove(MDC_SERVICE_NAME); + MDC.remove(Configuration.MDC_KEY_REQUEST_ID); + MDC.remove(Configuration.MDC_SERVICE_INSTANCE_ID); + MDC.remove(Configuration.MDC_SERVICE_NAME); MDC.remove(LoggingConstants.MDCKeys.PARTNER_NAME); } catch (Exception e) { - logger.error(e.getMessage(),e); + logger.error(e.getMessage(), e); } } - + public boolean deleteGenericVnfData(String vnf_id, String resourceVersion) throws AAIServiceException { boolean response = false; diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandExecutor.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandExecutor.java deleted file mode 100644 index f75e25e62..000000000 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandExecutor.java +++ /dev/null @@ -1,151 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017-2018 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. - * - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.executor; -/** - * - */ - - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.onap.appc.domainmodel.lcm.*; -import org.onap.appc.exceptions.APPCException; -import org.onap.appc.executionqueue.ExecutionQueueService; -import org.onap.appc.executor.impl.*; -import org.onap.appc.executor.impl.objects.CommandRequest; -import org.onap.appc.executor.objects.CommandExecutorInput; -import org.onap.appc.lifecyclemanager.LifecycleManager; -import org.onap.appc.requesthandler.RequestHandler; -import org.onap.appc.workflow.WorkFlowManager; -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.concurrent.TimeUnit; - -import static junit.framework.Assert.assertTrue; -import static org.powermock.api.support.membermodification.MemberMatcher.method; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({CommandTask.class,CommandExecutorImpl.class}) -public class TestCommandExecutor { - - private static final String TTL_FLAG= "TTL"; - private static final String API_VERSION= "2.0.0"; - private static final String ORIGINATOR_ID= "1"; - - private CommandExecutorImpl commandExecutor; - - private RequestHandler requestHandler; - private WorkFlowManager workflowManager; - private ExecutionQueueService executionQueueService; - - private Date timeStamp = new Date(); - private String requestId = "1"; - private CommandExecutorInput commandExecutorInputConfigure = pouplateCommandExecutorInput("FIREWALL", 30000, "1.0", - timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure,"15","") ; - private CommandExecutorInput commandExecutorInputSync = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", - timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync,"15","") ; - - @Before - public void init()throws Exception { - requestHandler= Mockito.mock(RequestHandler.class); - workflowManager= Mockito.mock(WorkFlowManager.class); - - executionQueueService = Mockito.mock(ExecutionQueueService.class); - - commandExecutor = new CommandExecutorImpl(); - commandExecutor.setExecutionQueueService(executionQueueService); - commandExecutor.setRequestHandler(requestHandler); - commandExecutor.setWorkflowManager(workflowManager); - commandExecutor.initialize(); - CommandTask commandTask = Mockito.mock(CommandTask.class); - Mockito.when(commandTask.getCommandRequest()).thenReturn(new CommandRequest(commandExecutorInputConfigure)); - PowerMockito.whenNew(CommandTask.class).withParameterTypes(RequestHandler.class,WorkFlowManager.class).withArguments(requestHandler,workflowManager).thenReturn(commandTask); - } - - - @Test - public void testPositiveFlow_LCM() throws Exception { - //Map flags = setTTLInFlags("30"); - try { - commandExecutor.executeCommand(commandExecutorInputConfigure); - } catch (APPCException e) { - Assert.fail(e.toString()); - } - - } - - @Test(expected = APPCException.class) - public void testNegativeFlow_LCM() throws APPCException{ - Mockito.doThrow(new APPCException("Failed to enqueue request")).when(executionQueueService).putMessage((Runnable) Mockito.anyObject(),Mockito.anyLong(),(TimeUnit) Mockito.anyObject()); - commandExecutor.executeCommand(commandExecutorInputSync); - } - - - private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){ - CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects(); - RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext(); - RequestContext requestContext = runtimeContext.getRequestContext(); - requestContext.getCommonHeader().getFlags().setTtl(ttl); - requestContext.getCommonHeader().setApiVer(apiVersion); - requestContext.getCommonHeader().setTimestamp(timeStamp); - requestContext.getCommonHeader().setRequestId(requestId); - requestContext.getCommonHeader().setSubRequestId(subRequestID); - requestContext.getCommonHeader().setOriginatorId(originatorID); - requestContext.setAction(action); - requestContext.setPayload(payload); - requestContext.getActionIdentifiers().setVnfId(vnfId); - VNFContext vnfContext = runtimeContext.getVnfContext(); - vnfContext.setType(vnfType); - vnfContext.setId(vnfId); - vnfContext.setVersion(vnfVersion); - return commandExecutorInput; - } - - private CommandExecutorInput createCommandExecutorInputWithSubObjects() { - CommandExecutorInput commandExecutorInput = new CommandExecutorInput(); - RuntimeContext runtimeContext = new RuntimeContext(); - commandExecutorInput.setRuntimeContext(runtimeContext); - RequestContext requestContext = new RequestContext(); - runtimeContext.setRequestContext(requestContext); - 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 commandExecutorInput; - } - -} - diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandTask.java deleted file mode 100644 index 4434e0bf9..000000000 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandTask.java +++ /dev/null @@ -1,230 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017-2018 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. - * - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.executor; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.onap.appc.domainmodel.lcm.*; -import org.onap.appc.executor.impl.CommandExecutorImpl; -import org.onap.appc.executor.impl.CommandTask; -import org.onap.appc.executor.impl.objects.CommandRequest; -import org.onap.appc.executor.objects.CommandExecutorInput; -import org.onap.appc.requesthandler.RequestHandler; -import org.onap.appc.workflow.WorkFlowManager; -import org.onap.appc.workflow.objects.WorkflowRequest; -import org.onap.appc.workflow.objects.WorkflowResponse; -import org.onap.ccsdk.sli.adaptors.aai.AAIService; -import org.osgi.framework.*; -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.HashMap; -import java.util.Map; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; - -/** - * @author sushilma - * @since September 04, 2017 - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({FrameworkUtil.class,AAIService.class,BundleContext.class,ServiceReference.class, - BundleReference.class,Bundle.class,Filter.class,BundleListener.class,InvalidSyntaxException.class, - BundleException.class,FrameworkListener.class,ServiceRegistration.class,ServiceListener.class, - Version.class}) -public class TestCommandTask { - CommandTask task ; - private RequestHandler requestHandler; - private WorkFlowManager workflowManager; - private CommandRequest commandRequest; - private AAIService aaiService; - - private BundleContext bundleContext = Mockito.mock(BundleContext.class); - private Bundle bundleService = Mockito.mock(Bundle.class); - private ServiceReference sref = Mockito.mock(ServiceReference.class); - - private static final String TTL_FLAG= "TTL"; - private static final String API_VERSION= "2.0.0"; - private static final String ORIGINATOR_ID= "1"; - @Before - public void init(){ - aaiService = Mockito.mock(AAIService.class); -/* PowerMockito.mockStatic(FrameworkUtil.class); - Mockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref); - Mockito.when(bundleContext.getService(any())).thenReturn(aaiService); - Mockito.when(FrameworkUtil.getBundle(AAIService.class).getBundleContext()).thenReturn(bundleContext);*/ - - 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); - - - requestHandler = Mockito.mock(RequestHandler.class); - workflowManager = Mockito.mock(WorkFlowManager.class); - task = new CommandTask(requestHandler ,workflowManager ); - } - - @Test - public void testRunPositive(){ - Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); - task.setWorkflowManager(workflowManager); - task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0")); - task.run(); - Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); - } - - @Test - public void testRunPositiveTerminateFailed(){ - Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); - task.setWorkflowManager(workflowManager); - task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "12" ,setTTLInFlags("30"),VNFOperation.Terminate, "2", "1.0")); - setResponseContext(300,task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); - task.run(); - Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); - } - - - @Ignore - public void testRunPositiveTerminateSuccess(){ - Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); - task.setWorkflowManager(workflowManager); - task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "12" ,setTTLInFlags("30"),VNFOperation.Terminate, "2", "1.0")); - setResponseContext(100,task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); - task.run(); - Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); - } - - private WorkflowResponse getWorkflowResponse (){ - WorkflowResponse wfResponse = new WorkflowResponse(); - ResponseContext responseContext = createResponseContextWithObjects(); - wfResponse.setResponseContext(responseContext); - responseContext.setPayload(""); - wfResponse.getResponseContext().getStatus().setCode(100); - return wfResponse; - } - - private ResponseContext createResponseContextWithObjects(){ - 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 void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){ - ResponseContext responseContext = createResponseContextWithObjects(); - responseContext.getStatus().setCode(statusCode); - runtimeContext.setResponseContext(responseContext); - } - - private CommandRequest getCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId, - Map flags, VNFOperation command , String vnfId, String vnfVersion ){ - - CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, ""); - CommandRequest request = new CommandRequest(commandExecutorInput); - request.setCommandExecutorInput(commandExecutorInput); - request.setCommandInTimeStamp(new Date()); - return request; - } - - private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){ - CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects(); - RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext(); - RequestContext requestContext = runtimeContext.getRequestContext(); - ResponseContext responseContext = createResponseContextWithSuObjects(); - runtimeContext.setResponseContext(responseContext); - - requestContext.getCommonHeader().getFlags().setTtl(ttl); - requestContext.getCommonHeader().setApiVer(apiVersion); - requestContext.getCommonHeader().setTimestamp(timeStamp); - requestContext.getCommonHeader().setRequestId(requestId); - requestContext.getCommonHeader().setSubRequestId(subRequestID); - requestContext.getCommonHeader().setOriginatorId(originatorID); - requestContext.setAction(action); - requestContext.setPayload(payload); - requestContext.getActionIdentifiers().setVnfId(vnfId); - requestContext.getActionIdentifiers().setServiceInstanceId("test"); - VNFContext vnfContext = runtimeContext.getVnfContext(); - vnfContext.setType(vnfType); - vnfContext.setId(vnfId); - vnfContext.setVersion(vnfVersion); - return commandExecutorInput; - } - - private CommandExecutorInput createCommandExecutorInputWithSubObjects() { - CommandExecutorInput commandExecutorInput = new CommandExecutorInput(); - RuntimeContext runtimeContext = createRuntimeContextWithSubObjects(); - commandExecutorInput.setRuntimeContext(runtimeContext); - return commandExecutorInput; - } - - 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 RuntimeContext createRuntimeContextWithSubObjects() { - RuntimeContext runtimeContext = new RuntimeContext(); - RequestContext requestContext = new RequestContext(); - runtimeContext.setRequestContext(requestContext); - 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 Map setTTLInFlags( String value){ - Map flags = new HashMap(); - if( value != null || !("".equalsIgnoreCase(value))){ - flags.put(TTL_FLAG, value); - } - return flags; - } -} diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java new file mode 100644 index 000000000..83c6a0f42 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= + * 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.onap.appc.executor.impl; +/** + * + */ + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.appc.domainmodel.lcm.*; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.executionqueue.ExecutionQueueService; +import org.onap.appc.executor.impl.objects.CommandRequest; +import org.onap.appc.executor.objects.CommandExecutorInput; +import org.onap.appc.requesthandler.RequestHandler; +import org.onap.appc.workflow.WorkFlowManager; +import org.powermock.api.mockito.PowerMockito; + +import java.util.Date; +import java.util.concurrent.TimeUnit; + + +public class TestCommandExecutor { + + private static final String API_VERSION= "2.0.0"; + private static final String ORIGINATOR_ID= "1"; + + private CommandExecutorImpl commandExecutor; + + private RequestHandler requestHandler; + private WorkFlowManager workflowManager; + private ExecutionQueueService executionQueueService; + + private Date timeStamp = new Date(); + private String requestId = "1"; + private CommandExecutorInput commandExecutorInputConfigure = pouplateCommandExecutorInput("FIREWALL", 30000, "1.0", + timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure,"15","") ; + private CommandExecutorInput commandExecutorInputSync = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", + timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync,"15","") ; + private CommandTask commandTask; + + @Before + public void init()throws Exception { + requestHandler= Mockito.mock(RequestHandler.class); + workflowManager= Mockito.mock(WorkFlowManager.class); + + executionQueueService = Mockito.mock(ExecutionQueueService.class); + + commandExecutor = Mockito.spy(new CommandExecutorImpl()); + commandExecutor.setExecutionQueueService(executionQueueService); + commandExecutor.setRequestHandler(requestHandler); + commandExecutor.setWorkflowManager(workflowManager); + commandExecutor.initialize(); + commandTask = Mockito.mock(CommandTask.class); + Mockito.when(commandTask.getCommandRequest()).thenReturn(new CommandRequest(commandExecutorInputConfigure)); + PowerMockito.whenNew(CommandTask.class).withParameterTypes(RequestHandler.class,WorkFlowManager.class).withArguments(requestHandler,workflowManager).thenReturn(commandTask); + } + + @Test + public void testPositiveFlow_LCM() throws Exception { + try { + Mockito.doReturn(commandTask).when(commandExecutor).getCommandTask(Mockito.any(), Mockito.any()); + commandExecutor.executeCommand(commandExecutorInputConfigure); + } catch (APPCException e) { + Assert.fail(e.toString()); + } + } + + @Test(expected = APPCException.class) + public void testNegativeFlow_LCM() throws APPCException{ + Mockito.doThrow(new APPCException("Failed to enqueue request")).when(executionQueueService).putMessage((Runnable) Mockito.anyObject(),Mockito.anyLong(),(TimeUnit) Mockito.anyObject()); + commandExecutor.executeCommand(commandExecutorInputSync); + } + + private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){ + CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects(); + RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext(); + RequestContext requestContext = runtimeContext.getRequestContext(); + requestContext.getCommonHeader().getFlags().setTtl(ttl); + requestContext.getCommonHeader().setApiVer(apiVersion); + requestContext.getCommonHeader().setTimestamp(timeStamp); + requestContext.getCommonHeader().setRequestId(requestId); + requestContext.getCommonHeader().setSubRequestId(subRequestID); + requestContext.getCommonHeader().setOriginatorId(originatorID); + requestContext.setAction(action); + requestContext.setPayload(payload); + requestContext.getActionIdentifiers().setVnfId(vnfId); + VNFContext vnfContext = runtimeContext.getVnfContext(); + vnfContext.setType(vnfType); + vnfContext.setId(vnfId); + vnfContext.setVersion(vnfVersion); + return commandExecutorInput; + } + + private CommandExecutorInput createCommandExecutorInputWithSubObjects() { + CommandExecutorInput commandExecutorInput = new CommandExecutorInput(); + RuntimeContext runtimeContext = new RuntimeContext(); + commandExecutorInput.setRuntimeContext(runtimeContext); + RequestContext requestContext = new RequestContext(); + runtimeContext.setRequestContext(requestContext); + 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 commandExecutorInput; + } +} + diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java new file mode 100644 index 000000000..1bcf8d3f1 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java @@ -0,0 +1,325 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= + * 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.onap.appc.executor.impl; + +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; +import static org.mockito.Matchers.anyObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.domainmodel.lcm.*; +import org.onap.appc.executor.impl.CommandTask; +import org.onap.appc.executor.impl.objects.CommandRequest; +import org.onap.appc.executor.objects.CommandExecutorInput; +import org.onap.appc.requesthandler.RequestHandler; +import org.onap.appc.workflow.WorkFlowManager; +import org.onap.appc.workflow.objects.WorkflowResponse; +import org.onap.ccsdk.sli.adaptors.aai.AAIRequest; +import org.onap.ccsdk.sli.adaptors.aai.AAIService; +import org.onap.ccsdk.sli.adaptors.aai.AAIServiceException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicResource; +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 org.powermock.reflect.Whitebox; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + + +/** + * @author sushilma + * @since September 04, 2017 + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({FrameworkUtil.class, InetAddress.class}) +public class TestCommandTask { + CommandTask task ; + private RequestHandler requestHandler; + private WorkFlowManager workflowManager; + private AAIService aaiService; + + private BundleContext bundleContext = Mockito.mock(BundleContext.class); + private Bundle bundleService = Mockito.mock(Bundle.class); + private ServiceReference sref = Mockito.mock(ServiceReference.class); + + private static final String TTL_FLAG= "TTL"; + private static final String API_VERSION= "2.0.0"; + private static final String ORIGINATOR_ID= "1"; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Before + public void init() throws UnknownHostException{ + 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); + requestHandler = Mockito.mock(RequestHandler.class); + workflowManager = Mockito.mock(WorkFlowManager.class); + task = Mockito.spy(new CommandTask(requestHandler, workflowManager)); + } + + @Test + public void testRunPositive(){ + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "11", setTTLInFlags("30"), VNFOperation.Sync, "1", "1.0")); + task.run(); + Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); + } + + @Test + public void testRunPositiveTerminateFailed(){ + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + setResponseContext(300, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); + task.run(); + Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); + } + + @Test + public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException{ + AAIService mockAai = Mockito.mock(AAIService.class); + + Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(SvcLogicResource.QueryStatus.SUCCESS); + Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); + Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true); + Whitebox.setInternalState(task, "aaiService", mockAai); + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); + task.run(); + } + + @Test + public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException{ + AAIService mockAai = Mockito.mock(AAIService.class); + + Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) + .thenReturn(SvcLogicResource.QueryStatus.NOT_FOUND); + Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); + Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true); + Whitebox.setInternalState(task, "aaiService", mockAai); + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); + expectedEx.expect(RuntimeException.class); + expectedEx.expectMessage("VNF not found for vnf_id = "); + task.run(); + } + + @Test + public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException{ + AAIService mockAai = Mockito.mock(AAIService.class); + + Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) + .thenReturn(SvcLogicResource.QueryStatus.FAILURE); + Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); + Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true); + Whitebox.setInternalState(task, "aaiService", mockAai); + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); + expectedEx.expect(RuntimeException.class); + expectedEx.expectMessage("Error Querying AAI with vnfID = "); + task.run(); + } + + @Test + public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException{ + AAIService mockAai = Mockito.mock(AAIService.class); + + Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) + .thenReturn(SvcLogicResource.QueryStatus.SUCCESS); + Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); + Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenThrow(new AAIServiceException("ERROR IN DELETE")); + Whitebox.setInternalState(task, "aaiService", mockAai); + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); + expectedEx.expect(RuntimeException.class); + expectedEx.expectCause(allOf(isA(AAIServiceException.class), + hasProperty("message", is("ERROR IN DELETE")))); + task.run(); + } + + @Test + public void testRunSvcLogicException() throws SvcLogicException, AAIServiceException { + AAIService mockAai = Mockito.mock(AAIService.class); + + Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) + .thenThrow(new SvcLogicException()); + Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); + Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true); + Whitebox.setInternalState(task, "aaiService", mockAai); + Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); + task.setWorkflowManager(workflowManager); + task.setRequestHandler(requestHandler); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); + expectedEx.expect(RuntimeException.class); + expectedEx.expectCause(isA(SvcLogicException.class)); + task.run(); + } + + private WorkflowResponse getWorkflowResponse (){ + WorkflowResponse wfResponse = new WorkflowResponse(); + ResponseContext responseContext = createResponseContextWithObjects(); + wfResponse.setResponseContext(responseContext); + responseContext.setPayload(""); + wfResponse.getResponseContext().getStatus().setCode(100); + return wfResponse; + } + + private ResponseContext createResponseContextWithObjects(){ + 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 void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){ + ResponseContext responseContext = createResponseContextWithObjects(); + responseContext.getStatus().setCode(statusCode); + runtimeContext.setResponseContext(responseContext); + } + + private CommandRequest getCommandRequest(String vnfType, Integer ttl, Date timeStamp, String requestId, + Map flags, VNFOperation command, String vnfId, String vnfVersion ){ + + CommandExecutorInput commandExecutorInput = + pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, ""); + CommandRequest request = new CommandRequest(commandExecutorInput); + request.setCommandExecutorInput(commandExecutorInput); + request.setCommandInTimeStamp(new Date()); + return request; + } + + private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, + String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){ + CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects(); + RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext(); + RequestContext requestContext = runtimeContext.getRequestContext(); + ResponseContext responseContext = createResponseContextWithSuObjects(); + runtimeContext.setResponseContext(responseContext); + + requestContext.getCommonHeader().getFlags().setTtl(ttl); + requestContext.getCommonHeader().setApiVer(apiVersion); + requestContext.getCommonHeader().setTimestamp(timeStamp); + requestContext.getCommonHeader().setRequestId(requestId); + requestContext.getCommonHeader().setSubRequestId(subRequestID); + requestContext.getCommonHeader().setOriginatorId(originatorID); + requestContext.setAction(action); + requestContext.setPayload(payload); + requestContext.getActionIdentifiers().setVnfId(vnfId); + requestContext.getActionIdentifiers().setServiceInstanceId("test"); + VNFContext vnfContext = runtimeContext.getVnfContext(); + vnfContext.setType(vnfType); + vnfContext.setId(vnfId); + vnfContext.setVersion(vnfVersion); + return commandExecutorInput; + } + + private CommandExecutorInput createCommandExecutorInputWithSubObjects() { + CommandExecutorInput commandExecutorInput = new CommandExecutorInput(); + RuntimeContext runtimeContext = createRuntimeContextWithSubObjects(); + commandExecutorInput.setRuntimeContext(runtimeContext); + return commandExecutorInput; + } + + 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 RuntimeContext createRuntimeContextWithSubObjects() { + RuntimeContext runtimeContext = new RuntimeContext(); + RequestContext requestContext = new RequestContext(); + runtimeContext.setRequestContext(requestContext); + 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 Map setTTLInFlags( String value){ + Map flags = new HashMap(); + if( value != null || !("".equalsIgnoreCase(value))){ + flags.put(TTL_FLAG, value); + } + return flags; + } +} -- cgit 1.2.3-korg