2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.executor.impl;
28 import static org.hamcrest.CoreMatchers.allOf;
29 import static org.hamcrest.CoreMatchers.is;
30 import static org.hamcrest.CoreMatchers.isA;
31 import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
32 import static org.mockito.Matchers.anyObject;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.appc.domainmodel.lcm.*;
41 import org.onap.appc.executor.impl.CommandTask;
42 import org.onap.appc.executor.impl.objects.CommandRequest;
43 import org.onap.appc.executor.objects.CommandExecutorInput;
44 import org.onap.appc.requesthandler.RequestHandler;
45 import org.onap.appc.workflow.WorkFlowManager;
46 import org.onap.appc.workflow.objects.WorkflowResponse;
47 import org.onap.ccsdk.sli.adaptors.aai.AAIRequest;
48 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
49 import org.onap.ccsdk.sli.adaptors.aai.AAIServiceException;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
51 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
52 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
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;
60 import org.powermock.reflect.Whitebox;
61 import java.net.InetAddress;
62 import java.net.UnknownHostException;
63 import java.util.Date;
64 import java.util.HashMap;
70 * @since September 04, 2017
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({FrameworkUtil.class, InetAddress.class})
74 public class TestCommandTask {
76 private RequestHandler requestHandler;
77 private WorkFlowManager workflowManager;
78 private AAIService aaiService;
80 private BundleContext bundleContext = Mockito.mock(BundleContext.class);
81 private Bundle bundleService = Mockito.mock(Bundle.class);
82 private ServiceReference sref = Mockito.mock(ServiceReference.class);
84 private static final String TTL_FLAG= "TTL";
85 private static final String API_VERSION= "2.0.0";
86 private static final String ORIGINATOR_ID= "1";
89 public ExpectedException expectedEx = ExpectedException.none();
92 public void init() throws UnknownHostException{
93 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 requestHandler = Mockito.mock(RequestHandler.class);
100 workflowManager = Mockito.mock(WorkFlowManager.class);
101 task = Mockito.spy(new CommandTask(requestHandler, workflowManager));
105 public void testRunPositive(){
106 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
107 task.setWorkflowManager(workflowManager);
108 task.setRequestHandler(requestHandler);
109 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "11", setTTLInFlags("30"), VNFOperation.Sync, "1", "1.0"));
111 Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
115 public void testRunPositiveTerminateFailed(){
116 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
117 task.setWorkflowManager(workflowManager);
118 task.setRequestHandler(requestHandler);
119 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0"));
120 setResponseContext(300, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
122 Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
126 public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException{
127 AAIService mockAai = Mockito.mock(AAIService.class);
129 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
130 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
131 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
132 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
133 Whitebox.setInternalState(task, "aaiService", mockAai);
134 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
135 task.setWorkflowManager(workflowManager);
136 task.setRequestHandler(requestHandler);
137 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0"));
138 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
143 public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException{
144 AAIService mockAai = Mockito.mock(AAIService.class);
146 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
147 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
148 .thenReturn(SvcLogicResource.QueryStatus.NOT_FOUND);
149 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
150 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
151 Whitebox.setInternalState(task, "aaiService", mockAai);
152 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
153 task.setWorkflowManager(workflowManager);
154 task.setRequestHandler(requestHandler);
155 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0"));
156 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
157 expectedEx.expect(RuntimeException.class);
158 expectedEx.expectMessage("VNF not found for vnf_id = ");
163 public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException{
164 AAIService mockAai = Mockito.mock(AAIService.class);
166 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
167 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
168 .thenReturn(SvcLogicResource.QueryStatus.FAILURE);
169 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
170 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
171 Whitebox.setInternalState(task, "aaiService", mockAai);
172 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
173 task.setWorkflowManager(workflowManager);
174 task.setRequestHandler(requestHandler);
175 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0"));
176 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
177 expectedEx.expect(RuntimeException.class);
178 expectedEx.expectMessage("Error Querying AAI with vnfID = ");
183 public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException{
184 AAIService mockAai = Mockito.mock(AAIService.class);
186 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
187 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
188 .thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
189 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
190 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenThrow(new AAIServiceException("ERROR IN DELETE"));
191 Whitebox.setInternalState(task, "aaiService", mockAai);
192 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
193 task.setWorkflowManager(workflowManager);
194 task.setRequestHandler(requestHandler);
195 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0"));
196 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
197 expectedEx.expect(RuntimeException.class);
198 expectedEx.expectCause(allOf(isA(AAIServiceException.class),
199 hasProperty("message", is("ERROR IN DELETE"))));
204 public void testRunSvcLogicException() throws SvcLogicException, AAIServiceException {
205 AAIService mockAai = Mockito.mock(AAIService.class);
207 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
208 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
209 .thenThrow(new SvcLogicException());
210 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
211 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
212 Whitebox.setInternalState(task, "aaiService", mockAai);
213 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
214 task.setWorkflowManager(workflowManager);
215 task.setRequestHandler(requestHandler);
216 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0"));
217 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
218 expectedEx.expect(RuntimeException.class);
219 expectedEx.expectCause(isA(SvcLogicException.class));
223 private WorkflowResponse getWorkflowResponse (){
224 WorkflowResponse wfResponse = new WorkflowResponse();
225 ResponseContext responseContext = createResponseContextWithObjects();
226 wfResponse.setResponseContext(responseContext);
227 responseContext.setPayload("");
228 wfResponse.getResponseContext().getStatus().setCode(100);
232 private ResponseContext createResponseContextWithObjects(){
233 ResponseContext responseContext = new ResponseContext();
234 CommonHeader commonHeader = new CommonHeader();
235 Flags flags = new Flags();
236 Status status = new Status();
237 responseContext.setCommonHeader(commonHeader);
238 responseContext.setStatus(status);
239 commonHeader.setFlags(flags);
240 return responseContext;
243 private void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){
244 ResponseContext responseContext = createResponseContextWithObjects();
245 responseContext.getStatus().setCode(statusCode);
246 runtimeContext.setResponseContext(responseContext);
249 private CommandRequest getCommandRequest(String vnfType, Integer ttl, Date timeStamp, String requestId,
250 Map<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion ){
252 CommandExecutorInput commandExecutorInput =
253 pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
254 CommandRequest request = new CommandRequest(commandExecutorInput);
255 request.setCommandExecutorInput(commandExecutorInput);
256 request.setCommandInTimeStamp(new Date());
260 private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion,
261 String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){
262 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
263 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
264 RequestContext requestContext = runtimeContext.getRequestContext();
265 ResponseContext responseContext = createResponseContextWithSuObjects();
266 runtimeContext.setResponseContext(responseContext);
268 requestContext.getCommonHeader().getFlags().setTtl(ttl);
269 requestContext.getCommonHeader().setApiVer(apiVersion);
270 requestContext.getCommonHeader().setTimestamp(timeStamp);
271 requestContext.getCommonHeader().setRequestId(requestId);
272 requestContext.getCommonHeader().setSubRequestId(subRequestID);
273 requestContext.getCommonHeader().setOriginatorId(originatorID);
274 requestContext.setAction(action);
275 requestContext.setPayload(payload);
276 requestContext.getActionIdentifiers().setVnfId(vnfId);
277 requestContext.getActionIdentifiers().setServiceInstanceId("test");
278 VNFContext vnfContext = runtimeContext.getVnfContext();
279 vnfContext.setType(vnfType);
280 vnfContext.setId(vnfId);
281 vnfContext.setVersion(vnfVersion);
282 return commandExecutorInput;
285 private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
286 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
287 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
288 commandExecutorInput.setRuntimeContext(runtimeContext);
289 return commandExecutorInput;
292 private ResponseContext createResponseContextWithSuObjects(){
293 ResponseContext responseContext = new ResponseContext();
294 CommonHeader commonHeader = new CommonHeader();
295 Flags flags = new Flags();
296 Status status = new Status();
297 responseContext.setCommonHeader(commonHeader);
298 responseContext.setStatus(status);
299 commonHeader.setFlags(flags);
300 return responseContext;
303 private RuntimeContext createRuntimeContextWithSubObjects() {
304 RuntimeContext runtimeContext = new RuntimeContext();
305 RequestContext requestContext = new RequestContext();
306 runtimeContext.setRequestContext(requestContext);
307 CommonHeader commonHeader = new CommonHeader();
308 requestContext.setCommonHeader(commonHeader);
309 Flags flags = new Flags();
310 commonHeader.setFlags(flags);
311 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
312 requestContext.setActionIdentifiers(actionIdentifiers);
313 VNFContext vnfContext = new VNFContext();
314 runtimeContext.setVnfContext(vnfContext);
315 return runtimeContext;
318 private Map<String,Object> setTTLInFlags( String value){
319 Map<String,Object> flags = new HashMap<String,Object>();
320 if( value != null || !("".equalsIgnoreCase(value))){
321 flags.put(TTL_FLAG, value);