Refactor CommandExecutorInput to be immutable
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / openecomp / appc / executor / TestCommandExecutionTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.executor;
23 /**
24  *
25  */
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mockito;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.openecomp.appc.domainmodel.lcm.*;
34 import org.openecomp.appc.executor.impl.CommandTask;
35 import org.openecomp.appc.executor.impl.CommandTaskFactory;
36 import org.openecomp.appc.executor.impl.LCMCommandTask;
37 import org.openecomp.appc.executor.impl.LCMReadonlyCommandTask;
38 import org.openecomp.appc.executor.impl.objects.CommandRequest;
39 import org.openecomp.appc.executor.impl.objects.LCMCommandRequest;
40 import org.openecomp.appc.executor.impl.objects.LCMReadOnlyCommandRequest;
41 import org.openecomp.appc.executor.objects.*;
42 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
43 import org.openecomp.appc.requesthandler.RequestHandler;
44 import org.openecomp.appc.workflow.WorkFlowManager;
45 import org.openecomp.appc.workflow.objects.WorkflowRequest;
46 import org.openecomp.appc.workflow.objects.WorkflowResponse;
47 import org.openecomp.sdnc.sli.SvcLogicContext;
48 import org.openecomp.sdnc.sli.SvcLogicException;
49 import org.openecomp.sdnc.sli.SvcLogicResource;
50 import org.openecomp.sdnc.sli.aai.AAIService;
51 import org.osgi.framework.Bundle;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.osgi.framework.ServiceReference;
55 import org.powermock.api.mockito.PowerMockito;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
58
59 import java.util.Date;
60 import java.util.HashMap;
61 import java.util.Map;
62 import java.util.Properties;
63
64 import static junit.framework.Assert.assertEquals;
65 import static org.mockito.Matchers.*;
66
67
68
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
71 public class TestCommandExecutionTask {
72
73         private static final String TTL_FLAG= "TTL";
74         private static final String API_VERSION= "2.0.0";
75         private static final String ORIGINATOR_ID= "1";
76         private LCMCommandTask executionTask;
77         private LCMReadonlyCommandTask LCMReadonlyCommandTask;
78         private CommandTaskFactory factory ;
79
80         private RequestHandler requestHandler;
81         private WorkFlowManager workflowManager;
82         private AAIService aaiService;
83         private LifecycleManager lifecyclemanager;
84
85         private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
86         private final Bundle bundleService=Mockito.mock(Bundle.class);
87         private final ServiceReference sref=Mockito.mock(ServiceReference.class);
88
89         @Before
90         public void init() throws SvcLogicException {
91
92                 // ***
93                 AAIService 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                 PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
100                         @Override
101                         public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
102                                 Object[] args = invocation.getArguments();
103                                 SvcLogicContext ctx =(SvcLogicContext)args[6];
104                                 String prefix = (String)args[4];
105                                 String key = (String)args[3];
106                                 if(key.contains("'28'")){
107                                         return  SvcLogicResource.QueryStatus.FAILURE ;
108                                 }else if ( key.contains("'8'")) {
109                                         return  SvcLogicResource.QueryStatus.NOT_FOUND ;
110                                 }else {
111                                         ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
112                                         ctx.setAttribute(prefix + ".orchestration-status", "INSTANTIATED");
113                                 }
114                                 return  SvcLogicResource.QueryStatus.SUCCESS ;
115                         }
116                 });
117                 PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
118
119                 requestHandler =  Mockito.mock(RequestHandler.class);
120                 workflowManager = Mockito.mock(WorkFlowManager.class);
121                 lifecyclemanager = Mockito.mock(LifecycleManager.class );
122
123                 executionTask = new LCMCommandTask(requestHandler,workflowManager,lifecyclemanager);
124                 LCMReadonlyCommandTask = new LCMReadonlyCommandTask(requestHandler,workflowManager);
125                 factory = new CommandTaskFactory();
126                 factory.setLifecyclemanager(lifecyclemanager);
127                 factory.setWorkflowManager(workflowManager);
128                 factory.setRequestHandler(requestHandler);
129                 Mockito.when(workflowManager.executeWorkflow((WorkflowRequest)anyObject())).thenReturn(getWorkflowResponse () );
130         }
131
132
133         @Test
134         public void testFactory(){
135                 CommandTask task;
136                 task = factory.getExecutionTask("Configure");
137                 assertEquals(LCMCommandTask.class,task.getClass() );
138                 task = factory.getExecutionTask("Sync");
139                 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
140
141         }
142
143
144
145         @Test
146         public void testOnRequestCompletion(){
147                 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
148                 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"), VNFOperation.Configure, "1", "1.0");
149                 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
150                 executionTask.onRequestCompletion(request, response);
151         }
152
153         @Test
154         public void testRunGetConfig(){
155                 LCMReadOnlyCommandRequest request = getConfigCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
156                 LCMReadonlyCommandTask.setCommandRequest(request);
157                 LCMReadonlyCommandTask.run();
158         }
159
160         @Test
161         public void testRun(){
162                 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
163                 executionTask.setCommandRequest(request);
164                 executionTask.run();
165         }
166
167         @Test
168         public void testRunNegative(){
169                 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
170                 executionTask.setCommandRequest(request);
171                 executionTask.run();
172         }
173
174
175         CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
176                 CommandResponse commandResponse = new CommandResponse();
177                 RuntimeContext runtimeContext = new RuntimeContext();
178                 commandResponse.setRuntimeContext(runtimeContext);
179                 ResponseContext responseContext = new ResponseContext();
180                 runtimeContext.setResponseContext(responseContext);
181                 RequestContext requestContext = new RequestContext();
182                 runtimeContext.setRequestContext(requestContext);
183                 CommonHeader commonHeader = new CommonHeader();
184                 requestContext.setCommonHeader(commonHeader);
185                 responseContext.setCommonHeader(commonHeader);
186                 Flags flags = new Flags();
187                 commonHeader.setFlags(flags);
188                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
189                 requestContext.setActionIdentifiers(actionIdentifiers);
190                 VNFContext vnfContext = new VNFContext();
191                 runtimeContext.setVnfContext(vnfContext);
192                 requestContext.setAction(action);
193                 runtimeContext.setRpcName(action.name().toLowerCase());
194                 commonHeader.setApiVer(API_VERSION);
195                 Status status = new Status();
196                 status.setCode(100);
197                 responseContext.setStatus(status);
198                 commonHeader.setRequestId(responseId);
199                 responseContext.setPayload(payload);
200                 commonHeader.setTimestamp(new Date());
201                 vnfContext.setId(vnfId);
202                 return commandResponse;
203         }
204
205
206
207         @Test
208         public void testPositiveFlow_configure()  {
209
210                 Date timeStamp = new Date();
211                 String requestId = "1";
212
213                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
214                 CommandRequest request = new CommandRequest(commandExecutorInput);
215         }
216
217
218         private Map<String,Object> setTTLInFlags( String value){
219                 Map<String,Object> flags = new HashMap<String,Object>();
220                 if( value != null || !("".equalsIgnoreCase(value))){
221                         flags.put(TTL_FLAG, value);
222                 }
223                 return flags;
224         }
225
226
227         private LCMReadOnlyCommandRequest getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
228                                                                                                                           Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
229
230                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
231                 LCMReadOnlyCommandRequest request = new LCMReadOnlyCommandRequest(commandExecutorInput);
232
233                 return request;
234         }
235
236         private LCMCommandRequest getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
237                                                                                          Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
238
239                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
240                 LCMCommandRequest request = new LCMCommandRequest(commandExecutorInput);
241
242                 return request;
243         }
244
245         public WorkflowResponse getWorkflowResponse (){
246                 WorkflowResponse wfResponse = new WorkflowResponse();
247                 ResponseContext responseContext = createResponseContextWithSuObjects();
248                 wfResponse.setResponseContext(responseContext);
249                 responseContext.setPayload("");
250                 wfResponse.getResponseContext().getStatus().setCode(100);
251                 return wfResponse;
252         }
253
254         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){
255                 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
256                 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
257                 RequestContext requestContext = runtimeContext.getRequestContext();
258                 ResponseContext responseContext = createResponseContextWithSuObjects();
259                 runtimeContext.setResponseContext(responseContext);
260
261                 requestContext.getCommonHeader().getFlags().setTtl(ttl);
262                 requestContext.getCommonHeader().setApiVer(apiVersion);
263                 requestContext.getCommonHeader().setTimestamp(timeStamp);
264                 requestContext.getCommonHeader().setRequestId(requestId);
265                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
266                 requestContext.getCommonHeader().setOriginatorId(originatorID);
267                 requestContext.setAction(action);
268                 requestContext.setPayload(payload);
269                 requestContext.getActionIdentifiers().setVnfId(vnfId);
270                 VNFContext vnfContext = runtimeContext.getVnfContext();
271                 vnfContext.setType(vnfType);
272                 vnfContext.setId(vnfId);
273                 vnfContext.setVersion(vnfVersion);
274                 return commandExecutorInput;
275         }
276
277         private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
278                 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
279         return new CommandExecutorInput(runtimeContext, 0);
280         }
281
282         private RuntimeContext createRuntimeContextWithSubObjects() {
283                 RuntimeContext runtimeContext = new RuntimeContext();
284                 RequestContext requestContext = new RequestContext();
285                 runtimeContext.setRequestContext(requestContext);
286                 CommonHeader commonHeader = new CommonHeader();
287                 requestContext.setCommonHeader(commonHeader);
288                 Flags flags = new Flags();
289                 commonHeader.setFlags(flags);
290                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
291                 requestContext.setActionIdentifiers(actionIdentifiers);
292                 VNFContext vnfContext = new VNFContext();
293                 runtimeContext.setVnfContext(vnfContext);
294                 return runtimeContext;
295
296         }
297
298         private ResponseContext createResponseContextWithSuObjects(){
299                 ResponseContext responseContext = new ResponseContext();
300                 CommonHeader commonHeader = new CommonHeader();
301                 Flags flags = new Flags();
302                 Status status = new Status();
303                 responseContext.setCommonHeader(commonHeader);
304                 responseContext.setStatus(status);
305                 commonHeader.setFlags(flags);
306                 return responseContext;
307         }
308
309 }
310