2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.workflow.impl;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mockito;
36 import org.onap.appc.common.constant.Constants;
37 import org.onap.appc.configuration.Configuration;
38 import org.onap.appc.configuration.ConfigurationFactory;
39 import org.onap.appc.dao.util.DBUtils;
40 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
41 import org.onap.appc.domainmodel.lcm.CommonHeader;
42 import org.onap.appc.domainmodel.lcm.Flags;
43 import org.onap.appc.domainmodel.lcm.RequestContext;
44 import org.onap.appc.domainmodel.lcm.ResponseContext;
45 import org.onap.appc.domainmodel.lcm.RuntimeContext;
46 import org.onap.appc.domainmodel.lcm.Status;
47 import org.onap.appc.domainmodel.lcm.VNFContext;
48 import org.onap.appc.domainmodel.lcm.VNFOperation;
49 import org.onap.appc.util.ObjectMapper;
50 import org.onap.appc.workflow.impl.WorkFlowManagerImpl;
51 import org.onap.appc.workflow.impl.WorkflowKey;
52 import org.onap.appc.workflow.impl.WorkflowResolver;
53 import org.onap.appc.workflow.objects.WorkflowExistsOutput;
54 import org.onap.appc.workflow.objects.WorkflowRequest;
55 import org.onap.appc.workflow.objects.WorkflowResponse;
56 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
57 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.modules.junit4.PowerMockRunner;
61 import java.sql.Connection;
62 import java.sql.PreparedStatement;
63 import java.sql.ResultSet;
64 import java.sql.SQLException;
65 import java.util.Date;
66 import java.util.Properties;
68 import static org.mockito.Matchers.anyObject;
69 import static org.mockito.Matchers.anyString;
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ConfigurationFactory.class, DBUtils.class, ObjectMapper.class})
73 public class TestWorkFlowManager {
76 public ExpectedException expectedEx = ExpectedException.none();
78 public TestWorkFlowManager() {
82 public WorkFlowManagerImpl workflowManger;
84 WorkflowResolver workflowResolver;
85 public SvcLogicService svcLogicService;
89 PowerMockito.mockStatic(ConfigurationFactory.class);
90 Configuration mockConfiguration = Mockito.mock(Configuration.class);
91 PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfiguration);
92 this.workflowResolver = Mockito.mock(WorkflowResolver.class);
93 this.svcLogicService = Mockito.mock(SvcLogicService.class);
94 workflowManger.setWorkflowResolver(workflowResolver);
95 workflowManger.setSvcLogicServiceRef(svcLogicService);
99 public void testExecuteWorkFlow() throws SvcLogicException{
101 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
102 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
103 .thenReturn(createSvcExexuteSuccessResponse());
105 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
106 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
108 WorkflowResponse response = workflowManger.executeWorkflow(workflowRequest);
109 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
115 public void testExecuteWorkFlowFalse() throws SvcLogicException{
117 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
118 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
119 .thenReturn(createSvcExexuteFailureResponse());
121 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
122 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
124 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
125 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
130 public void testExecuteWorkFlowAPIVersionStartWithOne() throws SvcLogicException{
131 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
132 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
133 .thenReturn(createSvcExexuteSuccessResponse());
135 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
136 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
138 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
139 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
143 public void testExecuteWorkFlowException() throws SvcLogicException{
144 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
145 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
146 .thenReturn(createSvcExexuteSuccessResponse());
148 WorkflowRequest workflowRequest = Mockito.spy(getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
149 VNFOperation.Lock, "mj13", Constants.PAYLOAD));
150 Mockito.when(workflowRequest.getRequestContext()).thenThrow(new RuntimeException());
152 WorkflowResponse response = workflowManger.executeWorkflow(workflowRequest);
153 Mockito.verify(workflowRequest, Mockito.times(2)).getResponseContext();
157 public void testExecuteWorkAPIVersion1Exception() throws SvcLogicException{
158 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
159 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
160 .thenReturn(createSvcExexuteSuccessResponse());
162 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
163 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
164 PowerMockito.mockStatic(ObjectMapper.class);
165 PowerMockito.when(ObjectMapper.map(Mockito.any())).thenThrow(new RuntimeException());
166 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
167 Assert.assertEquals(501, response.getResponseContext().getStatus().getCode());
171 public void testWorkFlowExist() throws SvcLogicException{
172 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
173 Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
175 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
176 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
178 WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
180 Assert.assertTrue(response.isMappingExist());
184 public void testWorkFlowNotExist() throws SvcLogicException {
185 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
186 Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(false);
188 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
189 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
191 WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
193 Assert.assertTrue(response.isMappingExist());
197 public void testWorkFlowExistNullKey() throws SvcLogicException{
198 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(null);
199 Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
200 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
201 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
202 WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
203 Assert.assertFalse(response.isMappingExist());
207 public void testWorkflowResolver() throws SQLException {
208 PowerMockito.mockStatic(DBUtils.class);
209 Connection mockConnection = Mockito.mock(Connection.class);
210 PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
211 ResultSet mockResultSet = Mockito.mock(ResultSet.class);
212 PowerMockito.when(DBUtils.getConnection(Mockito.anyString())).thenReturn(mockConnection);
213 PowerMockito.when(mockConnection.prepareStatement(Mockito.anyString())).thenReturn(mockStatement);
214 PowerMockito.when(mockStatement.executeQuery()).thenReturn(mockResultSet);
215 PowerMockito.when(mockResultSet.next()).thenReturn(true);
216 workflowResolver = new WorkflowResolver(1);
217 expectedEx.expect(IllegalArgumentException.class);
218 expectedEx.expectMessage("Duplicated configuration entry: ");
219 workflowResolver.resolve("ACTION", "VNF_TYPE", "VNF_VERSION", "API_VERSION");
222 private WorkflowRequest getWorkflowRequest(String vnfType, int ttl, Date timeStamp, String apiVersion, String requestId,
223 String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload) {
224 WorkflowRequest workflowRequest = new WorkflowRequest();
225 RuntimeContext runtimeContext = createRuntimeContext();
227 runtimeContext.getRequestContext().getCommonHeader().getFlags().setTtl(ttl);
228 runtimeContext.getRequestContext().getCommonHeader().setApiVer(apiVersion);
229 runtimeContext.getRequestContext().getCommonHeader().setTimestamp(timeStamp);
230 runtimeContext.getRequestContext().getCommonHeader().setRequestId(requestId);
231 runtimeContext.getRequestContext().getCommonHeader().setSubRequestId(subRequestID);
232 runtimeContext.getRequestContext().getCommonHeader().setOriginatorId(originatorID);
233 runtimeContext.getRequestContext().setAction(action);
234 runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
235 runtimeContext.getRequestContext().setPayload(payload);
237 runtimeContext.getVnfContext().setType(vnfType);
238 runtimeContext.getVnfContext().setId(vnfId);
240 workflowRequest.setRequestContext(runtimeContext.getRequestContext());
241 workflowRequest.setResponseContext(runtimeContext.getResponseContext());
242 workflowRequest.setVnfContext(runtimeContext.getVnfContext());
244 return workflowRequest;
247 private RequestContext creatRequestContext(){
248 RequestContext requestContext = new RequestContext();
249 CommonHeader commonHeader = new CommonHeader();
250 Flags flags = new Flags();
251 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
252 commonHeader.setFlags(flags);
253 requestContext.setCommonHeader(commonHeader);
254 requestContext.setActionIdentifiers(actionIdentifiers);
256 return requestContext;
259 private ResponseContext createResponseContext(){
260 ResponseContext responseContext = new ResponseContext();
261 CommonHeader commonHeader = new CommonHeader();
262 Flags flags = new Flags();
263 Status status = new Status();
264 responseContext.setCommonHeader(commonHeader);
265 responseContext.setStatus(status);
266 commonHeader.setFlags(flags);
268 return responseContext;
271 private RuntimeContext createRuntimeContext(){
272 RuntimeContext runtimeContext = new RuntimeContext();
273 RequestContext requestContext = creatRequestContext();
274 ResponseContext responseContext = createResponseContext();
275 runtimeContext.setRequestContext(requestContext);
276 runtimeContext.setResponseContext(responseContext);
277 VNFContext vnfContext = new VNFContext();
278 runtimeContext.setVnfContext(vnfContext);
280 return runtimeContext;
283 public WorkflowKey getWorkFlowKey(){
284 WorkflowKey workflowKey = new WorkflowKey("APPCDG", "2.0.0.0", "dgModule");
289 private Properties createSvcExexuteSuccessResponse(){
290 Properties properties = new Properties();
291 properties.setProperty("output.payload", "success");
292 properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "success");
293 properties.setProperty(Constants.DG_OUTPUT_STATUS_CODE, "400");
294 properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "success");
299 private Properties createSvcExexuteFailureResponse(){
300 Properties properties = new Properties();
301 properties.setProperty("output.payload", "failure");
302 properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "failure");
303 properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "failure");