Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / onap / appc / executor / impl / TestCommandTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.executor.impl;
27
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;
65 import java.util.Map;
66
67
68 /**
69  * @author sushilma
70  * @since September 04, 2017
71  */
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({FrameworkUtil.class, InetAddress.class})
74 public class TestCommandTask {
75     CommandTask task;
76     private RequestHandler requestHandler;
77     private WorkFlowManager workflowManager;
78     private AAIService aaiService;
79
80     private BundleContext bundleContext = Mockito.mock(BundleContext.class);
81     private Bundle bundleService = Mockito.mock(Bundle.class);
82     private ServiceReference sref = Mockito.mock(ServiceReference.class);
83
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";
87
88     @Rule
89     public ExpectedException expectedEx = ExpectedException.none();
90
91     @Before
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));
102     }
103
104     @Test
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"));
111         task.run();
112         Assert.assertNotNull(
113                 task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
114     }
115
116     @Test
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());
124         task.run();
125         Assert.assertNotNull(
126                 task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext());
127     }
128
129     @Test
130     public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException {
131         AAIService mockAai = Mockito.mock(AAIService.class);
132
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());
145         task.run();
146     }
147
148     @Test
149     public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException {
150         AAIService mockAai = Mockito.mock(AAIService.class);
151
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 = ");
166         task.run();
167     }
168
169     @Test
170     public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException {
171         AAIService mockAai = Mockito.mock(AAIService.class);
172
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 = ");
187         task.run();
188     }
189
190     @Test
191     public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException {
192         AAIService mockAai = Mockito.mock(AAIService.class);
193
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"))));
210         task.run();
211     }
212
213     @Test
214     public void testRunSvcLogicException() throws SvcLogicException, AAIServiceException {
215         AAIService mockAai = Mockito.mock(AAIService.class);
216
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));
231         task.run();
232     }
233
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);
240         return wfResponse;
241     }
242
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;
252     }
253
254     private void setResponseContext(int statusCode, RuntimeContext runtimeContext) {
255         ResponseContext responseContext = createResponseContextWithObjects();
256         responseContext.getStatus().setCode(statusCode);
257         runtimeContext.setResponseContext(responseContext);
258     }
259
260     private CommandRequest getCommandRequest(String vnfType, Integer ttl, Date timeStamp, String requestId,
261             Map<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion) {
262
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());
268         return request;
269     }
270
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);
280
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;
297     }
298
299     private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
300         CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
301         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
302         commandExecutorInput.setRuntimeContext(runtimeContext);
303         return commandExecutorInput;
304     }
305
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;
315     }
316
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;
330     }
331
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);
336         }
337         return flags;
338     }
339 }