b0a80ebc72aac0a1d02b51718f166ae773e151dc
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Matchers.anyString;
26 import static org.mockito.Matchers.isA;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.when;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Spy;
42 import org.onap.so.bpmn.BaseTaskTest;
43 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
44 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
45
46 public class WorkflowActionBBTasksTest extends BaseTaskTest {
47
48         @Mock
49         protected WorkflowAction workflowAction;
50         
51         @InjectMocks
52         @Spy
53         protected WorkflowActionBBTasks workflowActionBBTasks;
54         
55         private DelegateExecution execution;
56         
57         @Rule
58         public ExpectedException thrown = ExpectedException.none();
59         
60         @Before
61         public void before() throws Exception {
62                 execution = new DelegateExecutionFake();
63                 org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
64                 servInstance.setServiceInstanceId("TEST");
65                 when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
66                 workflowAction.setBbInputSetupUtils(bbSetupUtils);
67                 workflowAction.setBbInputSetup(bbInputSetup);
68         }
69         
70         @Test
71         public void selectBBTest() throws Exception{
72                 String gAction = "Delete-Network-Collection";
73                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
74                 execution.setVariable("requestAction", gAction);
75                 execution.setVariable("gCurrentSequence", 0);
76                 execution.setVariable("homing", false);
77                 execution.setVariable("calledHoming", false);
78                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
79                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
80                 flowsToExecute.add(ebb);
81                 execution.setVariable("flowsToExecute", flowsToExecute);
82                 workflowActionBBTasks.selectBB(execution);
83                 boolean success = (boolean) execution.getVariable("completed");
84                 assertEquals(true,success);
85         }
86         
87         @Test
88         public void select2BBTest() throws Exception{
89                 String gAction = "Delete-Network-Collection";
90                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
91                 execution.setVariable("requestAction", gAction);
92                 execution.setVariable("gCurrentSequence", 0);
93                 execution.setVariable("homing", false);
94                 execution.setVariable("calledHoming", false);
95                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
96                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
97                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
98                 flowsToExecute.add(ebb);
99                 flowsToExecute.add(ebb2);
100                 execution.setVariable("flowsToExecute", flowsToExecute);
101                 workflowActionBBTasks.selectBB(execution);
102                 boolean success = (boolean) execution.getVariable("completed");
103                 assertEquals(false,success);
104         }
105         
106         @Test
107         public void msoCompleteProcessTest() throws Exception{
108                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
109                 execution.setVariable("requestAction", "createInstance");
110                 execution.setVariable("resourceId", "123");
111                 execution.setVariable("source","MSO");
112                 execution.setVariable("resourceName", "Service");
113                 execution.setVariable("aLaCarte", true);
114                 workflowActionBBTasks.setupCompleteMsoProcess(execution);
115                 String response = (String) execution.getVariable("CompleteMsoProcessRequest");
116                 assertEquals(response,"<aetgt:MsoCompletionRequest xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\" xmlns:ns=\"http://org.onap/so/request/types/v1\"><request-info xmlns=\"http://org.onap/so/infra/vnf-request/v1\"><request-id>00f704ca-c5e5-4f95-a72c-6889db7b0688</request-id><action>createInstance</action><source>MSO</source></request-info><status-message>ALaCarte-Service-createInstance request was executed correctly.</status-message><serviceInstanceId>123</serviceInstanceId><mso-bpel-name>WorkflowActionBB</mso-bpel-name></aetgt:MsoCompletionRequest>");
117         }
118         
119         @Test
120         public void rollbackExecutionPathTest(){
121                 execution.setVariable("handlingCode", "Rollback");
122                 execution.setVariable("isRollback", false);
123                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
124                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
125                 BuildingBlock bb1 = new BuildingBlock();
126                 bb1.setBpmnFlowName("AssignVfModuleBB");
127                 ebb1.setBuildingBlock(bb1);
128                 flowsToExecute.add(ebb1);
129                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
130                 BuildingBlock bb2 = new BuildingBlock();
131                 bb2.setBpmnFlowName("CreateVfModuleBB");
132                 ebb2.setBuildingBlock(bb2);
133                 flowsToExecute.add(ebb2);
134                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
135                 BuildingBlock bb3 = new BuildingBlock();
136                 bb3.setBpmnFlowName("ActivateVfModuleBB");
137                 ebb3.setBuildingBlock(bb3);
138                 flowsToExecute.add(ebb3);
139                 
140                 execution.setVariable("flowsToExecute", flowsToExecute);
141                 execution.setVariable("gCurrentSequence", 3);
142                 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
143
144                 workflowActionBBTasks.rollbackExecutionPath(execution);
145                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
146                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB");
147                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
148                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
149                 assertEquals(0,execution.getVariable("gCurrentSequence"));
150         }
151         
152         @Test
153         public void rollbackExecutionPathUnfinishedFlowTest(){
154                 execution.setVariable("handlingCode", "Rollback");
155                 execution.setVariable("isRollback", false);
156                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
157                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
158                 BuildingBlock bb1 = new BuildingBlock();
159                 bb1.setBpmnFlowName("AssignVfModuleBB");
160                 ebb1.setBuildingBlock(bb1);
161                 flowsToExecute.add(ebb1);
162                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
163                 BuildingBlock bb2 = new BuildingBlock();
164                 bb2.setBpmnFlowName("CreateVfModuleBB");
165                 ebb2.setBuildingBlock(bb2);
166                 flowsToExecute.add(ebb2);
167                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
168                 BuildingBlock bb3 = new BuildingBlock();
169                 bb3.setBpmnFlowName("ActivateVfModuleBB");
170                 ebb3.setBuildingBlock(bb3);
171                 flowsToExecute.add(ebb3);
172                 
173                 execution.setVariable("flowsToExecute", flowsToExecute);
174                 execution.setVariable("gCurrentSequence", 2);
175                 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
176
177                 workflowActionBBTasks.rollbackExecutionPath(execution);
178                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
179                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
180                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");    
181                 assertEquals(0,execution.getVariable("gCurrentSequence"));
182                 assertEquals(0,execution.getVariable("retryCount"));
183         }
184         
185         @Test
186         public void rollbackExecutionTest(){
187                 execution.setVariable("handlingCode", "Rollback");
188                 execution.setVariable("isRollback", false);
189                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
190                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
191                 BuildingBlock bb1 = new BuildingBlock();
192                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
193                 ebb1.setBuildingBlock(bb1);
194                 flowsToExecute.add(ebb1);
195                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
196                 BuildingBlock bb2 = new BuildingBlock();
197                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
198                 ebb2.setBuildingBlock(bb2);
199                 flowsToExecute.add(ebb2);
200                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
201                 BuildingBlock bb3 = new BuildingBlock();
202                 bb3.setBpmnFlowName("AssignNetworkBB");
203                 ebb3.setBuildingBlock(bb3);
204                 flowsToExecute.add(ebb3);
205                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
206                 BuildingBlock bb4 = new BuildingBlock();
207                 bb4.setBpmnFlowName("CreateNetworkBB");
208                 ebb4.setBuildingBlock(bb4);
209                 flowsToExecute.add(ebb4);
210                 
211                 execution.setVariable("flowsToExecute", flowsToExecute);
212                 execution.setVariable("gCurrentSequence", 3);
213                 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
214
215                 workflowActionBBTasks.rollbackExecutionPath(execution);
216                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
217                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB");
218                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");     
219                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignServiceInstanceBB");
220                 assertEquals(0,execution.getVariable("gCurrentSequence"));
221         }
222         
223         @Test
224         public void rollbackExecutionRollbackToAssignedTest(){
225                 execution.setVariable("isRollback", false);
226                 execution.setVariable("handlingCode", "RollbackToAssigned");
227                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
228                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
229                 BuildingBlock bb1 = new BuildingBlock();
230                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
231                 ebb1.setBuildingBlock(bb1);
232                 flowsToExecute.add(ebb1);
233                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
234                 BuildingBlock bb2 = new BuildingBlock();
235                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
236                 ebb2.setBuildingBlock(bb2);
237                 flowsToExecute.add(ebb2);
238                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
239                 BuildingBlock bb3 = new BuildingBlock();
240                 bb3.setBpmnFlowName("AssignNetworkBB");
241                 ebb3.setBuildingBlock(bb3);
242                 flowsToExecute.add(ebb3);
243                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
244                 BuildingBlock bb4 = new BuildingBlock();
245                 bb4.setBpmnFlowName("CreateNetworkBB");
246                 ebb4.setBuildingBlock(bb4);
247                 flowsToExecute.add(ebb4);
248                 
249                 execution.setVariable("flowsToExecute", flowsToExecute);
250                 execution.setVariable("gCurrentSequence", 3);
251                 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
252
253                 workflowActionBBTasks.rollbackExecutionPath(execution);
254                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
255                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");
256                 assertEquals(0,execution.getVariable("gCurrentSequence"));
257         }
258         
259         @Test
260         public void checkRetryStatusTest(){
261                 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
262                 execution.setVariable("handlingCode","Retry");
263                 execution.setVariable("retryCount", 1);
264                 execution.setVariable("gCurrentSequence",1);
265                 workflowActionBBTasks.checkRetryStatus(execution);
266                 assertEquals(0,execution.getVariable("gCurrentSequence"));
267         }
268         
269         @Test
270         public void checkRetryStatusNoRetryTest(){
271                 execution.setVariable("retryCount", 3);
272                 execution.setVariable("handlingCode","Success");
273                 execution.setVariable("gCurrentSequence",1);
274                 workflowActionBBTasks.checkRetryStatus(execution);
275                 assertEquals(0,execution.getVariable("retryCount"));
276         }
277 }