7912ade9a0783de9d729838ff9eb75bf57b62fc5
[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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.workflow.impl;
25
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mockito;
32 import org.onap.appc.domainmodel.lcm.*;
33 import org.onap.appc.workflow.impl.WorkFlowManagerImpl;
34 import org.onap.appc.workflow.impl.WorkflowKey;
35 import org.onap.appc.workflow.impl.WorkflowResolver;
36 import org.onap.appc.workflow.objects.WorkflowExistsOutput;
37 import org.onap.appc.workflow.objects.WorkflowRequest;
38 import org.onap.appc.workflow.objects.WorkflowResponse;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43
44 import java.util.Date;
45 import java.util.Properties;
46
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Matchers.anyString;
49
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest( {  WorkflowResolver.class,WorkFlowManagerImpl.class} )
52 public class TestWorkFlowManager {
53     public TestWorkFlowManager() {
54     }
55
56     @InjectMocks
57     public WorkFlowManagerImpl workflowManger;
58
59     WorkflowResolver workflowResolver;
60     public SvcLogicService svcLogicService;
61
62     @Before
63     public void init(){
64
65         this.workflowResolver= Mockito.mock(WorkflowResolver.class);
66         this.svcLogicService=Mockito.mock(SvcLogicService.class);
67         workflowManger.setWorkflowResolver(workflowResolver);
68         workflowManger.setSvcLogicServiceRef(svcLogicService);
69
70     }
71     @Test
72     public void testExecuteWorkFlow() throws  SvcLogicException{
73
74         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
75         Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteSuccessResponse());
76
77         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
78
79         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
80         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
81
82
83     }
84
85     @Test
86     public  void testExecuteWorkFlowFalse() throws SvcLogicException{
87
88         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
89         Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteFailureResponse());
90
91         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
92
93         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
94         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
95
96     }
97
98     @Test
99     public void testExecuteWorkFlowAPIVersionStartWithOne() throws SvcLogicException{
100         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
101         Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteSuccessResponse());
102
103         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "1.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
104
105         WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
106         Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
107     }
108
109     @Test
110     public void testWorkFlowExist() throws  SvcLogicException{
111         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
112         Mockito.when(svcLogicService.hasGraph(anyString(),anyString(), anyString(),anyString())).thenReturn(true);
113
114         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
115
116         WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
117
118         Assert.assertTrue(response.isMappingExist());
119     }
120
121     @Test
122     public void testWorkFlowNotExist() throws  SvcLogicException{
123         Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
124         Mockito.when(svcLogicService.hasGraph(anyString(),anyString(), anyString(),anyString())).thenReturn(false);
125
126         WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
127
128         WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
129
130         Assert.assertTrue(response.isMappingExist());
131     }
132
133     private   WorkflowRequest getWorkflowRequest(String vnfType, int ttl,  Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId ,String payload){
134         WorkflowRequest workflowRequest=new WorkflowRequest();
135         RuntimeContext runtimeContext=createRuntimeContext();
136
137         runtimeContext.getRequestContext().getCommonHeader().getFlags().setTtl(ttl);
138         runtimeContext.getRequestContext().getCommonHeader().setApiVer(apiVersion);
139         runtimeContext.getRequestContext().getCommonHeader().setTimestamp(timeStamp);
140         runtimeContext.getRequestContext().getCommonHeader().setRequestId(requestId);
141         runtimeContext.getRequestContext().getCommonHeader().setSubRequestId(subRequestID);
142         runtimeContext.getRequestContext().getCommonHeader().setOriginatorId(originatorID);
143         runtimeContext.getRequestContext().setAction(action);
144         runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(vnfId);
145         runtimeContext.getRequestContext().setPayload(payload);
146
147         runtimeContext.getVnfContext().setType(vnfType);
148         runtimeContext.getVnfContext().setId(vnfId);
149
150         workflowRequest.setRequestContext(runtimeContext.getRequestContext());
151         workflowRequest.setResponseContext(runtimeContext.getResponseContext());
152         workflowRequest.setVnfContext(runtimeContext.getVnfContext());
153
154         return  workflowRequest;
155     }
156
157     private RequestContext creatRequestContext(){
158         RequestContext requestContext=new RequestContext();
159         CommonHeader commonHeader = new CommonHeader();
160         Flags flags = new Flags();
161         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
162         commonHeader.setFlags(flags);
163         requestContext.setCommonHeader(commonHeader);
164         requestContext.setActionIdentifiers(actionIdentifiers);
165
166         return  requestContext;
167     }
168     private  ResponseContext createResponseContext(){
169         ResponseContext responseContext=new ResponseContext();
170         CommonHeader commonHeader = new CommonHeader();
171         Flags flags = new Flags();
172         Status status = new Status();
173         responseContext.setCommonHeader(commonHeader);
174         responseContext.setStatus(status);
175         commonHeader.setFlags(flags);
176
177         return  responseContext;
178     }
179     private RuntimeContext createRuntimeContext(){
180         RuntimeContext runtimeContext=new RuntimeContext();
181         RequestContext requestContext=creatRequestContext();
182         ResponseContext responseContext=createResponseContext();
183         runtimeContext.setRequestContext(requestContext);
184         runtimeContext.setResponseContext(responseContext);
185         VNFContext vnfContext=new VNFContext();
186         runtimeContext.setVnfContext(vnfContext);
187
188         return runtimeContext;
189     }
190
191     public WorkflowKey getWorkFlowKey(){
192         WorkflowKey workflowKey=new WorkflowKey("APPCDG","2.0.0.0","dgModule");
193
194         return workflowKey;
195     }
196
197     private Properties createSvcExexuteSuccessResponse(){
198         Properties properties=new Properties();
199         properties.setProperty("output.payload","success");
200         properties.setProperty("SvcLogic.status","success");
201         properties.setProperty("output.status.code","400");
202         properties.setProperty("output.status.message","success");
203
204         return properties;
205     }
206
207     private Properties createSvcExexuteFailureResponse(){
208         Properties properties=new Properties();
209         properties.setProperty("output.payload","failure");
210         properties.setProperty("SvcLogic.status","failure");
211         properties.setProperty("output.status.message","failure");
212
213         return properties;
214     }
215
216 }