2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.workflow.impl;
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;
44 import java.util.Date;
45 import java.util.Properties;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Matchers.anyString;
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest( { WorkflowResolver.class,WorkFlowManagerImpl.class} )
52 public class TestWorkFlowManager {
53 public TestWorkFlowManager() {
57 public WorkFlowManagerImpl workflowManger;
59 WorkflowResolver workflowResolver;
60 public SvcLogicService svcLogicService;
65 this.workflowResolver= Mockito.mock(WorkflowResolver.class);
66 this.svcLogicService=Mockito.mock(SvcLogicService.class);
67 workflowManger.setWorkflowResolver(workflowResolver);
68 workflowManger.setSvcLogicServiceRef(svcLogicService);
72 public void testExecuteWorkFlow() throws SvcLogicException{
74 Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
75 Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteSuccessResponse());
77 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
79 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
80 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
86 public void testExecuteWorkFlowFalse() throws SvcLogicException{
88 Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
89 Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteFailureResponse());
91 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
93 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
94 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
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());
103 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "1.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
105 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
106 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
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);
114 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
116 WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
118 Assert.assertTrue(response.isMappingExist());
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);
126 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
128 WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
130 Assert.assertTrue(response.isMappingExist());
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();
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);
147 runtimeContext.getVnfContext().setType(vnfType);
148 runtimeContext.getVnfContext().setId(vnfId);
150 workflowRequest.setRequestContext(runtimeContext.getRequestContext());
151 workflowRequest.setResponseContext(runtimeContext.getResponseContext());
152 workflowRequest.setVnfContext(runtimeContext.getVnfContext());
154 return workflowRequest;
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);
166 return requestContext;
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);
177 return responseContext;
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);
188 return runtimeContext;
191 public WorkflowKey getWorkFlowKey(){
192 WorkflowKey workflowKey=new WorkflowKey("APPCDG","2.0.0.0","dgModule");
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");
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");