Test coverage for appc-workflow-management-core
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / onap / appc / executor / TestCommandTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.executor;
25
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.onap.appc.domainmodel.lcm.*;
33 import org.onap.appc.executor.impl.CommandExecutorImpl;
34 import org.onap.appc.executor.impl.CommandTask;
35 import org.onap.appc.executor.impl.objects.CommandRequest;
36 import org.onap.appc.executor.objects.CommandExecutorInput;
37 import org.onap.appc.requesthandler.RequestHandler;
38 import org.onap.appc.workflow.WorkFlowManager;
39 import org.onap.appc.workflow.objects.WorkflowRequest;
40 import org.onap.appc.workflow.objects.WorkflowResponse;
41 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
42 import org.osgi.framework.*;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46
47 import java.util.Date;
48 import java.util.HashMap;
49 import java.util.Map;
50
51 import static org.mockito.Matchers.any;
52 import static org.mockito.Matchers.anyObject;
53
54 /**
55  * @author sushilma
56  * @since September 04, 2017
57  */
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({FrameworkUtil.class,AAIService.class,BundleContext.class,ServiceReference.class,
60         BundleReference.class,Bundle.class,Filter.class,BundleListener.class,InvalidSyntaxException.class,
61         BundleException.class,FrameworkListener.class,ServiceRegistration.class,ServiceListener.class,
62         Version.class})
63 public class TestCommandTask {
64     CommandTask task ;
65     private RequestHandler requestHandler;
66     private WorkFlowManager workflowManager;
67     private CommandRequest commandRequest;
68     private AAIService aaiService;
69
70     private BundleContext bundleContext = Mockito.mock(BundleContext.class);
71     private Bundle bundleService = Mockito.mock(Bundle.class);
72     private ServiceReference sref = Mockito.mock(ServiceReference.class);
73
74     private static final String TTL_FLAG= "TTL";
75     private static final String API_VERSION= "2.0.0";
76     private static final String ORIGINATOR_ID= "1";
77     @Before
78     public void init(){
79         aaiService =  Mockito.mock(AAIService.class);
80 /*        PowerMockito.mockStatic(FrameworkUtil.class);
81         Mockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
82         Mockito.when(bundleContext.getService(any())).thenReturn(aaiService);
83         Mockito.when(FrameworkUtil.getBundle(AAIService.class).getBundleContext()).thenReturn(bundleContext);*/
84
85         PowerMockito.mockStatic(FrameworkUtil.class);
86         PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
87         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
88         PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
89         PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
90
91
92         requestHandler =  Mockito.mock(RequestHandler.class);
93         workflowManager = Mockito.mock(WorkFlowManager.class);
94         task = new CommandTask(requestHandler ,workflowManager );
95     }
96
97     @Test
98     public void testRunPositive(){
99         Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
100         task.setWorkflowManager(workflowManager);
101         task.setRequestHandler(requestHandler);
102         task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0"));
103         task.run();
104         Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
105     }
106
107     @Test
108     public void testRunPositiveTerminateFailed(){
109         Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
110         task.setWorkflowManager(workflowManager);
111         task.setRequestHandler(requestHandler);
112         task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "12" ,setTTLInFlags("30"),VNFOperation.Terminate, "2", "1.0"));
113         setResponseContext(300,task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
114         task.run();
115         Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
116     }
117
118
119     @Ignore
120     public void testRunPositiveTerminateSuccess(){
121         Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
122         task.setWorkflowManager(workflowManager);
123         task.setRequestHandler(requestHandler);
124         task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "12" ,setTTLInFlags("30"),VNFOperation.Terminate, "2", "1.0"));
125         setResponseContext(100,task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
126         task.run();
127         Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
128     }
129
130     private WorkflowResponse getWorkflowResponse (){
131         WorkflowResponse wfResponse = new WorkflowResponse();
132         ResponseContext responseContext = createResponseContextWithObjects();
133         wfResponse.setResponseContext(responseContext);
134         responseContext.setPayload("");
135         wfResponse.getResponseContext().getStatus().setCode(100);
136         return wfResponse;
137     }
138
139     private ResponseContext createResponseContextWithObjects(){
140         ResponseContext responseContext = new ResponseContext();
141         CommonHeader commonHeader = new CommonHeader();
142         Flags flags = new Flags();
143         Status status = new Status();
144         responseContext.setCommonHeader(commonHeader);
145         responseContext.setStatus(status);
146         commonHeader.setFlags(flags);
147         return responseContext;
148     }
149
150     private void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){
151         ResponseContext responseContext = createResponseContextWithObjects();
152         responseContext.getStatus().setCode(statusCode);
153         runtimeContext.setResponseContext(responseContext);
154     }
155
156     private CommandRequest getCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
157                                              Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
158
159         CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
160         CommandRequest request = new CommandRequest(commandExecutorInput);
161         request.setCommandExecutorInput(commandExecutorInput);
162         request.setCommandInTimeStamp(new Date());
163         return request;
164     }
165
166     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){
167         CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
168         RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
169         RequestContext requestContext = runtimeContext.getRequestContext();
170         ResponseContext responseContext = createResponseContextWithSuObjects();
171         runtimeContext.setResponseContext(responseContext);
172
173         requestContext.getCommonHeader().getFlags().setTtl(ttl);
174         requestContext.getCommonHeader().setApiVer(apiVersion);
175         requestContext.getCommonHeader().setTimestamp(timeStamp);
176         requestContext.getCommonHeader().setRequestId(requestId);
177         requestContext.getCommonHeader().setSubRequestId(subRequestID);
178         requestContext.getCommonHeader().setOriginatorId(originatorID);
179         requestContext.setAction(action);
180         requestContext.setPayload(payload);
181         requestContext.getActionIdentifiers().setVnfId(vnfId);
182         requestContext.getActionIdentifiers().setServiceInstanceId("test");
183         VNFContext vnfContext = runtimeContext.getVnfContext();
184         vnfContext.setType(vnfType);
185         vnfContext.setId(vnfId);
186         vnfContext.setVersion(vnfVersion);
187         return commandExecutorInput;
188     }
189
190     private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
191         CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
192         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
193         commandExecutorInput.setRuntimeContext(runtimeContext);
194         return commandExecutorInput;
195     }
196
197     private ResponseContext createResponseContextWithSuObjects(){
198         ResponseContext responseContext = new ResponseContext();
199         CommonHeader commonHeader = new CommonHeader();
200         Flags flags = new Flags();
201         Status status = new Status();
202         responseContext.setCommonHeader(commonHeader);
203         responseContext.setStatus(status);
204         commonHeader.setFlags(flags);
205         return responseContext;
206     }
207
208     private RuntimeContext createRuntimeContextWithSubObjects() {
209         RuntimeContext runtimeContext = new RuntimeContext();
210         RequestContext requestContext = new RequestContext();
211         runtimeContext.setRequestContext(requestContext);
212         CommonHeader commonHeader = new CommonHeader();
213         requestContext.setCommonHeader(commonHeader);
214         Flags flags = new Flags();
215         commonHeader.setFlags(flags);
216         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
217         requestContext.setActionIdentifiers(actionIdentifiers);
218         VNFContext vnfContext = new VNFContext();
219         runtimeContext.setVnfContext(vnfContext);
220         return runtimeContext;
221     }
222
223     private Map<String,Object> setTTLInFlags( String value){
224         Map<String,Object> flags = new HashMap<String,Object>();
225         if( value != null || !("".equalsIgnoreCase(value))){
226             flags.put(TTL_FLAG, value);
227         }
228         return flags;
229     }
230 }