aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-workflow-management/appc-workflow-management-core/src/test/java/org/onap/appc/workflow/impl/TestWorkFlowManager.java
blob: af7005365e32b92afa96994d2e684edde1dbe297 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * 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.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.workflow.impl;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.onap.appc.domainmodel.lcm.*;
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.SvcLogicGraph;
import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
import org.onap.ccsdk.sli.core.sli.provider.SvcLogicActivator;
import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Date;
import java.util.Properties;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest( {  WorkflowResolver.class,WorkFlowManagerImpl.class} )
public class TestWorkFlowManager {
    public TestWorkFlowManager() {
    }

    @InjectMocks
    public WorkFlowManagerImpl workflowManger;

    WorkflowResolver workflowResolver;
    public SvcLogicService svcLogicService;

    @Before
    public void init(){

        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","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","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","Payload");

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

    @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","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","Payload");

        WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);

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

    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("SvcLogic.status","success");
        properties.setProperty("output.status.code","400");
        properties.setProperty("output.status.message","success");

        return properties;
    }

    private Properties createSvcExexuteFailureResponse(){
        Properties properties=new Properties();
        properties.setProperty("output.payload","failure");
        properties.setProperty("SvcLogic.status","failure");
        properties.setProperty("output.status.message","failure");

        return properties;
    }

}