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