2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.executor;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mockito;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.openecomp.appc.domainmodel.lcm.*;
34 import org.openecomp.appc.executor.impl.CommandTask;
35 import org.openecomp.appc.executor.impl.CommandTaskFactory;
36 import org.openecomp.appc.executor.impl.LCMCommandTask;
37 import org.openecomp.appc.executor.impl.LCMReadonlyCommandTask;
38 import org.openecomp.appc.executor.impl.objects.CommandRequest;
39 import org.openecomp.appc.executor.impl.objects.LCMCommandRequest;
40 import org.openecomp.appc.executor.impl.objects.LCMReadOnlyCommandRequest;
41 import org.openecomp.appc.executor.objects.*;
42 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
43 import org.openecomp.appc.requesthandler.RequestHandler;
44 import org.openecomp.appc.workflow.WorkFlowManager;
45 import org.openecomp.appc.workflow.objects.WorkflowRequest;
46 import org.openecomp.appc.workflow.objects.WorkflowResponse;
47 import org.openecomp.sdnc.sli.SvcLogicContext;
48 import org.openecomp.sdnc.sli.SvcLogicException;
49 import org.openecomp.sdnc.sli.SvcLogicResource;
50 import org.openecomp.sdnc.sli.aai.AAIService;
51 import org.osgi.framework.Bundle;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.osgi.framework.ServiceReference;
55 import org.powermock.api.mockito.PowerMockito;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
59 import java.util.Date;
60 import java.util.HashMap;
62 import java.util.Properties;
64 import static junit.framework.Assert.assertEquals;
65 import static org.mockito.Matchers.*;
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
71 public class TestCommandExecutionTask {
73 private static final String TTL_FLAG= "TTL";
74 private static final String API_VERSION= "2.0.0";
75 private static final String ORIGINATOR_ID= "1";
76 private LCMCommandTask executionTask;
77 private LCMReadonlyCommandTask LCMReadonlyCommandTask;
78 private CommandTaskFactory factory ;
80 private RequestHandler requestHandler;
81 private WorkFlowManager workflowManager;
82 private AAIService aaiService;
83 private LifecycleManager lifecyclemanager;
85 private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
86 private final Bundle bundleService=Mockito.mock(Bundle.class);
87 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
90 public void init() throws SvcLogicException {
93 AAIService aaiService = Mockito.mock(AAIService.class);;
94 PowerMockito.mockStatic(FrameworkUtil.class);
95 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
96 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
97 PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
98 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
99 PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
101 public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
102 Object[] args = invocation.getArguments();
103 SvcLogicContext ctx =(SvcLogicContext)args[6];
104 String prefix = (String)args[4];
105 String key = (String)args[3];
106 if(key.contains("'28'")){
107 return SvcLogicResource.QueryStatus.FAILURE ;
108 }else if ( key.contains("'8'")) {
109 return SvcLogicResource.QueryStatus.NOT_FOUND ;
111 ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
112 ctx.setAttribute(prefix + ".orchestration-status", "INSTANTIATED");
114 return SvcLogicResource.QueryStatus.SUCCESS ;
117 PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
119 requestHandler = Mockito.mock(RequestHandler.class);
120 workflowManager = Mockito.mock(WorkFlowManager.class);
121 lifecyclemanager = Mockito.mock(LifecycleManager.class );
123 executionTask = new LCMCommandTask(requestHandler,workflowManager,lifecyclemanager);
124 LCMReadonlyCommandTask = new LCMReadonlyCommandTask(requestHandler,workflowManager);
125 factory = new CommandTaskFactory();
126 factory.setLifecyclemanager(lifecyclemanager);
127 factory.setWorkflowManager(workflowManager);
128 factory.setRequestHandler(requestHandler);
129 Mockito.when(workflowManager.executeWorkflow((WorkflowRequest)anyObject())).thenReturn(getWorkflowResponse () );
134 public void testFactory(){
136 task = factory.getExecutionTask("Configure");
137 assertEquals(LCMCommandTask.class,task.getClass() );
138 task = factory.getExecutionTask("Sync");
139 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
146 public void testOnRequestCompletion(){
147 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
148 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"), VNFOperation.Configure, "1", "1.0");
149 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
150 executionTask.onRequestCompletion(request, response);
154 public void testRunGetConfig(){
155 LCMReadOnlyCommandRequest request = getConfigCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
156 LCMReadonlyCommandTask.setCommandRequest(request);
157 LCMReadonlyCommandTask.run();
161 public void testRun(){
162 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
163 executionTask.setCommandRequest(request);
168 public void testRunNegative(){
169 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
170 executionTask.setCommandRequest(request);
175 CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
176 CommandResponse commandResponse = new CommandResponse();
177 RuntimeContext runtimeContext = new RuntimeContext();
178 commandResponse.setRuntimeContext(runtimeContext);
179 ResponseContext responseContext = new ResponseContext();
180 runtimeContext.setResponseContext(responseContext);
181 RequestContext requestContext = new RequestContext();
182 runtimeContext.setRequestContext(requestContext);
183 CommonHeader commonHeader = new CommonHeader();
184 requestContext.setCommonHeader(commonHeader);
185 responseContext.setCommonHeader(commonHeader);
186 Flags flags = new Flags();
187 commonHeader.setFlags(flags);
188 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
189 requestContext.setActionIdentifiers(actionIdentifiers);
190 VNFContext vnfContext = new VNFContext();
191 runtimeContext.setVnfContext(vnfContext);
192 requestContext.setAction(action);
193 runtimeContext.setRpcName(action.name().toLowerCase());
194 commonHeader.setApiVer(API_VERSION);
195 Status status = new Status();
197 responseContext.setStatus(status);
198 commonHeader.setRequestId(responseId);
199 responseContext.setPayload(payload);
200 commonHeader.setTimestamp(new Date());
201 vnfContext.setId(vnfId);
202 return commandResponse;
208 public void testPositiveFlow_configure() {
210 Date timeStamp = new Date();
211 String requestId = "1";
213 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
214 CommandRequest request = new CommandRequest(commandExecutorInput);
218 private Map<String,Object> setTTLInFlags( String value){
219 Map<String,Object> flags = new HashMap<String,Object>();
220 if( value != null || !("".equalsIgnoreCase(value))){
221 flags.put(TTL_FLAG, value);
227 private LCMReadOnlyCommandRequest getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
228 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
230 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
231 LCMReadOnlyCommandRequest request = new LCMReadOnlyCommandRequest(commandExecutorInput);
236 private LCMCommandRequest getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
237 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
239 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
240 LCMCommandRequest request = new LCMCommandRequest(commandExecutorInput);
245 public WorkflowResponse getWorkflowResponse (){
246 WorkflowResponse wfResponse = new WorkflowResponse();
247 ResponseContext responseContext = createResponseContextWithSuObjects();
248 wfResponse.setResponseContext(responseContext);
249 responseContext.setPayload("");
250 wfResponse.getResponseContext().getStatus().setCode(100);
254 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){
255 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
256 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
257 RequestContext requestContext = runtimeContext.getRequestContext();
258 ResponseContext responseContext = createResponseContextWithSuObjects();
259 runtimeContext.setResponseContext(responseContext);
261 requestContext.getCommonHeader().getFlags().setTtl(ttl);
262 requestContext.getCommonHeader().setApiVer(apiVersion);
263 requestContext.getCommonHeader().setTimestamp(timeStamp);
264 requestContext.getCommonHeader().setRequestId(requestId);
265 requestContext.getCommonHeader().setSubRequestId(subRequestID);
266 requestContext.getCommonHeader().setOriginatorId(originatorID);
267 requestContext.setAction(action);
268 requestContext.setPayload(payload);
269 requestContext.getActionIdentifiers().setVnfId(vnfId);
270 VNFContext vnfContext = runtimeContext.getVnfContext();
271 vnfContext.setType(vnfType);
272 vnfContext.setId(vnfId);
273 vnfContext.setVersion(vnfVersion);
274 return commandExecutorInput;
277 private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
278 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
279 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
280 commandExecutorInput.setRuntimeContext(runtimeContext);
281 return commandExecutorInput;
284 private RuntimeContext createRuntimeContextWithSubObjects() {
285 RuntimeContext runtimeContext = new RuntimeContext();
286 RequestContext requestContext = new RequestContext();
287 runtimeContext.setRequestContext(requestContext);
288 CommonHeader commonHeader = new CommonHeader();
289 requestContext.setCommonHeader(commonHeader);
290 Flags flags = new Flags();
291 commonHeader.setFlags(flags);
292 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
293 requestContext.setActionIdentifiers(actionIdentifiers);
294 VNFContext vnfContext = new VNFContext();
295 runtimeContext.setVnfContext(vnfContext);
296 return runtimeContext;
300 private ResponseContext createResponseContextWithSuObjects(){
301 ResponseContext responseContext = new ResponseContext();
302 CommonHeader commonHeader = new CommonHeader();
303 Flags flags = new Flags();
304 Status status = new Status();
305 responseContext.setCommonHeader(commonHeader);
306 responseContext.setStatus(status);
307 commonHeader.setFlags(flags);
308 return responseContext;