Parameterize raw generic type CommandTask<M>
[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                 Status status = new Status();
195                 status.setCode(100);
196                 responseContext.setStatus(status);
197                 commonHeader.setRequestId(responseId);
198                 responseContext.setPayload(payload);
199                 commonHeader.setTimestamp(new Date());
200                 vnfContext.setId(vnfId);
201                 return commandResponse;
202         }
203
204
205
206         @Test
207         public void testPositiveFlow_configure()  {
208
209                 Date timeStamp = new Date();
210                 String requestId = "1";
211
212                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
213                 CommandRequest request = new CommandRequest(commandExecutorInput);
214         }
215
216
217         private Map<String,Object> setTTLInFlags( String value){
218                 Map<String,Object> flags = new HashMap<String,Object>();
219                 if( value != null || !("".equalsIgnoreCase(value))){
220                         flags.put(TTL_FLAG, value);
221                 }
222                 return flags;
223         }
224
225
226         private LCMReadOnlyCommandRequest getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
227                                                                                                                           Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
228
229                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
230                 LCMReadOnlyCommandRequest request = new LCMReadOnlyCommandRequest(commandExecutorInput);
231
232                 return request;
233         }
234
235         private LCMCommandRequest getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
236                                                                                          Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){
237
238                 CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
239                 LCMCommandRequest request = new LCMCommandRequest(commandExecutorInput);
240
241                 return request;
242         }
243
244         public WorkflowResponse getWorkflowResponse (){
245                 WorkflowResponse wfResponse = new WorkflowResponse();
246                 ResponseContext responseContext = createResponseContextWithSuObjects();
247                 wfResponse.setResponseContext(responseContext);
248                 responseContext.setPayload("");
249                 wfResponse.getResponseContext().getStatus().setCode(100);
250                 return wfResponse;
251         }
252
253         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){
254                 CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
255                 RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
256                 RequestContext requestContext = runtimeContext.getRequestContext();
257                 ResponseContext responseContext = createResponseContextWithSuObjects();
258                 runtimeContext.setResponseContext(responseContext);
259
260                 requestContext.getCommonHeader().getFlags().setTtl(ttl);
261                 requestContext.getCommonHeader().setApiVer(apiVersion);
262                 requestContext.getCommonHeader().setTimestamp(timeStamp);
263                 requestContext.getCommonHeader().setRequestId(requestId);
264                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
265                 requestContext.getCommonHeader().setOriginatorId(originatorID);
266                 requestContext.setAction(action);
267                 requestContext.setPayload(payload);
268                 requestContext.getActionIdentifiers().setVnfId(vnfId);
269                 VNFContext vnfContext = runtimeContext.getVnfContext();
270                 vnfContext.setType(vnfType);
271                 vnfContext.setId(vnfId);
272                 vnfContext.setVersion(vnfVersion);
273                 return commandExecutorInput;
274         }
275
276         private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
277                 CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
278                 RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
279                 commandExecutorInput.setRuntimeContext(runtimeContext);
280                 return commandExecutorInput;
281         }
282
283         private RuntimeContext createRuntimeContextWithSubObjects() {
284                 RuntimeContext runtimeContext = new RuntimeContext();
285                 RequestContext requestContext = new RequestContext();
286                 runtimeContext.setRequestContext(requestContext);
287                 CommonHeader commonHeader = new CommonHeader();
288                 requestContext.setCommonHeader(commonHeader);
289                 Flags flags = new Flags();
290                 commonHeader.setFlags(flags);
291                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
292                 requestContext.setActionIdentifiers(actionIdentifiers);
293                 VNFContext vnfContext = new VNFContext();
294                 runtimeContext.setVnfContext(vnfContext);
295                 return runtimeContext;
296
297         }
298
299         private ResponseContext createResponseContextWithSuObjects(){
300                 ResponseContext responseContext = new ResponseContext();
301                 CommonHeader commonHeader = new CommonHeader();
302                 Flags flags = new Flags();
303                 Status status = new Status();
304                 responseContext.setCommonHeader(commonHeader);
305                 responseContext.setStatus(status);
306                 commonHeader.setFlags(flags);
307                 return responseContext;
308         }
309
310 }
311