Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBTasksTest.java
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.doThrow;
29 import static org.mockito.Mockito.when;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.camunda.bpm.engine.delegate.BpmnError;
35 import org.camunda.bpm.engine.delegate.DelegateExecution;
36 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.onap.so.bpmn.BaseTaskTest;
45 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
46 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
47 import org.onap.so.db.request.beans.InfraActiveRequests;
48 import org.springframework.beans.factory.annotation.Autowired;
49
50 public class WorkflowActionBBTasksTest extends BaseTaskTest {
51
52         @Mock
53         protected WorkflowAction workflowAction;
54         
55         @InjectMocks
56         protected WorkflowActionBBTasks workflowActionBBTasks;
57         
58         private DelegateExecution execution;
59         
60         @Rule
61         public ExpectedException thrown = ExpectedException.none();
62         
63         @Before
64         public void before() throws Exception {
65                 execution = new DelegateExecutionFake();
66                 org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
67                 servInstance.setServiceInstanceId("TEST");
68                 when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
69                 workflowAction.setBbInputSetupUtils(bbSetupUtils);
70                 workflowAction.setBbInputSetup(bbInputSetup);
71         }
72         
73         @Test
74         public void selectBBTest() throws Exception{
75                 String gAction = "Delete-Network-Collection";
76                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
77                 execution.setVariable("requestAction", gAction);
78                 execution.setVariable("gCurrentSequence", 0);
79                 execution.setVariable("homing", false);
80                 execution.setVariable("calledHoming", false);
81                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
82                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
83                 flowsToExecute.add(ebb);
84                 execution.setVariable("flowsToExecute", flowsToExecute);
85                 workflowActionBBTasks.selectBB(execution);
86                 boolean success = (boolean) execution.getVariable("completed");
87                 assertEquals(true,success);
88         }
89         
90         @Test
91         public void getUpdatedRequestTest() throws Exception{
92                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
93                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
94                 BuildingBlock bb1 = new BuildingBlock();
95                 bb1.setBpmnFlowName("CreateNetworkBB");
96                 flowsToExecute.add(ebb1);
97                 ebb1.setBuildingBlock(bb1);
98                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
99                 BuildingBlock bb2 = new BuildingBlock();
100                 bb2.setBpmnFlowName("ActivateNetworkBB");
101                 flowsToExecute.add(ebb2);
102                 ebb2.setBuildingBlock(bb2);
103                 String requestId = "requestId";
104                 execution.setVariable("mso-request-id", requestId);
105                 execution.setVariable("flowsToExecute", flowsToExecute);
106                 int currentSequence = 2;
107                 String expectedStatusMessage = "Execution of CreateNetworkBB has completed successfully, next invoking ActivateNetworkBB (Execution Path progress: BBs completed = 1; BBs remaining = 1).";
108                 Long expectedLong = new Long(52);
109                 InfraActiveRequests mockedRequest = new InfraActiveRequests();
110                 when(requestsDbClient.getInfraActiveRequestbyRequestId(requestId)).thenReturn(mockedRequest);
111                 InfraActiveRequests actual = workflowActionBBTasks.getUpdatedRequest(execution, currentSequence);
112                 assertEquals(expectedStatusMessage, actual.getStatusMessage());
113                 assertEquals(expectedLong, actual.getProgress());
114         }
115         
116         @Test
117         public void select2BBTest() throws Exception{
118                 String gAction = "Delete-Network-Collection";
119                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
120                 execution.setVariable("requestAction", gAction);
121                 execution.setVariable("gCurrentSequence", 0);
122                 execution.setVariable("homing", false);
123                 execution.setVariable("calledHoming", false);
124                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
125                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
126                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
127                 flowsToExecute.add(ebb);
128                 flowsToExecute.add(ebb2);
129                 execution.setVariable("flowsToExecute", flowsToExecute);
130                 workflowActionBBTasks.selectBB(execution);
131                 boolean success = (boolean) execution.getVariable("completed");
132                 assertEquals(false,success);
133         }
134         
135         @Test
136         public void msoCompleteProcessTest() throws Exception{
137                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
138                 execution.setVariable("requestAction", "createInstance");
139                 execution.setVariable("resourceId", "123");
140                 execution.setVariable("source","MSO");
141                 execution.setVariable("resourceName", "Service");
142                 execution.setVariable("aLaCarte", true);
143                 workflowActionBBTasks.setupCompleteMsoProcess(execution);
144                 String response = (String) execution.getVariable("CompleteMsoProcessRequest");
145                 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>");
146         }
147         
148         @Test
149         public void setupFalloutHandlerTest(){
150                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
151                 execution.setVariable("serviceInstanceId", "123");
152                 execution.setVariable("WorkflowActionErrorMessage", "Error in WorkFlowAction");
153                 execution.setVariable("requestAction", "createInstance");
154                 workflowActionBBTasks.setupFalloutHandler(execution);
155                 assertEquals(execution.getVariable("falloutRequest"),"<aetgt:FalloutHandlerRequest xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"xmlns:ns=\"http://org.onap/so/request/types/v1\"xmlns:wfsch=\"http://org.onap/so/workflow/schema/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>VID</source></request-info><aetgt:WorkflowException xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"><aetgt:ErrorMessage>Error in WorkFlowAction</aetgt:ErrorMessage><aetgt:ErrorCode>7000</aetgt:ErrorCode></aetgt:WorkflowException></aetgt:FalloutHandlerRequest>");
156         }
157         
158         @Test
159         public void rollbackExecutionPathTest(){
160                 execution.setVariable("isRollback", false);
161                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
162                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
163                 BuildingBlock bb1 = new BuildingBlock();
164                 bb1.setBpmnFlowName("AssignVfModuleBB");
165                 ebb1.setBuildingBlock(bb1);
166                 flowsToExecute.add(ebb1);
167                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
168                 BuildingBlock bb2 = new BuildingBlock();
169                 bb2.setBpmnFlowName("CreateVfModuleBB");
170                 ebb2.setBuildingBlock(bb2);
171                 flowsToExecute.add(ebb2);
172                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
173                 BuildingBlock bb3 = new BuildingBlock();
174                 bb3.setBpmnFlowName("ActivateVfModuleBB");
175                 ebb3.setBuildingBlock(bb3);
176                 flowsToExecute.add(ebb3);
177                 
178                 execution.setVariable("flowsToExecute", flowsToExecute);
179                 execution.setVariable("gCurrentSequence", 3);
180                 
181                 workflowActionBBTasks.rollbackExecutionPath(execution);
182                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
183                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB");
184                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
185                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
186                 assertEquals(0,execution.getVariable("gCurrentSequence"));
187         }
188         
189         @Test
190         public void rollbackExecutionPathUnfinishedFlowTest(){
191                 execution.setVariable("isRollback", false);
192                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
193                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
194                 BuildingBlock bb1 = new BuildingBlock();
195                 bb1.setBpmnFlowName("AssignVfModuleBB");
196                 ebb1.setBuildingBlock(bb1);
197                 flowsToExecute.add(ebb1);
198                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
199                 BuildingBlock bb2 = new BuildingBlock();
200                 bb2.setBpmnFlowName("CreateVfModuleBB");
201                 ebb2.setBuildingBlock(bb2);
202                 flowsToExecute.add(ebb2);
203                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
204                 BuildingBlock bb3 = new BuildingBlock();
205                 bb3.setBpmnFlowName("ActivateVfModuleBB");
206                 ebb3.setBuildingBlock(bb3);
207                 flowsToExecute.add(ebb3);
208                 
209                 execution.setVariable("flowsToExecute", flowsToExecute);
210                 execution.setVariable("gCurrentSequence", 2);
211                 
212                 workflowActionBBTasks.rollbackExecutionPath(execution);
213                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
214                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
215                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");    
216                 assertEquals(0,execution.getVariable("gCurrentSequence"));
217                 assertEquals(0,execution.getVariable("retryCount"));
218         }
219         
220         @Test
221         public void rollbackExecutionTest(){
222                 execution.setVariable("isRollback", false);
223                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
224                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
225                 BuildingBlock bb1 = new BuildingBlock();
226                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
227                 ebb1.setBuildingBlock(bb1);
228                 flowsToExecute.add(ebb1);
229                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
230                 BuildingBlock bb2 = new BuildingBlock();
231                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
232                 ebb2.setBuildingBlock(bb2);
233                 flowsToExecute.add(ebb2);
234                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
235                 BuildingBlock bb3 = new BuildingBlock();
236                 bb3.setBpmnFlowName("AssignNetworkBB");
237                 ebb3.setBuildingBlock(bb3);
238                 flowsToExecute.add(ebb3);
239                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
240                 BuildingBlock bb4 = new BuildingBlock();
241                 bb4.setBpmnFlowName("CreateNetworkBB");
242                 ebb4.setBuildingBlock(bb4);
243                 flowsToExecute.add(ebb4);
244                 
245                 execution.setVariable("flowsToExecute", flowsToExecute);
246                 execution.setVariable("gCurrentSequence", 3);
247                 
248                 workflowActionBBTasks.rollbackExecutionPath(execution);
249                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
250                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB");
251                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");     
252                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignServiceInstanceBB");
253                 assertEquals(0,execution.getVariable("gCurrentSequence"));
254         }
255         
256         @Test
257         public void checkRetryStatusTest(){
258                 execution.setVariable("handlingCode","Retry");
259                 execution.setVariable("retryCount", 1);
260                 execution.setVariable("gCurrentSequence",1);
261                 workflowActionBBTasks.checkRetryStatus(execution);
262                 assertEquals(0,execution.getVariable("gCurrentSequence"));
263         }
264         
265         @Test
266         public void checkRetryStatusNoRetryTest(){
267                 execution.setVariable("retryCount", 3);
268                 execution.setVariable("handlingCode","Success");
269                 execution.setVariable("gCurrentSequence",1);
270                 workflowActionBBTasks.checkRetryStatus(execution);
271                 assertEquals(0,execution.getVariable("retryCount"));
272         }
273 }