Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / onap / appc / executor / impl / TestCommandTask.java
index 1bcf8d3..4b94282 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 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=========================================================
  */
 
@@ -72,7 +72,7 @@ import java.util.Map;
 @RunWith(PowerMockRunner.class)
 @PrepareForTest({FrameworkUtil.class, InetAddress.class})
 public class TestCommandTask {
-    CommandTask task ;
+    CommandTask task;
     private RequestHandler requestHandler;
     private WorkFlowManager workflowManager;
     private AAIService aaiService;
@@ -81,66 +81,72 @@ public class TestCommandTask {
     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";
+    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);
+    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);
+        requestHandler = Mockito.mock(RequestHandler.class);
         workflowManager = Mockito.mock(WorkFlowManager.class);
         task = Mockito.spy(new CommandTask(requestHandler, workflowManager));
     }
 
     @Test
-    public void testRunPositive(){
+    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.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "11", setTTLInFlags("30"),
+                VNFOperation.Sync, "1", "1.0"));
         task.run();
-        Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
+        Assert.assertNotNull(
+                task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
     }
 
     @Test
-    public void testRunPositiveTerminateFailed(){
+    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"));
+        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());
+        Assert.assertNotNull(
+                task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
     }
 
     @Test
-    public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException{
+    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.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"));
+        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{
+    public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException {
         AAIService mockAai = Mockito.mock(AAIService.class);
 
         Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
@@ -152,7 +158,8 @@ 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"));
+        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 = ");
@@ -160,7 +167,7 @@ public class TestCommandTask {
     }
 
     @Test
-    public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException{
+    public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException {
         AAIService mockAai = Mockito.mock(AAIService.class);
 
         Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
@@ -172,7 +179,8 @@ 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"));
+        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 = ");
@@ -180,19 +188,21 @@ public class TestCommandTask {
     }
 
     @Test
-    public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException{
+    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"));
+        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"));
+        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),
@@ -213,14 +223,15 @@ 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"));
+        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 (){
+    private WorkflowResponse getWorkflowResponse () {
         WorkflowResponse wfResponse = new WorkflowResponse();
         ResponseContext responseContext = createResponseContextWithObjects();
         wfResponse.setResponseContext(responseContext);
@@ -229,7 +240,7 @@ public class TestCommandTask {
         return wfResponse;
     }
 
-    private ResponseContext createResponseContextWithObjects(){
+    private ResponseContext createResponseContextWithObjects() {
         ResponseContext responseContext = new ResponseContext();
         CommonHeader commonHeader = new CommonHeader();
         Flags flags = new Flags();
@@ -240,41 +251,44 @@ public class TestCommandTask {
         return responseContext;
     }
 
-    private void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){
+    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<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion ){
+            Map<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion) {
 
-        CommandExecutorInput commandExecutorInput =
-                pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
+        CommandExecutorInput commandExecutorInput = populateCommandExecutorInput(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 populateCommandExecutorInput(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();
+        CommonHeader commonHeader = requestContext.getCommonHeader();
         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);
+        commonHeader.getFlags().setTtl(ttl);
+        commonHeader.setApiVer(apiVersion);
+        commonHeader.setTimestamp(timeStamp);
+        commonHeader.setRequestId(requestId);
+        commonHeader.setSubRequestId(subRequestID);
+        commonHeader.setOriginatorId(originatorID);
         requestContext.setAction(action);
         requestContext.setPayload(payload);
-        requestContext.getActionIdentifiers().setVnfId(vnfId);
-        requestContext.getActionIdentifiers().setServiceInstanceId("test");
+        ActionIdentifiers actionIdentifiers = requestContext.getActionIdentifiers();
+        actionIdentifiers.setVnfId(vnfId);
+        actionIdentifiers.setServiceInstanceId("test");
         VNFContext vnfContext = runtimeContext.getVnfContext();
         vnfContext.setType(vnfType);
         vnfContext.setId(vnfId);
@@ -289,7 +303,7 @@ public class TestCommandTask {
         return commandExecutorInput;
     }
 
-    private ResponseContext createResponseContextWithSuObjects(){
+    private ResponseContext createResponseContextWithSuObjects() {
         ResponseContext responseContext = new ResponseContext();
         CommonHeader commonHeader = new CommonHeader();
         Flags flags = new Flags();
@@ -315,9 +329,9 @@ public class TestCommandTask {
         return runtimeContext;
     }
 
-    private Map<String,Object> setTTLInFlags( String value){
-        Map<String,Object> flags = new HashMap<String,Object>();
-        if( value != null || !("".equalsIgnoreCase(value))){
+    private Map<String, Object> setTTLInFlags(String value) {
+        Map<String, Object> flags = new HashMap<String, Object>();
+        if (value != null || !("".equalsIgnoreCase(value))) {
             flags.put(TTL_FLAG, value);
         }
         return flags;