From 01e7a30219bcb92f983d3f4eb9a061e108119257 Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Thu, 17 Jan 2019 08:08:00 +0000 Subject: [PATCH] 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 ++++----- .../executor/{ => impl}/TestCommandExecutor.java | 27 +--- .../appc/executor/{ => impl}/TestCommandTask.java | 163 ++++++++++++++++----- 5 files changed, 187 insertions(+), 99 deletions(-) rename appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/{ => impl}/TestCommandExecutor.java (88%) rename appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/{ => impl}/TestCommandTask.java (50%) 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/impl/TestCommandExecutor.java similarity index 88% rename from appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandExecutor.java rename to appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java index f75e25e62..83c6a0f42 100644 --- 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/impl/TestCommandExecutor.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. @@ -21,7 +23,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.executor; +package org.onap.appc.executor.impl; /** * */ @@ -29,35 +31,23 @@ 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"; @@ -73,6 +63,7 @@ public class TestCommandExecutor { 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 { @@ -81,26 +72,24 @@ public class TestCommandExecutor { executionQueueService = Mockito.mock(ExecutionQueueService.class); - commandExecutor = new CommandExecutorImpl(); + commandExecutor = Mockito.spy(new CommandExecutorImpl()); commandExecutor.setExecutionQueueService(executionQueueService); commandExecutor.setRequestHandler(requestHandler); commandExecutor.setWorkflowManager(workflowManager); commandExecutor.initialize(); - CommandTask commandTask = Mockito.mock(CommandTask.class); + 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 { + Mockito.doReturn(commandTask).when(commandExecutor).getCommandTask(Mockito.any(), Mockito.any()); commandExecutor.executeCommand(commandExecutorInputConfigure); } catch (APPCException e) { Assert.fail(e.toString()); } - } @Test(expected = APPCException.class) @@ -109,7 +98,6 @@ public class TestCommandExecutor { 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(); @@ -146,6 +134,5 @@ public class TestCommandExecutor { 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/impl/TestCommandTask.java similarity index 50% rename from appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/TestCommandTask.java rename to appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java index 4434e0bf9..1bcf8d3f1 100644 --- 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/impl/TestCommandTask.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. @@ -21,50 +23,58 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.executor; +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.Ignore; +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.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.AAIRequest; import org.onap.ccsdk.sli.adaptors.aai.AAIService; -import org.osgi.framework.*; +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; -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}) +@PrepareForTest({FrameworkUtil.class, InetAddress.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); @@ -74,24 +84,21 @@ public class TestCommandTask { 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(){ + public void init() throws UnknownHostException{ 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 ); + task = Mockito.spy(new CommandTask(requestHandler, workflowManager)); } @Test @@ -99,7 +106,7 @@ public class TestCommandTask { 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.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "11", setTTLInFlags("30"), VNFOperation.Sync, "1", "1.0")); task.run(); Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); } @@ -109,22 +116,108 @@ public class TestCommandTask { 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.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); - @Ignore - public void testRunPositiveTerminateSuccess(){ + 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.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(); - Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); } private WorkflowResponse getWorkflowResponse (){ @@ -153,17 +246,19 @@ public class TestCommandTask { runtimeContext.setResponseContext(responseContext); } - private CommandRequest getCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId, - Map flags, VNFOperation command , String vnfId, String vnfVersion ){ + 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, ""); + 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){ + 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(); -- 2.16.6