Applying license changes to all files
[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.setRequestHandler(requestHandler);
127                 Mockito.when(workflowManager.executeWorkflow((WorkflowRequest)anyObject())).thenReturn(getWorkflowResponse () );
128         }
129
130
131         @Test
132         public void testFactory(){
133                 CommandTask task = factory.getExecutionTask("Configure", null);
134                 assertEquals(LCMCommandTask.class,task.getClass() );
135                 task = factory.getExecutionTask("Sync", null);
136                 assertEquals(LCMReadonlyCommandTask.class,task.getClass() );
137
138         }
139
140
141
142         @Test
143         public void testOnRequestCompletion(){
144                 Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
145                 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Configure, "1", "");
146                 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
147         LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
148                 executionTask.onRequestCompletion(response);
149         }
150
151         @Test
152         public void testRunGetConfig(){
153                     RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
154         LCMReadonlyCommandTask readonlyCommandTask = new LCMReadonlyCommandTask(request, requestHandler,workflowManager);
155                 readonlyCommandTask.run();
156         }
157
158         @Test
159         public void testRun(){
160             RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
161                 LCMCommandTask executionTask = new LCMCommandTask(request, requestHandler,workflowManager,lifecyclemanager);
162                 executionTask.run();
163         }
164
165         @Test
166         public void testRunNegative(){
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
173         CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
174                 RuntimeContext runtimeContext = new RuntimeContext();
175                 ResponseContext responseContext = new ResponseContext();
176                 runtimeContext.setResponseContext(responseContext);
177                 RequestContext requestContext = new RequestContext();
178                 runtimeContext.setRequestContext(requestContext);
179                 CommonHeader commonHeader = new CommonHeader();
180                 requestContext.setCommonHeader(commonHeader);
181                 responseContext.setCommonHeader(commonHeader);
182                 commonHeader.setFlags(new Flags(null, false, 0));
183                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
184                 requestContext.setActionIdentifiers(actionIdentifiers);
185                 VNFContext vnfContext = new VNFContext();
186                 runtimeContext.setVnfContext(vnfContext);
187                 requestContext.setAction(action);
188                 runtimeContext.setRpcName(action.name().toLowerCase());
189                 commonHeader.setApiVer(API_VERSION);
190                 responseContext.setStatus(new Status(100, null));
191                 commonHeader.setRequestId(responseId);
192                 responseContext.setPayload(payload);
193                 commonHeader.setTimestamp(Instant.now());
194                 vnfContext.setId(vnfId);
195         return new CommandResponse(runtimeContext);
196         }
197
198
199
200         @Test
201         public void testPositiveFlow_configure()  {
202
203                 String requestId = "1";
204
205                 RuntimeContext commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", Instant.now(), API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
206         }
207
208
209         private Map<String,Object> setTTLInFlags( String value){
210                 Map<String,Object> flags = new HashMap<String,Object>();
211                 if( value != null || !("".equalsIgnoreCase(value))){
212                         flags.put(TTL_FLAG, value);
213                 }
214                 return flags;
215         }
216
217
218         public WorkflowResponse getWorkflowResponse (){
219                 WorkflowResponse wfResponse = new WorkflowResponse();
220                 ResponseContext responseContext = createResponseContextWithSuObjects();
221                 wfResponse.setResponseContext(responseContext);
222                 responseContext.setPayload("");
223                 wfResponse.getResponseContext().setStatus(new Status(100, null));
224                 return wfResponse;
225         }
226
227         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){
228                 RuntimeContext commandExecutorInput = createCommandExecutorInputWithSubObjects();
229                 RequestContext requestContext = commandExecutorInput.getRequestContext();
230                 ResponseContext responseContext = createResponseContextWithSuObjects();
231                 commandExecutorInput.setResponseContext(responseContext);
232
233                 requestContext.getCommonHeader().setFlags(new Flags(null, false, ttl));
234                 requestContext.getCommonHeader().setApiVer(apiVersion);
235                 requestContext.getCommonHeader().setTimestamp(timeStamp);
236                 requestContext.getCommonHeader().setRequestId(requestId);
237                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
238                 requestContext.getCommonHeader().setOriginatorId(originatorID);
239                 requestContext.setAction(action);
240                 requestContext.setPayload(payload);
241                 requestContext.getActionIdentifiers().setVnfId(vnfId);
242                 VNFContext vnfContext = commandExecutorInput.getVnfContext();
243                 vnfContext.setType(vnfType);
244                 vnfContext.setId(vnfId);
245                 vnfContext.setVersion(vnfVersion);
246                 return commandExecutorInput;
247         }
248
249         private RuntimeContext createCommandExecutorInputWithSubObjects() {
250                 return createRuntimeContextWithSubObjects();
251         }
252
253         private RuntimeContext createRuntimeContextWithSubObjects() {
254                 RuntimeContext runtimeContext = new RuntimeContext();
255                 RequestContext requestContext = new RequestContext();
256                 runtimeContext.setRequestContext(requestContext);
257                 CommonHeader commonHeader = new CommonHeader();
258                 requestContext.setCommonHeader(commonHeader);
259                 commonHeader.setFlags(new Flags(null, false, 0));
260                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
261                 requestContext.setActionIdentifiers(actionIdentifiers);
262                 VNFContext vnfContext = new VNFContext();
263                 runtimeContext.setVnfContext(vnfContext);
264                 return runtimeContext;
265
266         }
267
268         private ResponseContext createResponseContextWithSuObjects(){
269                 ResponseContext responseContext = new ResponseContext();
270                 CommonHeader commonHeader = new CommonHeader();
271                 responseContext.setCommonHeader(commonHeader);
272                 responseContext.setStatus(new Status(0, null));
273                 commonHeader.setFlags(new Flags(null, false, 0));
274                 return responseContext;
275         }
276
277 }
278