Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-workflow-management / appc-workflow-management-core / src / test / java / org / onap / appc / workflow / impl / TestWorkFlowManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.workflow.impl;
27
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;
67
68 import static org.mockito.Matchers.any;
69 import static org.mockito.Matchers.anyObject;
70 import static org.mockito.Matchers.anyString;
71
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ConfigurationFactory.class, DBUtils.class, ObjectMapper.class, WorkFlowManagerImpl.class})
74 public class TestWorkFlowManager {
75
76     @Rule
77     public ExpectedException expectedEx = ExpectedException.none();
78
79     public TestWorkFlowManager() {
80     }
81
82     @InjectMocks
83     public WorkFlowManagerImpl workflowManger;
84
85     WorkflowResolver workflowResolver;
86     public SvcLogicService svcLogicService;
87
88     @Before
89     public void init() {
90         PowerMockito.mockStatic(ConfigurationFactory.class);
91         Configuration mockConfiguration = Mockito.mock(Configuration.class);
92         PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfiguration);
93         this.workflowResolver = Mockito.mock(WorkflowResolver.class);
94         this.svcLogicService = Mockito.mock(SvcLogicService.class);
95         workflowManger.setWorkflowResolver(workflowResolver);
96         workflowManger.setSvcLogicServiceRef(svcLogicService);
97     }
98
99     @Test
100     public void testExecuteWorkFlow() throws SvcLogicException, Exception {
101
102         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
103                 .thenReturn(getWorkFlowKey());
104         // Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
105         //        .thenReturn(createSvcExexuteSuccessResponse());
106
107         WorkflowRequest workflowRequest =
108                 getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
109                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
110
111         WorkFlowManagerImpl mockManager = Mockito.spy(workflowManger);
112         Mockito.when(mockManager.workflowExecute(anyString(), anyString(), anyString(), anyString(), anyObject()))
113                 .thenReturn(createSvcExexuteSuccessResponse());
114
115         WorkflowResponse response = mockManager.executeWorkflow(workflowRequest);
116         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
117
118
119     }
120
121     @Test
122     public void testExecuteWorkFlowFalse() throws SvcLogicException, Exception {
123
124         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
125                 .thenReturn(getWorkFlowKey());
126         // Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
127         //        .thenReturn(createSvcExexuteFailureResponse());
128
129         WorkflowRequest workflowRequest =
130                 getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
131                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
132
133         WorkFlowManagerImpl mockManager = Mockito.spy(workflowManger);
134         Mockito.when(mockManager.workflowExecute(anyString(), anyString(), anyString(), anyString(), anyObject()))
135                 .thenReturn(createSvcExexuteFailureResponse());
136
137         WorkflowResponse response = mockManager.executeWorkflow(workflowRequest);
138         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
139
140     }
141
142     @Test
143     public void testExecuteWorkFlowAPIVersionStartWithOne() throws SvcLogicException, Exception {
144         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
145                 .thenReturn(getWorkFlowKey());
146         // Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
147         //        .thenReturn(createSvcExexuteSuccessResponse());
148
149         WorkflowRequest workflowRequest =
150                 getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
151                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
152
153         WorkFlowManagerImpl mockManager = Mockito.spy(workflowManger);
154         Mockito.when(mockManager.workflowExecute(anyString(), anyString(), anyString(), anyString(), anyObject()))
155                 .thenReturn(createSvcExexuteSuccessResponse());
156
157         WorkflowResponse response = mockManager.executeWorkflow(workflowRequest);
158         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
159     }
160
161     @Test
162     public void testExecuteWorkFlowException() throws SvcLogicException, Exception {
163         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
164                 .thenReturn(getWorkFlowKey());
165         // Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
166         //        .thenReturn(createSvcExexuteSuccessResponse());
167
168         WorkflowRequest workflowRequest =
169                 Mockito.spy(getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
170                     VNFOperation.Lock, "mj13", Constants.PAYLOAD));
171         Mockito.when(workflowRequest.getRequestContext()).thenThrow(new RuntimeException());
172
173         WorkFlowManagerImpl mockManager = Mockito.spy(workflowManger);
174         Mockito.when(mockManager.workflowExecute(anyString(), anyString(), anyString(), anyString(), anyObject()))
175                 .thenReturn(createSvcExexuteSuccessResponse());
176
177         WorkflowResponse response = mockManager.executeWorkflow(workflowRequest);
178         Mockito.verify(workflowRequest, Mockito.times(2)).getResponseContext();
179     }
180
181     @Test
182     public void testExecuteWorkAPIVersion1Exception() throws SvcLogicException, Exception {
183         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
184                 .thenReturn(getWorkFlowKey());
185         // Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
186         //        .thenReturn(createSvcExexuteSuccessResponse());
187
188         WorkflowRequest workflowRequest =
189                 getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
190                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
191         PowerMockito.mockStatic(ObjectMapper.class);
192         PowerMockito.when(ObjectMapper.map(Mockito.any())).thenThrow(new RuntimeException());
193         WorkFlowManagerImpl mockManager = Mockito.spy(workflowManger);
194         Mockito.when(mockManager.workflowExecute(anyString(), anyString(), anyString(), anyString(), anyObject()))
195                 .thenReturn(createSvcExexuteSuccessResponse());
196
197         WorkflowResponse response = mockManager.executeWorkflow(workflowRequest);
198         Assert.assertEquals(501, response.getResponseContext().getStatus().getCode());
199     }
200
201     @Test
202     public void testWorkFlowExist() throws SvcLogicException {
203         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
204                 .thenReturn(getWorkFlowKey());
205         Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString()))
206                 .thenReturn(true);
207
208         WorkflowRequest workflowRequest =
209                 getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
210                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
211
212         WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
213
214         Assert.assertTrue(response.isMappingExist());
215     }
216
217     @Test
218     public void testWorkFlowNotExist() throws SvcLogicException {
219         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
220                 .thenReturn(getWorkFlowKey());
221         Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString()))
222                 .thenReturn(false);
223
224         WorkflowRequest workflowRequest =
225                 getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
226                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
227
228         WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
229
230         Assert.assertTrue(response.isMappingExist());
231     }
232
233     @Test
234     public void testWorkFlowExistNullKey() throws SvcLogicException {
235         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString()))
236                 .thenReturn(null);
237         Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString()))
238                 .thenReturn(true);
239         WorkflowRequest workflowRequest =
240                 getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
241                     VNFOperation.Lock, "mj13", Constants.PAYLOAD);
242         WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
243         Assert.assertFalse(response.isMappingExist());
244     }
245
246     @Test
247     public void testWorkflowResolver() throws SQLException {
248         PowerMockito.mockStatic(DBUtils.class);
249         Connection mockConnection = Mockito.mock(Connection.class);
250         PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
251         ResultSet mockResultSet = Mockito.mock(ResultSet.class);
252         PowerMockito.when(DBUtils.getConnection(Mockito.anyString())).thenReturn(mockConnection);
253         PowerMockito.when(mockConnection.prepareStatement(Mockito.anyString())).thenReturn(mockStatement);
254         PowerMockito.when(mockStatement.executeQuery()).thenReturn(mockResultSet);
255         PowerMockito.when(mockResultSet.next()).thenReturn(true);
256         workflowResolver = new WorkflowResolver(1);
257         expectedEx.expect(IllegalArgumentException.class);
258         expectedEx.expectMessage("Duplicated configuration entry: ");
259         workflowResolver.resolve("ACTION", "VNF_TYPE", "VNF_VERSION", "API_VERSION");
260     }
261
262     private WorkflowRequest getWorkflowRequest(String vnfType, int ttl, Date timeStamp, String apiVersion,
263             String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId,
264             String payload) {
265         WorkflowRequest workflowRequest = new WorkflowRequest();
266         RuntimeContext runtimeContext = createRuntimeContext();
267
268         runtimeContext.getRequestContext().getCommonHeader().getFlags().setTtl(ttl);
269         runtimeContext.getRequestContext().getCommonHeader().setApiVer(apiVersion);
270         runtimeContext.getRequestContext().getCommonHeader().setTimestamp(timeStamp);
271         runtimeContext.getRequestContext().getCommonHeader().setRequestId(requestId);
272         runtimeContext.getRequestContext().getCommonHeader().setSubRequestId(subRequestID);
273         runtimeContext.getRequestContext().getCommonHeader().setOriginatorId(originatorID);
274         runtimeContext.getRequestContext().setAction(action);
275         runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
276         runtimeContext.getRequestContext().setPayload(payload);
277
278         runtimeContext.getVnfContext().setType(vnfType);
279         runtimeContext.getVnfContext().setId(vnfId);
280
281         workflowRequest.setRequestContext(runtimeContext.getRequestContext());
282         workflowRequest.setResponseContext(runtimeContext.getResponseContext());
283         workflowRequest.setVnfContext(runtimeContext.getVnfContext());
284
285         return workflowRequest;
286     }
287
288     private RequestContext creatRequestContext() {
289         RequestContext requestContext = new RequestContext();
290         CommonHeader commonHeader = new CommonHeader();
291         Flags flags = new Flags();
292         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
293         commonHeader.setFlags(flags);
294         requestContext.setCommonHeader(commonHeader);
295         requestContext.setActionIdentifiers(actionIdentifiers);
296
297         return requestContext;
298     }
299
300     private ResponseContext createResponseContext() {
301         ResponseContext responseContext = new ResponseContext();
302         CommonHeader commonHeader = new CommonHeader();
303         Flags flags = new Flags();
304         Status status = new Status();
305         responseContext.setCommonHeader(commonHeader);
306         responseContext.setStatus(status);
307         commonHeader.setFlags(flags);
308
309         return responseContext;
310     }
311
312     private RuntimeContext createRuntimeContext() {
313         RuntimeContext runtimeContext = new RuntimeContext();
314         RequestContext requestContext = creatRequestContext();
315         ResponseContext responseContext = createResponseContext();
316         runtimeContext.setRequestContext(requestContext);
317         runtimeContext.setResponseContext(responseContext);
318         VNFContext vnfContext = new VNFContext();
319         runtimeContext.setVnfContext(vnfContext);
320
321         return runtimeContext;
322     }
323
324     public WorkflowKey getWorkFlowKey() {
325         WorkflowKey workflowKey = new WorkflowKey("APPCDG", "2.0.0.0", "dgModule");
326
327         return workflowKey;
328     }
329
330     private Properties createSvcExexuteSuccessResponse() {
331         Properties properties = new Properties();
332         properties.setProperty("output.payload", "success");
333         properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "success");
334         properties.setProperty(Constants.DG_OUTPUT_STATUS_CODE, "400");
335         properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "success");
336
337         return properties;
338     }
339
340     private Properties createSvcExexuteFailureResponse() {
341         Properties properties = new Properties();
342         properties.setProperty("output.payload", "failure");
343         properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "failure");
344         properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "failure");
345
346         return properties;
347     }
348
349 }