2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 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.openecomp.appc.domainmodel.lcm.ActionIdentifiers;
32 import org.openecomp.appc.domainmodel.lcm.CommonHeader;
33 import org.openecomp.appc.domainmodel.lcm.Flags;
34 import org.openecomp.appc.domainmodel.lcm.RequestContext;
35 import org.openecomp.appc.domainmodel.lcm.ResponseContext;
36 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
37 import org.openecomp.appc.domainmodel.lcm.Status;
38 import org.openecomp.appc.domainmodel.lcm.VNFContext;
39 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
40 import org.openecomp.appc.executor.impl.CommandTask;
41 import org.openecomp.appc.executor.impl.CommandTaskFactory;
42 import org.openecomp.appc.executor.impl.LCMCommandTask;
43 import org.openecomp.appc.executor.impl.LCMReadonlyCommandTask;
44 import org.openecomp.appc.executor.objects.CommandResponse;
45 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
46 import org.openecomp.appc.requesthandler.RequestHandler;
47 import org.openecomp.appc.workflow.WorkFlowManager;
48 import org.openecomp.appc.workflow.objects.WorkflowResponse;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
51 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
52 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
53 import org.osgi.framework.Bundle;
54 import org.osgi.framework.BundleContext;
55 import org.osgi.framework.FrameworkUtil;
56 import org.osgi.framework.ServiceReference;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.core.classloader.annotations.PrepareForTest;
59 import org.powermock.modules.junit4.PowerMockRunner;
61 import java.time.Instant;
63 import static junit.framework.Assert.assertEquals;
64 import static org.mockito.Matchers.*;
66 @SuppressWarnings("unchecked")
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
69 public class CommandExecutionTaskTest {
71 private final String TTL_FLAG= "TTL";
72 private final String API_VERSION= "2.0.0";
73 private final String ORIGINATOR_ID= "1";
74 private CommandTaskFactory factory ;
76 private RequestHandler requestHandler;
77 private WorkFlowManager workflowManager;
78 private AAIService aaiService;
79 private LifecycleManager lifecyclemanager;
81 private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
82 private final Bundle bundleService=Mockito.mock(Bundle.class);
83 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
86 public void init() throws SvcLogicException {
89 AAIService aaiService = Mockito.mock(AAIService.class);
90 PowerMockito.mockStatic(FrameworkUtil.class);
91 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
92 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
93 PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
94 PowerMockito.when(bundleContext.<AAIService>getService(sref)).thenReturn(aaiService);
95 PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),
96 anyString(), anyObject())).thenAnswer(invocation -> {
97 Object[] args = invocation.getArguments();
98 SvcLogicContext ctx =(SvcLogicContext)args[6];
99 String prefix = (String)args[4];
100 String key = (String)args[3];
101 if(key.contains("'28'")){
102 return SvcLogicResource.QueryStatus.FAILURE ;
103 }else if ( key.contains("'8'")) {
104 return SvcLogicResource.QueryStatus.NOT_FOUND ;
106 ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
107 ctx.setAttribute(prefix + ".orchestration-status", "INSTANTIATED");
109 return SvcLogicResource.QueryStatus.SUCCESS ;
111 PowerMockito.when(aaiService.update(anyString(), anyString(), anyObject(), anyString(),
112 anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
114 requestHandler = Mockito.mock(RequestHandler.class);
115 workflowManager = Mockito.mock(WorkFlowManager.class);
116 lifecyclemanager = Mockito.mock(LifecycleManager.class );
118 factory = new CommandTaskFactory();
119 factory.setLifecyclemanager(lifecyclemanager);
120 factory.setWorkflowManager(workflowManager);
121 factory.setVnfRequestHandler(requestHandler);
122 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse ());
127 public void testFactory(){
129 Instant timeStamp = Instant.now();
130 String requestId = "1";
131 RuntimeContext commandExecutorInputConfigure = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
132 timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure,"15","") ;
133 task = factory.getExecutionTask(commandExecutorInputConfigure);
134 assertEquals(LCMCommandTask.class,task.getClass() );
135 RuntimeContext commandExecutorInputSync = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
136 timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync,"15","") ;
137 task = factory.getExecutionTask(commandExecutorInputSync);
138 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
145 public void testOnRequestCompletion(){
146 Mockito.doNothing().when(requestHandler).onRequestTTLEnd(anyObject(),anyBoolean());
147 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
148 Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Configure,
150 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11",
152 LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
153 executionTask.onRequestCompletion(response);
157 public void testRunGetConfig(){
158 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
159 Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync,
161 LCMReadonlyCommandTask readonlyCommandTask = new LCMReadonlyCommandTask(
162 request, requestHandler,workflowManager);
163 readonlyCommandTask.run();
167 public void testRun(){
168 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
169 Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync,
171 LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
176 public void testRunNegative(){
177 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
178 Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync,
180 LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
185 private CommandResponse getCommandResponse(VNFOperation action,
190 RuntimeContext runtimeContext = new RuntimeContext();
191 ResponseContext responseContext = new ResponseContext();
192 runtimeContext.setResponseContext(responseContext);
193 RequestContext requestContext = new RequestContext();
194 runtimeContext.setRequestContext(requestContext);
195 CommonHeader commonHeader = new CommonHeader();
196 requestContext.setCommonHeader(commonHeader);
197 responseContext.setCommonHeader(commonHeader);
198 commonHeader.setFlags(new Flags(null, false, 0));
199 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
200 requestContext.setActionIdentifiers(actionIdentifiers);
201 VNFContext vnfContext = new VNFContext();
202 runtimeContext.setVnfContext(vnfContext);
203 requestContext.setAction(action);
204 runtimeContext.setRpcName(action.name().toLowerCase());
205 commonHeader.setApiVer(API_VERSION);
206 responseContext.setStatus(new Status(100, null));
207 commonHeader.setRequestId(responseId);
208 responseContext.setPayload(payload);
209 commonHeader.setTimestamp(Instant.now());
210 vnfContext.setId(vnfId);
211 return new CommandResponse(runtimeContext);
217 public void testPositiveFlow_configure() {
219 String requestId = "1";
221 pouplateCommandExecutorInput("FIREWALL",30,
222 "1.0", Instant.now(), API_VERSION, requestId, ORIGINATOR_ID, "",
223 VNFOperation.Configure, "33", "");
226 public WorkflowResponse getWorkflowResponse (){
227 WorkflowResponse wfResponse = new WorkflowResponse();
228 ResponseContext responseContext = createResponseContextWithSuObjects();
229 wfResponse.setResponseContext(responseContext);
230 responseContext.setPayload("");
231 wfResponse.getResponseContext().setStatus(new Status(100, null));
235 private RuntimeContext pouplateCommandExecutorInput(String vnfType,
246 RuntimeContext commandExecutorInput = createCommandExecutorInputWithSubObjects();
247 RequestContext requestContext = commandExecutorInput.getRequestContext();
248 ResponseContext responseContext = createResponseContextWithSuObjects();
249 commandExecutorInput.setResponseContext(responseContext);
251 requestContext.getCommonHeader().setFlags(new Flags(null, false, ttl));
252 requestContext.getCommonHeader().setApiVer(apiVersion);
253 requestContext.getCommonHeader().setTimestamp(timeStamp);
254 requestContext.getCommonHeader().setRequestId(requestId);
255 requestContext.getCommonHeader().setSubRequestId(subRequestID);
256 requestContext.getCommonHeader().setOriginatorId(originatorID);
257 requestContext.setAction(action);
258 requestContext.setPayload(payload);
259 requestContext.getActionIdentifiers().setVnfId(vnfId);
260 VNFContext vnfContext = commandExecutorInput.getVnfContext();
261 vnfContext.setType(vnfType);
262 vnfContext.setId(vnfId);
263 vnfContext.setVersion(vnfVersion);
264 return commandExecutorInput;
267 private RuntimeContext createCommandExecutorInputWithSubObjects() {
268 return createRuntimeContextWithSubObjects();
271 private RuntimeContext createRuntimeContextWithSubObjects() {
272 RuntimeContext runtimeContext = new RuntimeContext();
273 RequestContext requestContext = new RequestContext();
274 runtimeContext.setRequestContext(requestContext);
275 CommonHeader commonHeader = new CommonHeader();
276 requestContext.setCommonHeader(commonHeader);
277 commonHeader.setFlags(new Flags(null, false, 0));
278 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
279 requestContext.setActionIdentifiers(actionIdentifiers);
280 VNFContext vnfContext = new VNFContext();
281 runtimeContext.setVnfContext(vnfContext);
282 return runtimeContext;
286 private ResponseContext createResponseContextWithSuObjects(){
287 ResponseContext responseContext = new ResponseContext();
288 CommonHeader commonHeader = new CommonHeader();
289 responseContext.setCommonHeader(commonHeader);
290 responseContext.setStatus(new Status(0, null));
291 commonHeader.setFlags(new Flags(null, false, 0));
292 return responseContext;