added generic fabric support to SO
[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         
57         @Before
58         public void before() {
59                 serviceInstance = setServiceInstance();
60                 customer = setCustomer();
61                 vnf = setGenericVnf();
62                 
63                 buildingBlock = new BuildingBlock();
64                 buildingBlock.setBpmnFlowName("AssignServiceInstanceBB");
65                 
66                 executeBuildingBlock = new ExecuteBuildingBlock();
67                 executeBuildingBlock.setBuildingBlock(buildingBlock);
68                 
69                 delegateExecution.setVariable("gBBInput", gBBInput);
70                 delegateExecution.setVariable("WorkflowException", new WorkflowException("", 7000, ""));
71                 delegateExecution.setVariable("buildingBlock", executeBuildingBlock);
72                 delegateExecution.setVariable("lookupKeyMap", lookupKeyMap);
73         }
74         
75         @Test
76         public void setRetryTimerTest() throws Exception{
77                 delegateExecution.setVariable("retryCount", 2);
78                 executeBuildingBlockRainyDay.setRetryTimer(delegateExecution);
79                 assertEquals("PT25M",delegateExecution.getVariable("RetryDuration"));
80         }
81         
82         @Test
83         public void setRetryTimerExceptionTest() {
84                 expectedException.expect(BpmnError.class);
85                 DelegateExecution execution = mock(DelegateExecution.class);
86                 when(execution.getVariable(eq("retryCount"))).thenThrow(Exception.class);
87                 executeBuildingBlockRainyDay.setRetryTimer(execution);
88         }
89         
90         @Test
91         public void queryRainyDayTableExists() throws Exception{
92                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
93                 serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
94                 vnf.setVnfType("vnft1");
95                 
96                 RainyDayHandlerStatus rainyDayHandlerStatus = new RainyDayHandlerStatus();
97                 rainyDayHandlerStatus.setErrorCode("7000");
98                 rainyDayHandlerStatus.setFlowName("AssignServiceInstanceBB");
99                 rainyDayHandlerStatus.setServiceType("st1");
100                 rainyDayHandlerStatus.setVnfType("vnft1");
101                 rainyDayHandlerStatus.setPolicy("Rollback");
102                 rainyDayHandlerStatus.setWorkStep("ASTERISK");
103                 
104                 doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep("AssignServiceInstanceBB", "st1", "vnft1", "7000", "*");
105                 
106                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
107                 
108                 assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
109         }
110         
111         @Test
112         public void queryRainyDayTableDefault() throws Exception{
113                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
114                 serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
115                 vnf.setVnfType("vnft1");
116
117                 RainyDayHandlerStatus rainyDayHandlerStatus = new RainyDayHandlerStatus();
118                 rainyDayHandlerStatus.setErrorCode("ASTERISK");
119                 rainyDayHandlerStatus.setFlowName("AssignServiceInstanceBB");
120                 rainyDayHandlerStatus.setServiceType("ASTERISK");
121                 rainyDayHandlerStatus.setVnfType("ASTERISK");
122                 rainyDayHandlerStatus.setPolicy("Rollback");
123                 rainyDayHandlerStatus.setWorkStep("ASTERISK");
124                 
125                 doReturn(null).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep("AssignServiceInstanceBB", "st1", "vnft1", "7000", "ASTERISK");
126                 doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep("AssignServiceInstanceBB", "ASTERISK", "ASTERISK", "ASTERISK", "ASTERISK");
127                 
128                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
129                 
130                 assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
131         }
132         
133         @Test
134         public void queryRainyDayTableDoesNotExist() throws Exception{
135                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
136                 serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
137                 vnf.setVnfType("vnft1");
138
139                 doReturn(null).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class));
140
141                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
142                 
143                 assertEquals("Abort", delegateExecution.getVariable("handlingCode"));
144         }
145         
146         @Test
147         public void queryRainyDayTableExceptionTest() {
148                 doThrow(Exception.class).when(MOCK_catalogDbClient).getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class));
149                 
150                 executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution);
151                 
152                 assertEquals("Abort", delegateExecution.getVariable("handlingCode"));
153         }
154 }