2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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"),
110 VNFOperation.Sync, "1", "1.0"));
112 Assert.assertNotNull(
113 task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
117 public void testRunPositiveTerminateFailed() {
118 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
119 task.setWorkflowManager(workflowManager);
120 task.setRequestHandler(requestHandler);
121 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"),
122 VNFOperation.Terminate, "2", "1.0"));
123 setResponseContext(300, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
125 Assert.assertNotNull(
126 task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
130 public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException {
131 AAIService mockAai = Mockito.mock(AAIService.class);
133 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
134 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
135 .thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
136 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
137 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
138 Whitebox.setInternalState(task, "aaiService", mockAai);
139 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
140 task.setWorkflowManager(workflowManager);
141 task.setRequestHandler(requestHandler);
142 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"),
143 VNFOperation.Terminate, "2", "1.0"));
144 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
149 public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException {
150 AAIService mockAai = Mockito.mock(AAIService.class);
152 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
153 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
154 .thenReturn(SvcLogicResource.QueryStatus.NOT_FOUND);
155 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
156 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
157 Whitebox.setInternalState(task, "aaiService", mockAai);
158 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
159 task.setWorkflowManager(workflowManager);
160 task.setRequestHandler(requestHandler);
161 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"),
162 VNFOperation.Terminate, "2", "1.0"));
163 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
164 expectedEx.expect(RuntimeException.class);
165 expectedEx.expectMessage("VNF not found for vnf_id = ");
170 public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException {
171 AAIService mockAai = Mockito.mock(AAIService.class);
173 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
174 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
175 .thenReturn(SvcLogicResource.QueryStatus.FAILURE);
176 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
177 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
178 Whitebox.setInternalState(task, "aaiService", mockAai);
179 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
180 task.setWorkflowManager(workflowManager);
181 task.setRequestHandler(requestHandler);
182 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"),
183 VNFOperation.Terminate, "2", "1.0"));
184 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
185 expectedEx.expect(RuntimeException.class);
186 expectedEx.expectMessage("Error Querying AAI with vnfID = ");
191 public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException {
192 AAIService mockAai = Mockito.mock(AAIService.class);
194 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
195 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
196 .thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
197 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
198 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString()))
199 .thenThrow(new AAIServiceException("ERROR IN DELETE"));
200 Whitebox.setInternalState(task, "aaiService", mockAai);
201 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
202 task.setWorkflowManager(workflowManager);
203 task.setRequestHandler(requestHandler);
204 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"),
205 VNFOperation.Terminate, "2", "1.0"));
206 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
207 expectedEx.expect(RuntimeException.class);
208 expectedEx.expectCause(allOf(isA(AAIServiceException.class),
209 hasProperty("message", is("ERROR IN DELETE"))));
214 public void testRunSvcLogicException() throws SvcLogicException, AAIServiceException {
215 AAIService mockAai = Mockito.mock(AAIService.class);
217 Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
218 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class)))
219 .thenThrow(new SvcLogicException());
220 Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class));
221 Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true);
222 Whitebox.setInternalState(task, "aaiService", mockAai);
223 Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse());
224 task.setWorkflowManager(workflowManager);
225 task.setRequestHandler(requestHandler);
226 task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"),
227 VNFOperation.Terminate, "2", "1.0"));
228 setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext());
229 expectedEx.expect(RuntimeException.class);
230 expectedEx.expectCause(isA(SvcLogicException.class));
234 private WorkflowResponse getWorkflowResponse () {
235 WorkflowResponse wfResponse = new WorkflowResponse();
236 ResponseContext responseContext = createResponseContextWithObjects();
237 wfResponse.setResponseContext(responseContext);
238 responseContext.setPayload("");
239 wfResponse.getResponseContext().getStatus().setCode(100);
243 private ResponseContext createResponseContextWithObjects() {
244 ResponseContext responseContext = new ResponseContext();
245 CommonHeader commonHeader = new CommonHeader();
246 Flags flags = new Flags();
247 Status status = new Status();
248 responseContext.setCommonHeader(commonHeader);
249 responseContext.setStatus(status);
250 commonHeader.setFlags(flags);
251 return responseContext;
254 private void setResponseContext(int statusCode, RuntimeContext runtimeContext) {
255 ResponseContext responseContext = createResponseContextWithObjects();
256 responseContext.getStatus().setCode(statusCode);
257 runtimeContext.setResponseContext(responseContext);
260 private CommandRequest getCommandRequest(String vnfType, Integer ttl, Date timeStamp, String requestId,
261 Map<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion) {
263 CommandExecutorInput commandExecutorInput = populateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp,
264 API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
265 CommandRequest request = new CommandRequest(commandExecutorInput);
266 request.setCommandExecutorInput(commandExecutorInput);
267 request.setCommandInTimeStamp(new Date());
271 private CommandExecutorInput populateCommandExecutorInput(String vnfType, int ttl, String vnfVersion,
272 Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID,
273 VNFOperation action, String vnfId, String payload) {
274 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
275 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
276 RequestContext requestContext = runtimeContext.getRequestContext();
277 CommonHeader commonHeader = requestContext.getCommonHeader();
278 ResponseContext responseContext = createResponseContextWithSuObjects();
279 runtimeContext.setResponseContext(responseContext);
281 commonHeader.getFlags().setTtl(ttl);
282 commonHeader.setApiVer(apiVersion);
283 commonHeader.setTimestamp(timeStamp);
284 commonHeader.setRequestId(requestId);
285 commonHeader.setSubRequestId(subRequestID);
286 commonHeader.setOriginatorId(originatorID);
287 requestContext.setAction(action);
288 requestContext.setPayload(payload);
289 ActionIdentifiers actionIdentifiers = requestContext.getActionIdentifiers();
290 actionIdentifiers.setVnfId(vnfId);
291 actionIdentifiers.setServiceInstanceId("test");
292 VNFContext vnfContext = runtimeContext.getVnfContext();
293 vnfContext.setType(vnfType);
294 vnfContext.setId(vnfId);
295 vnfContext.setVersion(vnfVersion);
296 return commandExecutorInput;
299 private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
300 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
301 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
302 commandExecutorInput.setRuntimeContext(runtimeContext);
303 return commandExecutorInput;
306 private ResponseContext createResponseContextWithSuObjects() {
307 ResponseContext responseContext = new ResponseContext();
308 CommonHeader commonHeader = new CommonHeader();
309 Flags flags = new Flags();
310 Status status = new Status();
311 responseContext.setCommonHeader(commonHeader);
312 responseContext.setStatus(status);
313 commonHeader.setFlags(flags);
314 return responseContext;
317 private RuntimeContext createRuntimeContextWithSubObjects() {
318 RuntimeContext runtimeContext = new RuntimeContext();
319 RequestContext requestContext = new RequestContext();
320 runtimeContext.setRequestContext(requestContext);
321 CommonHeader commonHeader = new CommonHeader();
322 requestContext.setCommonHeader(commonHeader);
323 Flags flags = new Flags();
324 commonHeader.setFlags(flags);
325 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
326 requestContext.setActionIdentifiers(actionIdentifiers);
327 VNFContext vnfContext = new VNFContext();
328 runtimeContext.setVnfContext(vnfContext);
329 return runtimeContext;
332 private Map<String, Object> setTTLInFlags(String value) {
333 Map<String, Object> flags = new HashMap<String, Object>();
334 if (value != null || !("".equalsIgnoreCase(value))) {
335 flags.put(TTL_FLAG, value);