aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/openecomp/appc/executor/TestCommandExecutionTask.java
blob: 0504c3998e32fd3160ca4db79352a3dedb55fb36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.executor;
/**
 *
 */

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.openecomp.appc.domainmodel.lcm.*;
import org.openecomp.appc.executor.impl.CommandTask;
import org.openecomp.appc.executor.impl.CommandTaskFactory;
import org.openecomp.appc.executor.impl.LCMCommandTask;
import org.openecomp.appc.executor.impl.LCMReadonlyCommandTask;
import org.openecomp.appc.executor.impl.objects.CommandRequest;
import org.openecomp.appc.executor.impl.objects.LCMCommandRequest;
import org.openecomp.appc.executor.impl.objects.LCMReadOnlyCommandRequest;
import org.openecomp.appc.executor.objects.*;
import org.openecomp.appc.lifecyclemanager.LifecycleManager;
import org.openecomp.appc.requesthandler.RequestHandler;
import org.openecomp.appc.workflow.WorkFlowManager;
import org.openecomp.appc.workflow.objects.WorkflowRequest;
import org.openecomp.appc.workflow.objects.WorkflowResponse;
import org.openecomp.sdnc.sli.SvcLogicContext;
import org.openecomp.sdnc.sli.SvcLogicException;
import org.openecomp.sdnc.sli.SvcLogicResource;
import org.openecomp.sdnc.sli.aai.AAIService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.*;



@RunWith(PowerMockRunner.class)
@PrepareForTest( {FrameworkUtil.class, CommandTask.class, LCMCommandTask.class})
public class TestCommandExecutionTask {

	private static final String TTL_FLAG= "TTL";
	private static final String API_VERSION= "2.0.0";
	private static final String ORIGINATOR_ID= "1";
	private LCMCommandTask executionTask;
	private LCMReadonlyCommandTask LCMReadonlyCommandTask;
	private CommandTaskFactory factory ;

	private RequestHandler requestHandler;
	private WorkFlowManager workflowManager;
	private AAIService aaiService;
	private LifecycleManager lifecyclemanager;

	private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
	private final Bundle bundleService=Mockito.mock(Bundle.class);
	private final ServiceReference sref=Mockito.mock(ServiceReference.class);

	@Before
	public void init() throws SvcLogicException {

		// ***
		AAIService aaiService = Mockito.mock(AAIService.class);;
		PowerMockito.mockStatic(FrameworkUtil.class);
		PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
		PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
		PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
		PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
		PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
			@Override
			public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
				Object[] args = invocation.getArguments();
				SvcLogicContext ctx =(SvcLogicContext)args[6];
				String prefix = (String)args[4];
				String key = (String)args[3];
				if(key.contains("'28'")){
					return  SvcLogicResource.QueryStatus.FAILURE ;
				}else if ( key.contains("'8'")) {
					return  SvcLogicResource.QueryStatus.NOT_FOUND ;
				}else {
					ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
					ctx.setAttribute(prefix + ".orchestration-status", "INSTANTIATED");
				}
				return  SvcLogicResource.QueryStatus.SUCCESS ;
			}
		});
		PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);

		requestHandler =  Mockito.mock(RequestHandler.class);
		workflowManager = Mockito.mock(WorkFlowManager.class);
		lifecyclemanager = Mockito.mock(LifecycleManager.class );

		executionTask = new LCMCommandTask(requestHandler,workflowManager,lifecyclemanager);
		LCMReadonlyCommandTask = new LCMReadonlyCommandTask(requestHandler,workflowManager);
		factory = new CommandTaskFactory();
		factory.setLifecyclemanager(lifecyclemanager);
		factory.setWorkflowManager(workflowManager);
		factory.setRequestHandler(requestHandler);
		Mockito.when(workflowManager.executeWorkflow((WorkflowRequest)anyObject())).thenReturn(getWorkflowResponse () );
	}


	@Test
	public void testFactory(){
		CommandTask task;
		task = factory.getExecutionTask("Configure");
		assertEquals(LCMCommandTask.class,task.getClass() );
		task = factory.getExecutionTask("Sync");
		assertEquals(LCMReadonlyCommandTask.class,task.getClass() );

	}



	@Test
	public void testOnRequestCompletion(){
		Mockito.doNothing().when(requestHandler).onRequestTTLEnd((RuntimeContext) anyObject(),anyBoolean());
		LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"), VNFOperation.Configure, "1", "1.0");
		CommandResponse response = getCommandResponse(VNFOperation.Configure, true, "11", "","1");
		executionTask.onRequestCompletion(request, response);
	}

	@Test
	public void testRunGetConfig(){
		LCMReadOnlyCommandRequest request = getConfigCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
		LCMReadonlyCommandTask.setCommandRequest(request);
		LCMReadonlyCommandTask.run();
	}

	@Test
	public void testRun(){
		LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
		executionTask.setCommandRequest(request);
		executionTask.run();
	}

	@Test
	public void testRunNegative(){
		LCMCommandRequest request = getLCMCommandRequest("FIREWALL",30,new Date(), "11" ,setTTLInFlags("30"),VNFOperation.Sync, "1", "1.0");
		executionTask.setCommandRequest(request);
		executionTask.run();
	}


	CommandResponse getCommandResponse(VNFOperation action , boolean success, String responseId, String payload, String vnfId){
		CommandResponse commandResponse = new CommandResponse();
		RuntimeContext runtimeContext = new RuntimeContext();
		commandResponse.setRuntimeContext(runtimeContext);
		ResponseContext responseContext = new ResponseContext();
		runtimeContext.setResponseContext(responseContext);
		RequestContext requestContext = new RequestContext();
		runtimeContext.setRequestContext(requestContext);
		CommonHeader commonHeader = new CommonHeader();
		requestContext.setCommonHeader(commonHeader);
		responseContext.setCommonHeader(commonHeader);
		Flags flags = new Flags();
		commonHeader.setFlags(flags);
		ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
		requestContext.setActionIdentifiers(actionIdentifiers);
		VNFContext vnfContext = new VNFContext();
		runtimeContext.setVnfContext(vnfContext);
		requestContext.setAction(action);
		runtimeContext.setRpcName(action.name().toLowerCase());
		commonHeader.setApiVer(API_VERSION);
		Status status = new Status();
		status.setCode(100);
		responseContext.setStatus(status);
		commonHeader.setRequestId(responseId);
		responseContext.setPayload(payload);
		commonHeader.setTimestamp(new Date());
		vnfContext.setId(vnfId);
		return commandResponse;
	}



	@Test
	public void testPositiveFlow_configure()  {

		Date timeStamp = new Date();
		String requestId = "1";

		CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput("FIREWALL",30, "1.0", timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", VNFOperation.Configure, "33", "");
		CommandRequest request = new CommandRequest(commandExecutorInput);
	}


	private Map<String,Object> setTTLInFlags( String value){
		Map<String,Object> flags = new HashMap<String,Object>();
		if( value != null || !("".equalsIgnoreCase(value))){
			flags.put(TTL_FLAG, value);
		}
		return flags;
	}


	private LCMReadOnlyCommandRequest getConfigCommandRequest(String vnfType , Integer ttl , Date timeStamp, String requestId,
															  Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){

		CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
		LCMReadOnlyCommandRequest request = new LCMReadOnlyCommandRequest(commandExecutorInput);

		return request;
	}

	private LCMCommandRequest getLCMCommandRequest(String vnfType , Integer ttl ,Date timeStamp, String requestId,
											 Map<String,Object> flags, VNFOperation command , String vnfId, String vnfVersion ){

		CommandExecutorInput commandExecutorInput = pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, "");
		LCMCommandRequest request = new LCMCommandRequest(commandExecutorInput);

		return request;
	}

	public WorkflowResponse getWorkflowResponse (){
		WorkflowResponse wfResponse = new WorkflowResponse();
		ResponseContext responseContext = createResponseContextWithSuObjects();
		wfResponse.setResponseContext(responseContext);
		responseContext.setPayload("");
		wfResponse.getResponseContext().getStatus().setCode(100);
		return wfResponse;
	}

	private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){
		CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
		RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
		RequestContext requestContext = runtimeContext.getRequestContext();
		ResponseContext responseContext = createResponseContextWithSuObjects();
		runtimeContext.setResponseContext(responseContext);

		requestContext.getCommonHeader().getFlags().setTtl(ttl);
		requestContext.getCommonHeader().setApiVer(apiVersion);
		requestContext.getCommonHeader().setTimestamp(timeStamp);
		requestContext.getCommonHeader().setRequestId(requestId);
		requestContext.getCommonHeader().setSubRequestId(subRequestID);
		requestContext.getCommonHeader().setOriginatorId(originatorID);
		requestContext.setAction(action);
		requestContext.setPayload(payload);
		requestContext.getActionIdentifiers().setVnfId(vnfId);
		VNFContext vnfContext = runtimeContext.getVnfContext();
		vnfContext.setType(vnfType);
		vnfContext.setId(vnfId);
		vnfContext.setVersion(vnfVersion);
		return commandExecutorInput;
	}

	private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
		CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
		RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
		commandExecutorInput.setRuntimeContext(runtimeContext);
		return commandExecutorInput;
	}

	private RuntimeContext createRuntimeContextWithSubObjects() {
		RuntimeContext runtimeContext = new RuntimeContext();
		RequestContext requestContext = new RequestContext();
		runtimeContext.setRequestContext(requestContext);
		CommonHeader commonHeader = new CommonHeader();
		requestContext.setCommonHeader(commonHeader);
		Flags flags = new Flags();
		commonHeader.setFlags(flags);
		ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
		requestContext.setActionIdentifiers(actionIdentifiers);
		VNFContext vnfContext = new VNFContext();
		runtimeContext.setVnfContext(vnfContext);
		return runtimeContext;

	}

	private ResponseContext createResponseContextWithSuObjects(){
		ResponseContext responseContext = new ResponseContext();
		CommonHeader commonHeader = new CommonHeader();
		Flags flags = new Flags();
		Status status = new Status();
		responseContext.setCommonHeader(commonHeader);
		responseContext.setStatus(status);
		commonHeader.setFlags(flags);
		return responseContext;
	}

}