Merge "Refactor Status 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<? extends CommandRequest> task = factory.getExecutionTask("Configure");
136                 assertEquals(LCMCommandTask.class,task.getClass() );
137                 task = factory.getExecutionTask("Sync");
138                 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
139
140         }
141
142
143
144         @Test
145         public void testOnRequestCompletion(){
146                 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
147                 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"), VNFOperation.Configure, "1", "1.0");
148                 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
149                 executionTask.onRequestCompletion(request, response);
150         }
151
152         @Test
153         public void testRunGetConfig(){
154                 LCMReadOnlyCommandRequest request = getConfigCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
155                 LCMReadonlyCommandTask.setCommandRequest(request);
156                 LCMReadonlyCommandTask.run();
157         }
158
159         @Test
160         public void testRun(){
161                 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
162                 executionTask.setCommandRequest(request);
163                 executionTask.run();
164         }
165
166         @Test
167         public void testRunNegative(){
168                 LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
169                 executionTask.setCommandRequest(request);
170                 executionTask.run();
171         }
172
173
174         CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
175                 CommandResponse commandResponse = new CommandResponse();
176                 RuntimeContext runtimeContext = new RuntimeContext();
177                 commandResponse.setRuntimeContext(runtimeContext);
178                 ResponseContext responseContext = new ResponseContext();
179                 runtimeContext.setResponseContext(responseContext);
180                 RequestContext requestContext = new RequestContext();
181                 runtimeContext.setRequestContext(requestContext);
182                 CommonHeader commonHeader = new CommonHeader();
183                 requestContext.setCommonHeader(commonHeader);
184                 responseContext.setCommonHeader(commonHeader);
185                 Flags flags = new Flags();
186                 commonHeader.setFlags(flags);
187                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
188                 requestContext.setActionIdentifiers(actionIdentifiers);
189                 VNFContext vnfContext = new VNFContext();
190                 runtimeContext.setVnfContext(vnfContext);
191                 requestContext.setAction(action);
192                 runtimeContext.setRpcName(action.name().toLowerCase());
193                 commonHeader.setApiVer(API_VERSION);
194                 responseContext.setStatus(new Status(100, null));
195                 commonHeader.setRequestId(responseId);
196                 responseContext.setPayload(payload);
197                 commonHeader.setTimestamp(new Date());
198                 vnfContext.setId(vnfId);
199                 return commandResponse;
200         }
201
202
203
204         @Test
205         public void testPositiveFlow_configure()  {
206
207                 Date timeStamp = new Date();
208                 String requestId = "1";
209
210                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
211                 CommandRequest request = new CommandRequest(commandExecutorInput);
212         }
213
214
215         private Map<String,Object> setTTLInFlags( String value){
216                 Map<String,Object> flags = new HashMap<String,Object>();
217                 if( value != null || !("".equalsIgnoreCase(value))){
218                         flags.put(TTL_FLAG, value);
219                 }
220                 return flags;
221         }
222
223
224         private LCMReadOnlyCommandRequest getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
225                                                                                                                           Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
226
227                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
228                 LCMReadOnlyCommandRequest request = new LCMReadOnlyCommandRequest(commandExecutorInput);
229
230                 return request;
231         }
232
233         private LCMCommandRequest getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
234                                                                                          Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
235
236                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
237                 LCMCommandRequest request = new LCMCommandRequest(commandExecutorInput);
238
239                 return request;
240         }
241
242         public WorkflowResponse getWorkflowResponse (){
243                 WorkflowResponse wfResponse = new WorkflowResponse();
244                 ResponseContext responseContext = createResponseContextWithSuObjects();
245                 wfResponse.setResponseContext(responseContext);
246                 responseContext.setPayload("");
247                 wfResponse.getResponseContext().setStatus(new Status(100, null));
248                 return wfResponse;
249         }
250
251         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){
252                 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
253                 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
254                 RequestContext requestContext = runtimeContext.getRequestContext();
255                 ResponseContext responseContext = createResponseContextWithSuObjects();
256                 runtimeContext.setResponseContext(responseContext);
257
258                 requestContext.getCommonHeader().getFlags().setTtl(ttl);
259                 requestContext.getCommonHeader().setApiVer(apiVersion);
260                 requestContext.getCommonHeader().setTimestamp(timeStamp);
261                 requestContext.getCommonHeader().setRequestId(requestId);
262                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
263                 requestContext.getCommonHeader().setOriginatorId(originatorID);
264                 requestContext.setAction(action);
265                 requestContext.setPayload(payload);
266                 requestContext.getActionIdentifiers().setVnfId(vnfId);
267                 VNFContext vnfContext = runtimeContext.getVnfContext();
268                 vnfContext.setType(vnfType);
269                 vnfContext.setId(vnfId);
270                 vnfContext.setVersion(vnfVersion);
271                 return commandExecutorInput;
272         }
273
274         private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
275                 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
276                 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
277                 commandExecutorInput.setRuntimeContext(runtimeContext);
278                 return commandExecutorInput;
279         }
280
281         private RuntimeContext createRuntimeContextWithSubObjects() {
282                 RuntimeContext runtimeContext = new RuntimeContext();
283                 RequestContext requestContext = new RequestContext();
284                 runtimeContext.setRequestContext(requestContext);
285                 CommonHeader commonHeader = new CommonHeader();
286                 requestContext.setCommonHeader(commonHeader);
287                 Flags flags = new Flags();
288                 commonHeader.setFlags(flags);
289                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
290                 requestContext.setActionIdentifiers(actionIdentifiers);
291                 VNFContext vnfContext = new VNFContext();
292                 runtimeContext.setVnfContext(vnfContext);
293                 return runtimeContext;
294
295         }
296
297         private ResponseContext createResponseContextWithSuObjects(){
298                 ResponseContext responseContext = new ResponseContext();
299                 CommonHeader commonHeader = new CommonHeader();
300                 Flags flags = new Flags();
301                 responseContext.setCommonHeader(commonHeader);
302                 responseContext.setStatus(new Status(0, null));
303                 commonHeader.setFlags(flags);
304                 return responseContext;
305         }
306
307 }
308