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.onap.appc.executor;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.appc.domainmodel.lcm.*;
34 import org.onap.appc.executor.impl.CommandExecutorImpl;
35 import org.onap.appc.executor.impl.CommandTask;
36 import org.onap.appc.executor.impl.objects.CommandRequest;
37 import org.onap.appc.executor.objects.CommandExecutorInput;
38 import org.onap.appc.requesthandler.RequestHandler;
39 import org.onap.appc.workflow.WorkFlowManager;
40 import org.onap.appc.workflow.objects.WorkflowRequest;
41 import org.onap.appc.workflow.objects.WorkflowResponse;
42 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
43 import org.osgi.framework.*;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 import java.util.Date;
49 import java.util.HashMap;
52 import static org.mockito.Matchers.any;
53 import static org.mockito.Matchers.anyObject;
57 * @since September 04, 2017
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest({FrameworkUtil.class,AAIService.class,BundleContext.class,ServiceReference.class,
61 BundleReference.class,Bundle.class,Filter.class,BundleListener.class,InvalidSyntaxException.class,
62 BundleException.class,FrameworkListener.class,ServiceRegistration.class,ServiceListener.class,
64 public class TestCommandTask {
66 private RequestHandler requestHandler;
67 private WorkFlowManager workflowManager;
68 private CommandRequest commandRequest;
69 private AAIService aaiService;
71 private BundleContext bundleContext = Mockito.mock(BundleContext.class);
72 private Bundle bundleService = Mockito.mock(Bundle.class);
73 private ServiceReference sref = Mockito.mock(ServiceReference.class);
75 private static final String TTL_FLAG= "TTL";
76 private static final String API_VERSION= "2.0.0";
77 private static final String ORIGINATOR_ID= "1";
80 aaiService = Mockito.mock(AAIService.class);
81 /* PowerMockito.mockStatic(FrameworkUtil.class);
82 Mockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
83 Mockito.when(bundleContext.getService(any())).thenReturn(aaiService);
84 Mockito.when(FrameworkUtil.getBundle(AAIService.class).getBundleContext()).thenReturn(bundleContext);*/
86 PowerMockito.mockStatic(FrameworkUtil.class);
87 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
88 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
89 PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
90 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
93 requestHandler = Mockito.mock(RequestHandler.class);
94 workflowManager = Mockito.mock(WorkFlowManager.class);
95 task = new CommandTask(requestHandler ,workflowManager );
99 public void testRunPositive(){
100 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
101 task.setWorkflowManager(workflowManager);
102 task.setRequestHandler(requestHandler);
103 task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0"));
105 Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
109 public void testRunPositiveTerminateFailed(){
110 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
111 task.setWorkflowManager(workflowManager);
112 task.setRequestHandler(requestHandler);
113 task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "12" ,setTTLInFlags("30"),VNFOperation.Terminate, "2", "1.0"));
114 setResponseContext(300,task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
116 Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
121 public void testRunPositiveTerminateSuccess(){
122 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
123 task.setWorkflowManager(workflowManager);
124 task.setRequestHandler(requestHandler);
125 task.setCommandRequest(getCommandRequest("FIREWALL",30,new Date(), "12" ,setTTLInFlags("30"),VNFOperation.Terminate, "2", "1.0"));
126 setResponseContext(100,task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
128 Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
131 private WorkflowResponse getWorkflowResponse (){
132 WorkflowResponse wfResponse = new WorkflowResponse();
133 ResponseContext responseContext = createResponseContextWithObjects();
134 wfResponse.setResponseContext(responseContext);
135 responseContext.setPayload("");
136 wfResponse.getResponseContext().getStatus().setCode(100);
140 private ResponseContext createResponseContextWithObjects(){
141 ResponseContext responseContext = new ResponseContext();
142 CommonHeader commonHeader = new CommonHeader();
143 Flags flags = new Flags();
144 Status status = new Status();
145 responseContext.setCommonHeader(commonHeader);
146 responseContext.setStatus(status);
147 commonHeader.setFlags(flags);
148 return responseContext;
151 private void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){
152 ResponseContext responseContext = createResponseContextWithObjects();
153 responseContext.getStatus().setCode(statusCode);
154 runtimeContext.setResponseContext(responseContext);
157 private CommandRequest getCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
158 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
160 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
161 CommandRequest request = new CommandRequest(commandExecutorInput);
162 request.setCommandExecutorInput(commandExecutorInput);
163 request.setCommandInTimeStamp(new Date());
167 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){
168 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
169 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
170 RequestContext requestContext = runtimeContext.getRequestContext();
171 ResponseContext responseContext = createResponseContextWithSuObjects();
172 runtimeContext.setResponseContext(responseContext);
174 requestContext.getCommonHeader().getFlags().setTtl(ttl);
175 requestContext.getCommonHeader().setApiVer(apiVersion);
176 requestContext.getCommonHeader().setTimestamp(timeStamp);
177 requestContext.getCommonHeader().setRequestId(requestId);
178 requestContext.getCommonHeader().setSubRequestId(subRequestID);
179 requestContext.getCommonHeader().setOriginatorId(originatorID);
180 requestContext.setAction(action);
181 requestContext.setPayload(payload);
182 requestContext.getActionIdentifiers().setVnfId(vnfId);
183 requestContext.getActionIdentifiers().setServiceInstanceId("test");
184 VNFContext vnfContext = runtimeContext.getVnfContext();
185 vnfContext.setType(vnfType);
186 vnfContext.setId(vnfId);
187 vnfContext.setVersion(vnfVersion);
188 return commandExecutorInput;
191 private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
192 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
193 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
194 commandExecutorInput.setRuntimeContext(runtimeContext);
195 return commandExecutorInput;
198 private ResponseContext createResponseContextWithSuObjects(){
199 ResponseContext responseContext = new ResponseContext();
200 CommonHeader commonHeader = new CommonHeader();
201 Flags flags = new Flags();
202 Status status = new Status();
203 responseContext.setCommonHeader(commonHeader);
204 responseContext.setStatus(status);
205 commonHeader.setFlags(flags);
206 return responseContext;
209 private RuntimeContext createRuntimeContextWithSubObjects() {
210 RuntimeContext runtimeContext = new RuntimeContext();
211 RequestContext requestContext = new RequestContext();
212 runtimeContext.setRequestContext(requestContext);
213 CommonHeader commonHeader = new CommonHeader();
214 requestContext.setCommonHeader(commonHeader);
215 Flags flags = new Flags();
216 commonHeader.setFlags(flags);
217 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
218 requestContext.setActionIdentifiers(actionIdentifiers);
219 VNFContext vnfContext = new VNFContext();
220 runtimeContext.setVnfContext(vnfContext);
221 return runtimeContext;
224 private Map<String,Object> setTTLInFlags( String value){
225 Map<String,Object> flags = new HashMap<String,Object>();
226 if( value != null || !("".equalsIgnoreCase(value))){
227 flags.put(TTL_FLAG, value);