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.workflow.impl.WorkFlowManagerImpl;
50 import org.onap.appc.workflow.impl.WorkflowKey;
51 import org.onap.appc.workflow.impl.WorkflowResolver;
52 import org.onap.appc.workflow.objects.WorkflowExistsOutput;
53 import org.onap.appc.workflow.objects.WorkflowRequest;
54 import org.onap.appc.workflow.objects.WorkflowResponse;
55 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
56 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.core.classloader.annotations.PrepareForTest;
59 import org.powermock.modules.junit4.PowerMockRunner;
60 import java.sql.Connection;
61 import java.sql.PreparedStatement;
62 import java.sql.ResultSet;
63 import java.sql.SQLException;
64 import java.util.Date;
65 import java.util.Properties;
67 import static org.mockito.Matchers.anyObject;
68 import static org.mockito.Matchers.anyString;
70 @RunWith(PowerMockRunner.class)
71 @PrepareForTest({ConfigurationFactory.class, DBUtils.class})
72 public class TestWorkFlowManager {
75 public ExpectedException expectedEx = ExpectedException.none();
77 public TestWorkFlowManager() {
81 public WorkFlowManagerImpl workflowManger;
83 WorkflowResolver workflowResolver;
84 public SvcLogicService svcLogicService;
88 PowerMockito.mockStatic(ConfigurationFactory.class);
89 Configuration mockConfiguration = Mockito.mock(Configuration.class);
90 PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfiguration);
91 this.workflowResolver = Mockito.mock(WorkflowResolver.class);
92 this.svcLogicService = Mockito.mock(SvcLogicService.class);
93 workflowManger.setWorkflowResolver(workflowResolver);
94 workflowManger.setSvcLogicServiceRef(svcLogicService);
98 public void testExecuteWorkFlow() throws SvcLogicException{
100 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
101 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
102 .thenReturn(createSvcExexuteSuccessResponse());
104 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
105 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
107 WorkflowResponse response = workflowManger.executeWorkflow(workflowRequest);
108 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
114 public void testExecuteWorkFlowFalse() throws SvcLogicException{
116 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
117 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
118 .thenReturn(createSvcExexuteFailureResponse());
120 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
121 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
123 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
124 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
129 public void testExecuteWorkFlowAPIVersionStartWithOne() throws SvcLogicException{
130 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
131 Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
132 .thenReturn(createSvcExexuteSuccessResponse());
134 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
135 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
137 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
138 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
142 public void testWorkFlowExist() throws SvcLogicException{
143 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
144 Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
146 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
147 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
149 WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
151 Assert.assertTrue(response.isMappingExist());
155 public void testWorkFlowNotExist() throws SvcLogicException {
156 Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
157 Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(false);
159 WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
160 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
162 WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
164 Assert.assertTrue(response.isMappingExist());
168 public void testWorkflowResolver() throws SQLException {
169 PowerMockito.mockStatic(DBUtils.class);
170 Connection mockConnection = Mockito.mock(Connection.class);
171 PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
172 ResultSet mockResultSet = Mockito.mock(ResultSet.class);
173 PowerMockito.when(DBUtils.getConnection(Mockito.anyString())).thenReturn(mockConnection);
174 PowerMockito.when(mockConnection.prepareStatement(Mockito.anyString())).thenReturn(mockStatement);
175 PowerMockito.when(mockStatement.executeQuery()).thenReturn(mockResultSet);
176 PowerMockito.when(mockResultSet.next()).thenReturn(true);
177 workflowResolver = new WorkflowResolver(1);
178 expectedEx.expect(IllegalArgumentException.class);
179 expectedEx.expectMessage("Duplicated configuration entry: ");
180 workflowResolver.resolve("ACTION", "VNF_TYPE", "VNF_VERSION", "API_VERSION");
183 private WorkflowRequest getWorkflowRequest(String vnfType, int ttl, Date timeStamp, String apiVersion, String requestId,
184 String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload) {
185 WorkflowRequest workflowRequest = new WorkflowRequest();
186 RuntimeContext runtimeContext = createRuntimeContext();
188 runtimeContext.getRequestContext().getCommonHeader().getFlags().setTtl(ttl);
189 runtimeContext.getRequestContext().getCommonHeader().setApiVer(apiVersion);
190 runtimeContext.getRequestContext().getCommonHeader().setTimestamp(timeStamp);
191 runtimeContext.getRequestContext().getCommonHeader().setRequestId(requestId);
192 runtimeContext.getRequestContext().getCommonHeader().setSubRequestId(subRequestID);
193 runtimeContext.getRequestContext().getCommonHeader().setOriginatorId(originatorID);
194 runtimeContext.getRequestContext().setAction(action);
195 runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
196 runtimeContext.getRequestContext().setPayload(payload);
198 runtimeContext.getVnfContext().setType(vnfType);
199 runtimeContext.getVnfContext().setId(vnfId);
201 workflowRequest.setRequestContext(runtimeContext.getRequestContext());
202 workflowRequest.setResponseContext(runtimeContext.getResponseContext());
203 workflowRequest.setVnfContext(runtimeContext.getVnfContext());
205 return workflowRequest;
208 private RequestContext creatRequestContext(){
209 RequestContext requestContext = new RequestContext();
210 CommonHeader commonHeader = new CommonHeader();
211 Flags flags = new Flags();
212 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
213 commonHeader.setFlags(flags);
214 requestContext.setCommonHeader(commonHeader);
215 requestContext.setActionIdentifiers(actionIdentifiers);
217 return requestContext;
219 private ResponseContext createResponseContext(){
220 ResponseContext responseContext = new ResponseContext();
221 CommonHeader commonHeader = new CommonHeader();
222 Flags flags = new Flags();
223 Status status = new Status();
224 responseContext.setCommonHeader(commonHeader);
225 responseContext.setStatus(status);
226 commonHeader.setFlags(flags);
228 return responseContext;
230 private RuntimeContext createRuntimeContext(){
231 RuntimeContext runtimeContext = new RuntimeContext();
232 RequestContext requestContext = creatRequestContext();
233 ResponseContext responseContext = createResponseContext();
234 runtimeContext.setRequestContext(requestContext);
235 runtimeContext.setResponseContext(responseContext);
236 VNFContext vnfContext = new VNFContext();
237 runtimeContext.setVnfContext(vnfContext);
239 return runtimeContext;
242 public WorkflowKey getWorkFlowKey(){
243 WorkflowKey workflowKey = new WorkflowKey("APPCDG", "2.0.0.0", "dgModule");
248 private Properties createSvcExexuteSuccessResponse(){
249 Properties properties = new Properties();
250 properties.setProperty("output.payload", "success");
251 properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "success");
252 properties.setProperty(Constants.DG_OUTPUT_STATUS_CODE, "400");
253 properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "success");
258 private Properties createSvcExexuteFailureResponse(){
259 Properties properties = new Properties();
260 properties.setProperty("output.payload", "failure");
261 properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "failure");
262 properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "failure");