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