Make Flags immutable
[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.util.Date;
58 import java.util.HashMap;
59 import java.util.Map;
60 import java.util.Properties;
61
62 import static junit.framework.Assert.assertEquals;
63 import static org.mockito.Matchers.*;
64
65
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
69 public class TestCommandExecutionTask {
70
71         private static final String TTL_FLAG= "TTL";
72         private static final String API_VERSION= "2.0.0";
73         private static final String ORIGINATOR_ID= "1";
74         private LCMCommandTask executionTask;
75         private LCMReadonlyCommandTask LCMReadonlyCommandTask;
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.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                 executionTask = new LCMCommandTask(requestHandler,workflowManager,lifecyclemanager);
122                 LCMReadonlyCommandTask = new LCMReadonlyCommandTask(requestHandler,workflowManager);
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");
134                 assertEquals(LCMCommandTask.class,task.getClass() );
135                 task = factory.getExecutionTask("Sync");
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", new Date(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Configure, "1", "");
146                 CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
147                 executionTask.onRequestCompletion(request, response);
148         }
149
150         @Test
151         public void testRunGetConfig(){
152                 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", new Date(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
153                 LCMReadonlyCommandTask.setCommandRequest(request);
154                 LCMReadonlyCommandTask.run();
155         }
156
157         @Test
158         public void testRun(){
159                 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", new Date(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
160                 executionTask.setCommandRequest(request);
161                 executionTask.run();
162         }
163
164         @Test
165         public void testRunNegative(){
166                 RuntimeContext request = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", new Date(), API_VERSION, "11", ORIGINATOR_ID, "", VNFOperation.Sync, "1", "");
167                 executionTask.setCommandRequest(request);
168                 executionTask.run();
169         }
170
171
172         CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
173                 CommandResponse commandResponse = new CommandResponse();
174                 RuntimeContext runtimeContext = new RuntimeContext();
175                 commandResponse.setRuntimeContext(runtimeContext);
176                 ResponseContext responseContext = new ResponseContext();
177                 runtimeContext.setResponseContext(responseContext);
178                 RequestContext requestContext = new RequestContext();
179                 runtimeContext.setRequestContext(requestContext);
180                 CommonHeader commonHeader = new CommonHeader();
181                 requestContext.setCommonHeader(commonHeader);
182                 responseContext.setCommonHeader(commonHeader);
183                 commonHeader.setFlags(new Flags(null, false, 0));
184                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
185                 requestContext.setActionIdentifiers(actionIdentifiers);
186                 VNFContext vnfContext = new VNFContext();
187                 runtimeContext.setVnfContext(vnfContext);
188                 requestContext.setAction(action);
189                 runtimeContext.setRpcName(action.name().toLowerCase());
190                 commonHeader.setApiVer(API_VERSION);
191                 responseContext.setStatus(new Status(100, null));
192                 commonHeader.setRequestId(responseId);
193                 responseContext.setPayload(payload);
194                 commonHeader.setTimestamp(new Date());
195                 vnfContext.setId(vnfId);
196                 return commandResponse;
197         }
198
199
200
201         @Test
202         public void testPositiveFlow_configure()  {
203
204                 Date timeStamp = new Date();
205                 String requestId = "1";
206
207                 RuntimeContext commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
208         }
209
210
211         private Map<String,Object> setTTLInFlags( String value){
212                 Map<String,Object> flags = new HashMap<String,Object>();
213                 if( value != null || !("".equalsIgnoreCase(value))){
214                         flags.put(TTL_FLAG, value);
215                 }
216                 return flags;
217         }
218
219
220         public WorkflowResponse getWorkflowResponse (){
221                 WorkflowResponse wfResponse = new WorkflowResponse();
222                 ResponseContext responseContext = createResponseContextWithSuObjects();
223                 wfResponse.setResponseContext(responseContext);
224                 responseContext.setPayload("");
225                 wfResponse.getResponseContext().setStatus(new Status(100, null));
226                 return wfResponse;
227         }
228
229         private RuntimeContext pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){
230                 RuntimeContext commandExecutorInput = createCommandExecutorInputWithSubObjects();
231                 RequestContext requestContext = commandExecutorInput.getRequestContext();
232                 ResponseContext responseContext = createResponseContextWithSuObjects();
233                 commandExecutorInput.setResponseContext(responseContext);
234
235                 requestContext.getCommonHeader().setFlags(new Flags(null, false, ttl));
236                 requestContext.getCommonHeader().setApiVer(apiVersion);
237                 requestContext.getCommonHeader().setTimestamp(timeStamp);
238                 requestContext.getCommonHeader().setRequestId(requestId);
239                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
240                 requestContext.getCommonHeader().setOriginatorId(originatorID);
241                 requestContext.setAction(action);
242                 requestContext.setPayload(payload);
243                 requestContext.getActionIdentifiers().setVnfId(vnfId);
244                 VNFContext vnfContext = commandExecutorInput.getVnfContext();
245                 vnfContext.setType(vnfType);
246                 vnfContext.setId(vnfId);
247                 vnfContext.setVersion(vnfVersion);
248                 return commandExecutorInput;
249         }
250
251         private RuntimeContext createCommandExecutorInputWithSubObjects() {
252                 return createRuntimeContextWithSubObjects();
253         }
254
255         private RuntimeContext createRuntimeContextWithSubObjects() {
256                 RuntimeContext runtimeContext = new RuntimeContext();
257                 RequestContext requestContext = new RequestContext();
258                 runtimeContext.setRequestContext(requestContext);
259                 CommonHeader commonHeader = new CommonHeader();
260                 requestContext.setCommonHeader(commonHeader);
261                 commonHeader.setFlags(new Flags(null, false, 0));
262                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
263                 requestContext.setActionIdentifiers(actionIdentifiers);
264                 VNFContext vnfContext = new VNFContext();
265                 runtimeContext.setVnfContext(vnfContext);
266                 return runtimeContext;
267
268         }
269
270         private ResponseContext createResponseContextWithSuObjects(){
271                 ResponseContext responseContext = new ResponseContext();
272                 CommonHeader commonHeader = new CommonHeader();
273                 responseContext.setCommonHeader(commonHeader);
274                 responseContext.setStatus(new Status(0, null));
275                 commonHeader.setFlags(new Flags(null, false, 0));
276                 return responseContext;
277         }
278
279 }
280