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.objects.*;
39 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
40 import org.openecomp.appc.requesthandler.RequestHandler;
41 import org.openecomp.appc.workflow.WorkFlowManager;
42 import org.openecomp.appc.workflow.objects.WorkflowRequest;
43 import org.openecomp.appc.workflow.objects.WorkflowResponse;
44 import org.openecomp.sdnc.sli.SvcLogicContext;
45 import org.openecomp.sdnc.sli.SvcLogicException;
46 import org.openecomp.sdnc.sli.SvcLogicResource;
47 import org.openecomp.sdnc.sli.aai.AAIService;
48 import org.osgi.framework.Bundle;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.FrameworkUtil;
51 import org.osgi.framework.ServiceReference;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.core.classloader.annotations.PrepareForTest;
54 import org.powermock.modules.junit4.PowerMockRunner;
56 import java.util.Date;
57 import java.util.HashMap;
59 import java.util.Properties;
61 import static junit.framework.Assert.assertEquals;
62 import static org.mockito.Matchers.*;
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
68 public class TestCommandExecutionTask {
70 private static final String TTL_FLAG= "TTL";
71 private static final String API_VERSION= "2.0.0";
72 private static final String ORIGINATOR_ID= "1";
73 private LCMCommandTask executionTask;
74 private LCMReadonlyCommandTask LCMReadonlyCommandTask;
75 private CommandTaskFactory factory ;
77 private RequestHandler requestHandler;
78 private WorkFlowManager workflowManager;
79 private AAIService aaiService;
80 private LifecycleManager lifecyclemanager;
82 private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
83 private final Bundle bundleService=Mockito.mock(Bundle.class);
84 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
87 public void init() throws SvcLogicException {
90 AAIService aaiService = Mockito.mock(AAIService.class);;
91 PowerMockito.mockStatic(FrameworkUtil.class);
92 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
93 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
94 PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
95 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
96 PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
98 public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
99 Object[] args = invocation.getArguments();
100 SvcLogicContext ctx =(SvcLogicContext)args[6];
101 String prefix = (String)args[4];
102 String key = (String)args[3];
103 if(key.contains("'28'")){
104 return SvcLogicResource.QueryStatus.FAILURE ;
105 }else if ( key.contains("'8'")) {
106 return SvcLogicResource.QueryStatus.NOT_FOUND ;
108 ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
109 ctx.setAttribute(prefix + ".orchestration-status", "INSTANTIATED");
111 return SvcLogicResource.QueryStatus.SUCCESS ;
114 PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
116 requestHandler = Mockito.mock(RequestHandler.class);
117 workflowManager = Mockito.mock(WorkFlowManager.class);
118 lifecyclemanager = Mockito.mock(LifecycleManager.class );
120 executionTask = new LCMCommandTask(requestHandler,workflowManager,lifecyclemanager);
121 LCMReadonlyCommandTask = new LCMReadonlyCommandTask(requestHandler,workflowManager);
122 factory = new CommandTaskFactory();
123 factory.setLifecyclemanager(lifecyclemanager);
124 factory.setWorkflowManager(workflowManager);
125 factory.setRequestHandler(requestHandler);
126 Mockito.when(workflowManager.executeWorkflow((WorkflowRequest)anyObject())).thenReturn(getWorkflowResponse () );
131 public void testFactory(){
132 CommandTask task = factory.getExecutionTask("Configure");
133 assertEquals(LCMCommandTask.class,task.getClass() );
134 task = factory.getExecutionTask("Sync");
135 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
142 public void testOnRequestCompletion(){
143 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
144 CommandExecutorInput request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"), VNFOperation.Configure, "1", "1.0");
145 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
146 executionTask.onRequestCompletion(request, response);
150 public void testRunGetConfig(){
151 CommandExecutorInput request = getConfigCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
152 LCMReadonlyCommandTask.setCommandRequest(request);
153 LCMReadonlyCommandTask.run();
157 public void testRun(){
158 CommandExecutorInput request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
159 executionTask.setCommandRequest(request);
164 public void testRunNegative(){
165 CommandExecutorInput request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
166 executionTask.setCommandRequest(request);
171 CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
172 CommandResponse commandResponse = new CommandResponse();
173 RuntimeContext runtimeContext = new RuntimeContext();
174 commandResponse.setRuntimeContext(runtimeContext);
175 ResponseContext responseContext = new ResponseContext();
176 runtimeContext.setResponseContext(responseContext);
177 RequestContext requestContext = new RequestContext();
178 runtimeContext.setRequestContext(requestContext);
179 CommonHeader commonHeader = new CommonHeader();
180 requestContext.setCommonHeader(commonHeader);
181 responseContext.setCommonHeader(commonHeader);
182 Flags flags = new Flags();
183 commonHeader.setFlags(flags);
184 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
185 requestContext.setActionIdentifiers(actionIdentifiers);
186 VNFContext vnfContext = new VNFContext();
187 runtimeContext.setVnfContext(vnfContext);
188 requestContext.setAction(action);
189 runtimeContext.setRpcName(action.name().toLowerCase());
190 commonHeader.setApiVer(API_VERSION);
191 responseContext.setStatus(new Status(100, null));
192 commonHeader.setRequestId(responseId);
193 responseContext.setPayload(payload);
194 commonHeader.setTimestamp(new Date());
195 vnfContext.setId(vnfId);
196 return commandResponse;
202 public void testPositiveFlow_configure() {
204 Date timeStamp = new Date();
205 String requestId = "1";
207 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
211 private Map<String,Object> setTTLInFlags( String value){
212 Map<String,Object> flags = new HashMap<String,Object>();
213 if( value != null || !("".equalsIgnoreCase(value))){
214 flags.put(TTL_FLAG, value);
220 private CommandExecutorInput getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
221 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
223 return pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
226 private CommandExecutorInput getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
227 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
229 return pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
232 public WorkflowResponse getWorkflowResponse (){
233 WorkflowResponse wfResponse = new WorkflowResponse();
234 ResponseContext responseContext = createResponseContextWithSuObjects();
235 wfResponse.setResponseContext(responseContext);
236 responseContext.setPayload("");
237 wfResponse.getResponseContext().setStatus(new Status(100, null));
241 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){
242 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
243 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
244 RequestContext requestContext = runtimeContext.getRequestContext();
245 ResponseContext responseContext = createResponseContextWithSuObjects();
246 runtimeContext.setResponseContext(responseContext);
248 requestContext.getCommonHeader().getFlags().setTtl(ttl);
249 requestContext.getCommonHeader().setApiVer(apiVersion);
250 requestContext.getCommonHeader().setTimestamp(timeStamp);
251 requestContext.getCommonHeader().setRequestId(requestId);
252 requestContext.getCommonHeader().setSubRequestId(subRequestID);
253 requestContext.getCommonHeader().setOriginatorId(originatorID);
254 requestContext.setAction(action);
255 requestContext.setPayload(payload);
256 requestContext.getActionIdentifiers().setVnfId(vnfId);
257 VNFContext vnfContext = runtimeContext.getVnfContext();
258 vnfContext.setType(vnfType);
259 vnfContext.setId(vnfId);
260 vnfContext.setVersion(vnfVersion);
261 return commandExecutorInput;
264 private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
265 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
266 return new CommandExecutorInput(runtimeContext, 0);
269 private RuntimeContext createRuntimeContextWithSubObjects() {
270 RuntimeContext runtimeContext = new RuntimeContext();
271 RequestContext requestContext = new RequestContext();
272 runtimeContext.setRequestContext(requestContext);
273 CommonHeader commonHeader = new CommonHeader();
274 requestContext.setCommonHeader(commonHeader);
275 Flags flags = new Flags();
276 commonHeader.setFlags(flags);
277 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
278 requestContext.setActionIdentifiers(actionIdentifiers);
279 VNFContext vnfContext = new VNFContext();
280 runtimeContext.setVnfContext(vnfContext);
281 return runtimeContext;
285 private ResponseContext createResponseContextWithSuObjects(){
286 ResponseContext responseContext = new ResponseContext();
287 CommonHeader commonHeader = new CommonHeader();
288 Flags flags = new Flags();
289 responseContext.setCommonHeader(commonHeader);
290 responseContext.setStatus(new Status(0, null));
291 commonHeader.setFlags(flags);
292 return responseContext;