aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-workflow-management/appc-workflow-management-core/src/test/java/org/onap/appc/workflow/impl/TestWorkFlowManager.java
blob: 8689fa70d9487852fc06a091925e7248ebf1e72a (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * Modifications (C) 2019 Ericsson
 * =============================================================================
 * 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.onap.appc.workflow.impl;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.onap.appc.common.constant.Constants;
import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import org.onap.appc.dao.util.DBUtils;
import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
import org.onap.appc.domainmodel.lcm.CommonHeader;
import org.onap.appc.domainmodel.lcm.Flags;
import org.onap.appc.domainmodel.lcm.RequestContext;
import org.onap.appc.domainmodel.lcm.ResponseContext;
import org.onap.appc.domainmodel.lcm.RuntimeContext;
import org.onap.appc.domainmodel.lcm.Status;
import org.onap.appc.domainmodel.lcm.VNFContext;
import org.onap.appc.domainmodel.lcm.VNFOperation;
import org.onap.appc.util.ObjectMapper;
import org.onap.appc.workflow.impl.WorkFlowManagerImpl;
import org.onap.appc.workflow.impl.WorkflowKey;
import org.onap.appc.workflow.impl.WorkflowResolver;
import org.onap.appc.workflow.objects.WorkflowExistsOutput;
import org.onap.appc.workflow.objects.WorkflowRequest;
import org.onap.appc.workflow.objects.WorkflowResponse;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ConfigurationFactory.class, DBUtils.class, ObjectMapper.class})
public class TestWorkFlowManager {

    @Rule
    public ExpectedException expectedEx = ExpectedException.none();
    
    public TestWorkFlowManager() {
    }

    @InjectMocks
    public WorkFlowManagerImpl workflowManger;

    WorkflowResolver workflowResolver;
    public SvcLogicService svcLogicService;

    @Before
    public void init(){
        PowerMockito.mockStatic(ConfigurationFactory.class);
        Configuration mockConfiguration = Mockito.mock(Configuration.class);
        PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfiguration);
        this.workflowResolver = Mockito.mock(WorkflowResolver.class);
        this.svcLogicService = Mockito.mock(SvcLogicService.class);
        workflowManger.setWorkflowResolver(workflowResolver);
        workflowManger.setSvcLogicServiceRef(svcLogicService);

    }
    @Test
    public void testExecuteWorkFlow() throws  SvcLogicException{

        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
            .thenReturn(createSvcExexuteSuccessResponse());

        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);

        WorkflowResponse response = workflowManger.executeWorkflow(workflowRequest);
        Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));


    }

    @Test
    public  void testExecuteWorkFlowFalse() throws SvcLogicException{

        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
            .thenReturn(createSvcExexuteFailureResponse());

        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);

        WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
        Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));

    }

    @Test
    public void testExecuteWorkFlowAPIVersionStartWithOne() throws SvcLogicException{
        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
            .thenReturn(createSvcExexuteSuccessResponse());

        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);

        WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
        Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
    }

    @Test
    public void testExecuteWorkFlowException() throws  SvcLogicException{
        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
            .thenReturn(createSvcExexuteSuccessResponse());

        WorkflowRequest workflowRequest = Mockito.spy(getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD));
        Mockito.when(workflowRequest.getRequestContext()).thenThrow(new RuntimeException());

        WorkflowResponse response = workflowManger.executeWorkflow(workflowRequest);
        Mockito.verify(workflowRequest, Mockito.times(2)).getResponseContext();
    }

    @Test
    public  void testExecuteWorkAPIVersion1Exception() throws SvcLogicException{
        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.execute(anyString(), anyString(), anyString(), anyString(), anyObject()))
            .thenReturn(createSvcExexuteSuccessResponse());

        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "1.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);
        PowerMockito.mockStatic(ObjectMapper.class);
        PowerMockito.when(ObjectMapper.map(Mockito.any())).thenThrow(new RuntimeException());
        WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
        Assert.assertEquals(501, response.getResponseContext().getStatus().getCode());
    }

    @Test
    public void testWorkFlowExist() throws  SvcLogicException{
        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(true);

        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);

        WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);

        Assert.assertTrue(response.isMappingExist());
    }

    @Test
    public void testWorkFlowNotExist() throws  SvcLogicException {
        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(getWorkFlowKey());
        Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(false);

        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);

        WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);

        Assert.assertTrue(response.isMappingExist());
    }

    @Test
    public void testWorkFlowExistNullKey() throws  SvcLogicException{
        Mockito.when(workflowResolver.resolve(anyString(), anyString(), anyString(), anyString())).thenReturn(null);
        Mockito.when(svcLogicService.hasGraph(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
        WorkflowRequest workflowRequest = getWorkflowRequest("vSCP", 300, new Date(), "2.00", "ST_249", "O1652", "uid34",
                VNFOperation.Lock, "mj13", Constants.PAYLOAD);
        WorkflowExistsOutput response = workflowManger.workflowExists(workflowRequest);
        Assert.assertFalse(response.isMappingExist());
    }

    @Test
    public void testWorkflowResolver() throws SQLException {
        PowerMockito.mockStatic(DBUtils.class);
        Connection mockConnection = Mockito.mock(Connection.class);
        PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
        ResultSet mockResultSet = Mockito.mock(ResultSet.class);
        PowerMockito.when(DBUtils.getConnection(Mockito.anyString())).thenReturn(mockConnection);
        PowerMockito.when(mockConnection.prepareStatement(Mockito.anyString())).thenReturn(mockStatement);
        PowerMockito.when(mockStatement.executeQuery()).thenReturn(mockResultSet);
        PowerMockito.when(mockResultSet.next()).thenReturn(true);
        workflowResolver = new WorkflowResolver(1);
        expectedEx.expect(IllegalArgumentException.class);
        expectedEx.expectMessage("Duplicated configuration entry: ");
        workflowResolver.resolve("ACTION", "VNF_TYPE", "VNF_VERSION", "API_VERSION");
    }

    private WorkflowRequest getWorkflowRequest(String vnfType, int ttl,  Date timeStamp, String apiVersion, String requestId,
            String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload) {
        WorkflowRequest workflowRequest = new WorkflowRequest();
        RuntimeContext runtimeContext = createRuntimeContext();

        runtimeContext.getRequestContext().getCommonHeader().getFlags().setTtl(ttl);
        runtimeContext.getRequestContext().getCommonHeader().setApiVer(apiVersion);
        runtimeContext.getRequestContext().getCommonHeader().setTimestamp(timeStamp);
        runtimeContext.getRequestContext().getCommonHeader().setRequestId(requestId);
        runtimeContext.getRequestContext().getCommonHeader().setSubRequestId(subRequestID);
        runtimeContext.getRequestContext().getCommonHeader().setOriginatorId(originatorID);
        runtimeContext.getRequestContext().setAction(action);
        runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
        runtimeContext.getRequestContext().setPayload(payload);

        runtimeContext.getVnfContext().setType(vnfType);
        runtimeContext.getVnfContext().setId(vnfId);

        workflowRequest.setRequestContext(runtimeContext.getRequestContext());
        workflowRequest.setResponseContext(runtimeContext.getResponseContext());
        workflowRequest.setVnfContext(runtimeContext.getVnfContext());

        return  workflowRequest;
    }

    private RequestContext creatRequestContext(){
        RequestContext requestContext = new RequestContext();
        CommonHeader commonHeader = new CommonHeader();
        Flags flags = new Flags();
        ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
        commonHeader.setFlags(flags);
        requestContext.setCommonHeader(commonHeader);
        requestContext.setActionIdentifiers(actionIdentifiers);

        return  requestContext;
    }

    private  ResponseContext createResponseContext(){
        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;
    }

    private RuntimeContext createRuntimeContext(){
        RuntimeContext runtimeContext = new RuntimeContext();
        RequestContext requestContext = creatRequestContext();
        ResponseContext responseContext = createResponseContext();
        runtimeContext.setRequestContext(requestContext);
        runtimeContext.setResponseContext(responseContext);
        VNFContext vnfContext = new VNFContext();
        runtimeContext.setVnfContext(vnfContext);

        return runtimeContext;
    }

    public WorkflowKey getWorkFlowKey(){
        WorkflowKey workflowKey = new WorkflowKey("APPCDG", "2.0.0.0", "dgModule");

        return workflowKey;
    }

    private Properties createSvcExexuteSuccessResponse(){
        Properties properties = new Properties();
        properties.setProperty("output.payload", "success");
        properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "success");
        properties.setProperty(Constants.DG_OUTPUT_STATUS_CODE, "400");
        properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "success");

        return properties;
    }

    private Properties createSvcExexuteFailureResponse(){
        Properties properties = new Properties();
        properties.setProperty("output.payload", "failure");
        properties.setProperty(Constants.DG_ATTRIBUTE_STATUS, "failure");
        properties.setProperty(Constants.DG_OUTPUT_STATUS_MESSAGE, "failure");

        return properties;
    }

}