2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.workflow;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mockito;
29 import org.openecomp.appc.workflow.impl.WorkFlowManagerImpl;
30 import org.openecomp.appc.workflow.objects.WorkflowRequest;
31 import org.openecomp.sdnc.sli.SvcLogicException;
32 import org.openecomp.sdnc.sli.SvcLogicGraph;
33 import org.openecomp.sdnc.sli.SvcLogicNode;
34 import org.openecomp.sdnc.sli.SvcLogicStore;
35 import org.openecomp.sdnc.sli.provider.SvcLogicActivator;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest( {SvcLogicActivator.class, FrameworkUtil.class, WorkFlowManagerImpl.class} )
45 public class TestWorkFlowManager {
46 public TestWorkFlowManager() {
49 private WorkFlowManagerImpl workflowManger ;
50 private String command="Configure";
51 protected SvcLogicGraph svcLogicGraph=null;
55 private final SvcLogicStore svcLogicStore= Mockito.mock(SvcLogicStore.class);
56 private final BundleContext bundleContext=Mockito.mock(BundleContext.class);
57 private final Bundle bundleSvcLogicService=Mockito.mock(Bundle.class);
58 private final ServiceReference serviceReferenceSvcLogicService=Mockito.mock(ServiceReference.class);
63 public void setupMock() throws Exception {
66 dao = Mockito.mock(AppcDAOImpl.class);
67 PowerMockito.whenNew(AppcDAOImpl.class).withNoArguments().thenReturn(dao);
70 SvcLogicServiceImpl svcLogicService=new SvcLogicServiceImpl();
71 PowerMockito.mockStatic(SvcLogicActivator.class);
72 PowerMockito.mockStatic(FrameworkUtil.class);
73 PowerMockito.when(SvcLogicActivator.getStore()).thenReturn(svcLogicStore);
74 PowerMockito.when(FrameworkUtil.getBundle(SvcLogicService.class)).thenReturn(bundleSvcLogicService);
75 PowerMockito.when(bundleSvcLogicService.getBundleContext()).thenReturn(bundleContext);
76 PowerMockito.when(bundleContext.getServiceReference(SvcLogicService.NAME)).thenReturn(serviceReferenceSvcLogicService);
77 PowerMockito.when(bundleContext.getService(serviceReferenceSvcLogicService)).thenReturn(svcLogicService);
80 PowerMockito.when(svcLogicStore.fetch(anyString(), eq("FIREWALL_Configure"), anyString(), anyString())).thenReturn(createGraph("FIREWALL_Configure"));
81 PowerMockito.when(svcLogicStore.fetch(anyString(), eq("FIREWALL_Restart"), anyString(), anyString())).thenReturn(createGraph("FIREWALL_Restart"));
82 PowerMockito.when(svcLogicStore.fetch(anyString(), eq("FIREWALL_Test"), anyString(), anyString())).thenReturn(createGraph("FIREWALL_Test"));
83 PowerMockito.when(svcLogicStore.fetch(anyString(), eq("FIREWALL_Rebuild"), anyString(), anyString())).thenReturn(createGraph("FIREWALL_Rebuild"));
84 PowerMockito.when(svcLogicStore.fetch(anyString(), eq("FIREWALL_Terminate"), anyString(), anyString())).thenReturn(createGraph("FIREWALL_Terminate"));
85 PowerMockito.when(svcLogicStore.fetch(anyString(), eq("FIREWALL_Start"), anyString(), anyString())).thenReturn(createGraph("FIREWALL_Start"));
86 svcLogicService.registerExecutor("switch", new SwitchNodeExecutor());
87 svcLogicService.registerExecutor("execute",new ReturnNodeExecutor());
88 svcLogicService.registerExecutor("return",new ReturnNodeExecutor());
89 } catch (SvcLogicException e) {
93 workflowManger = new WorkFlowManagerImpl();
95 PowerMockito.when(getDao().retrieveWorkflowDetails("FIREWALL","Configure")).thenReturn(getWorkflow());
96 PowerMockito.when(getDao().retrieveWorkflowDetails("FIREWALL","")).thenThrow(new DAOException());
97 PowerMockito.when(getDao().retrieveWorkflowDetails("","Configure")).thenThrow(new DAOException());
102 public void testEmptyVnfTypeFlow(){
104 WorkflowRequest workflowRequest = getWorkflowRequest("","1","1",command);
105 setSvcLogicGraph(createGraph(""+"_"+command));
106 WorkflowResponse response =workflowManger.executeWorkflow(workflowRequest);
107 assertFalse(response.isExecutionSuccess());
113 public void testExecuteWorkflow(){
114 //PowerMockito.when(getDao().retrieveWorkflowDetails(anyString(),anyString())).thenReturn(getWorkflow());
115 WorkflowRequest workflowRequest = getWorkflowRequest("FIREWALL","1","1",command);
116 setSvcLogicGraph(createGraph("FIREWALL"+"_"+command));
117 WorkflowResponse response =workflowManger.executeWorkflow(workflowRequest);
118 assertFalse(response.isExecutionSuccess());
122 public void testExecuteWorkflowEmptyPayload(){
123 //PowerMockito.when(getDao().retrieveWorkflowDetails(anyString(),anyString())).thenReturn(getWorkflow());
124 WorkflowRequest workflowRequest = getWorkflowRequest("FIREWALL","1","1",command);
125 workflowRequest.setPayload("{payload:\"payload\"}");
126 setSvcLogicGraph(createGraph(""+"_"+command));
127 WorkflowResponse response =workflowManger.executeWorkflow(workflowRequest);
128 assertFalse(response.isExecutionSuccess());
132 public void testWorkflowExist(){
133 //PowerMockito.when(getDao().queryWorkflow(anyString(),anyString())).thenReturn(true);
134 WorkflowRequest workflowRequest = getWorkflowRequest("FIREWALL","1","1",command);
135 boolean success = workflowManger.workflowExists(workflowRequest);
140 public void testWorkflowExistFalse(){
141 //PowerMockito.when(getDao().queryWorkflow(anyString(),anyString())).thenReturn(false);
142 WorkflowRequest workflowRequest = getWorkflowRequest("FIREWALL","1","1",command);
143 setSvcLogicGraph(createGraph(""+"_"+command));
144 boolean success = workflowManger.workflowExists(workflowRequest);
145 assertFalse(success);
150 public void testEmptyCommandFlow(){
151 WorkflowRequest workflowRequest = getWorkflowRequest("FIREWALL","1","1","");
152 WorkflowResponse response =workflowManger.executeWorkflow(workflowRequest);
153 assertFalse(response.isExecutionSuccess());
158 public void setSvcLogicGraph(SvcLogicGraph svcLogicGraph) {
159 this.svcLogicGraph = svcLogicGraph;
162 public SvcLogicGraph getSvcLogicGraph() {
163 return svcLogicGraph;
166 protected SvcLogicGraph createGraph(String rpc) {
167 SvcLogicGraph svcLogicGraph = new SvcLogicGraph();
168 svcLogicGraph.setModule("APPC");
169 svcLogicGraph.setRpc(rpc);
170 svcLogicGraph.setMode("sync");
171 svcLogicGraph.setVersion("2.0.0");
172 SvcLogicNode svcLogicRootNode = new SvcLogicNode(1, "switch", svcLogicGraph);
173 SvcLogicNode svcLogicConfigureNode = new SvcLogicNode(2, "return", svcLogicGraph);
174 SvcLogicNode svcLogicOtherNode = new SvcLogicNode(3, "return", svcLogicGraph);
176 svcLogicConfigureNode.setAttribute("status", "success");
177 svcLogicOtherNode.setAttribute("status", "failure");
178 svcLogicRootNode.setAttribute("test", "$org.openecomp.appc.action");
179 svcLogicRootNode.addOutcome("Configure", svcLogicConfigureNode);
180 svcLogicRootNode.addOutcome("Other", svcLogicOtherNode);
181 } catch (SvcLogicException e) {
184 svcLogicGraph.setRootNode(svcLogicRootNode);
185 return svcLogicGraph;