0c2a95f5d15a16b5af4e6a092404a33e86c705f1
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / servicedecomposition / tasks / ExecuteBuildlingBlockRainyDayTest.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.servicedecomposition.tasks;
22
23 import static org.hamcrest.CoreMatchers.any;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Matchers.isA;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.camunda.bpm.engine.delegate.DelegateExecution;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.so.bpmn.core.WorkflowException;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
42 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
43 import org.onap.so.BaseTest;
44 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 public class ExecuteBuildlingBlockRainyDayTest extends BaseTest {
48         @Autowired
49         private ExecuteBuildingBlockRainyDay executeBuildingBlockRainyDay;
50         
51         private ServiceInstance serviceInstance;
52         private Customer customer; //will build service sub
53         private GenericVnf vnf;
54         private BuildingBlock buildingBlock;
55         private ExecuteBuildingBlock executeBuildingBlock;
56         private static final String ASTERISK = "*";
57         
58         @Before
59         public void before() {
60                 serviceInstance = setServiceInstance();
61                 customer = setCustomer();
62                 vnf = setGenericVnf();
63                 
64                 buildingBlock = new BuildingBlock();
65                 buildingBlock.setBpmnFlowName("AssignServiceInstanceBB");
66                 
67                 executeBuildingBlock = new ExecuteBuildingBlock();
68                 executeBuildingBlock.setBuildingBlock(buildingBlock);
69                 
70                 delegateExecution.setVariable("gBBInput", gBBInput);
71                 delegateExecution.setVariable("WorkflowException", new WorkflowException("", 7000, ""));
72                 delegateExecution.setVariable("buildingBlock", executeBuildingBlock);
73                 delegateExecution.setVariable("lookupKeyMap", lookupKeyMap);
74         }
75         
76         @Test
77         public void setRetryTimerTest() throws Exception{
78                 delegateExecution.setVariable("retryCount", 2);
79                 executeBuildingBlockRainyDay.setRetryTimer(delegateExecution);
80                 assertEquals("PT25M",delegateExecution.getVariable("RetryDuration"));
81         }
82         
83         @Test
84         public void setRetryTimerExceptionTest() {
85                 expectedException.expect(BpmnError.class);
86                 DelegateExecution execution = mock(DelegateExecution.class);
87                 when(execution.getVariable(eq("retryCount"))).thenThrow(Exception.class);
88                 executeBuildingBlockRainyDay.setRetryTimer(execution);
89         }
90         
91         @Test
92         public void queryRainyDayTableExists() throws Exception{
93                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
94                 serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
95                 vnf.setVnfType("vnft1");
96                 
97                 RainyDayHandlerStatus rainyDayHandlerStatus = new RainyDayHandlerStatus();
98                 rainyDayHandlerStatus.setErrorCode("7000");
99                 rainyDayHandlerStatus.setFlowName("AssignServiceInstanceBB");
100                 rainyDayHandlerStatus.setServiceType("st1");
101                 rainyDayHandlerStatus.setVnfType("vnft1");
102                 rainyDayHandlerStatus.setPolicy("Rollback");
103                 rainyDayHandlerStatus.setWorkStep(ASTERISK);
104                 
105                 doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep("AssignServiceInstanceBB", "st1", "vnft1", "7000", "*");
106                 
107                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
108                 
109                 assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
110         }
111         
112         @Test
113         public void queryRainyDayTableDefault() throws Exception{
114                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
115                 serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
116                 vnf.setVnfType("vnft1");
117
118                 RainyDayHandlerStatus rainyDayHandlerStatus = new RainyDayHandlerStatus();
119                 rainyDayHandlerStatus.setErrorCode(ASTERISK);
120                 rainyDayHandlerStatus.setFlowName("AssignServiceInstanceBB");
121                 rainyDayHandlerStatus.setServiceType(ASTERISK);
122                 rainyDayHandlerStatus.setVnfType(ASTERISK);
123                 rainyDayHandlerStatus.setPolicy("Rollback");
124                 rainyDayHandlerStatus.setWorkStep(ASTERISK);
125                 
126                 doReturn(null).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep("AssignServiceInstanceBB", "st1", "vnft1", "7000", ASTERISK);
127                 doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep("AssignServiceInstanceBB", ASTERISK, ASTERISK, ASTERISK, ASTERISK);
128                 
129                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
130                 
131                 assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
132         }
133         
134         @Test
135         public void queryRainyDayTableDoesNotExist() throws Exception{
136                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
137                 serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
138                 vnf.setVnfType("vnft1");
139
140                 doReturn(null).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class));
141
142                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
143                 
144                 assertEquals("Abort", delegateExecution.getVariable("handlingCode"));
145         }
146         
147         @Test
148         public void queryRainyDayTableExceptionTest() {
149                 doThrow(Exception.class).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class));
150                 
151                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
152                 
153                 assertEquals("Abort", delegateExecution.getVariable("handlingCode"));
154         }
155 }