af7005365e32b92afa96994d2e684edde1dbe297
[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 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.workflow.impl;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mockito;
33 import org.onap.appc.domainmodel.lcm.*;
34 import org.onap.appc.workflow.impl.WorkFlowManagerImpl;
35 import org.onap.appc.workflow.impl.WorkflowKey;
36 import org.onap.appc.workflow.impl.WorkflowResolver;
37 import org.onap.appc.workflow.objects.WorkflowExistsOutput;
38 import org.onap.appc.workflow.objects.WorkflowRequest;
39 import org.onap.appc.workflow.objects.WorkflowResponse;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
44 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicActivator;
45 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
46 import org.osgi.framework.Bundle;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.FrameworkUtil;
49 import org.osgi.framework.ServiceReference;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52
53 import java.util.Date;
54 import java.util.Properties;
55
56 import static org.mockito.Matchers.anyObject;
57 import static org.mockito.Matchers.anyString;
58
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest( {  WorkflowResolver.class,WorkFlowManagerImpl.class} )
61 public class TestWorkFlowManager {
62     public TestWorkFlowManager() {
63     }
64
65     @InjectMocks
66     public WorkFlowManagerImpl workflowManger;
67
68     WorkflowResolver workflowResolver;
69     public SvcLogicService svcLogicService;
70
71     @Before
72     public void init(){
73
74         this.workflowResolver= Mockito.mock(WorkflowResolver.class);
75         this.svcLogicService=Mockito.mock(SvcLogicService.class);
76         workflowManger.setWorkflowResolver(workflowResolver);
77         workflowManger.setSvcLogicServiceRef(svcLogicService);
78
79     }
80     @Test
81     public void testExecuteWorkFlow() throws  SvcLogicException{
82
83         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
84         Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteSuccessResponse());
85
86         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
87
88         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
89         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
90
91
92     }
93
94     @Test
95     public  void testExecuteWorkFlowFalse() throws SvcLogicException{
96
97         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
98         Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteFailureResponse());
99
100         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
101
102         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
103         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
104
105     }
106
107     @Test
108     public void testExecuteWorkFlowAPIVersionStartWithOne() throws SvcLogicException{
109         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
110         Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteSuccessResponse());
111
112         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "1.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
113
114         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
115         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
116     }
117
118     @Test
119     public void testWorkFlowExist() throws  SvcLogicException{
120         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
121         Mockito.when(svcLogicService.hasGraph(anyString(),anyString(), anyString(),anyString())).thenReturn(true);
122
123         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
124
125         WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
126
127         Assert.assertTrue(response.isMappingExist());
128     }
129
130     @Test
131     public void testWorkFlowNotExist() throws  SvcLogicException{
132         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
133         Mockito.when(svcLogicService.hasGraph(anyString(),anyString(), anyString(),anyString())).thenReturn(false);
134
135         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
136
137         WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
138
139         Assert.assertTrue(response.isMappingExist());
140     }
141
142     private   WorkflowRequest getWorkflowRequest(String vnfType, int ttl,  Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId ,String payload){
143         WorkflowRequest workflowRequest=new WorkflowRequest();
144         RuntimeContext runtimeContext=createRuntimeContext();
145
146         runtimeContext.getRequestContext().getCommonHeader().getFlags().setTtl(ttl);
147         runtimeContext.getRequestContext().getCommonHeader().setApiVer(apiVersion);
148         runtimeContext.getRequestContext().getCommonHeader().setTimestamp(timeStamp);
149         runtimeContext.getRequestContext().getCommonHeader().setRequestId(requestId);
150         runtimeContext.getRequestContext().getCommonHeader().setSubRequestId(subRequestID);
151         runtimeContext.getRequestContext().getCommonHeader().setOriginatorId(originatorID);
152         runtimeContext.getRequestContext().setAction(action);
153         runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
154         runtimeContext.getRequestContext().setPayload(payload);
155
156         runtimeContext.getVnfContext().setType(vnfType);
157         runtimeContext.getVnfContext().setId(vnfId);
158
159         workflowRequest.setRequestContext(runtimeContext.getRequestContext());
160         workflowRequest.setResponseContext(runtimeContext.getResponseContext());
161         workflowRequest.setVnfContext(runtimeContext.getVnfContext());
162
163         return  workflowRequest;
164     }
165
166     private RequestContext creatRequestContext(){
167         RequestContext requestContext=new RequestContext();
168         CommonHeader commonHeader = new CommonHeader();
169         Flags flags = new Flags();
170         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
171         commonHeader.setFlags(flags);
172         requestContext.setCommonHeader(commonHeader);
173         requestContext.setActionIdentifiers(actionIdentifiers);
174
175         return  requestContext;
176     }
177     private  ResponseContext createResponseContext(){
178         ResponseContext responseContext=new ResponseContext();
179         CommonHeader commonHeader = new CommonHeader();
180         Flags flags = new Flags();
181         Status status = new Status();
182         responseContext.setCommonHeader(commonHeader);
183         responseContext.setStatus(status);
184         commonHeader.setFlags(flags);
185
186         return  responseContext;
187     }
188     private RuntimeContext createRuntimeContext(){
189         RuntimeContext runtimeContext=new RuntimeContext();
190         RequestContext requestContext=creatRequestContext();
191         ResponseContext responseContext=createResponseContext();
192         runtimeContext.setRequestContext(requestContext);
193         runtimeContext.setResponseContext(responseContext);
194         VNFContext vnfContext=new VNFContext();
195         runtimeContext.setVnfContext(vnfContext);
196
197         return runtimeContext;
198     }
199
200     public WorkflowKey getWorkFlowKey(){
201         WorkflowKey workflowKey=new WorkflowKey("APPCDG","2.0.0.0","dgModule");
202
203         return workflowKey;
204     }
205
206     private Properties createSvcExexuteSuccessResponse(){
207         Properties properties=new Properties();
208         properties.setProperty("output.payload","success");
209         properties.setProperty("SvcLogic.status","success");
210         properties.setProperty("output.status.code","400");
211         properties.setProperty("output.status.message","success");
212
213         return properties;
214     }
215
216     private Properties createSvcExexuteFailureResponse(){
217         Properties properties=new Properties();
218         properties.setProperty("output.payload","failure");
219         properties.setProperty("SvcLogic.status","failure");
220         properties.setProperty("output.status.message","failure");
221
222         return properties;
223     }
224
225 }