f2c30990e4c42f15f449b91c44430935de94bcbd
[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.domainmodel.lcm.Flags.Mode;
35 import org.openecomp.appc.executor.impl.CommandTask;
36 import org.openecomp.appc.executor.impl.CommandTaskFactory;
37 import org.openecomp.appc.executor.impl.LCMCommandTask;
38 import org.openecomp.appc.executor.impl.LCMReadonlyCommandTask;
39 import org.openecomp.appc.executor.objects.*;
40 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
41 import org.openecomp.appc.requesthandler.RequestHandler;
42 import org.openecomp.appc.workflow.WorkFlowManager;
43 import org.openecomp.appc.workflow.objects.WorkflowRequest;
44 import org.openecomp.appc.workflow.objects.WorkflowResponse;
45 import org.openecomp.sdnc.sli.SvcLogicContext;
46 import org.openecomp.sdnc.sli.SvcLogicException;
47 import org.openecomp.sdnc.sli.SvcLogicResource;
48 import org.openecomp.sdnc.sli.aai.AAIService;
49 import org.osgi.framework.Bundle;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.FrameworkUtil;
52 import org.osgi.framework.ServiceReference;
53 import org.powermock.api.mockito.PowerMockito;
54 import org.powermock.core.classloader.annotations.PrepareForTest;
55 import org.powermock.modules.junit4.PowerMockRunner;
56
57 import java.time.Instant;
58 import java.util.Date;
59 import java.util.HashMap;
60 import java.util.Map;
61 import java.util.Properties;
62
63 import static junit.framework.Assert.assertEquals;
64 import static org.mockito.Matchers.*;
65
66
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
70 public class TestCommandExecutionTask {
71
72         private static final String TTL_FLAG= "TTL";
73         private static final String API_VERSION= "2.0.0";
74         private static final String ORIGINATOR_ID= "1";
75         private CommandTaskFactory factory ;
76
77         private RequestHandler requestHandler;
78         private WorkFlowManager workflowManager;
79         private AAIService aaiService;
80         private LifecycleManager lifecyclemanager;
81
82         private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
83         private final Bundle bundleService=Mockito.mock(Bundle.class);
84         private final ServiceReference sref=Mockito.mock(ServiceReference.class);
85
86         @Before
87         public void init() throws SvcLogicException {
88
89                 // ***
90                 AAIService aaiService = Mockito.mock(AAIService.class);;
91                 PowerMockito.mockStatic(FrameworkUtil.class);
92                 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
93                 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
94                 PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
95                 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
96                 PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
97                         @Override
98                         public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
99                                 Object[] args = invocation.getArguments();
100                                 SvcLogicContext ctx =(SvcLogicContext)args[6];
101                                 String prefix = (String)args[4];
102                                 String key = (String)args[3];
103                                 if(key.contains("'28'")){
104                                         return  SvcLogicResource.QueryStatus.FAILURE ;
105                                 }else if ( key.contains("'8'")) {
106                                         return  SvcLogicResource.QueryStatus.NOT_FOUND ;
107                                 }else {
108                                         ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
109                                         ctx.setAttribute(prefix + ".orchestration-status", "INSTANTIATED");
110                                 }
111                                 return  SvcLogicResource.QueryStatus.SUCCESS ;
112                         }
113                 });
114                 PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
115
116                 requestHandler =  Mockito.mock(RequestHandler.class);
117                 workflowManager = Mockito.mock(WorkFlowManager.class);
118                 lifecyclemanager = Mockito.mock(LifecycleManager.class );
119
120                 factory = new CommandTaskFactory();
121                 factory.setLifecyclemanager(lifecyclemanager);
122                 factory.setWorkflowManager(workflowManager);
123                 factory.setRequestHandler(requestHandler);
124                 Mockito.when(workflowManager.executeWorkflow((WorkflowRequest)anyObject())).thenReturn(getWorkflowResponse () );
125         }
126
127
128         @Test
129         public void testFactory(){
130                 CommandTask task = factory.getExecutionTask("Configure", null);
131                 assertEquals(LCMCommandTask.class,task.getClass() );
132                 task = factory.getExecutionTask("Sync", null);
133                 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
134
135         }
136
137
138
139         @Test
140         public void testOnRequestCompletion(){
141                 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
142                 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Configure, "1", "");
143                 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
144         LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
145                 executionTask.onRequestCompletion(response);
146         }
147
148         @Test
149         public void testRunGetConfig(){
150                     RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
151         LCMReadonlyCommandTask readonlyCommandTask = new LCMReadonlyCommandTask(request, requestHandler,workflowManager);
152                 readonlyCommandTask.run();
153         }
154
155         @Test
156         public void testRun(){
157             RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
158                 LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
159                 executionTask.run();
160         }
161
162         @Test
163         public void testRunNegative(){
164             RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
165         LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
166                 executionTask.run();
167         }
168
169
170         CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
171                 RuntimeContext runtimeContext = new RuntimeContext();
172                 ResponseContext responseContext = new ResponseContext();
173                 runtimeContext.setResponseContext(responseContext);
174                 RequestContext requestContext = new RequestContext();
175                 runtimeContext.setRequestContext(requestContext);
176                 CommonHeader commonHeader = new CommonHeader();
177                 requestContext.setCommonHeader(commonHeader);
178                 responseContext.setCommonHeader(commonHeader);
179                 commonHeader.setFlags(new Flags(null, false, 0));
180                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
181                 requestContext.setActionIdentifiers(actionIdentifiers);
182                 VNFContext vnfContext = new VNFContext();
183                 runtimeContext.setVnfContext(vnfContext);
184                 requestContext.setAction(action);
185                 runtimeContext.setRpcName(action.name().toLowerCase());
186                 commonHeader.setApiVer(API_VERSION);
187                 responseContext.setStatus(new Status(100, null));
188                 commonHeader.setRequestId(responseId);
189                 responseContext.setPayload(payload);
190                 commonHeader.setTimestamp(Instant.now());
191                 vnfContext.setId(vnfId);
192         return new CommandResponse(runtimeContext);
193         }
194
195
196
197         @Test
198         public void testPositiveFlow_configure()  {
199
200                 String requestId = "1";
201
202                 RuntimeContext commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", Instant.now(), API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
203         }
204
205
206         private Map<String,Object> setTTLInFlags( String value){
207                 Map<String,Object> flags = new HashMap<String,Object>();
208                 if( value != null || !("".equalsIgnoreCase(value))){
209                         flags.put(TTL_FLAG, value);
210                 }
211                 return flags;
212         }
213
214
215         public WorkflowResponse getWorkflowResponse (){
216                 WorkflowResponse wfResponse = new WorkflowResponse();
217                 ResponseContext responseContext = createResponseContextWithSuObjects();
218                 wfResponse.setResponseContext(responseContext);
219                 responseContext.setPayload("");
220                 wfResponse.getResponseContext().setStatus(new Status(100, null));
221                 return wfResponse;
222         }
223
224         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){
225                 RuntimeContext commandExecutorInput = createCommandExecutorInputWithSubObjects();
226                 RequestContext requestContext = commandExecutorInput.getRequestContext();
227                 ResponseContext responseContext = createResponseContextWithSuObjects();
228                 commandExecutorInput.setResponseContext(responseContext);
229
230                 requestContext.getCommonHeader().setFlags(new Flags(null, false, ttl));
231                 requestContext.getCommonHeader().setApiVer(apiVersion);
232                 requestContext.getCommonHeader().setTimestamp(timeStamp);
233                 requestContext.getCommonHeader().setRequestId(requestId);
234                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
235                 requestContext.getCommonHeader().setOriginatorId(originatorID);
236                 requestContext.setAction(action);
237                 requestContext.setPayload(payload);
238                 requestContext.getActionIdentifiers().setVnfId(vnfId);
239                 VNFContext vnfContext = commandExecutorInput.getVnfContext();
240                 vnfContext.setType(vnfType);
241                 vnfContext.setId(vnfId);
242                 vnfContext.setVersion(vnfVersion);
243                 return commandExecutorInput;
244         }
245
246         private RuntimeContext createCommandExecutorInputWithSubObjects() {
247                 return createRuntimeContextWithSubObjects();
248         }
249
250         private RuntimeContext createRuntimeContextWithSubObjects() {
251                 RuntimeContext runtimeContext = new RuntimeContext();
252                 RequestContext requestContext = new RequestContext();
253                 runtimeContext.setRequestContext(requestContext);
254                 CommonHeader commonHeader = new CommonHeader();
255                 requestContext.setCommonHeader(commonHeader);
256                 commonHeader.setFlags(new Flags(null, false, 0));
257                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
258                 requestContext.setActionIdentifiers(actionIdentifiers);
259                 VNFContext vnfContext = new VNFContext();
260                 runtimeContext.setVnfContext(vnfContext);
261                 return runtimeContext;
262
263         }
264
265         private ResponseContext createResponseContextWithSuObjects(){
266                 ResponseContext responseContext = new ResponseContext();
267                 CommonHeader commonHeader = new CommonHeader();
268                 responseContext.setCommonHeader(commonHeader);
269                 responseContext.setStatus(new Status(0, null));
270                 commonHeader.setFlags(new Flags(null, false, 0));
271                 return responseContext;
272         }
273
274 }
275