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(){
135 CommandTask<? extends CommandRequest> task = factory.getExecutionTask("Configure");
136 assertEquals(LCMCommandTask.class,task.getClass() );
137 task = factory.getExecutionTask("Sync");
138 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
145 public void testOnRequestCompletion(){
146 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
147 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"), VNFOperation.Configure, "1", "1.0");
148 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
149 executionTask.onRequestCompletion(request, response);
153 public void testRunGetConfig(){
154 LCMReadOnlyCommandRequest request = getConfigCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
155 LCMReadonlyCommandTask.setCommandRequest(request);
156 LCMReadonlyCommandTask.run();
160 public void testRun(){
161 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
162 executionTask.setCommandRequest(request);
167 public void testRunNegative(){
168 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
169 executionTask.setCommandRequest(request);
174 CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
175 CommandResponse commandResponse = new CommandResponse();
176 RuntimeContext runtimeContext = new RuntimeContext();
177 commandResponse.setRuntimeContext(runtimeContext);
178 ResponseContext responseContext = new ResponseContext();
179 runtimeContext.setResponseContext(responseContext);
180 RequestContext requestContext = new RequestContext();
181 runtimeContext.setRequestContext(requestContext);
182 CommonHeader commonHeader = new CommonHeader();
183 requestContext.setCommonHeader(commonHeader);
184 responseContext.setCommonHeader(commonHeader);
185 Flags flags = new Flags();
186 commonHeader.setFlags(flags);
187 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
188 requestContext.setActionIdentifiers(actionIdentifiers);
189 VNFContext vnfContext = new VNFContext();
190 runtimeContext.setVnfContext(vnfContext);
191 requestContext.setAction(action);
192 runtimeContext.setRpcName(action.name().toLowerCase());
193 commonHeader.setApiVer(API_VERSION);
194 responseContext.setStatus(new Status(100, null));
195 commonHeader.setRequestId(responseId);
196 responseContext.setPayload(payload);
197 commonHeader.setTimestamp(new Date());
198 vnfContext.setId(vnfId);
199 return commandResponse;
205 public void testPositiveFlow_configure() {
207 Date timeStamp = new Date();
208 String requestId = "1";
210 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
211 CommandRequest request = new CommandRequest(commandExecutorInput);
215 private Map<String,Object> setTTLInFlags( String value){
216 Map<String,Object> flags = new HashMap<String,Object>();
217 if( value != null || !("".equalsIgnoreCase(value))){
218 flags.put(TTL_FLAG, value);
224 private LCMReadOnlyCommandRequest getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
225 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
227 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
228 LCMReadOnlyCommandRequest request = new LCMReadOnlyCommandRequest(commandExecutorInput);
233 private LCMCommandRequest getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
234 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
236 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
237 LCMCommandRequest request = new LCMCommandRequest(commandExecutorInput);
242 public WorkflowResponse getWorkflowResponse (){
243 WorkflowResponse wfResponse = new WorkflowResponse();
244 ResponseContext responseContext = createResponseContextWithSuObjects();
245 wfResponse.setResponseContext(responseContext);
246 responseContext.setPayload("");
247 wfResponse.getResponseContext().setStatus(new Status(100, null));
251 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){
252 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
253 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
254 RequestContext requestContext = runtimeContext.getRequestContext();
255 ResponseContext responseContext = createResponseContextWithSuObjects();
256 runtimeContext.setResponseContext(responseContext);
258 requestContext.getCommonHeader().getFlags().setTtl(ttl);
259 requestContext.getCommonHeader().setApiVer(apiVersion);
260 requestContext.getCommonHeader().setTimestamp(timeStamp);
261 requestContext.getCommonHeader().setRequestId(requestId);
262 requestContext.getCommonHeader().setSubRequestId(subRequestID);
263 requestContext.getCommonHeader().setOriginatorId(originatorID);
264 requestContext.setAction(action);
265 requestContext.setPayload(payload);
266 requestContext.getActionIdentifiers().setVnfId(vnfId);
267 VNFContext vnfContext = runtimeContext.getVnfContext();
268 vnfContext.setType(vnfType);
269 vnfContext.setId(vnfId);
270 vnfContext.setVersion(vnfVersion);
271 return commandExecutorInput;
274 private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
275 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
276 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
277 commandExecutorInput.setRuntimeContext(runtimeContext);
278 return commandExecutorInput;
281 private RuntimeContext createRuntimeContextWithSubObjects() {
282 RuntimeContext runtimeContext = new RuntimeContext();
283 RequestContext requestContext = new RequestContext();
284 runtimeContext.setRequestContext(requestContext);
285 CommonHeader commonHeader = new CommonHeader();
286 requestContext.setCommonHeader(commonHeader);
287 Flags flags = new Flags();
288 commonHeader.setFlags(flags);
289 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
290 requestContext.setActionIdentifiers(actionIdentifiers);
291 VNFContext vnfContext = new VNFContext();
292 runtimeContext.setVnfContext(vnfContext);
293 return runtimeContext;
297 private ResponseContext createResponseContextWithSuObjects(){
298 ResponseContext responseContext = new ResponseContext();
299 CommonHeader commonHeader = new CommonHeader();
300 Flags flags = new Flags();
301 responseContext.setCommonHeader(commonHeader);
302 responseContext.setStatus(new Status(0, null));
303 commonHeader.setFlags(flags);
304 return responseContext;