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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.workflow.impl;
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;
53 import java.util.Date;
54 import java.util.Properties;
56 import static org.mockito.Matchers.anyObject;
57 import static org.mockito.Matchers.anyString;
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest( { WorkflowResolver.class,WorkFlowManagerImpl.class} )
61 public class TestWorkFlowManager {
62 public TestWorkFlowManager() {
66 public WorkFlowManagerImpl workflowManger;
68 WorkflowResolver workflowResolver;
69 public SvcLogicService svcLogicService;
74 this.workflowResolver= Mockito.mock(WorkflowResolver.class);
75 this.svcLogicService=Mockito.mock(SvcLogicService.class);
76 workflowManger.setWorkflowResolver(workflowResolver);
77 workflowManger.setSvcLogicServiceRef(svcLogicService);
81 public void testExecuteWorkFlow() throws SvcLogicException{
83 Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
84 Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteSuccessResponse());
86 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
88 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
89 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
95 public void testExecuteWorkFlowFalse() throws SvcLogicException{
97 Mockito.when(workflowResolver.resolve(anyString(),anyString(),anyString(),anyString())).thenReturn(getWorkFlowKey());
98 Mockito.when(svcLogicService.execute(anyString(),anyString(), anyString(),anyString(),anyObject())).thenReturn(createSvcExexuteFailureResponse());
100 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
102 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
103 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("failure"));
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());
112 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "1.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
114 WorkflowResponse response=workflowManger.executeWorkflow(workflowRequest);
115 Assert.assertTrue(response.getResponseContext().getStatus().getMessage().equals("success"));
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);
123 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
125 WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
127 Assert.assertTrue(response.isMappingExist());
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);
135 WorkflowRequest workflowRequest =getWorkflowRequest("vSCP",300,new Date(), "2.00" ,"ST_249","O1652", "uid34", VNFOperation.Lock,"mj13","Payload");
137 WorkflowExistsOutput response=workflowManger.workflowExists(workflowRequest);
139 Assert.assertTrue(response.isMappingExist());
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();
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);
156 runtimeContext.getVnfContext().setType(vnfType);
157 runtimeContext.getVnfContext().setId(vnfId);
159 workflowRequest.setRequestContext(runtimeContext.getRequestContext());
160 workflowRequest.setResponseContext(runtimeContext.getResponseContext());
161 workflowRequest.setVnfContext(runtimeContext.getVnfContext());
163 return workflowRequest;
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);
175 return requestContext;
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);
186 return responseContext;
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);
197 return runtimeContext;
200 public WorkflowKey getWorkFlowKey(){
201 WorkflowKey workflowKey=new WorkflowKey("APPCDG","2.0.0.0","dgModule");
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");
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");