eb8a8d4f63ec7d394f501e00d4bb361f1643caee
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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.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;
66
67 import static org.mockito.Matchers.anyObject;
68 import static org.mockito.Matchers.anyString;
69
70 @RunWith(PowerMockRunner.class)
71 @PrepareForTest({ConfigurationFactory.class, DBUtils.class})
72 public class TestWorkFlowManager {
73
74     @Rule
75     public ExpectedException expectedEx = ExpectedException.none();
76     
77     public TestWorkFlowManager() {
78     }
79
80     @InjectMocks
81     public WorkFlowManagerImpl workflowManger;
82
83     WorkflowResolver workflowResolver;
84     public SvcLogicService svcLogicService;
85
86     @Before
87     public void init(){
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);
95
96     }
97     @Test
98     public void testExecuteWorkFlow() throws  SvcLogicException{
99
100         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
101         Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
102             .thenReturn(createSvcExexuteSuccessResponse());
103
104         WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
105                 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
106
107         WorkflowResponse response = workflowManger.executeWorkflow(workflowRequest);
108         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
109
110
111     }
112
113     @Test
114     public  void testExecuteWorkFlowFalse() throws SvcLogicException{
115
116         Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
117         Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
118             .thenReturn(createSvcExexuteFailureResponse());
119
120         WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
121                 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
122
123         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
124         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
125
126     }
127
128     @Test
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());
133
134         WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
135                 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
136
137         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
138         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
139     }
140
141     @Test
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);
145
146         WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
147                 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
148
149         WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
150
151         Assert.assertTrue(response.isMappingExist());
152     }
153
154     @Test
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);
158
159         WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
160                 VNFOperation.Lock, "mj13", Constants.PAYLOAD);
161
162         WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
163
164         Assert.assertTrue(response.isMappingExist());
165     }
166
167     @Test
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");
181     }
182
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();
187
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);
197
198         runtimeContext.getVnfContext().setType(vnfType);
199         runtimeContext.getVnfContext().setId(vnfId);
200
201         workflowRequest.setRequestContext(runtimeContext.getRequestContext());
202         workflowRequest.setResponseContext(runtimeContext.getResponseContext());
203         workflowRequest.setVnfContext(runtimeContext.getVnfContext());
204
205         return  workflowRequest;
206     }
207
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);
216
217         return  requestContext;
218     }
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);
227
228         return  responseContext;
229     }
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);
238
239         return runtimeContext;
240     }
241
242     public WorkflowKey getWorkFlowKey(){
243         WorkflowKey workflowKey = new WorkflowKey("APPCDG", "2.0.0.0", "dgModule");
244
245         return workflowKey;
246     }
247
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");
254
255         return properties;
256     }
257
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");
263
264         return properties;
265     }
266
267 }